Search code examples
c++openglparadigms

Why does OpenGL have global functions?


Why isn't openGL object-orientied? Everybody teaches Object Orientated Programming + Design Patterns, but OpenGL has many global functions. Isn't this bad style?


Solution

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

    • Polymorphism adds unnecessary function call overhead.
    • It forces you to use some relatively difficult calling convention, which reduces portability.
    • You cannot wrap an object-oriented architecture to make it procedural, but you can do the reverse; so, 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 should really question what you've been taught about OOP. Despite what your college or university may tell you, OOP is not a panacea of program design. There are very good reasons why there is absolutely no object-orientation in the C++ STL (and most of Boost for that matter).

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