Grimless
Grimless

Reputation: 1266

glDrawElements VAO/VBO crash on iOS

I am creating a batching class that uses VAOs and VBOs to manage meshes. However, when attempting to use glDrawElements, I get EXEC_BAD_ACCESS and GL_INVALID_OPERATION when binding back to my VAO. Here is the code:

glGenVertexArraysOES(1, &arrayID);      
glBindVertexArrayOES(arrayID);    // Bind INTO VAO, opening state

// Load shaders and textures and bind them using glUseProgram etc.

glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glGenBuffers(1, &indexID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID); 
glBindVertexArrayOES(0);    // Bind AWAY from VAO, saving state

Glfloat data[length];

glBindVertexArrayOES(arrayID);    // Bind INTO VAO, open state

unsigned int glfloatsize = sizeof(GLfloat);
unsigned int stride = kStride * glfloatsize;

// Fill Vertex information
glBufferData(GL_ARRAY_BUFFER, vertCount * glfloatsize * kStride, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertCount * glfloatsize * kStride, data);   

glEnableVertexAttribArray(kPositionLocation);
glVertexAttribPointer(kPositionLocation, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(0));

glEnableVertexAttribArray(kNormalLocation);
glVertexAttribPointer(kNormalLocation, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(3));

glEnableVertexAttribArray(kColorLocation);
glVertexAttribPointer(kColorLocation, 4, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(6));

glClientActiveTexture(GL_TEXTURE0);
glEnableVertexAttribArray(kTextureLocation);
glVertexAttribPointer(kTextureLocation, 2, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(10));

// Fill Index information
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(GLushort), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexCount * sizeof(GLushort), index);

glBindVertexArrayOES(0);    // Bind AWAY from VAO, saving state

// DO OTHER STUFF

/** RENDER (EXPLODES WITH EXEC_BAD_ACCESS) **/
glBindVertexArrayOES(arrayID);
glDrawElements(renderMode, indexCount, GL_UNSIGNED_SHORT, 0);
glBindVertexArrayOES(0);

/** RENDER (WORKS CORRECTLY [index is a scoped array of GLushorts that are uploaded to the VBO above...]) **/
glBindVertexArrayOES(arrayID);
glDrawElements(renderMode, indexCount, GL_UNSIGNED_SHORT, index);
glBindVertexArrayOES(0);

Any idea why I am receiving EXEC_BAD_ACCESS when attempting to use a GL_ELEMENT_ARRAY_BUFFER VBO?

Upvotes: 0

Views: 2235

Answers (1)

Mārtiņš Možeiko
Mārtiņš Možeiko

Reputation: 12927

Are you sure that following statements are true?

  • all OpenGL function doesn't set error - call glGetError after EACH of glXYZ function and check the result.
  • kStride >= 24
  • length == vertCount * kStride
  • index array has indexCount elements with GLushort type
  • all elements of index array has value less than vertCount value
  • there are no other glEnableVertexAttribArray calls

Upvotes: 1

Related Questions