Assist me to understand OpenGL glGenBuffers()

僤鯓⒐⒋嵵緔 提交于 2019-12-03 03:40:28

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

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

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

A 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

Assuming that vertexBuffers is declared as something like:

GLuint vertexBuffers[2];

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

glGenBuffers (2, vertexBuffers);

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!