2017年3月20日 星期一

Week04 閻覃的上課筆記

Week04 閻覃的上課筆記.md

Week04 閻覃的上課筆記

三個重要英文單詞

旋轉 Rotate

移動 Translate

縮放 Scale

測試移動函式

通過測試程式可以得出三個參數的含義。

5F95529D-2511-4573-8DC5-9FA50C6E7D0F

移動茶壺

主要函式:

 
1
//清空畫面
2
extern void glClear(GLbitfield mask)
3
//mask使用以下常量即可
4
#define GL_COLOR_BUFFER_BIT               0x00004000
5
#define GL_DEPTH_BUFFER_BIT               0x00000100
6
//將矩陣入棧出棧(備份還原)
7
extern void glPushMatrix(void)
8
extern void glPopMatrix(void)
9
//移動茶壺
10
extern void glTranslatef(GLfloat x, GLfloat y, GLfloat z)

程式碼:

 
1
static void display(void) {
2
3
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
4
    glPushMatrix();
5
    {
6
        glTranslatef(_x, 0, 0);
7
        glutSolidTeapot(0.3);
8
    }
9
    glPopMatrix();
10
    glutSwapBuffers();//交換雙緩存區
11
}

7A8DA84A-0161-48B8-9B7D-17895248139C

用滑鼠移動茶壺(Translate)

原理和上週控制小精靈的嘴巴一樣。

不過,這裡要進行坐標系統的變換。

 
1
float mouseX,mouseY;
2
3
static void display(void) {
4
5
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
6
    glPushMatrix();
7
    {
8
        glTranslatef((mouseX-250) / 250.0f,-(mouseY-250) / 250.0f, 0);
9
        //坐標系統的變換
10
        glutSolidTeapot(0.3);
11
    }
12
    glPopMatrix();
13
    glutSwapBuffers();//交換雙緩存區
14
}
15
16
void motion(int x, int y) {
17
    mouseX = x;
18
    mouseY = y;
19
    glutPostRedisplay();//重畫畫面
20
}

用滑鼠縮放茶壺(Scale)

重要函式:

 
1
extern void glScalef(GLfloat x, GLfloat y, GLfloat z)
2
//縮放

程式碼中將原來的glTranslatef替換為glScalef就可以了。

運行后會發現,什麼鬼東西?

10B32DB9-BAD8-41BA-9523-DEBBB695234E

沒有留言:

張貼留言