2018年3月23日 星期五

Week04_吳幸俞

動手玩Transformation.exe
(1)進入FB社團開啟老師給的網址:http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/

(2)下載 data.zip, windows.zip, glut32.dll 這3個檔案

(3)解壓縮windows.zip

(4)解壓縮data.zip

(5)將glut32.dll與data資料夾(各種模型)放進windows資料夾

(6)開啟Transformation.exe

(7)此畫面點選右鍵可以換圖形

(8)滑鼠左鍵按著可以調數值

旋轉茶壺(不好)
(1)新增GLUT專案
(2)打上以下程式碼
#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); //依照茶壺的座標(teapotX,teapotY)移動
    glRotatef(angle,0,0,1);///滑鼠要對著(0,0,1)做旋轉angle度
    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("Yee");
   glutDisplayFunc(display);
   //glutMouseFunc(mouse);
   glutMotionFunc(motion);
   glutMainLoop();//主要GLUT迴圈

  }
(3)執行後,在畫面中滑鼠左鍵按著茶壺可以隨意移動、旋轉
旋轉茶壺(很好)
(1)將程式碼改寫為
#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); ///依照茶壺的座標(teapotX,teapotY)移動
    glRotatef(angle,0,0,1);///茶壺要對著(0,0,1)做旋轉angle度
    glutSolidTeapot(0.3);
    glPopMatrix();//還原矩陣
    glutSwapBuffers(); //搭配GLUT_DOUBLE兩倍顯示
}
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;
    angle=x;///滑鼠的座標,就是轉的角度
    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("Yee");
   glutDisplayFunc(display);
   glutMouseFunc(mouse);
   glutMotionFunc(motion);
   glutMainLoop();//主要GLUT迴圈
}
(2)左鍵可以旋轉,右鍵可以移動

沒有留言:

張貼留言