PDA

View Full Version : Update vertices in VBO



ITISAGLN
04-09-2014, 07:28 PM
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::passToOpenGL(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..

reto.koradi
04-09-2014, 10:02 PM
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?

ITISAGLN
04-10-2014, 03:57 AM
Thanks for the response...
Right. I suppose you need to know what did not work:biggrin-new:
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);
}

ITISAGLN
04-10-2014, 03:58 AM
Thanks for the response...
Right. I suppose you need to know what did not work:biggrin-new:
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;

fiodis
04-17-2014, 08:58 AM
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. :doh: