Greg G
Greg G

Reputation: 697

glGenBuffers 2 names?

I have been implementing a small opengl application that I based off of this tutorial: http://openglbook.com/the-book/chapter-4-entering-the-third-dimension/

I understand most of the code but I am really confused about this line:

glGenBuffers(2, &BufferIds[1]); 

Which is then followed by

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

I assume I only need one free name/id to bind my buffer data to, but if I change

 glGenBuffers(2,

to

 glGenBuffers(1,

The buffer fails to bind and nothing works.

BufferIds is 3 in size (GLuint BufferIds[3]). I would like to make it BufferIds[2] using the first slot for the VAO & the second for the 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);

Upvotes: 2

Views: 1583

Answers (1)

MB-F
MB-F

Reputation: 23637

That is because you pass BufferIds[1] to glBindBuffer. Index 1 is actually the second element, so your code crashes when you only create one buffer.

Try that:

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

Upvotes: 1

Related Questions