1
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

5
  • 1
    C++ objects are created with ClassName objectName. OpenGL objects aren't objects in the OOP way of thinking. What you have is a handle (or Id) to an object, the objects itself are managed by the OpenGL implementation. Note that none of the methods you show in your code actually exist in OpenGL, I have no clue where you got them from. Commented Jun 1, 2024 at 11:58
  • OpenGL "objects" are not the same as C++ "objects". The term "object", like many others, can mean multiple things. Commented Jun 1, 2024 at 11:59
  • @BDL is there a reference you could give me where I can study this concept in more depth? Commented Jun 1, 2024 at 12:05
  • In C an object is just a memory region that can represent a value. Commented Jun 1, 2024 at 12:09
  • VBO is an object, too, according to the C++ object model. From an OOP point of view, it's just a variable and less an object. The term "object" simply has different meanings in different contexts. Commented Jun 1, 2024 at 12:14

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"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" That's only true if you use multiple contexts or otherwise have some need to verify such things. Most OpenGL applications presume that a particular context is bound; they don't constantly re-verify it.
@NicolBolas this is technically correct. However, it also means that if one uses 3rd party libraries that use OpenGL, one has to trust that these libraries are well behaved. Conversely, if one develops a library that uses OpenGL, doing it properly means going through the extra steps not stomping all over your hosts stuff. For example more than 2/3 of the code in my libvoglr volume raycaster library are concerned with setting it up tailored to the host environment and most of the rendering code is about saving and restoring state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.