OpenGL: required lifetime of vertex arrays
OpenGL newbie question: if I do something like this:
GLfloat vertices[] = { .... };
glVertexPointe开发者_JAVA百科r(3, GL_FLOAT, 0, vertices);
... set other stuff ...
glDrawArrays(...);
What is the required lifetime of the 'vertices' array? (Or in other words, will OpenGL take a copy of the relevant portion and at what point?) For example, is it OK for the array to reside on the stack as it implicitly would be, or is it required to exist after glDrawArrays() is called?
[For what it's worth, I'm specifically programming for iOS, and at the moment working with code inside the drawFrame method created in an OpenGL project as set up by default in XCode.]
Your array must live until glDrawArrays, it can be destroyed afterwards and as implied by this, it can reside on the stack.
The contents of the vertex array will be copied each time you call glDrawArrays/Elements
and therefore have to still exist at this point in time (until you don't call glDrawArrays/Elements
anymore or change the vertex array by a call to gl...Pointer
).
To actually store vertex (and other) data on the GPU and let the driver manage its memory (together with the performance improvement of not needing to transfer the data at every draw call), you can use vertex buffer objects. Once you copied your data into such a VBO it resides in GPU memory (or where the driver thinks it fits best) and you actually don't need your CPU copy anymore. But these are simplified statements, consult material on VBOs for more information.
精彩评论