Iphone – OpenGL to OpenGL-ES – glBegin();

iphoneopengl-es

I am trying to learn to write OpenGL apps for the iPhone. How can I port the following code to work with OpenGL-ES? I know that I must store the vertices in an array and then call glDrawArrays(), but is there an optimal way to do this? My thought is to create a very large array and simply keep a counter of how many spaces are filled. This there a better approach? What about using an NSArray and then converting back to a c array?

glBegin(GL_LINE_STRIP);

z = -50.0f;
for(angle = 0.0f; angle <= (2.0f*3.1415f)*3.0f; angle += 0.1f)
    {
    x = 50.0f*sin(angle);
    y = 50.0f*cos(angle);

    // Specify the point and move the Z value up a little   
    glVertex3f(x, y, z);
    z += 0.5f;
    }

// Done drawing points
glEnd();

Best Solution

If you have sufficient space and know the maximum size of the array, it's simplest to use one big statically allocated array and just keep track of its current logical size.

If memory is scant but processor resources are plentiful then pack the array on the fly and then register the arrays and call glDrawArrays().

The hybrid approach would be to use a dynamically allocated array that can be updated (if necessary). You can do this manually by reallocating a dynamic array when it approaches becoming full or by using an NSMutableArray of some sort. (N.B. NSArray is for static arrays; NSMutableArrays (subclass) are dynamic. See here.)

Hope this helped :)

Related Question