Hi, i try to implement a OpenGL ES2 rendering engine, and what ever I do I get "EXC_BAD_ACCESS" when calling glDrawElements in my draw function. I use a vertex buffer object for the vertex data and one for the indices.
Here is the init function:
// generate the buffers
GL_CHECK(glGenBuffers(1, &_vbo));
GL_CHECK(glGenBuffers(1, &_ibo));
// vertex data
int vertexcount = 8;
int structsize = sizeof(VertexData);
LOG_TRACE("number of vertices: %d, size of vertexdata struct: %d", vertexcount, structsize);
GLsizeiptr sizeVertexData = sizeof(VertexData)* vertexcount;
LOG_TRACE("sizeVertexData=%d", sizeVertexData);
// trace the first 3 variables of container
LOG_TRACE("vertexdata, first 3 values: x=%f|y=%f|z=%f", aVertices[0].px, aVertices[0].py, aVertices[0].pz);
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, _vbo));
GL_CHECK(glBufferData(GL_ARRAY_BUFFER, sizeVertexData, &aVertices[0], GL_STATIC_DRAW ));
// vertex indices
GLsizeiptr sizeVertexIndex = sizeof(unsigned short)*12*3;
LOG_TRACE("sizeVertexIndex=%d", sizeVertexIndex);
// trace the first 3 variables of container
LOG_TRACE("faceindices, first 3 values: A=%d|B=%d|C=%d", aIndices[0], aIndices[1], aIndices[2]);
GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo));
GL_CHECK(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeVertexIndex, &aIndices[0], GL_STATIC_DRAW ));
// release them
GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
_iLocPosition = shaderProgram()->attributeLocation("a_position");
LOG_DEBUG("_iLocPosition=%d", _iLocPosition);
_iLocColor = shaderProgram()->attributeLocation("a_color");
LOG_DEBUG("_iLocColor=%d", _iLocColor);
//_iLocNormal = shaderProgram()->attributeLocation("a_normal");
_iLocModelMatrix = shaderProgram()->uniformLocation("u_modelMatrix");
LOG_DEBUG("_iLocModelMatrix=%d", _iLocModelMatrix);And here is the draw function:
float m[16];
Matrix4X4C modelMat = modelMatrix();
modelMat.Rotate(_zRot, _xRot, _yRot);
setModelMatrix(modelMat);
modelMat.GetOpenGL(m);
GL_CHECK(glUniformMatrix4fv(_iLocModelMatrix, 1, GL_FALSE, m));
//LOG_TRACE("setting model matrix into shader to uniform id: %d", _iLocModelMatrix);
//shaderProgram()->bind();
// bind the buffers
//LOG_TRACE("bind VBO & IBO");
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, _vbo));
GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo));
//LOG_TRACE("enable Vertex array for attribute: %d", _iLocPosition);
GL_CHECK(glEnableVertexAttribArray(_iLocPosition));
//LOG_TRACE("enable Vertex array for attribute: %d", _iLocColor);
GL_CHECK(glEnableVertexAttribArray(_iLocColor));
//GL_CHECK(glEnableVertexAttribArray(_iLocNormal));
void* offset = BUFFER_OFFSET(0);
GL_CHECK(glVertexAttribPointer(_iLocPosition, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, sizeof(VertexData), offset));
offset = BUFFER_OFFSET(VERTEX_POS_SIZE*VERTEX_ELEMENT_TYPE_SIZE);
GL_CHECK(glVertexAttribPointer(_iLocColor, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, sizeof(VertexData), offset));
//GL_CHECK(glVertexAttribPointer(_iLocNormal, VERTEX_NORMAL_SIZE, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(VERTEX_POS_SIZE*VERTEX_ELEMENT_TYPE_SIZE)));
GL_CHECK(glDrawElements(GL_TRIANGLES, 12*3, GL_UNSIGNED_SHORT, 0));
//GL_CHECK(glDisableVertexAttribArray(_iLocNormal));
GL_CHECK(glDisableVertexAttribArray(_iLocPosition));
GL_CHECK(glDisableVertexAttribArray(_iLocColor));
// release them
GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0)); The same code runs fine under Windows via the ARM OpenGL ES 2.0 emulator framework (http://www.malideveloper.com/opengl-es-20-emulator.php).
Could someone help here?