2018年3月23日 星期五

Week04劉峻宇

                                                            上課實作1

老師今天教如何讓一個動畫模型任意旋轉
1.先到http://jsyeh.org/3dcg10下載這三個檔案windows.zip, data.zip , glut32.dll
然後解壓縮玩開啟windows資料夾,再把解壓縮的data和glut32.dll 放到裡面


2.執行transformation.exe


3.Y軸箭頭往上,向右旋轉
  
    Y軸箭頭往上,向左旋轉 


4.X軸向左,向上旋轉
   
  X軸向右,向下旋轉


5.Z軸向後,向右旋轉


6.Z軸向前,向左旋轉




                                                 上課實作2

1.開一個glut專案,然後把原本程式碼刪除,貼上上星期教的程式碼
 讓茶壺移動 
     #include <stdio.h>

     #include <GL/glut.h>
     float teapotX=0, teapotY=0;
     void display()
    {
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();//備份矩陣
    glTranslatef(teapotX, teapotY, 0); //依照茶壺的座標移動  
    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的座標
    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迴圈
  }


2.這星期新教的,讓茶壺轉動
程式碼
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0, teapotY=0, angle=0;///Now: rotate旋轉要的angle
int oldX=0, oldY=0, nowRotate=0;///Now3: oldX, oldY幫忙,了解你走了多少地方
void display()
{
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();//備份矩陣
        glTranslatef(teapotX, teapotY, 0); //依照茶壺的座標移動
        glRotatef(angle, 0,0,1);///Now: 茶壺要對著 (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);///Now: 滑鼠的座標,就是轉的角度 Now2: 不好的移動 Now3: 新的動法, 加上新的增加量
    else{
        teapotX += (x-oldX)/150.0; /// 依照motion時的x來改teapot的座標 Now2: 不好的旋轉 Now3: 新的動法, 加上新的增加量
        teapotY += (oldY-y)/150.0;/// 依照motion時的y來改teapot的座標 Now2: 不好的旋轉 Now3: 新的動法, 加上新的增加量
    }
    oldX=x; oldY=y;///Now3: 新的動法, 要記下來 oldX, oldY在哪兒
    glutPostRedisplay(); //重畫畫面
}
void mouse(int button, int state, int x, int y)
{
   if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;///Now2:左鍵做旋轉
   if(button==GLUT_RIGHT_BUTTON &state==GLUT_DOWN) nowRotate=0;///Now2:右鍵做移動
   oldX=x; oldY=y;///Now3: 新的動法, 要記下來 oldX, oldY在哪兒
}
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迴圈
}









沒有留言:

張貼留言