opengl

Can I create and use an OpenGL object without generating its name?


Typically an object such as a texture, framebuffer, shader, or vertex array, is set up by first generating a name, then binding it, then setting parameters:

GLuint texture_id;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameteri(GL_TEXTURE_2D, ...);
glTexImage2D(GL_TEXTURE_2D, ...);

It's noteworthy that glGenTextures does not actually create a texture object, only a name (ID number) for one, and marks it as used. Can I just pick a number myself that is known to be unused, as in the following code?

// no other textures have been created so all numbers are available
glBindTexture(GL_TEXTURE_2D, 42);
glTexParameteri(GL_TEXTURE_2D, ...);
glTexImage2D(GL_TEXTURE_2D, ...);

Solution

  • In compatibility contexts, you're allow to just make up a (non-zero) number and pretend that's an object. In core contexts, you must generate them.