2018年3月23日 星期五

Week 03 張勝豐

(1)用程式複習上周教的函式

1. 首先去老師以前寫的網站裡下載下圖三個檔案並且解壓縮
     http://jsyeh.org/3dcg10













2. 再來把glut32.dll丟到windows資料夾裡面,執行shapes.exe

    (  必須把glut32.dll丟進去資料夾裡才可執行shapes.exe  )
















3. 打開程式之後可以看到左右臉邊的方框,左邊是顯示出來的圖案
    右邊是我們的程式,在右邊點擊右鍵可以更改我們之前所使用過
    的函式以及一些更進階的式子。













   在左邊點擊右鍵則可以改變點的粗細以及線條













   右邊綠色的數值可以用滑鼠上下拉動調整數值













(2)滑鼠

(1) 開啟GLUT以及打上茶壺的程式碼
      (Week02以及Week03有複習)


(2) 改寫程式,讓我們能知道滑鼠點擊的位置的座標












     
     code :
     #include <stdio.h>
     #include <GL/glut.h>
     void display()
     {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glutSolidTeapot(0.3);
     glutSwapBuffers();
     }
     void mouse (int button, int state, int x, int y)
    {
    //button 代表左 中 右鍵,state 代表按下去/彈起來的mouse位置
    printf("%d %d %d %d\n",button,state,x,y);
    }
    int main(int argc, char* argv[]) //完整的main()參數
   {
    glutInit(&argc,argv); //初始參數
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); //兩個顯示的參數
    glutCreateWindow("week03 Mouse"); //window視窗標題

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    //glutMotionFunc(motion);

    glutMainLoop(); //主要迴圈
    }

(3)讓圖形可以跟著滑鼠移動




     code ;

     #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迴圈
  }

沒有留言:

張貼留言