[ACCEPTED]-Assist me to understand OpenGL glGenBuffers()-nsopenglview

Accepted answer
Score: 30

glGenBuffers doesn't work quite like what you expect. When 18 you call glGenBuffers, it doesn't actually create anything. It 17 just returns a list of integers that are 16 not currently used as buffer names.

The actual 15 'object' is not created until you call glBindBuffer. So 14 you can just make up any integer you like 13 and pass it to glBindBuffer and a valid 12 buffer will be created at that index. glGenBuffers is 11 actually not required at all, it's just 10 there as a convenience function to give 9 you an unused integer.

So if you just create 8 an array of random integers, as long as 7 none of them overlap, you can use that as 6 your list of buffers without calling glGenBuffers. That's 5 why your code works whether you tell glGenBuffers 4 to create 1 or 2 buffers. As long as you 3 have two buffers with two different names, it 2 doesn't matter where the integer came from.

A 1 little demonstration:

int buf;
glGenBuffers(1, &buf);
glIsBuffer(buf); //FALSE - buffer has not been created yet
glBindBuffer(GL_ARRAY_BUFFER, buf);
glIsBuffer(buf); //TRUE - buffer created on bind
Score: 11

Assuming that vertexBuffers is declared as something 9 like:

GLuint vertexBuffers[2];

then your code is probably not quite 8 doing what you think. The first argument 7 to glGenBuffers() is the number of buffers you want to 6 generate, and the second argument is a pointer 5 to an array of that many buffers. So you 4 probably want to do:

glGenBuffers (2, vertexBuffers);

This tells OpenGL to 3 generate 2 buffer names and to store them 2 in 2 contiguous locations in memory starting 1 at the address of vertexBuffers.

Score: 2

I figured this out yesterday, I need to 2 do a glBindBuffer(GL_ARRAY_BUFFER, 0) after 1 each the glBufferData(...).

More Related questions