I’m starting to work with OpenGL in iOS. I have always learned to draw stuff in OpenGL using glBegin() and glEnd() so this is kind of new to me.
I am trying to draw a simple triangle. I can draw a white triangle nicely, I even can draw an entire colored triangle using glColor. But whenever I try to assign a color to each vertex using this code below, I get a EXC_BAD_ACCESS when drawing the array. I am using iOS 4.3 simulator for this. What am I doing wrong?
- (void) render:(CADisplayLink*)displayLink {
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat vertices [] = {0,0,0, 0,100,0, 100,0,0};
GLfloat colours [] = {1.0,1.0,1.0, 1.0,1.0,1.0, 1.0,1.0,1.0};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colours);
glDrawArrays(GL_TRIANGLES, 0, 3); <-- CRASHES HERE
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
[self.context presentRenderbuffer:GL_RENDERBUFFER];
}
The line
glColorPointer(3, GL_FLOAT, 0, colours)generates theGL_INVALID_VALUEerror (you can see that doingpo glGetErrorright after executing that line, it will print1281).The reason is that OpenGL ES doesn’t support 3 color components, the documentation states:
You code will be ok if you change the number of color components to 4 by adding alpha.