简体   繁体   中英

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 ?

不是真的,除非你有一个不合理的愚蠢设计或者在一个严重受限的平台上,c在c ++上没有性能优势。

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.

除非您正在开发一个严重缺乏内存的平台,否则C ++通常是更好的选择(特别是在其标准库中,它确实使用更多内存,但通常这样做是为了提高速度)。

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM