Hello Guest

Updating opengl buffers after changing attributes of a mesh

  • 1 Replies


I am using opengl in order to visualize geometric deformations on 3d meshes. I am using a vao to draw the mesh. This is the code I use to setup the buffers and the vao
void setupDrawingBuffers()

        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);
        glGenBuffers(1, &EBO);


        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(MyVertex),
            &vertices[0], GL_DYNAMIC_DRAW);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint),
            &indices[0], GL_DYNAMIC_DRAW);

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex),

        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex),
            (GLvoid*)offsetof(MyVertex, Normal));

        glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex),
            (GLvoid*)offsetof(MyVertex, Color));


After I do a transformation of the mesh data (deforming some vertices lets say) I want to update the buffers so the changes I did can be rendered. How do I correctly do that?

What I tried:
1)After the deformation I called setupDrawingBuffers() but nothing was rendered on screen.
2)Instead of calling setupDrawingBuffers() after the deformation I called it in my rendering loop. That worked but it is obviously wrong since my setupDrawingBuffers function generates new buffers each time it is called which results in a memory leak on every rendering cycle.



Re: Updating opengl buffers after changing attributes of a mesh
« Reply #1 on: June 03, 2017, 17:26:12 »
First of all, this isn't Java/LWJGL. And second, are you sure you understand what that code is doing?
Have a look at glBufferSubData.
It allows to update (a subrange of) the buffer data.