问题
I have a simple 2d triangle displayed on the screen, I want to update the color buffer data every frame, so the color of the triangle changes constantly, but im not sure how to update the data efficiently.
this is the code for the color buffer:
GLfloat colourVert[] = {
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
GLuint colourBuffer;
glGenBuffers(1, &colourBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colourBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(colourVert), colourVert, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
Do I need to add something in these lines or do I need to modify the shaders, can someone explain please, thanks for any help.
回答1:
You should definitely not generate a new buffer every frame. Just reuse the already existing one. Also setting up attribute pointers can also be done just once since they are stored in the VAO. The only two lines actually necessary are glBindBuffer
and glBufferData
(or even better use glBufferSubData
since this will reuse the allocated memory instead of allocating a new memory segment).
Note, that this answer only applies to OpenGL 3.3 Core (and newer) and OpenGL-ES 3.0 (and newer). For OpenGL versions that don't support/require VAOs, the attribute setup might also have to happen in every frame.
来源:https://stackoverflow.com/questions/41784790/how-to-update-vertex-buffer-data-frequently-every-frame-opengl