【发布时间】:2015-11-09 10:32:57
【问题描述】:
当我第一次向缓冲区添加一些顶点时,这些是我正在调用的相关函数
// Create and bind the object's Vertex Array Object:
glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);
// Create and load vertex data into a Vertex Buffer Object:
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW);
// Tells OpenGL that there is vertex data in this buffer object and what form that vertex data takes:
// Obtain attribute handles:
_posAttrib = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(_posAttrib);
glVertexAttribPointer(_posAttrib, // attribute handle
4, // number of scalars per vertex
GL_FLOAT, // scalar type
GL_FALSE,
0,
0);
// Unbind vertex array:
glBindVertexArray(0);
但稍后在我的程序中,我想添加更多顶点。
我通过以下方式执行此操作(在单独的函数中:
add_vertices(x,y); //adds the necessary vertices to the vector.
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, (TRIANGLE_AMOUNT+1)*4*_number_of_circles * sizeof(float), &vertices[0], GL_STATIC_DRAW);
假设 glBufferData 的第二个参数中的时髦大小很好,我错过了什么吗?还有其他需要调用的OpenGL函数吗?
我没有收到任何错误,但是当我尝试通过使用不同的顶点子集循环 glDrawArrays 来使用新顶点绘制额外的形状时,什么也没有发生。只绘制第一个形状。
我希望这是半连贯的...如果有任何我没有提供的信息,请告诉我。
干杯。
【问题讨论】:
-
如果你只是想替换缓冲区中的数据,你不应该生成一个新的。
glBindBuffer+glBufferData在这种情况下就足够了。当你再次调用 glGenBuffers 时,你会得到一个全新的缓冲区,这意味着你必须更新 vao。 -
啊,这绝对是进步。它现在正在绘制新的东西......但它们恰好是斑点而不是圆圈。谢谢老哥!
标签: c++ opengl graphics shapes vertices