1、glBufferSubData 适用于相同数据类型
void SetPositionY(float y)
{
vector<Vector3<float>>::iterator ver3;
for(ver3 = vertices.begin();ver3!=vertices.end();ver3++)
{
ver3->y +=y;
}
glBindBuffer(GL_ARRAY_BUFFER,posHandler);
glBufferSubData(GL_ARRAY_BUFFER,0,vertices.size() * sizeof(vertices[0]),&vertices[0]);
glBindBuffer(GL_ARRAY_BUFFER,0);
display( );
}
2、glMapBuffer 适用于对缓冲区大部分数据更新
void SetPositionY(float y)
{
....................
glBindBuffer(GL_ARRAY_BUFFER,posHandler);
void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(buf, &vertices[0], sizeof(vertices[0])*vertices.size());
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER,0);
..............
}
以上两种方式效果图:
3、glMapBufferRange 适用于大缓冲区里面小部分数据更新
void SetPositionY(float y)
{
.............
glBindBuffer(GL_ARRAY_BUFFER,posHandler);
void *buf = glMapBufferRange(GL_ARRAY_BUFFER, 0, sizeof(vertices[0])*50, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
memcpy(buf, &vertices[0], sizeof(vertices[0])*50);
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER,0);
..............................
}
因为所有字是一个数组 更新前50个顶点 所以有点乱
特别说明:更新完数据 一定要调从绘函数 也就是glutDisplayFunc绑定的回调函数 ,本文为display( );
参考:http://iqnix.blog.163.com/blog/static/21721982014228115223718/
http://www.zwqxin.com/archives/opengl/learn-vbo.html