为什么OpenGL有全局函数?
为什么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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
低级 API 的重点是使其尽可能最小化和可移植。给它一个面向对象的体系结构将不允许这种情况:
最后,您应该真正质疑您所学到的有关 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:
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".
OpenGL
总的来说——OpenGL的设计是为了让我们拥有所有的自由,并且不为我们做任何选择。我所说的自由是指选择平台、语言、编程范式、引擎设计、方法论以及效率与可读性水平的自由。
为此我赞扬 OpenGL,也因此我讨厌 Direct X。
阿们。
旁注:每个人都教授面向对象编程,因为它最容易掌握。这不是唯一的真实范式。有函数式编程、逻辑编程、契约编程,甚至还有一种用 C 语言编写的面向对象的方法。计算机科学中没有单一的真理。至于设计模式,我可以举出多个 OpenGL 架构中使用的设计模式。风格不好?我见过漂亮的 C 程序,它们有 aaaaallll 全局函数......
OpenGL
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...
一般来说,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.
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.
嗯,有几个原因。
Well, there are a few reasons.