2024年3月4日 星期一

電腦圖學坐牢中 week03

1. Mouse 滑鼠

1-0. 先把 FreeGLUT 裝好(解壓縮到桌面上。C:\Users\Administrator\Desktop\freeglut)

1-1. 再把 C:\Users\Administrator\Desktop\freeglut 的 lib 目錄裡,複製一個「libfreeglut.a」,並將其名稱改為「libglut32.a」 

1-2. File ⮕ New ⮕ Project,開啟 GLUT project

1-3. Fodler to create project in:(的...)要選目錄是桌面,上面的專案名稱 week02_glut_color_teapot

1-4. 選 GLUT 的目錄 C:\Users\Administrator\Desktop\freeglut 就完成了

1-5. 剪貼之前自己打的十行程式。

1-6. 增加三行程式

```cpp

void mouse(int button, int state, int x, int y){

    printf("Hello World\n");

}

glutCreateWindow("week03_mouse");

glutMouseFunc(mouse);

```


1-7. 定義 mouse 的函式 button:左鍵、1:中鍵、2:右鍵

```cpp

    printf("%d %d %d\n", button, state, x, y);

```

2. week03_mouse_glTranslatef

2-1. File ⮕ New ⮕ Project,開啟 GLUT project檔案名稱 week03_mouse_glTranslatef

2-2. 增加新的程式碼,並修改 mouse 的函式內容

```cpp

float teapotX = 0, teapotY = 0; ///新加的程式,用來放茶壺的座標

```

2-3. 修改 mouse 的程式

```cpp

void mouse(int button, int state, int x, int y){

    teapotX = (x-150)/150.0;

    teapotY = -(y-150)/150.0;

```

2-4. display 中新增

```cpp

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清空背景

glPushMatrix();///備份矩陣

    glTranslatef(teapotX, teapotY, 0);///把座標移動

    glutSolidTeapot( 0.3 );

glPopMatrix();///還原矩陣

glutSwapBuffers();

```





3. week03_mouse_glTranslatef

3-1. File ⮕ New ⮕ Project,開啟 GLUT project檔案名稱 week03_mouse_glRotatef

3-2. 複製前一個的程式碼

3-3. 新加程式碼,motion 函數

```cpp

void motion(int x, int y)

{

    angle = x;

    display();

}

```


4. week03_mouse_translate_rotate

4-1. File ⮕ New ⮕ Project,開啟 GLUT project檔案名稱week03_mouse_translate_rotate

4-2. 複製前一個的程式碼

4-3. 新加程式碼

```cpp

void keyboard(unsigned char key, int x, int y)

{

    if(key == 'e') method=1; /// Rotate轉動

    if(key == 'w') method=2; /// Translate移動

}

```

4-4. 修改程式碼

```cpp

void motion(int x, int y)

{

    if(method == 1) angle += x-oldX;

    else if(method == 2){

        teapotX += (x - oldX) / 150.0;

        teapotY += (y - oldY) / 150.0;

}

    oldX = x;

    oldY = y;

    display();

}

```




沒有留言:

張貼留言