2018年3月23日 星期五

Week04_蔡明憲

1.執行並觀察Transformation.exe
(1)去以下這個網址: jsyeh.org/3dcg10
下載 glut32.lib, data, win32
(2)把glut32.lib以及data解壓縮後的資料夾放進windows資料夾
(3)執行Transformation.exe並觀察圖形轉動的狀況
X,Y,Z軸的部分數值調到1即可固定該軸
2.讓茶壺旋轉移動
(1)打開codeblock並開啟GLUT project(一樣用freeglut開啟)
(2)複製並貼上上一周可讓茶壺移動的程式碼
(3)新增幾行旋轉角度的程式碼,程式碼如下:
     #include <stdio.h>
     #include <GL/glut.h>
     float teapotX=0, teapotY=0,angle=0;
     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(); //搭配GLUT_DOUBLE兩倍顯示
    }
    void motion(int x, int y) //mouse motion 事件
   {
    //teapotX = (x-150)/150.0; // 依照motion時的x來改teapot的座標
    //teapotY = (150-y)/150.0;// 依照motion時的y來改teapot的座標
    angle=x;
    glutPostRedisplay(); //重畫畫面
   }
   int main(int argc, char** argv)
  {
   glutInit(&argc,argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
   glutCreateWindow("Week03 Mouse");
   glutDisplayFunc(display);
   //glutMouseFunc(mouse);
   glutMotionFunc(motion);
   glutMainLoop();//主要GLUT迴圈
  }
(4)為了解決移動不太順利的問題,增加幾行程式碼來優化:
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0, teapotY=0,angle=0;
int oldX=0, oldY=0, nowRotate=0;
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(); //搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y) //mouse motion 事件
{
    //teapotX = (x-150)/150.0; // 依照motion時的x來改teapot的座標
    //teapotY = (150-y)/150.0;// 依照motion時的y來改teapot的座標
    if(nowRotate==1)angle+=(x-oldX);
    else{
            teapotX+=(x-oldX)/150.0;
            teapotY+=(oldY-y)/150.0;
        }
    oldX=x; oldY=y;
    glutPostRedisplay(); //重畫畫面
   }
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;
   }
   int main(int argc, char** argv)
  {
   glutInit(&argc,argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
   glutCreateWindow("Week03 Mouse");
   glutDisplayFunc(display);
   glutMouseFunc(mouse);
   glutMotionFunc(motion);
   glutMainLoop();//主要GLUT迴圈
  }
滑鼠左鍵旋轉,右鍵移動
這樣就大功告成拉~~

沒有留言:

張貼留言