为什么OpenGL有全局函数?

发布于 2024-08-19 15:54:36 字数 86 浏览 6 评论 0原文

为什么openGL不是面向对象的?每个人都教面向对象编程+设计模式,但是OpenGL有很多全局函数。 这不是很糟糕的风格吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你穿错了嫁妆 2024-08-26 15:54:36

低级 API 的重点是使其尽可能最小化和可移植。给它一个面向对象的体系结构将不允许这种情况:

  • 多态性增加了不必要的函数调用开销。
  • 它迫使您使用一些相对困难的调用约定,从而降低了可移植性。
  • 你不能将面向对象的架构包装起来使其成为程序化的,但你可以做相反的事情;因此,让事情尽可能灵活是有意义的。如果您愿意,围绕 OpenGL 编写一个面向对象的包装器是很简单的。

最后,您应该真正质疑您所学到的有关 OOP 的知识。不管你的学院或大学可能告诉你什么,OOP 并不是程序设计的灵丹妙药。 C++ STL(以及大多数 Boost)中绝对没有面向对象,这是有充分理由的。

面向对象在某些情况下很有用,但是您应该了解它何时有用,何时无用,并且在任何情况下您都不应该相信任何非 OOP 的东西都是“糟糕的风格”。

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

还给你自由 2024-08-26 15:54:36

OpenGL

  • OpenGL 应该支持所有平台 - 在这方面没有任何接近 C 的东西 - 由于几乎每个设备都可以使用相同的 API
  • OpenGL 应该支持所有语言 - 在这方面也没有接近 C 的东西 - 感谢这一点,每个支持调用 C 库的语言(几乎所有都支持)可以使用 OpenGL
  • OpenGL 是一个 API,而不是一个引擎——它旨在为图形硬件提供低级接口,但也有足够的高级接口,以是不同硬件的抽象 - C 比 C++ 低得多,OOP 不是低级
  • OpenGL 是一个构建框架,而不是一个完整的解决方案 - 没有唯一正确的方法 编写图形代码,OpenGL 不应该强迫我们做任何事情 - 通过成为 OOP,它会迫使我们接受他们的“解决方案
  • OpenGL与任何特定的编程范式相关联 - 因此我们可以将 OpenGL 包装成函数式、逻辑式或 OOP 语言——或者按程序使用它
  • OpenGL 关乎效率——并且您无法比直接函数调用获得更高的效率。 OOP 的效率取决于它适合特定任务的程度。

总的来说——OpenGL的设计是为了让我们拥有所有的自由,并且不为我们做任何选择。我所说的自由是指选择平台、语言、编程范式、引擎设计、方法论以及效率与可读性水平的自由。

为此我赞扬 OpenGL,也因此我讨厌 Direct X。

阿们。

旁注:每个人都教授面向对象编程,因为它最容易掌握。这不是唯一的真实范式。有函数式编程、逻辑编程、契约编程,甚至还有一种用 C 语言编写的面向对象的方法。计算机科学中没有单一的真理。至于设计模式,我可以举出多个 OpenGL 架构中使用的设计模式。风格不好?我见过漂亮的 C 程序,它们有 aaaaallll 全局函数......

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

若有似无的小暗淡 2024-08-26 15:54:36

一般来说,OpenGL面向对象的。它只是用不直接支持 OOP 的语言实现的。但 API 是面向对象的:它由许多不同的对象类型以及针对每个对象定义的一组操作组成。每个对象类型的内部结构对用户都是隐藏的。它满足 OOP 的所有要求。它恰好是用 C 实现的,而 C 没有方便的类或成员方法语法。

除此之外,全局函数绝对没有任何问题。在 C++ 中,一个常见的建议是尽可能优先使用它们而不是成员方法。在函数式编程中,全局函数是默认的。

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.

人│生佛魔见 2024-08-26 15:54:36

OpenGL 是为 C 语言创建的,当时这些东西还不存在。即使是现在,他们仍然希望保留 C 接口,因为 C 仍然是一种广泛使用的语言。

他们应该同时维护 C 接口和 C++ 包装器,放弃 C 并只使用 C++,还是保留 C 接口?我认为后者是最好的解决方案:对他们来说很容易,对我们来说也不太难。

也就是说,OpenGL 界面确实很粗糙。很多东西“应该”被弃用,但可惜的是,这些都被推迟到了晚些时候。

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.

太阳男子 2024-08-26 15:54:36

嗯,有几个原因。

  • 您应该将 OpenGL 上下文视为状态机。任何时候都只有其中一个处于活动状态。在所有内容前面放一点 Opengl.whatever 没有什么区别。
  • 速度,OpenGL 被设计为一个最小的 API
  • 如果它是面向对象的,你会用什么语言来编写它? C++?那么每个人都必须编写复杂的绑定。 C 更容易包装。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文