mr.bio
mr.bio

Reputation: 1686

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?

Upvotes: 4

Views: 2041

Answers (5)

Kornel Kisielewicz
Kornel Kisielewicz

Reputation: 57555

OpenGL

  • OpenGL should support all platforms -- there's nothing near to C in this regard - thanks to that almost every device can use the same API
  • OpenGL should support all languages -- there's also nothing near to C in this regard - thanks to that, each language that supports calling C libraries (and almost all do) can use OpenGL
  • OpenGL is an API, not a engine -- it intends to provide low level interface to the graphics harware, yet enough high level, to be an abstraction to different hardwares -- C is a lot more low level than C++, OOP is not low level
  • OpenGL is a framework to build upon, not a complete solution -- there is no one and true way to write graphics code, and OpenGL isn't supposed to force us to anything - by being OOP it would force us to their "colution
  • OpenGL is not tied to any specific programming paradigm -- hence we can wrap OpenGL into a functional, logical or OOP language -- or use it procedurally
  • OpenGL is about efficiency -- and you can't get more efficient than by direct function calls. OOP is as efficient as it is suited for a particular task.

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

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

Amen.

Sidenote: Everybody teaches Object Orientated programming because it's the easiest to grasp. It's not the one and only true paradigm. There's functional programming, logical programming, contract programming, and even a object oriented way to write in C. There's no one truth in computer science. As for Design Patterns, I could name more than a few that are used in OpenGL's architecture. Bad Style? I've seen beautiful C programs that had aaaaallll global functions...

Upvotes: 11

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247999

In general, OpenGL is object oriented. It is just implemented in a language that doesn't directly support OOP. But the API is object-oriented: It consists of a number of different object types, and a set of operations defined on each. And the internals of each object type are hidden from the user. It fulfills all the requirements for OOP. It just so happens to be implemented in C, which doesn't have a convenient class or member method syntax.

Apart from this, there is absolutely nothing wrong with global functions. In C++, a common recommendation is to prefer them over member methods whenever possible. In functional programmming, global functions are the default.

Upvotes: 8

Peter Alexander
Peter Alexander

Reputation: 54270

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".

Upvotes: 19

Chris H
Chris H

Reputation: 6581

Well, there are a few reasons.

  • You should think of an OpenGL context as a state machine. There is only one of them active at any time. There is no difference between putting a little Opengl.whatever in front of everything.
  • Speed, OpenGL was designed to be a minimal API
  • If it was object oriented, what language would you write it in? C++? Then everybody would ahve to write complex bindings. C is WAY easier to wrap.

Upvotes: 4

GManNickG
GManNickG

Reputation: 503913

OpenGL was created for and in C, and none of that stuff existed then. Even now, they still want to keep a C interface, because C is still a widely used language.

Should they maintain both C interfaces and C++ wrappers, ditch C and just use C++, or keep a C interface? I'd argue the latter is the best solution: easy on them, not too hard for us.

That said, the OpenGL interface is admittedly gross. Lot's of stuff was "suppose" to be deprecated, but alas that got moved to a later date.

Upvotes: 4

Related Questions