Spherical Mesh.

For my next openGL hurdle, I'm making the jump to 3 dimensional and pseudo-3D graphics. For now, all I need the game to be able to draw is texture-mapped planes and spheres, the former of which I can handle without trouble.

Anyways, whipping out my first-second year calculus book, I found the section on spherical coordinates (who knew polar coordinates would ACTUALLY be practical).

I came up with the following code for initializing a quadrilateral mesh for a sphere. Keep in mind I want the spheres in my game to appear as rounded as possible. This one has 36*16 + 2 vertices = 578 vertices. I'm not sure how to convert that to "number of polygons," but I know there's a simple formula out there.

so what I do is set the sphere's bottom and top "axis" points, or north and south points as I call them. Then at ten degree increments, I set the appropriate 3 dimensional pixel coordinates.

for drawing, I'm thinking I should first draw triangles from the north and south points to their respective "nearest meridians," then take all (map_x, map_y) points up to the second last, and draw quadrilaterals at indexes (map_x, map_y) = {(x,y); (x+1,y); (x,y+1); (x+1,y+1)}.

I haven't yet tested this, and I'm afraid to because I expect there is something fundamentally wrong with my approach. Any suggestions / criticism and / or sample code are greatly appreciated!

This is my code for defining a spherical mesh. Note I define three new vertices for each triangle in the mesh. It would be nicer to define each vertex once and just assign vertex indices to the triangles.

Along the lines of making spheres, can someone help me identify what PID2 means in the following code? I figure TWOPI is simply 2 * M_PI, but PID2 still gets me. I know this isn't an efficient way of making spheres (unless calling it in a display list and using that), but I just want to make a sphere. I've identified where the PID2 shows up using a <------- after the line. This might also help Dave out with figuring out if his code will work by comparing it to this (though I can't be sure this works until I figure out that PID2 variable).

It is indeed M_PI / 2. It's used to adjust the texture as the way the code does it results in the poles being around the equator. By subtracting pi/2 it fixes this problem. That code I posted does in fact work now that I know that. Thanks.

Hi TomorrowPlusX.
I too am trying to get up the steep learning curve of OpenGL ES for iPhone development.
I'm embarrassed to say I have little Objective-C experience too.

My first step is to create a rotating smooth sphere (subdivided icosahedron) with a tennis-ball texture and then build from there. I can get an icosahedron to rotate, but I'm stumped when it comes to 1) subdividing it to smooth it out, and then assuming I can get it to appear 2) apply a texture to it.

I'm pulling my hair out trying to get your code to work - I just get a black screen when trying to subdivide!