glDrawElements VAO / VBO在iOS上崩溃

时间:2012-03-22 00:37:41

标签: iphone ios opengl-es 3d opengl-es-2.0

我正在创建一个使用VAO和VBO来管理网格的批处理类。但是,在尝试使用glDrawElements时,在绑定回我的VAO时会得到EXEC_BAD_ACCESS和GL_INVALID_OPERATION。这是代码:

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);

在尝试使用GL_ELEMENT_ARRAY_BUFFER VBO时,我知道为什么会收到EXEC_BAD_ACCESS?

1 个答案:

答案 0 :(得分:1)

您确定以下陈述是正确的吗?

  • 所有OpenGL函数都没有设置错误 - 在EX的glXYZ函数之后调用glGetError并检查结果。
  • kStride> = 24
  • length == vertCount * kStride
  • index数组具有GLushort类型
  • 的indexCount元素
  • 索引数组的所有元素的值都小于vertCount值
  • 没有其他glEnableVertexAttribArray调用