LWJGL Forum

Please login or register.

Login with username, password and session length

Author Topic: Updating opengl buffers after changing attributes of a mesh  (Read 147 times)

IasonM

  • Guest

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);

        glBindVertexArray(VAO);

        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),
            (GLvoid*)0);
        glEnableVertexAttribArray(0);

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

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

        glBindVertexArray(0);
}

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.
Logged

Kai

  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 925
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.
Logged