2019年3月21日 星期四

Week05 鍾嘉穎 2019-03-21

1. 主題: 旋轉 Rotation
       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

       Rotate: 旋轉 (使用右手定律 找到旋轉的方向)
      +: 跟著手指的方向
      -: 反方向
      軸: 大母指 (會從(0,0,0) 開始到設定的點)
      角度會沿著大母指轉
      
      

      glRotatef(角度, X軸, Y軸, Z軸);
      
      

2. 實作: glRotatef();
      1. 先載入freeglut (跟第一週一樣)
           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. 把main的程式碼刪掉
      3. 把程式碼打上去

3. 滑鼠轉, 自動轉

      自己轉的程式碼:

                #include <GL/glut.h>
                float angle=0; ///先宣告一個變數
                void display()
                {
                    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                    glPushMatrix(); ///備份保護Matrix
                        glRotatef(angle,0,1,0); ///旋轉的指令
                        glutSolidTeapot(0.3);
                    glPopMatrix();///還原Matrix
                    glutSwapBuffers();
                    angle+=3; ///每執行一次角度都會加一點點
                }
                int main(int argc, char**argv)
                {
                    glutInit(&argc, argv);
                    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
                    glutCreateWindow("06160912 rotate");
                    glutDisplayFunc(display);
                    glutIdleFunc(display);///閒閒沒事會再做一次
                    glutMainLoop();
                }

      






      用滑鼠控制的程式碼: (改上面的程式) (但會卡卡的)(加了綠色的程式碼)

                #include <GL/glut.h>
                float angle=0; ///先宣告一個變數
                void display()
                {
                    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///不寫這句會有殘影
                    glPushMatrix(); ///備份保護Matrix
                        glRotatef(angle,0,1,0); ///旋轉的指令    對Y軸, 轉角度
                        glutSolidTeapot(0.3);
                    glPopMatrix();///還原Matrix
                    glutSwapBuffers();
                    ///angle+=3;  ///加了motion之後不用再自己加了
                }
                void motion (int x, int y) ///當你的mouse去drag, 就可以做了
                {
                    angle=x; ///更新angle
                    display();
                }
                int main(int argc, char**argv)
                {
                    glutInit(&argc, argv);
                    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
                    glutCreateWindow("06160912 rotate");
                    glutDisplayFunc(display);
                    glutIdleFunc(display);///閒閒沒事會再做一次
                    glutMotionFunc(motion); ///跑motion
                    glutMainLoop(); ///如果忘了寫這句或忘了括號() 就會離開 (跑不出來)
                }




      
     
               
4. 滑鼠事件: 把大象放入冰箱

      為了不會卡卡的, 所以要多加一個滑鼠的函式 (加了藍色的程式碼)

                #include <GL/glut.h>
                  #include <stdio.h> ///加printf要用到
                float angle=0; ///先宣告一個變數
                void display()
                {
                    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///不寫這句會有殘影
                    glPushMatrix(); ///備份保護Matrix
                        glRotatef(angle,0,1,0); ///旋轉的指令    對Y軸, 轉角度
                        glutSolidTeapot(0.3);
                    glPopMatrix();///還原Matrix
                    glutSwapBuffers();
                    ///angle+=3;  ///加了motion之後不用再自己加了
                }
                void motion (int x, int y) ///當你的mouse去drag, 就可以做了
                {
                    angle=x; ///更新angle
                    display();
                }
                void mouse(int button, int state, int x, int y)///button指按了滑鼠的哪個鍵(1=左  2=右  3,4=中鍵)
                {   ///state指有沒有按到(1=沒按到  0=有按到)   ///x, y指坐標(左到右<x=0-300>  上到下<y=0-300>)
                    printf("%d %d %d %d\n", button, state, x, y);
                }
                int main(int argc, char**argv)
                {
                    glutInit(&argc, argv);
                    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
                    glutCreateWindow("06160912 rotate");
                    glutDisplayFunc(display);
                    glutIdleFunc(display);///閒閒沒事會再做一次
                    glutMotionFunc(motion); ///跑motion
                      glutMouseFunc(mouse);

                    glutMainLoop(); ///如果忘了寫這句或忘了括號() 就會離開 (跑不出來)
                }
      

      
      

沒有留言:

張貼留言