Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Update vertices in VBO

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    7

    Update vertices in VBO

    I have a passToOpenGL function below to generate a vao, vbo, index buffer. After I generate, how can I update the vertices?

    I thought it would be to create a function like this but it did not work:
    void VBOLineLoop::update(GLfloat fWidth, GLfloat fHeight)
    {
    float *vVerts;
    vVerts=new float[usVerts*3];
    GLuint bytes_positions=sizeof(GLfloat)*3*usVerts;
    float fOuterHalfWidth=fWidth/2.0f; float fOuterHalfHeight=fHeight/2.0f;
    GLushort usIndexSubi=0;
    vVerts[0]=-fOuterHalfWidth;
    vVerts[1]=-fOuterHalfHeight;
    vVerts[2]=0.0f;

    vVerts[3]=+fOuterHalfWidth;
    vVerts[4]=-fOuterHalfHeight;
    vVerts[5]=0.0f;

    vVerts[6]=+fOuterHalfWidth;
    vVerts[7]=+fOuterHalfHeight;
    vVerts[8]=0.0f;

    vVerts[9]=-fOuterHalfWidth;
    vVerts[10]=+fOuterHalfHeight;
    vVerts[11]=0.0f;

    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
    glBufferSubData(GL_ARRAY_BUFFER, 0, bytes_positions, vVerts);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    delete vVerts;
    }

    void VBOLineLoop:assToOpenGL(float * vVerts, float *vColor)
    {
    GLuint bytes_positions=sizeof(GLfloat)*3*usVerts;
    GLuint bytes_colors=sizeof(GLfloat)*4*usVerts;
    GLuint bytes_elements=sizeof(GLushort)*usNumberElements;

    // Set up the vertex attributes
    glGenVertexArrays(1, vao);
    glBindVertexArray(vao[0]);

    // Set up the element array buffer
    glGenBuffers(1, ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, bytes_elements, elementsofLineLoop, GL_DYNAMIC_DRAW);

    glGenBuffers(1, vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
    glBufferData(GL_ARRAY_BUFFER, bytes_positions + bytes_colors , NULL, GL_DYNAMIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, bytes_positions, vVerts);
    glBufferSubData(GL_ARRAY_BUFFER, bytes_positions, bytes_colors, vColor);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)(bytes_positions));
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(2);
    }
    Any help is appreciated..
    Last edited by ITISAGLN; 04-10-2014 at 03:51 AM.

  2. #2
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    When you say "it did not work", what exactly are the symptoms? Does everything render correctly as long as you don't call your update() function? If yes, what happens after you call it? Is the rendering unchanged? Wrong? Or does it not render at all?

    From scanning through the code, I don't see any major issues. I would bind the index buffer after binding the VAO, so that the index buffer binding becomes part of the VAO state. But as long as you don't change any bindings elsewhere in the code, it should still work for now.

    I figure the value of usVerts is 4?

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    7
    Thanks for the response...
    Right. I suppose you need to know what did not work
    I draw a bunch of VBOLineLoop & a bunch of other geometry; the other geometry classes have similar structure to this class; so yes there are other bindings.

    The scene is draw and everything renders correctly. Then I call mpLineLoop->update(4.0f,4.0f) but the line loop does not update (change size); it remains it's previous size.

    The VBOLineLoop function render is called but it renders the previous lineloop data; not the updated vertex data. (I move the index bind after the vao bind and tested; no luck)

    void VBOLineLoop::render() const {
    // Set up for a glDrawElements call
    glBindVertexArray(vao[0]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);

    glDrawElements(GL_LINE_LOOP, usNumberElements, GL_UNSIGNED_SHORT, NULL);

    glBindVertexArray(0);
    }

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    7
    Thanks for the response...
    Right. I suppose you need to know what did not work
    I draw a bunch of VBOLineLoop & a bunch of other geometry; the other geometry classes have similar structure to this class; so yes there are other bindings.

    The scene is drawn and everything renders correctly. Then I call mpLineLoop->update(4.0f,4.0f) but the line loop does not update (change size); it remains it's previous size.

    After, the update, the VBOLineLoop function render() is called but it renders the previous lineloop data; not the updated vertex data. (I moved the index bind after the vao bind and tested; no luck)

    void VBOLineLoop::render() const {
    // Set up for a glDrawElements call
    glBindVertexArray(vao[0]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);

    glDrawElements(GL_LINE_LOOP, usNumberElements, GL_UNSIGNED_SHORT, NULL);

    glBindVertexArray(0);
    }

    Yep, usVerts=4;
    Last edited by Zubaja; 04-03-2014 at 05:34 AM.

  5. #5
    Intern Contributor
    Join Date
    Jun 2012
    Posts
    71
    Should you have the VAO bound when you update the VBO that's attached to it?

    Actually, I don't think that's necessary. But I can't find a way to delete this post.
    Last edited by fiodis; 04-17-2014 at 09:06 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •