主題1:了解如何使用 Transformation
第一步 依照上禮拜從 http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
下載 data.zip, windows.zip, glut32.dll 三個
第二步 將 glut32.dll、data資料夾複製到 windows 資料夾內
第三步 打開 Transformation
第四步 在 Screen-space view(右上角的部份)按右鍵可以切換各種模型
第五步 glTranslatef(x,y,z) 可以調整物件在x,y,z軸的位置(在數值上按住滑鼠左鍵上下移動可以調整數值的大小)
第六步 glRotatef(angle,x,y,z) 可以旋轉物鍵,angle代表旋轉的角度後面三個(x,y,z)分別代表x,y,z軸的軸向量
第七步 glScalef(x,y,z) 可以調整物件的大小
第八步 在Command manipulation window(下方的視窗)上按右鍵
-> Reset parameters 可以重設所有數值(還原為預設狀態)
主題2:用滑鼠移動和旋轉茶壺
第一步 新增一個glut project專案
第二步 將以下的程式碼貼入main.cpp,成果如下(滑鼠左鍵為旋轉,右鍵為移動位置)
備註:程式碼
#include <stdio.h>
#include <GL/glut.h> //整GLUT外掛
float teapotX=0, teapotY=0, angle=0; //茶壺的座標,rotate旋轉
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); //依照茶壺的座標(teapotX, teapotY)移動
glRotatef(angle, 0,0,1); //茶壺要對著 (0,0,1)做旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix();//還原矩陣
glutSwapBuffers(); //搭配GLUT_DOUBLE兩倍顯示
}
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);//2個顯示的參數
glutCreateWindow("Week04 Mouse");//設定視窗的名稱
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();//主要迴圈
}




沒有留言:
張貼留言