PDA

View Full Version : dynamically change vertices Buffer Data



advorak
10-20-2015, 12:00 PM
I am writing a program to display a textured quad with data from a real time capture of a video signal.

Because of an Image Generator limitation, i will need to alter the position and texture coordinates of the vertices buffer.

If I was statically declaring the vertices, I would use the following code:


GLfloat vertices[20] = { 1.0f, 1.0f, 0.9f, 1.0f, 1.0f, // Top Right
1.0f, -1.0f, 0.9f, 1.0f, 0.0f, // Bottom Right
-1.0f, -1.0f, 0.9f, 0.0f, 0.0f, // Bottom Left
-1.0f, 1.0f, 0.9f, 0.0f, 1.0f }; // Top Left

GLuint indices[6] = { 0, 1, 3, // First Triangle
1, 2, 3 };// Second Traingle

glGenVertexArrays( 1, &VAO[Output] );
glGenBuffers( 1, &VBO[Output] );
glGenBuffers( 1, &EBO[Output] );

glBindVertexArray( VAO[Output] );

glBindBuffer( GL_ARRAY_BUFFER, VBO[Output] );
glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW );

glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, EBO[Output] );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW );

glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0 );
glEnableVertexAttribArray( 0 );

glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)) );
glEnableVertexAttribArray( 1 );


If I need to modify the vertices and texture coordinates:

Should I declare a Vertex Array Object (VAO) at all?
If I declare a VAO, can I just modify the vertices and only call glBindVertexArray, glBindBuffer and glBufferData for the vertices inside the main loop?
If no VAO, would that just be a glBindBuffer and glBufferData for the vertices inside the main loop?

I realize i can not have an element array buffer without a VAO, so I will just increase the total vertices to cover the two distinct triangles.

thanks in advance for your help.

Alfonse Reinheart
10-20-2015, 12:20 PM
You need a VAO to render if you're using the core profile of OpenGL, period. Not just for element buffers. And if you're not using the core profile, the default VAO object will work with element buffers.

Given a VAO, the only thing you need to do in your render loop is bind the VAO. Since you're not changing the source vertex format or changing which buffer object the vertices come from (or where in that buffer the vertices are stored), the VAO does not care.

If the vertex data changes within the buffer object, you simply need to update the buffer object's storage accordingly (https://www.opengl.org/wiki/Buffer_Object_Streaming). The VAO need not be bound.

However, it is important that you bind the VAO in your render loop, which will ensure that changes to the buffer object (possibly made on another thread) will be visible to that VAO and rendering command.

advorak
10-20-2015, 04:27 PM
so if I read the Buffer Object Streaming page correctly, I would bind the VBO, then call glBufferData with a null pointer, and then call glBufferData again with the new vertices data?

Am i reading this correctly?