2018年7月3日 星期二

week04 李宜謙

使用 Transformation

(1) 準備 data.zip, windows.zip, glut32.dll 三個檔案

(2) 將 glut32.dll、data資料夾複製到 windows 資料夾內


(3) 打開 Transformation,在 Screen-space view 按右鍵可以切換各種模型



(4) glTranslatef(x,y,z) 可以調整物件在x,y,z軸的位置(在數值上按住滑鼠左鍵)

(5) glRotatef(angle,x,y,z) 可以旋轉物件,第一個參數(angle)代表旋轉的角度後面三個(x,y,z)分別代表x,y,z軸的軸向量,選轉方向依照右手定則。軸線並用右手握住這條軸線,然後大母指指向的方向為由(0,0,0) 到 (x,y,z) 的方向,而四指緊握軸線代表旋轉的方向

(6) glScalef(x,y,z) 可以調整物件的大小


滑鼠移動和旋轉茶壺


將以下的程式碼貼入main.cpp,成果如下(滑鼠左鍵為旋轉,右鍵為移動位置)


#include <stdio.h>
#include <GL/glut.h> 
float teapotX=0, teapotY=0, angle=0; //茶壺的座標,rotate旋轉要的angle
int oldX=0, oldY=0, nowRotate=0; //紀錄mouse在哪裡按下去,oldX, oldY 了解你走多少地方
void display()
{    
glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);    
glPushMatrix();
        glTranslatef(teapotX, teapotY, 0); 
        glRotatef(angle, 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) //mouse motion 事件
{
if(nowRotate==1) angle += (x-oldX); //滑鼠的座標,就是璇轉的角度,加上新的增加量    
else
{        
teapotX += (x-oldX)/150.0;//依照motion時的x來改teapot的座標,加上新的增加量
teapotY += (oldY-y)/150.0;//依照motion時的y來改teapot的座標,加上新的增加量
}    
oldX = x;       oldY = y;  //紀錄移動後新的位置
glutPostRedisplay(); //重畫畫面
}
int main(int argc, char** argv)
{    
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 Mouse");
    glutDisplayFunc(display);    
    glutMouseFunc(mouse);    
    glutMotionFunc(motion);             
    glutMainLoop();
}


沒有留言:

張貼留言