西蒙iphone-OpenGL ES 教程 -08

const GLfloat pyramidVertices[] = {
// Our pyramid consists of 4 triangles and a square base.
-1.0, -1.0, 1.0,            // front left of base
1.0, -1.0, 1.0,             // front right of base
1.0, -1.0, -1.0,            // rear left of base
-1.0, -1.0, -1.0,           // rear right of base

// Front face
-1.0, -1.0, 1.0,            // bottom left of triangle
1.0, -1.0, 1.0,             // bottom right
0.0, 1.0, 0.0,              // top centre -- all triangle vertices
//     will meet here

   // Our new object definition code goes here const GLfloat pyramidVertices[] = { // Our pyramid consists of 4 triangles and a square base. // We'll start with the square base -1.0, -1.0, 1.0, // front left of base 1.0, -1.0, 1.0, // front right of base 1.0, -1.0, -1.0, // rear left of base -1.0, -1.0, -1.0, // rear right of base   // Front face -1.0, -1.0, 1.0, // bottom left of triangle 1.0, -1.0, 1.0, // bottom right 0.0, 1.0, 0.0, // top centre -- all triangle vertices // will meet here   // Rear face 1.0, -1.0, -1.0, // bottom right (when viewed through front face) -1.0, -1.0, -1.0, // bottom left 0.0, 1.0, 0.0, // top centre   // left face -1.0, -1.0, -1.0, // bottom rear -1.0, -1.0, 1.0, // bottom front 0.0, 1.0, 0.0, // top centre   // right face 1.0, -1.0, 1.0, // bottom front 1.0, -1.0, -1.0, // bottom rear 0.0, 1.0, 0.0 // top centre };    

OpenGL 为我们使用这两个函数提供了快捷的方法。

glPushMatrix();

// Translation and drawing code goes here....

glPopMatrix();

OpenGL 告诉我们如果要将我们的矩阵变换放如到堆栈中就要使用 glPushMatrix().我将我们的pyramidVertices[] 和 cubeVertices[] 目标 “推” 到 OpenGL’s 的堆栈中.

Note: 如果你不明白我说的堆栈是什么意思，那么你可能需要一本c或者object c 的工具书。

glPushMatrix();
{
glTranslatef(-2.0, 0.0, -8.0);
glRotatef(rota, 1.0, 0.0, 0.0);
glVertexPointer(3, GL_FLOAT, 0, pyramidVertices);
glEnableClientState(GL_VERTEX_ARRAY);

// Draw the pyramid
// Draw the base -- it's a square remember
glColor4f(1.0, 0.0, 0.0, 1.0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

Ok,我们完成矩形了，现在开始绘制第一个正面的三角形。

// Front Face
glColor4f(0.0, 1.0, 0.0, 1.0);
glDrawArrays(GL_TRIANGLES, 4, 3);

// Rear Face
glColor4f(0.0, 0.0, 1.0, 1.0);
glDrawArrays(GL_TRIANGLES, 7, 3);

// Right Face
glColor4f(1.0, 1.0, 0.0, 1.0);
glDrawArrays(GL_TRIANGLES, 10, 3);

// Left Face
glColor4f(1.0, 0.0, 1.0, 1.0);
glDrawArrays(GL_TRIANGLES, 13, 3);
}
glPopMatrix();

   glPushMatrix(); { glTranslatef(2.0, 0.0, -8.0); glRotatef(rota, 1.0, 1.0, 1.0); glVertexPointer(3, GL_FLOAT, 0, cubeVertices); glEnableClientState(GL_VERTEX_ARRAY);   // Draw the front face in Red glColor4f(1.0, 0.0, 0.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 0, 4);   // Draw the top face in green glColor4f(0.0, 1.0, 0.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 4, 4);   // Draw the rear face in Blue glColor4f(0.0, 0.0, 1.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 8, 4);   // Draw the bottom face glColor4f(1.0, 1.0, 0.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 12, 4);   // Draw the left face glColor4f(0.0, 1.0, 1.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 16, 4);   // Draw the right face glColor4f(1.0, 0.0, 1.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 20, 4); } glPopMatrix();  

   - (void)drawView {   // Our new object definition code goes here const GLfloat pyramidVertices[] = { // Our pyramid consists of 4 triangles and a square base. // We'll start with the square base -1.0, -1.0, 1.0, // front left of base 1.0, -1.0, 1.0, // front right of base 1.0, -1.0, -1.0, // rear left of base -1.0, -1.0, -1.0, // rear right of base   // Front face -1.0, -1.0, 1.0, // bottom left of triangle 1.0, -1.0, 1.0, // bottom right 0.0, 1.0, 0.0, // top centre -- all triangle vertices // will meet here   // Rear face 1.0, -1.0, -1.0, // bottom right (when viewed through front face) -1.0, -1.0, -1.0, // bottom left 0.0, 1.0, 0.0, // top centre   // left face -1.0, -1.0, -1.0, // bottom rear -1.0, -1.0, 1.0, // bottom front 0.0, 1.0, 0.0, // top centre   // right face 1.0, -1.0, 1.0, // bottom front 1.0, -1.0, -1.0, // bottom rear 0.0, 1.0, 0.0 // top centre };   const GLfloat cubeVertices[] = {   // Define the front face -1.0, 1.0, 1.0, // top left -1.0, -1.0, 1.0, // bottom left 1.0, -1.0, 1.0, // bottom right 1.0, 1.0, 1.0, // top right   // Top face -1.0, 1.0, -1.0, // top left (at rear) -1.0, 1.0, 1.0, // bottom left (at front) 1.0, 1.0, 1.0, // bottom right (at front) 1.0, 1.0, -1.0, // top right (at rear)   // Rear face 1.0, 1.0, -1.0, // top right (when viewed from front) 1.0, -1.0, -1.0, // bottom right -1.0, -1.0, -1.0, // bottom left -1.0, 1.0, -1.0, // top left   // bottom face -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0,   // left face -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0,   // right face 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0 };   const GLshort squareTextureCoords[] = { // Front face 0, 1, // top left 0, 0, // bottom left 1, 0, // bottom right 1, 1, // top right   // Top face 0, 1, // top left 0, 0, // bottom left 1, 0, // bottom right 1, 1, // top right   // Rear face 0, 1, // top left 0, 0, // bottom left 1, 0, // bottom right 1, 1, // top right   // Bottom face 0, 1, // top left 0, 0, // bottom left 1, 0, // bottom right 1, 1, // top right   // Left face 0, 1, // top left 0, 0, // bottom left 1, 0, // bottom right 1, 1, // top right   // Right face 0, 1, // top left 0, 0, // bottom left 1, 0, // bottom right 1, 1, // top right };   [EAGLContext setCurrentContext:context]; glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); glViewport(0, 0, backingWidth, backingHeight); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW);   glTexCoordPointer(2, GL_SHORT, 0, squareTextureCoords); glEnableClientState(GL_TEXTURE_COORD_ARRAY);   // Our new drawing code goes here rota += 0.5;   glPushMatrix(); { glTranslatef(-2.0, 0.0, -8.0); glRotatef(rota, 1.0, 0.0, 0.0); glVertexPointer(3, GL_FLOAT, 0, pyramidVertices); glEnableClientState(GL_VERTEX_ARRAY);   // Draw the pyramid // Draw the base -- it's a square remember glColor4f(1.0, 0.0, 0.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 0, 4);   // Front Face glColor4f(0.0, 1.0, 0.0, 1.0); glDrawArrays(GL_TRIANGLES, 4, 3);   // Rear Face glColor4f(0.0, 0.0, 1.0, 1.0); glDrawArrays(GL_TRIANGLES, 7, 3);   // Right Face glColor4f(1.0, 1.0, 0.0, 1.0); glDrawArrays(GL_TRIANGLES, 10, 3);   // Left Face glColor4f(1.0, 0.0, 1.0, 1.0); glDrawArrays(GL_TRIANGLES, 13, 3); } glPopMatrix();   glPushMatrix(); { glTranslatef(2.0, 0.0, -8.0); glRotatef(rota, 1.0, 1.0, 1.0); glVertexPointer(3, GL_FLOAT, 0, cubeVertices); glEnableClientState(GL_VERTEX_ARRAY);   // Draw the front face in Red glColor4f(1.0, 0.0, 0.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 0, 4);   // Draw the top face in green glColor4f(0.0, 1.0, 0.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 4, 4);   // Draw the rear face in Blue glColor4f(0.0, 0.0, 1.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 8, 4);   // Draw the bottom face glColor4f(1.0, 1.0, 0.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 12, 4);   // Draw the left face glColor4f(0.0, 1.0, 1.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 16, 4);   // Draw the right face glColor4f(1.0, 0.0, 1.0, 1.0); glDrawArrays(GL_TRIANGLE_FAN, 20, 4); } glPopMatrix();     glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); [context presentRenderbuffer:GL_RENDERBUFFER_OES];   [self checkGLError:NO]; }    

• 评论

• 下一篇
• 上一篇