Question

In my code I have a Mesh class that is a wrapper for basic VAO and VBO functionality. It's constructor takes an array of vertices and indices, and has a draw() function.

I call the glGen* functions during its instantiation and glDelete* functions in its destructor.

Clearly this leaves problems during assignment / copy construction:

Mesh A;    
{    
     Mesh B( myVerts, myIndices );  
     A = B;
     A.draw();   // this works because the VAO and VBOs of B are still around
}
A.draw();        // fails because the destructor of B calls glDelete

To remedy this, in the assignment and copy constructor I rebuffer VBO data using glMapBuffer:

Mesh& Mesh::operator = ( const Mesh &otherMesh ) 
{
    glGenBuffers( 1, &_vertexBufferObject );

    otherMesh.bind();
    void *data = glMapBuffer( GL_ARRAY_BUFFER, GL_READ_WRITE );

    bind();
    glBufferData(GL_ARRAY_BUFFER, _numBytes, data, GL_STREAM_DRAW);

    otherMesh.bind();
    glUnmapBuffer(GL_ARRAY_BUFFER);   
}

Is there a way to copy the VAO state from one VAO to another. Or must I re-call all the glVertexAttribPointer() etc functions for a fresh VAO.

Was it helpful?

Solution

Is there a reason you are using meshes as the actual drawable type?

Typically a mesh/triangle soup would be a shared resource and you would instantiate them for drawing using a model/object. When you instantiate a model/object that needs a particular mesh you would increase a reference count and store a pointer - this is resource management 101 in a nut shell.

Call them what you will, I think you are not properly distinguishing between where the data is stored and the interface necessary to render it. You should not need a VAO for each and every instance of an object, and deletion of VAO/VBO should only occur once nothing references the vertex data any longer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top