views:

64

answers:

2
+3  A: 

The reason you're seeing weird results is because you're not drawing what you think you are.

glDrawArrays(GL_TRIANGLES, 0, 9);

This means draw 9 vertices, hence 3 triangles. You only have 3 vertices declared in your array, so what the other 2 triangles will end up being is anybody's guess. You can see in the second picture that you indeed have more than 1 triangle... The data it ends up using is whatever else is on the stack at that time.

glDrawArrays does transfer on call, and I seriously doubt the iPhone would not be compliant on this. It's really basic GL, that (I believe) gets tested for conformance.

Bahbar
Of course, you are right. Apologies for not spotting that. I would delete my answer if I could (but it is still the accepted answer).
Josef Grahn
Gah! What a red herring. Thank you.
Jaka Jančar
+1  A: 

You are rendering uninitialized data in both cases. It just happens to look correct in the static case.

The third parameter to glDrawArrays is the number of vertices (which should be 3 in your case because you are trying to draw a single triangle).

You already told the GL that you are specifying 3 GLfloat per vertex (the first parameter of glVertexPointer). So the GL can figure out the total number of GLfloat to expect.

This should work:

GLfloat vertices[3][3] = 
{    
    {0.0, 1.0, 0.0},
    {1.0, 0.0, 0.0},
    {-1.0, 0.0, 0.0}
};

glColor4ub(255, 0, 0, 255);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
Jon-Eric