简体   繁体   English

用于OpenGL图形的C或C ++

[英]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)? 选择C ++和面向对象的模型(类)在OpenGL(或DirectX)中实现模拟有什么缺点吗? It's preferred to use C and a procedural programming paradigm ? 使用C和过程编程范例是首选的吗?

不是真的,除非你有一个不合理的愚蠢设计或者在一个严重受限的平台上,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. OOP经常(但不一定)导致编写对单个元素进行操作的函数,并利用标准库将这些函数推广到数组。 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. 请注意,使用C ++并不意味着严格的面向对象编程,您可以将它用于许多范例。 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. 因此,如果您使用C ++编写引擎代码,您仍然可以利用所有现有的OOP样式库(如Qt),同时使用您喜欢的任何范例作为核心。 Although all of this is also possible in C, C++ might be a bit more comfortable. 虽然C中也可以实现所有这些,但C ++可能会更舒适一些。

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

It depends. 这取决于。 How many objects are you rendering? 你渲染了多少个物体? 100s? 100S? 1000s? 1000? 1,000,000s? 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. 假设您正在查看对象数的频谱的低端,并且您对C ++更熟练,那么我会继续使用它。 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. 如果你正在查看需要渲染的大量对象,那么我将确保我使用连续的,同类的数据数组来最小化缓存抖动(两者都是Justicle和Malte Clasen提供了一些你应该阅读的好链接(特别是前2个:)。如果愿意,您仍然可以提供OO接口,但要确保引擎通过迭代平面阵列来专注于渲染。

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. 如果你想要性能,我会非常谨慎地使用天真的scenetree实现 - 这些虽然易于理解,但在没有完全重写的情况下进行优化往往很痛苦。

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. 因此,回答你的问题,使用C ++而不是C没有任何缺点,但如果天真地使用,使用面向对象的方法存在潜在的性能问题。

OO is particularly unsuited to high performance graphics. OO特别不适合高性能图形。 http://research.scee.net/files/presentations/gcapaustralia09/Pitfalls_of_Object_Oriented_Programming_GCAP_09.pdf 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. 但是,如果您只是在学习(而不是开发商业控制台游戏),OO对于构建和概念化引擎非常有用。 The usual trade offs between C and C++ programming still apply, OpenGL really doesn't come into it. C和C ++编程之间通常的权衡仍然适用,OpenGL确实没有进入它。

I don't think so. 我不这么认为。 You can still use the (procedural) opengl interface easily from C++. 您仍然可以从C ++轻松使用(程序)opengl接口。 That's what most programs I've seen do. 这就是我见过的大多数程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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