Get Updates By E-mail 

nehe

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.


  •  
  •  
  •  
  •  
  •  

Three More Great OpenGL ES Resources

  •  
  •  
  •  
  •  
  •  

Last week I posted a  list containing 8 resources for learning iPhone OpenGL ES.

Since then I’ve found out about 3 more great resources.

1. Nehe Tutorials Converted To OpenGL ES
Michael Gorchak, creator of the GluES library informed me that his site contains conversions of the Nehe OpenGL ES.  While these tutorials are not specifically designed to run on the iPhone there is much to be learned by looking at the draw code contained within the tutorials, as OpenGL ES 1.x and SDL 1.3 are utilized which both run on the iPhone.  These tutorials are extremely well comented.  Thanks Michael!

2. SDL for iPhone (SDL Version 1.3)
The popular SDL library used in the creation of many games is now running on the iPhone.  Version 1.3 includes examples that run on the iPhone.   If you’re not familiar with SDL it stands for Simple DirectMedia Layer, and is extremely well documented with numerous books having been written about this library.  I haven’t had a chance to play with it on the iPhone yet, but it definitely looks worth checking out.

3. Typhoon Labs OpenGL ES Tutorials
These tutorials were designed to run on Windows Mobile/CE devices using OpenGL ES 1.x, but the draw code can be used on the iPhone.

That’s it, I’ll get into explaining a couple of the differences that can throw off OpenGL ES beginners next time.

Thanks for reading.


  •  
  •  
  •  
  •  
  •  

Nehe OpenGL Tutorial #2 In iPhone OpenGL ES

  •  
  •  
  •  
  •  
  •  

As promised this is the Nehe OpenGL tutorial 2 converted to run onthe iPhone.

You can download my Xcode project file here: Nehe Tutorial #2 Converted To iPhone

Now, I won’t get into the specifics as we move along, but  I will give a breakdown of the steps I took in creating this tutorial.

1. Created an OpenGL ES project in Xcode.
2. Looked through the C++ version of the Nehe code, and placed the draw code into the Xcode project.
3. Added in missing GLU functions by taking them out of the GluES library, and removing any asm calls.
4. Placed the triangle and square vertices into a vertex array.
5. Placed the shapes a little further back to adjust for the iPhone’s  portrait view.

And that’s it! I will get into more details as we move along, and I suggest that you look through all the code, and try to find what’s different.

See you later!

PS. You can check out Jeff Lamarche’s Nehe Tutorial 2 conversion on the Iphone here.  His approach was different from mine, as he recreated it all from the ground up while I used every resource I could find since I’m all about speed of implementation.

Nehe Tutorial #2 In Iphone OpenGL ES

Nehe Tutorial #2 In Iphone OpenGL ES


  •  
  •  
  •  
  •  
  •