Is it safe to use a vertex array that's on the stack when calling glVertexPointer()
(and the other related functions)? It's unclear to me when OpenGL actually copies the data from the passed in structure.
If isn't safe, then how do you know when it's safe to destroy/reuse the structure you passed to glVertexPointer()
?
(Not using VBOs)
In the vertex array case the pointer will be dereferenced during the execution of glDrawElements()
and friends, assuming GL_VERTEX_ARRAY
has been glEnableClientState()
ed.
As soon as glDrawElements()
returns OpenGL will have all the data it needs, so you're free to free()
.
For VBOs you never pass in a real pointer, so it doesn't really matter :)
So something like this should work:
void draw()
{
vector< float > verts;
verts.push_back( 0 );
verts.push_back( 0 );
verts.push_back( 0 );
verts.push_back( 10 );
verts.push_back( 0 );
verts.push_back( 0 );
verts.push_back( 10 );
verts.push_back( 10 );
verts.push_back( 0 );
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &verts[0]);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
}
glVertexPointer never copies data, it just sets a pointer, which is read and transferred to the GPU when you call glDrawArrays and glDrawElements, after those functions have returned, the data is safe to be released.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With