Search code examples
c++opengl

Difference between C++ and OpenGL objects


unsigned int VBO;
glGenBuffers(1, &VBO);  
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

I thought objects are created by ClassName objectName. I've failed to wrap my head around this one


Solution

  • The term "object" is highly abstract and is not tied to anything in particular. In programming objects come in many forms and an OpenGL object has absolutely no relationship whatsoever to C++ objects. As a matter of fact, whenever people attempt to create C++ wrappers that attempt to give OpenGL objects a C++ interface, they will quickly find, that those two concepts are difficult to conciliate.

    OpenGL objects are "things" that are part of the OpenGL context. To use those "things", you have to make the context current, which however, only happens on the thread that makes the call; which means if you were to wrap OpenGL objects in C++, for each and every access to it, you'd have to check, if the call is actually made from a thread that has the associated context bound. And if that's not the case somehow deal with it. Save yourself the trouble and don't try to bring those distinct concepts together – neither mentally, nor programmatically.