OpenGL, glDrawArrays and iOS, poor access?

I am starting to work with OpenGL on iOS. I have always learned how to draw material in OpenGL using glBegin() and glEnd() , so for me this is a bit new.

I am trying to draw a simple triangle. I can draw a white triangle beautifully, I can even draw an entire color triangle using glColor . But whenever I try to assign a color to each vertex using this code below, I get EXC_BAD_ACCESS when drawing the array. For this, I use the iOS 4.3 simulator. 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]; } 
+4
source share
2 answers

The line glColorPointer(3, GL_FLOAT, 0, colours) generates a GL_INVALID_VALUE error (you can see that the execution of po glGetError immediately after the execution of this line will print 1281 ).

The reason is that OpenGL ES does not support 3 color components, the documentation says:

GL_INVALID_VALUE is created if the size is not 4.

The code will be fine if you change the number of color components to 4 by adding alpha.

+5
source

Your OpenGL code looks correct. Does calling glDrawArrays bad access or bad access to it? I just can imagine that the glDrawArrays func pointer is not initialized, vertex arrays should be accessible.

You can call this function after glEnableClientState(GL_COLOR_ARRAY); as a test to reset any other obsolete array pointers that might cause bad access:

 ///@brief Emulation of call glClientAttribDefaultEXT(GL_CLIENT_VERTEX_ARRAY_BIT) according to GL_EXT_direct_state_access. static void ClientAttribDefaultVertexArray(void) { int i; GLint max; glBindBufferARB(GL_ARRAY_BUFFER, 0); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0); glDisableClientState(GL_EDGE_FLAG_ARRAY); glEdgeFlagPointer(0, 0); glDisableClientState(GL_INDEX_ARRAY); glIndexPointer(GL_FLOAT, 0, 0); glDisableClientState(GL_SECONDARY_COLOR_ARRAY); glSecondaryColorPointer(4, GL_FLOAT, 0, 0); glDisableClientState(GL_FOG_COORD_ARRAY); glFogCoordPointer(GL_FLOAT, 0, 0); glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max); for (i = 0; i < max; ++i) { glClientActiveTextureARB(GL_TEXTURE0 + i); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(4, GL_FLOAT, 0, 0); } glDisableClientState(GL_COLOR_ARRAY); glColorPointer(4, GL_FLOAT, 0, 0); glDisableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, 0, 0); glDisableClientState(GL_VERTEX_ARRAY); glVertexPointer(4, GL_FLOAT, 0, 0); glDisableClientState(GL_WEIGHT_ARRAY_ARB); glWeightPointerARB(0, GL_FLOAT, 0, 0); glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max); for (i = 0; i < max; ++i) { glDisableVertexAttribArrayARB(i); glVertexAttribPointerARB(i, 4, GL_FLOAT, GL_FALSE, 0, 0); } glClientActiveTextureARB(GL_TEXTURE0); } 

In addition, you can click and display the state of the vertex array in the client attribute stack:

 glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); ///vertex array init and usage glPopClientAttrib(); 
+1
source

Source: https://habr.com/ru/post/1396035/


All Articles