陷入构建游戏引擎的困境

发布于 2024-08-07 14:48:24 字数 531 浏览 5 评论 0原文

我正在尝试使用 c++、SDL 和 OpenGL 构建一个(简单的)游戏引擎,但我似乎无法弄清楚下一步。这就是我到目前为止所拥有的......

一个控制主游戏循环的引擎对象

一个将渲染场景的场景渲染器

可以推送和弹出的游戏状态堆栈

每个状态都有一个参与者的集合,每个参与者都有一个三角形的集合。 场景渲染器成功设置了视图投影矩阵

我不确定我遇到的问题是否与如何存储演员位置或如何创建渲染队列有关。

我读到,创建一个渲染队列非常有效,该队列将从前到后绘制不透明多边形,然后从后到前绘制透明多边形。因此,我的演员调用场景渲染器对象的“queueTriangle”方法。然后场景渲染器对象存储一个指向每个演员三角形的指针,然后根据它们的位置对它们进行排序,然后渲染它们。

我面临的问题是,要发生这种情况,三角形需要知道它在世界坐标中的位置,但如果我使用 glTranslatef 和 glRotatef 我不知道这些坐标!

请有人为我提供一个解决方案,或者将我链接到有关如何解决此问题的(简单)指南。

谢谢你!

I'm trying to build a (simple) game engine using c++, SDL and OpenGL but I can't seem to figure out the next step. This is what I have so far...

An engine object which controls the main game loop

A scene renderer which will render the scene

A stack of game states that can be pushed and popped

Each state has a collection of actors and each actor has a collection of triangles.
The scene renderer successfully sets up the view projection matrix

I'm not sure if the problem I am having relates to how to store an actors position or how to create a rendering queue.

I have read that it is efficient to create a rendering queue that will draw opaque polygons front to back and then draw transparent polygons from back to front. Because of this my actors make calls to the "queueTriangle" method of the scene renderer object. The scene renderer object then stores a pointer to each of the actors triangles, then sorts them based on their position and then renders them.

The problem I am facing is that for this to happen the triangle needs to know its position in world coordinates, but if I'm using glTranslatef and glRotatef I don't know these coordinates!

Could someone please, please, please offer me a solution or perhaps link me to a (simple) guide on how to solve this.

Thankyou!

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

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

发布评论

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

评论(2

や三分注定 2024-08-14 14:48:24

如果您编写一个相机类并使用其函数在世界中移动/旋转它,您可以使用从内部四元数获得的矩阵来变换顶点,为您提供相机空间中的位置,以便您可以将三角形从后到排序正面。

If you write a camera class and use its functions to move/rotate it in the world, you can use the matrix you get from the internal quaternion to transform the vertices, giving you the position in camera space so you can sort triangles from back to front.

GRAY°灰色天空 2024-08-14 14:48:24

在我看来,“queueTriangle”调用效率非常低。现代引擎通常一次处理数千个三角形,因此您通常几乎不会处理单个三角形级别的任何内容。如果您大量更改纹理来完成此排序,那么情况会更糟。

我建议采用一种更简单的方法 - 通过对世界空间中的演员位置而不是单个三角形的位置进行排序,以不太严格的顺序绘制不透明多边形,并从前到后渲染演员,一次一个演员。您的透明/半透明多边形仍然需要从后到前的方法(前提是您没有使用预乘 alpha),但其他一切都应该更简单、更快。

A 'queueTriangle' call sounds to me to be very inefficient. Modern engines often work with many thousands of triangles at a time so you'd normally hardly ever be working with anything on the level of a single triangle. And if you were changing textures a lot to accomplish this ordering then that is even worse.

I'd recommend a simpler approach - draw your opaque polygons in a much less rigorous order by sorting the actor positions in world space rather than the positions of individual triangles and render the actors from front to back, an actor at a time. Your transparent/translucent polygons still require the back-to-front approach (providing you're not using premultiplied alpha) but everything else should be simpler and faster.

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