2018年3月23日 星期五

Week04_黃偉愷

了解如何使用 Transformation

(1) 從 http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
     下載 data.zip, windows.zip, glut32.dll 三個

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

(3) 打開 Transformation

(4) 在 Screen-space view(右上角的部份)按右鍵可以切換各種模型

(5) glTranslatef(x,y,z) 可以調整物件在x,y,z軸的位置(在數值上按住滑鼠左鍵上下移動可以調整數值的大小)

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

(7) glScalef(x,y,z) 可以調整物件的大小,(x,y,z)分別代表要沿著哪一條軸線放大縮小

(8) 在Command manipulation window(下方的視窗)上按右鍵
-> Reset parameters 可以重設所有數值(還原為預設狀態)

用滑鼠移動和旋轉茶壺

(1) 新增一個glut專案

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

code:

#include <stdio.h>
#include <GL/glut.h> //整GLUT外掛
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); //依照茶壺的座標(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();//主要GLUT迴圈
}

沒有留言:

張貼留言