Why does OpenGL have global features?

Why is an OpenGL object object oriented? Everyone teaches Object Orientated Programming + Design Patterns, but OpenGL has many global features. Isn't that a bad style?

+4
source share
5 answers

The whole point of the low-level API is to make it as minimal and portable as possible. Providing an object-oriented architecture would not allow this:

  • Polymorphism adds unnecessary function calls.
  • This forces you to use some relatively complex calling conventions, which reduces portability.
  • You cannot wrap an object-oriented architecture to make it procedural, but you can do the opposite; therefore, it makes sense to make things as flexible as possible. It's trivial to write an object-oriented wrapper around OpenGL if you want.

Finally, you really should ask a question about what the OOP taught you. Although your college or university can tell you, OOP is not a panacea for program design. There are very good reasons why there is absolutely no object orientation in C ++ STL (and in most cases Boost).

Object orientation is useful in some cases, but you must learn when it is useful and when not, and under no circumstances should you believe that everything that is not OOP is a "bad style."

+17
source

Opengl

  • OpenGL should support all platforms - in this regard, there is nothing close to C - thanks to this, almost all devices can use the same API
  • OpenGL should support all languages ​​- there is also nothing close to C in this regard - thanks to this, every language that supports C library calls (and almost all) can use OpenGL
  • OpenGL is an API, not an engine - it is designed to provide a low-level interface for a graphic headset, but high enough to be an abstraction for different hardware - C is very much lower than C ++, OOP is not low.
  • OpenGL is the basis for development, not a complete solution - there is no one and true way to write graphic code, and OpenGL should not force us to anything - being OOP, it will force us to "colution
  • OpenGL is not tied to any particular programming paradigm, so we can port OpenGL to a functional, logical or OOP language - or use it procedurally
  • OpenGL is efficiency - and you cannot get better performance than direct function calls. OOP is as effective as it is for a specific task.

In general - OpenGL is designed to allow us to have all the freedom and not make any choices for us. And by freedom, I mean freedom to choose a platform, language, programming paradigm, engine design, methodology and level of effectiveness versus readability.

And for that I praise OpenGL, and for that I hate Direct X.

Amen.

Sidenote:. Everyone is teaching Object Orientated programming because it is the easiest to understand. This is not the only true paradigm. There is functional programming, logical programming, contract programming, and even an object-oriented way of writing in C. There is no truth in computer science. Regarding design patterns, I could name a few that are used in the OpenGL architecture. Bad style? I saw beautiful C programs that had aaaallall global functions ...

+11
source

In general, OpenGL is geared towards . It is simply implemented in a language that does not directly support OOP. But the API is object-oriented: it consists of several different types of objects and a set of operations defined for each. And the insides of each type of object are hidden from the user. It fulfills all OOP requirements. This just happens in C, which does not have a convenient class or element syntax.

In addition, there is nothing wrong with global functions. In C ++, the general recommendation is to use them according to member methods whenever possible. In functional programming, global functions are standard.

+8
source

OpenGL was created for and in C, and then there was nothing like that. Even now, they still want to support the C interface because C is still a widely used language.

Should they support both C-interfaces and C ++ shells, ditch C and just use C ++ or support C-interface? I would say that the latter is the best solution: easy for them, not too difficult for us.

However, the OpenGL interface is admittedly gross. Many things were "supposedly" to be obsolete, but, alas, this was carried forward to a later date.

+4
source

Well, there are several reasons.

  • You should consider the OpenGL context as a state machine. Only one of them is active at any time. There is no difference between hosting a small Opengl. Whatever happens in front of everything.
  • Speed, OpenGL was designed as a minimal API
  • If it were object oriented, in what language did you write it? C ++? Then everyone will write complex bindings. C WAY is easier to carry.
+4
source

Source: https://habr.com/ru/post/1299801/


All Articles