2019年3月14日 星期四

Week04 鍾嘉穎 2019-03-14

1. 主題: 移動
      1. 點進去小葉的網頁: http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
      2. 下載 Example裏的 win32, glut32.dll, data
      3. 解壓縮到下載
      4. 把已解壓的 data 資料夾拉到windows裏
      5. 把已解壓的 glut32.dll 資料夾拉到windows裏
      6. 打開 Transformation.exe

      在Transformation.exe裏
          左上角: 顯示坐標
          右上角: 可用滑鼠右鍵換Model
          下方: 可用滑鼠改位置, 大小, 角度
          Translate: 移動
          Rotate: 旋轉
          Scale: 大小

      




























2. 示範: glTranslatef(x, y, z);
      1. 在 Code blocks 把 GLUT 建立好
          1. Google search "freeglut windows"
           2. 從Transmission Zero 下載 "freeglut 3.0.0 MinGW Package"
           3. 解壓縮後放到桌面

           4. 在lib複製一個"libfreeglut.a"命名為"libglut32.a"
           5. CodeBlocks 玩
           6. File  ->  New -> Project -> GLUT Project
           7. 把location改成"C:\Users\user\Desktop\freeglut"

      

      2. 把GLUT的程式碼都刪掉
      3. 做一個茶壺
          #include <GL/glut.h>
          void display()
          {
              glutSolidTeapot(0.3);
              glutSwapBuffers();
           }

          int main(int argc, char**argv)
          {
              glutInit(&argc, argv);
              glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
              glutCreateWindow("week04");
              glutDisplayFunc(display);
              glutMainLoop();
          }

      
      
      4. 把茶壺移動 (加 glTranslate(0.3, 0.3, 0); ) 但是會跑掉...
      
      

       5. 要加一個矩陣把他們包起來
      
          #include <GL/glut.h>
          void display()
          {
              glPushMatrix(); ///備份矩陣
                  glTranslatef(0.3, 0.3, 0); ///移動
                  glutSolidTeapot(0.3);
              glPopMatrix();///還原矩陣, 才不會下次跑掉
              glutSwapBuffers();
          }

          int main(int argc, char**argv)
          {
              glutInit(&argc, argv);
              glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
              glutCreateWindow("week04");
              glutDisplayFunc(display);
              glutMainLoop();
          }

3. 主題: 滑鼠
      1. 圖案會跟著滑鼠移動
      
      
          #include <GL/glut.h>
          float x=0, y=0; ///滑鼠motion 新增兩個變數
          void display()
          {
              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///滑鼠motion 把圖案clear掉
              glPushMatrix(); ///備份矩陣
                 glTranslatef((x-150)/150.0, -(y-150)/150.0, 0);///滑鼠motion 坐標要改變 所以裏面要變數
                 glutSolidTeapot(0.3);
             glPopMatrix();///還原矩陣, 才不會下次跑掉
             glutSwapBuffers();
         }

         void motion(int nowX, int nowY)///滑鼠motion
         {
             x=nowX;  y=nowY;
             display();  ///把x跟y馬上更新
         }
         int main(int argc, char**argv)
         {
             glutInit(&argc, argv);
             glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
             glutCreateWindow("week04");
             glutDisplayFunc(display);
             glutMotionFunc(motion); ///滑鼠motion 呼叫上面的motion
             glutMainLoop();
         }


      

       2. 如果沒有加  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       3. 會跑出很多圖案 (如下圖) (因為沒有把之前的茶壺clear掉)
      






沒有留言:

張貼留言