0

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


kadie16
  • 49
  • 4
  • You really need error checking. Everywhere. – Ivan Aksamentov - Drop Aug 17 '15 at 06:36
  • I have it after every openGL call in these functions, but I removed it for the post. [Here is the whole file](https://github.com/kadie16/A-STAR-IHPC-Project/blob/master/project_vVBO/model.cpp) if you care to take a look. – kadie16 Aug 17 '15 at 06:49
  • 2
    Is it possible that you're (unintentionally) copying the `model` object? Try setting a breakpoint in the destructor, or add some logging there, to verify that the buffer is not deleted. – Reto Koradi Aug 17 '15 at 07:04
  • @RetoKoradi Wow, I didn't realize I was doing that. The buffer was being deleted -__- Thank you so so so much!!! – kadie16 Aug 17 '15 at 07:24
  • Welcome to the club! It happens very frequently when people wrap OpenGL objects in C++ classes. I wrote a fairly detailed answer on it here: http://stackoverflow.com/questions/28929452/mesh-class-called-with-default-constructor-not-working-opengl-c/28930189#28930189. And here: http://stackoverflow.com/questions/26665382/color-passed-to-shaders-doesnt-work/26667992#26667992. – Reto Koradi Aug 17 '15 at 07:31

1 Answers1

0

Solved

I didn't realize that I was making a copy of the "model" object, which caused the deconstructor to be called and delete the buffer I created.

model::~model()
{
    if (_vboID != 0)
        glDeleteBuffers(1, &_vboID);
}

Many thanks @RetoKoradi for bringing this to my attention

kadie16
  • 49
  • 4