Get Updates By E-mail 

Article Written By:
Johann Dowa

iPhone OpenGL ES Nehe Lesson #3 Explained

  •  
  •  
  •  
  •  
  •  

In my previous post I added a download link for Nehe Lesson #3 converted to run in iPhone OpenGL ES.

This is an explanation of what’s different in iPhone OpenGL ES when compared to the original version.

In this lesson color was added to the primitive shapes from Nehe Lesson #2, now color must be done differently on the iPhone than on the original version.

In the Nehe OpenGL version of the tutorial color was added through the use of the glColorf function, specifically the lines:

glColor3f(1.0f,0.0f,0.0f);          // Set The Color To Red
glColor3f(0.0f,1.0f,0.0f);          // Set The Color To Green
glColor3f(0.0f,0.0f,1.0f);          // Set The Color To Blue
glColor3f(0.5f,0.5f,1.0f);              // Set The Color To Blue One Time Only

were added into the Lesson with these lines placed immediately after each vertex was drawn using the glVertex3f command, and just before the light blue square on the right was drawn. Now, just like the glVertex3f command the glColor3f command does not exist in OpenGL ES.

Now because these commands are missing, just like the triangle was drawn using vertex arrays, the colors for the triangle are drawn using a color array.

const GLfloat triangleColors[] = {
1.0f,   0.0f,   0.0f,   0.0f,
0.0f,   1.0f,   0.0f,   1.0f,
0.0f,   0.0f,   1.0f,   1.0f
};

Is declared, before using this color array the client state to draw this color array must be changed:

glEnableClientState(GL_COLOR_ARRAY);                    // Enable drawing of colors array

Then when drawing the triangle the command:

glColorPointer(4, GL_FLOAT, 0, triangleColors);         // create an array of colors using Glfloat colors array

is executed. As you can see this lnes up with the vertices used in the color array. After the triangle is drawn we disable the state for drawing color arrays.

glDisableClientState(GL_COLOR_ARRAY);                   // Disable drawing of colors array

Now to make the square on the right blue, and since we are only drawing a solid color we do not need to create a color array but use the glColor4f command. This is similar to the glColor3f function used in the Nehe tutorial only with an added value for alpha. In this case we use:

glColor4f(0.5f,0.5f,1.0f,1.0f);                         // Set color to blue

which causes the square to be blue, and that’s all there is to it. If you have any questions about this tutorial please feel free to post them below.


  •  
  •  
  •  
  •  
  •  

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

Comments