OpenGL ES ... glEnable(GL_LIGHTING) causes crash?
So I am trying to add lighting to an opengl es iphone app and I have run into a bit of a snag. If I mak a glEnable(GL_LIGHTING) call the program will crash whenever I make a glDrawElements() call. If I comment out the glEnable(GL_LIGHTING) call then the program runs fine (obviously without lighting though). Here is my entire setup.
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
CGRect rect = view.bounds;
glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size /
(rect.size.width / rect.size.height), zNear, zFar);
glViewport(0, 0, rect.size.width, rect.size.height);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_LIGHTING); //***********//
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
What am I doing that openGL ES is objecting to? THNX
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
CGRect rect = view.bounds;
glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size /
(rect.size.width / rect.size.height), zNear, zFar);
glViewport(0, 0, rect.size.width, rect.size.height);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_LIGHTING); //***********//
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
What am I doing that openGL ES is objecting to? THNX
That code's all mixed up and not enough to go off of. We'll probably need more than that to help you out.
I would make sure your normal array is correct. DrawElements will crash if it reads an invalid address while iterating across your array.
As a first order test, call glDisableClientState(GL_NORMAL_ARRAY) before the call to DrawElements.
As a first order test, call glDisableClientState(GL_NORMAL_ARRAY) before the call to DrawElements.
Heck, it seems like any mis-set client state will do that (colors, verts, etc.). I was going to suggest the same, but... I don't trust my mind reading skills!
Frogblast Wrote:I would make sure your normal array is correct. DrawElements will crash if it reads an invalid address while iterating across your array.
As a first order test, call glDisableClientState(GL_NORMAL_ARRAY) before the call to DrawElements.
Yah. That solved the crashing problem. I am drawing a normal icosahedron so I was just using the vertex array as the normal array. This is the relevant code
glVertexPointer(3, GL_FLOAT, 0, sphere->icosVertices);
glTexCoordPointer(2, GL_FLOAT, 0, sphere->icosTextureVertices);
glNormalPointer(3 , GL_FLOAT , sphere->icosVertices);
for(int i = 0; i < sphere->numFaces ; i ++)
{ glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, &sphere->icosFaces[i*3]);
}
Not sure why it would like the vertex array for the vertices but dislike it for the normals?
Awe crap. That call should be
glNormalPointer( GL_FLOAT ,0, sphere->icosVertices);
Sry. It's been 3 years or so since I've done any coding. Works now.
glNormalPointer( GL_FLOAT ,0, sphere->icosVertices);
Sry. It's been 3 years or so since I've done any coding. Works now.

