views:

1395

answers:

2

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();
+1  A: 

Sounds like the classic speed/memory trade off. If memory is very scarce, then try to use a data structure that is dynamic. If you have a reasonable bound for the size of the array, and from that equation you know exactly how many verts you need, then just use a plain old static array with a counter like you suggested.

Looks like you know about the arrays, and draw arrays, so I won't go into that.

Good luck!

JPhi1618
+1  A: 

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 :)

ParoXoN