每帧更改顶点的颜色(OpenGL)

时间:2012-12-09 20:04:28

标签: c++ opengl

我正在使用本教程:http://www.opengl-tutorial.org/beginners-tutorials/tutorial-4-a-colored-cube/

这是练习:完成后,让每个帧都改变颜色。你必须每帧调用glBufferData。确保之前绑定了相应的缓冲区(glBindBuffer)!

我不知道该怎么做。我知道如何修改颜色缓冲区来改变颜色,但不知道如何修改每一帧的颜色。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

如练习所述:致电glBufferData!您已经完成了第一次设置颜色。

更改数据后,使用glBindBuffer重新绑定颜色缓冲区,然后重复调用glBufferData - 两者的参数与第一次相同 - 并且新的颜色缓冲区数据将是发送到GPU。

关于如何实际更改数据,例如,您可以使用如下所示的循环在颜色数据数组的每个单元格中插入相同的值:

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
}

或者,如果要在颜色的每个组件中插入特定值:

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
}

这些循环应位于渲染循环内glBufferData的调用之前。

相关问题