Changing the color of a vertex each frame (OpenGL)

c++, opengl

Solution

As the exercise states: Call `glBufferData`! You've already done it to set the colour the first time.

After changing the data, re-bind the colour buffer with `glBindBuffer`, then repeat the call to `glBufferData` - both with the same arguments as the first time - and your new colour buffer data will be sent to the GPU.

On how to actually change the data, you could, for instance, insert the same value in each cell of the colour data array with a loop like the following:

for (int i = 0; i < 12 * 3; ++i) // Replace 12 with the correct amount of points if it's wrong, the 3 is the amount of components per colour
{
    g_color_buffer_data[i] = 1.0f; // Replace 1.0f with your desired colour component value
}

Alternatively, if you want to insert a specific value in each component of the colour:

for (int i = 0; i < 12; ++i)
{
    g_color_buffer_data[i*3+0] = 1.0f; // i * 3 is the start of a colour
    g_color_buffer_data[i*3+1] = 0.5f; // i * 3 + 1 is the second component
    g_color_buffer_data[i*3+2] = 0.0f; // This you should be able to figure out
    // Again, replace component values with your own ones
}

These loops should be located before the call to `glBufferData`, inside your rendering loop.

Problem

I'm using this tutorial: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-4-a-colored-cube/ And here's the exercise: Once you’ve done that, make the colors change each frame. You’ll have to call glBufferData each frame. Make sure the appropriate buffer is bound (glBindBuffer) before ! I don't know how to do this. I know how to modify the color buffer to change colors, but not how to change them each frame. Can anyone help?

Original source