Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any plans or existing projects to port OpenGL API to C++? [closed]

Tags:

c++

opengl

The OpenGL standard pages states that the OpenGL is callable from C and C++. The API, however, is of course in pure C. As the OpenGL uses for example a lot of enumerations, using enum-classes (from C++11) could greatly reduce number of errors and make the API more feasible for beginners. It could be seen that lot of the bindings like OpenTK (for C#) are created; creating good C++ API shouldn't be much harder.

I weren't able to find anything that was more than an obscure wrapper, hence my questions:

  1. Is there a well-known C++ wrapper using C++11 facilities for OpenGL? and if not,
  2. Is something like this planned by anyone well-known (which especially means the Khronos)?
like image 308
Bartek Banachewicz Avatar asked Aug 23 '12 10:08

Bartek Banachewicz


Video Answer


1 Answers

The whole way OpenGL works does not really well map to OOP: http://www.opengl.org/wiki/Common_Mistakes#The_Object_Oriented_Language_Problem

What's not stated in this article is the context affinity problem. In OpenGL everything happens on the context, so a class "Texture", to be correct would be nothing more than a glorifed handle to be used with the context.

This is wrong:

class Texture {
/* ... */

public:
    void bind();
}

It would only work if the texture was part of the currently active context.

This is no better either:

class Texture {
/* ... */

public:
    void bind(Context &ctx);
}

The texture still must be part of the context ctx, and it would only work, if ctx was active at the moment.

So what about this:

class Context {
/* ... */
public:
    void bindTextureToUnit(TextureUnit &tu, Texture &t);
};

Better, but still not correct as the context must be the one currently active in the current thread. You may think "oh, I'll just throw an exception if context is not active". Please don't do this.

So what about this

class ActiveContext : Context {
/* ... */
public:
    void bindTextureToUnit(TextureUnit &tu, Texture &t);

}

Now you've ended up with making sure that there can be only one ActiveContext instance per thread. Which ends you up in all kinds of weird thread singleton mess.

In fact I numerously tried to implement a clean and sane mapping from OpenGL state and objects into a set of C++ classes, but there are always cases where is simply doesn't work out or ends up in horrible code mess.

IMHO it's far better to not try mapping the OpenGL API into a set of C++ classes (it can't be done sanely), but instead use the regular OpenGL API from specialized classes. Any OpenGL context management is so dependent in the program in questions, that it must be tailored specifically to said program.

like image 161
datenwolf Avatar answered Oct 24 '22 05:10

datenwolf