Week08 閻覃的上課筆記
打光
新建GLUT專案
保留Main函數中的程式碼如下:
361int main(int argc, char *argv[]) {
2 glutInit(&argc, argv);
3 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
4 glutInitWindowSize(500, 500);
5 glutCreateWindow("打得不錯");
6
7 glutDisplayFunc(display);
8
9 glClearColor(1, 1, 1, 1);
10 glEnable(GL_CULL_FACE);
11 glCullFace(GL_BACK);
12
13 glEnable(GL_DEPTH_TEST);
14 glDepthFunc(GL_LESS);
15
16 glEnable(GL_LIGHT0);
17 glEnable(GL_NORMALIZE);
18 glEnable(GL_COLOR_MATERIAL);
19 glEnable(GL_LIGHTING);
20
21 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
22 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
23 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
24 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
25
26 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
27 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
28 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
29 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
30
31
32 glutMouseFunc(mouse);
33 glutMotionFunc(motion);
34 glutMainLoop();
35
36}
全局變數:
101float mouseX, mouseY;
2const GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
3const GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
4const GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
5GLfloat light_position[] = {0.0f, 0.0f, -1.0f, 0.0f};
6
7const GLfloat mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
8const GLfloat mat_diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
9const GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
10const GLfloat high_shininess[] = {100.0f};
display函數如下:
141static void display(void) {
2
3 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
4 glPushMatrix();
5 {
6 float x = (mouseX - 250) / 250.0f;
7 float y = (mouseY - 250) / 250.0f;
8 glRotatef(x * 180, 0, 1, 0);
9 glRotatef(y * 180, 1, 0, 0);
10 glutSolidTeapot(0.3);
11 }
12 glPopMatrix();
13 glutSwapBuffers();//交換雙緩存區
14}
void motion(int x, int y) { mouseX = x; mouseY = y; glutPostRedisplay(); }
這時就會得到一個可以跟隨滑鼠轉動的茶壺
根據我的嘗試和推測,打光的方向是:
11GLfloat light_position[] = {0.0f, 0.0f, -1.0f, 0.0f};
前三個分別是x,y,z坐標。最後一個暫時未知。
一顆茶壶自動旋轉
11extern void glutIdleFunc(void (*)(void) func)
這個函數是設定空閒時的作業。
31void idle(void) {
2 glutPostRedisplay();
3}
空閒時重新繪製。
得到了一只會自動旋轉的茶壺。
兩顆茶壺一顆旋轉一顆不動
修改程式碼:
191int angle = 0;
2
3static void display(void) {
4
5 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
6 glPushMatrix();
7 {
8 glRotatef(angle++, 0, 1, 0);
9 glutSolidTeapot(0.3);
10 }
11 glPopMatrix();
12 glPushMatrix();
13 {
14 glTranslatef(0, 0.5, 0);
15 glutSolidTeapot(0.3);
16 }
17 glPopMatrix();
18 glutSwapBuffers();//交換雙緩存區
19}
上面不動下面動
基礎圖形
圓錐
- 第一個參數是半徑
- 高度
- 切片數
- 堆疊數
11 void glutSolidCone(GLdouble base, GLdouble height, GLint slices, GLint stacks)
十面體
11void glutSolidDodecahedron(void)
正方體
11void glutSolidCube(GLdouble size)
甜甜圈
11void glutSolidTorus(GLdouble innerRadius, GLdouble outerRadius, GLint sides, GLint rings)
沒有留言:
張貼留言