glGenBuffers 2个名字?

时间:2013-12-23 09:24:29

标签: c++ opengl buffer

我一直在实现一个基于本教程的小型opengl应用程序: http://openglbook.com/the-book/chapter-4-entering-the-third-dimension/

我理解大部分代码,但我对这一行感到困惑:

glGenBuffers(2, &BufferIds[1]); 

然后是

glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]);
glBufferData(GL_ARRAY_BUFFER, size,  &theModel->theMesh.pos[0], GL_STATIC_DRAW);

我假设我只需要一个自由名称/ id来绑定我的缓冲区数据,但是如果我改变了

 glGenBuffers(2,

 glGenBuffers(1,

缓冲区无法绑定,无效。

BufferIds的大小为3(GLuint BufferIds [3])。我想使用VAO&的第一个插槽制作BufferIds [2]。 VBO的第二个。

glGenVertexArrays(1, &BufferIds[0]); 
ExitOnGLError("ERROR: Could not generate the VAO"); 
glBindVertexArray(BufferIds[0]); 
ExitOnGLError("ERROR: Could not bind the VAO"); 

glEnableVertexAttribArray(0); 
glEnableVertexAttribArray(1); 
glEnableVertexAttribArray(2); 

ExitOnGLError("ERROR: Could not enable vertex attributes"); 

glGenBuffers(2, &BufferIds[1]);  //if this gets from changed 2 to 1 ...
ExitOnGLError("ERROR: Could not generate the buffer objects"); 

int size = theModel->theMesh.pos.size() * sizeof(theModel->theMesh.pos[0]) ; 

glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]); 
ExitOnGLError("ERROR: Could not bind the VBO to the VAO");  // ...this error triggers
glBufferData(GL_ARRAY_BUFFER, size, &theModel->theMesh.pos[0], GL_STATIC_DRAW);

1 个答案:

答案 0 :(得分:1)

那是因为你将BufferIds [1]传递给了glBindBuffer。索引1实际上是第二个元素,因此当您只创建一个缓冲区时,代码会崩溃。

试试:

glGenBuffers(1, &BufferIds[0]);
glBindBuffer(GL_ARRAY_BUFFER, BufferIds[0]);