Lucas
Lucas

Reputation: 3119

C or C++ for OpenGL graphics

there is any drawback in choose C++ and an object oriented model (classes) to implement a simulation in OpenGL (or DirectX)? It's preferred to use C and a procedural programming paradigm ?

Upvotes: 13

Views: 10459

Answers (6)

MarkR
MarkR

Reputation: 63538

I don't think so. You can still use the (procedural) opengl interface easily from C++. That's what most programs I've seen do.

Upvotes: 0

Tony Albrecht
Tony Albrecht

Reputation: 31

It depends. How many objects are you rendering? 100s? 1000s? 1,000,000s? What platform? What level of performance do you require?

Assuming that you're looking at the low end of the spectrum for object numbers and you're more proficient in C++, then I would go with that. Keep it simple - just make it work. If you need to optimise it later then do it when you know where your bottlenecks are.

If you are looking at a large number of objects that need to be rendered then I'd be ensuring that I was using contiguous, homogeneous arrays of data to minimise cache thrashing (Both Justicle and Malte Clasen gave some good links that you should read (especially the first 2 :). You can still provide an OO interface if you like, but ensure that the engine focuses on rendering via iterating through your flat arrays.

I'd be very wary of using a naive scenetree implementation if you want performance - these, while easy to understand, are often painful to optimise without completely rewriting.

So, to answer your question, there is no drawback in using C++ over C but there is a potential performance issue in using object oriented methodologies if used naively.

Upvotes: 2

Justicle
Justicle

Reputation: 15143

OO is particularly unsuited to high performance graphics. http://research.scee.net/files/presentations/gcapaustralia09/Pitfalls_of_Object_Oriented_Programming_GCAP_09.pdf

However, if you are just learning (and not developing a commercial console game), OO is useful to build and conceptualize the engine. The usual trade offs between C and C++ programming still apply, OpenGL really doesn't come into it.

Upvotes: 0

Malte Clasen
Malte Clasen

Reputation: 5637

The most common drawback of object oriented programming in the context of high performance graphics (games etc.) is the memory bottleneck. OOP often (but not necessarily) leads to writing functions that operate on single elements and leveraging the standard library to generalize these to arrays. It might be preferable to operate on arrays in the first place, for example cull against all six frustum planes instead of calling the single plane culling routine six times.

Check out the following resources for more details:

Note that using C++ does not imply strict object oriented programming, you can use it for many paradigms. So if you code your engine in C++, you can still leverage all existing OOP style libraries such as Qt, while using any paradigm you like for the core. Although all of this is also possible in C, C++ might be a bit more comfortable.

Upvotes: 7

Martin Beckett
Martin Beckett

Reputation: 96119

Not really, unless you have an unreasonably stupid design or are on a seriously limited platform there is no performance advantage of c over c++.

Upvotes: 7

Jerry Coffin
Jerry Coffin

Reputation: 490048

Unless you're developing for a platform that's seriously short of memory, C++ is usually a better choice (especially in its standard library, it does use more memory, but usually does so to improve speed).

Upvotes: 5

Related Questions