Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does glVertexPointer() copy data?

Tags:

opengl

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)

like image 281
Sean Avatar asked Aug 08 '11 19:08

Sean


2 Answers

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);
}
like image 160
genpfault Avatar answered Nov 20 '22 15:11

genpfault


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.

like image 40
Dr. Snoopy Avatar answered Nov 20 '22 16:11

Dr. Snoopy