使用Transformation來看移動、旋轉、縮放
先到http://jsyeh.org/3dcg10下載data、windows、glut32.dll檔案,解壓縮後把glut32.dll及data整個資料夾複製到windows裡面,再開啟Transformation即可
(1) glTranslatef(x,y,z)為移動物件位置
(2) glRotate(旋轉角度,x,y,z)為旋轉物件(3) glScale(x,y,z)為物件大小縮放
旋轉、移動茶壺
先複製上禮拜茶壺的程式碼,再予以加入新的程式碼
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0,angle=0;
int oldX=0,oldY=0,nowRotate=0;oldX和oldY記下滑鼠點的位置
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
glRotatef(angle,0,0,1);茶壺要對著(0,0,1)做旋轉
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;左鍵旋轉
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0;右鍵移動
oldX=x; oldY=y;滑鼠點下的的位置
}
void motion(int x,int y)
{
if(nowRotate==1) angle += (x-oldX);
else
{
teapotX += (x-oldX)/150.0;
teapotY += (oldY-y)/150.0;
}
oldX=x; oldY=y; 記錄新的座標
glutPostRedisplay();
}
int main (int argc,char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week05 Mouse_Rotate");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言