I want to start using VBOs to render my models, but I'm running into an error I haven't been able to resolve. I've seen that this occurs when people forget to unbind the buffer, but as far as I can tell I'm doing that correctly. I'll spare you the print statements but I have also determined there is an openGL error 1281 after I call
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
in the drawMe() function. I guess 1281 indicates bad value... but I don't know what that means here. If you can see what is going wrong, please enlighten me.
I initialize _vboID to 0 in the construction of the model object.
_vboID = 0;
Then :
void model::genBuffers() {
if (_vboID == 0){
glGenBuffers(1, &_vboID);
float vertexData[numFaces*3*3]; // numFaces * 3 vertices per face * 3 dimensions per vertex
/* fill */
int i = 0;
Polyhedron::Halfedge_const_handle h;
for (Polyhedron::Facet_const_iterator faceIter = polyhedron.facets_begin(); faceIter != polyhedron.facets_end(); ++faceIter) {
CGAL::Point_3<Kernel> p1,p2,p3;
h = faceIter->halfedge();
p1 = h->vertex()->point();
p2 = h->next()->vertex()->point();
p3 = h->prev()->vertex()->point();
vertexData[i] = p1.hx(); i++;
vertexData[i] = p1.hy(); i++;
vertexData[i] = p1.hz(); i++;
vertexData[i] = p2.hx(); i++;
vertexData[i] = p2.hy(); i++;
vertexData[i] = p2.hz(); i++;
vertexData[i] = p3.hx(); i++;
vertexData[i] = p3.hy(); i++;
vertexData[i] = p3.hz(); i++;
}
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind Buffer
}
}
void model::drawMe() {
glBindBuffer(GL_ARRAY_BUFFER, _vboID); /* glError 1281 generated after this line?? */
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);<<std::endl;
glDrawArrays(GL_TRIANGLES, 0, numFaces*3); /* crashes here */
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Error Message:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000008
VM Regions Near 0x8: --> __TEXT 0000000107ed3000-00000001081a9000 [ 2904K] r-x/rwx SM=COW /Users/USER/Documents/*/project_vVBO.app/Contents/MacOS/project_vVBO
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 GLEngine 0x00007fff99088f9f gleRunVertexSubmitImmediate + 10223
1 GLEngine 0x00007fff98f558ce glDrawArrays_Exec + 626