範例程式
執行結果
程式說明
- 這個範例程式使用的是 immediate mode 渲染方式,必須在 OpenGL 3.1 以下版本執行。由於這種渲染方式的效率較差,因此 OpenGL 3.1 之後的版本已經不再支援這種渲染方式。
- 新的版本採用 shaders 來進行渲染,必須提供 vertex shader 和 fragment shader。為了避免一開始入門就涉及到 shaders、VAO、VBO 等稍難理解的知識點,因此以 immediate mode 的範例來切入學習。
- 頂點座標數據:
|
GLfloat vertices[][3] = { {-1.0,-1.0,-1.0}, { 1.0,-1.0,-1.0}, { 1.0, 1.0,-1.0}, {-1.0, 1.0,-1.0}, {-1.0,-1.0, 1.0}, { 1.0,-1.0, 1.0}, { 1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0} }; |
分別依序對應到下方圖示的八個頂點:
- 頂點法向量數據:
|
GLfloat normals[][3] = { {-1.0,-1.0,-1.0}, { 1.0,-1.0,-1.0}, { 1.0, 1.0,-1.0}, {-1.0, 1.0,-1.0}, {-1.0,-1.0, 1.0}, { 1.0,-1.0, 1.0}, { 1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0} }; |
- 頂點顏色數據:
|
GLfloat colors[][3] = { {0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0} }; |
頂點 0:黑色
頂點 1:紅色
頂點 2:黃色
頂點 3:綠色
頂點 4:藍色
頂點 5:桃紅色
頂點 6:白色
頂點 7:淡藍色
- 主函式:
|
int main(int argc, char **argv) { glutInit(&argc, argv);
/* need both double buffering and z buffer */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube");
glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glutMainLoop();
return 0; } |
glutInit(&argc, argv):初始化 GLUT 函式庫。
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH):設定顯示模式使用 double buffers, depth buffer 和 RGBA 顏色模式。
glutInitWindowSize(500, 500):設定視窗大小為 500 像素 * 500 像素。
glutCreateWindow("colorcube"):創建一個視窗,視窗標題為「colorcube」。
glutReshapeFunc(myReshape):註冊 reshape callback function。當視窗被調整大小 (resize) 後,系統會調用 myReshape() 函式。
glutDisplayFunc(display):註冊 display callback function。當視窗需要重繪時,系統會調用 display() 函式。
glutIdleFunc(spinCube):註冊 idle callback function。當沒有 event 要處理時,系統會調用 spinCube() 函式。
glutMouseFunc(mouse):註冊 mouse callback function。當有 mouse event 要處理時,系統會調用 mouse() 函式。
glEnable(GL_DEPTH_TEST):啟用 depth test,以檢測圖像的 depth 資訊,並更新到 depth buffer。
glutMainLoop():進入 main event loop,不斷循環進行 event 接收與處理。
- display() 函式:
|
void display(void) { /* display callback, clear frame buffer and z buffer, rotate cube and draw, swap buffers */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glFlush(); glutSwapBuffers(); } |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT):清除 frame buffer 和 depth buffer。
glLoadIdentity():重置當前指定的矩陣為單位矩陣。
glRotatef(theta[0], 1.0, 0.0, 0.0):以 X 軸為軸心旋轉 theta[0] 度。
glRotatef(theta[1], 0.0, 1.0, 0.0):以 Y 軸為軸心旋轉 theta[1] 度。
glRotatef(theta[2], 0.0, 0.0, 1.0):以 Z 軸為軸心旋轉 theta[2] 度。
glFlush():刷新命令緩衝區,將緩衝區的所有繪圖指令立刻提交給 GPU 執行。此函式會立即返回,不會等待 GPU 執行完所有指令才返回,有別於 glFinish()。
glutSwapBuffers():置換 front buffer 和 back buffer,將 back buffer 置前。即在螢幕上顯示原為 back buffer 所設置的圖像。
- colorcube() 函式:
|
void colorcube(void) { /* map vertices to faces */ polygon(0, 3, 2, 1); polygon(2, 3, 7, 6); polygon(0, 4, 7, 3); polygon(1, 2, 6, 5); polygon(4, 5, 6, 7); polygon(0, 1, 5, 4); } |
polygon(0, 3, 2, 1):繪製由 vertices[0], vertices[3], vertices[2] 和 vertices[1] 所構成的多邊形。
polygon(2, 3, 7, 6):繪製由 vertices[2], vertices[3], vertices[7] 和 vertices[6] 所構成的多邊形。
以此類推,總共繪製 6 個多邊形,也就是正六面體的六個面。
- polygon() 函式:
|
void polygon(int a, int b, int c , int d) { /* draw a polygon via list of vertices */ glBegin(GL_POLYGON); glColor3fv(colors[a]); glNormal3fv(normals[a]); glVertex3fv(vertices[a]); glColor3fv(colors[b]); glNormal3fv(normals[b]); glVertex3fv(vertices[b]); glColor3fv(colors[c]); glNormal3fv(normals[c]); glVertex3fv(vertices[c]); glColor3fv(colors[d]); glNormal3fv(normals[d]); glVertex3fv(vertices[d]); glEnd(); } |
glBegin(GL_POLYGON):繪製 polygon 圖元 (primitive)。
glColor3fv():設定當前使用的顏色。
glNormal3fv():設定法向量。
glVertex3fv():設定頂點所在座標。
glBegin() & glEnd():在這兩個函式之間,可以設定一組或多組圖元的頂點配置。在此設定了四個頂點的屬性,繪製了一個由四個頂點所形成的多邊形。
這個範例也可以不設定法向量,設置頂點法向量是為了讓光照效果較為平滑,但是有可能會讓有稜角的地方變得平滑化而不明顯,要解決這個問題,可以針對一個頂點在不同平面給予不同的法向量。
可以參考下方連結說明:
https://zh.m.wikipedia.org/zh-tw/%E9%A0%82%E9%BB%9E%E6%B3%95%E5%90%91%E9%87%8F
- 旋轉軸與旋轉角度:
|
static GLfloat theta[] = {0.0, 0.0, 0.0}; static GLint axis = 2; |
變數 axis 儲存當前的旋轉軸設定:設定值 0 表示以 X 軸為旋轉軸,1 表示以 Y 軸為旋轉軸,2 表示以 Z 軸為旋轉軸。預設以 Z 軸為旋轉軸。
陣列 theta 儲存當前的旋轉角度:theta[0], theta[1], theta[2] 分別對應到以 X 軸、Y 軸、Z 軸為軸心的旋轉角度。預設皆由 0 度開始遞增。
- spinCube() 函式:
|
void spinCube() { /* Idle callback, spin cube 2 degrees about selected axis */ theta[axis] += 0.2; if (theta[axis] > 360.0) theta[axis] -= 360.0; /* display(); */ glutPostRedisplay(); } |
系統每次呼叫此函式,就將當前旋轉軸的旋轉角度累加 0.2 度。若超過 360 度,就往回扣到 360 度以內。
glutPostRedisplay():標記當前視窗需要重新繪製,即通知系統調用 display() 函式進行重繪。
- mouse() 函式:
|
void mouse(int btn, int state, int x, int y) { /* mouse callback, selects an axis about which to rotate */ if (btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if (btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if (btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; } |
若滑鼠左鍵被按下,就將當前旋轉軸設定為 X 軸;若滑鼠中鍵被按下,就將當前旋轉軸設定為 Y 軸;若滑鼠右鍵被按下,就將當前旋轉軸設定為 Z 軸。
- myReshape() 函式:
|
void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); } |
glViewport(0, 0, w, h):指定視窗的一個矩形區域作為視口 (viewport),在視口中顯示繪製的圖形。在此設定以整個視窗作為視口。
glMatrixMode(GL_PROJECTION):將投影矩陣設定為當前矩陣,即後續的矩陣操作將作用在投影矩陣上。
glLoadIdentity():重置當前指定的矩陣為單位矩陣。
glOrtho():設定正交平行取景區的大小和比例。若視窗的高度大於寬度,就等比例放大上、下邊界;若視窗的寬度大於高度,就等比例放大左、右邊界。這樣才能配合視窗的顯示比例,否則顯示出來的圖形會變形。
glMatrixMode(GL_MODELVIEW):將當前矩陣設定回 ModelView 矩陣,ModelView 矩陣是較常操作的矩陣,因此設定完其他矩陣後,會切回 ModelView 矩陣。
參考資料
- Computer Graphics 2013
http://140.122.185.90/CG/2013/
- openGL 函数 - glBegin,glEnd
https://blog.csdn.net/qq844352155/article/details/28458029
- opengl第一课 glBegin glend 绘图
https://blog.csdn.net/cai13160674275/article/details/59125331
- 頂點法向量
https://zh.m.wikipedia.org/zh-tw/%E9%A0%82%E9%BB%9E%E6%B3%95%E5%90%91%E9%87%8F
- glViewport()函数和glOrtho()函数的理解
https://blog.csdn.net/shizhipeng/article/details/4939529
- glOrtho 函式
https://docs.microsoft.com/zh-tw/windows/win32/opengl/glortho


沒有留言:
發佈留言