views:

147

answers:

1

Okay, so I'm working on an OpenGL ES application for the iPhone, and I ran into an interesting issue.

I have a function that computes the vertices, normals, and texture coordinates of a sphere dependent upon a detail level and a range of spherical coordinates.

Originally, storing a vertex in an array looked something like this:

//After I figure out the size of the vertices:
GLfloat* vertices = (GLfloat *) (malloc(sizeof(GLfloat) * arraySize)));

//Later on when I'm computing vertices...
GLfloat* vertPosition = vertices;

vertPosition[0] = px;
vertPosition[1] = py;
vertPosition[2] = pz;
vertPosition += 3;

This turned out to be a disaster. I ended up with something like the AT&T logo. :-) A bit more work I discovered adding a counter and using that to index the array fixed everything:

vertPosition[(vertexCount * 3) + 0] = px;
vertPosition[(vertexCount * 3) + 1] = py;
vertPosition[(vertexCount * 3) + 2] = pz;
vertexCount++;

My questions are: What happened with using a temporary pointer and moving it ahead? Is what I've got now just as efficient, or would the pointer arithmetic be better?

+2  A: 

From the looks of what you posted it should be exactly the same. The first thing that I can think of is that the bug is somewhere else in your code.

The only other thing that I can think of is if instead of taking an array of verts, it might actually takes an array of coords and the coords might have padding in them then that could also explain the difference.

KPexEA