學習OpenGL:筆記五
阿新 • • 發佈:2018-12-19
繪製一個正方體
正方體其實就是由6個矩形組成的幾何體,X,Y,Z軸上各兩個矩形。
先繪製X軸上兩個矩形
- (void)drawXPlanes { static GLfloat triangleData[] = { // X軸0.5處的平面 0.5, -0.5, 0.5f, 1, 0, 0, 0.5, -0.5f, -0.5f, 1, 0, 0, 0.5, 0.5f, -0.5f, 1, 0, 0, 0.5, 0.5, -0.5f, 1, 0, 0, 0.5, 0.5f, 0.5f, 1, 0, 0, 0.5, -0.5f, 0.5f, 1, 0, 0, // X軸-0.5處的平面 -0.5, -0.5, 0.5f, 1, 0, 0, -0.5, -0.5f, -0.5f, 1, 0, 0, -0.5, 0.5f, -0.5f, 1, 0, 0, -0.5, 0.5, -0.5f, 1, 0, 0, -0.5, 0.5f, 0.5f, 1, 0, 0, -0.5, -0.5f, 0.5f, 1, 0, 0, }; [self bindAttribs:triangleData]; glDrawArrays(GL_TRIANGLES,0, 12); }
效果如下圖:
接下來繪製Y軸和Z軸上的兩個矩形:
- (void)drawYPlanes { static GLfloat triangleData[] = { -0.5, 0.5, 0.5f, 0, 1, 0, -0.5f, 0.5, -0.5f, 0, 1, 0, 0.5f, 0.5, -0.5f, 0, 1, 0, 0.5, 0.5, -0.5f, 0, 1, 0, 0.5f, 0.5, 0.5f, 0, 1, 0, -0.5f, 0.5, 0.5f, 0, 1, 0, -0.5, -0.5, 0.5f, 0, 1, 0, -0.5f, -0.5, -0.5f, 0, 1, 0, 0.5f, -0.5, -0.5f, 0, 1, 0, 0.5, -0.5, -0.5f, 0, 1, 0, 0.5f, -0.5, 0.5f, 0, 1, 0, -0.5f, -0.5, 0.5f, 0, 1, 0, }; [self bindAttribs:triangleData]; glDrawArrays(GL_TRIANGLES, 0, 12); } - (void)drawZPlanes { static GLfloat triangleData[] = { -0.5, 0.5f, 0.5, 0, 0, 1, -0.5f, -0.5f, 0.5, 0, 0, 1, 0.5f, -0.5f, 0.5, 0, 0, 1, 0.5, -0.5f, 0.5, 0, 0, 1, 0.5f, 0.5f, 0.5, 0, 0, 1, -0.5f, 0.5f, 0.5, 0, 0, 1, -0.5, 0.5f, -0.5, 0, 0, 1, -0.5f, -0.5f, -0.5, 0, 0, 1, 0.5f, -0.5f, -0.5, 0, 0, 1, 0.5, -0.5f, -0.5, 0, 0, 1, 0.5f, 0.5f, -0.5, 0, 0, 1, -0.5f, 0.5f, -0.5, 0, 0, 1, }; [self bindAttribs:triangleData]; glDrawArrays(GL_TRIANGLES, 0, 12); }
效果如圖: