为什么我们应该在 OpenGL 管道的片段阶段使用眼空间坐标?

发布于 2024-11-19 12:23:19 字数 146 浏览 7 评论 0原文

我目前正在编写一个小型 3D 引擎,我想知道为什么我应该在片段着色器中使用眼空间坐标。为此,我必须将相机矩阵放入统一的矩阵中,以转换眼坐标中的光位置,并使用camera_normal矩阵将光方向放入眼坐标中。

为什么每个人都使用这些坐标?我实在看不出有什么优势。

I'm currently programming a small 3D engine, and I was wondering why I should go in eye-space coordinates in the fragment shader. To do that, I have to put my camera matrix in a uniform to convert light positions in eye-coordinates, and a camera_normal matrix to put the light directions in eye-coordinates.

Why is everyone using these coordinates? I don't really see any advantage.

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

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

发布评论

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

评论(2

强者自强 2024-11-26 12:23:19

使用眼距的原因有几个:

  1. 方便。这是一个存在的明确定义的空间,并且您无论如何都会在转换位置的过程中计算该空间。
  2. 它与世界空间具有相同的规模,但没有世界空间所存在的问题。眼睛空间总是(相对)接近于零(因为眼睛位于 0),因此它是具有显式变换矩阵的合理空间。比例很重要,因为您可以提供在世界空间中计算的距离(如光衰减项)。眼睛空间中的距离不会改变。
  3. 无论如何,您需要将其转换为线性空间。在像后投影空间这样的非线性空间中进行照明,尤其是衰减照明,是很棘手的。因此,您必须在某种线性空间中提供法线和位置,因此它也可能是眼睛空间。
  4. 它需要最少的转换。眼睛空间是投影变换之前的空间。如果您必须反向转换为线性空间(例如延迟渲染),则眼睛空间是需要最少操作的空间。

There are several reasons eye-space is used:

  1. It's convenient. It's a well-defined space that exists, and one that you compute on the way to transforming positions anyway.
  2. It has the same scale as world space, but doesn't have the problems world space does. Eye space is always (relatively) close to zero (since the eye is at 0), so it's a reasonable space for having an explicit transform matrix for. The scale is important, because you can provide distances (like the light attenuation terms) that are computed in world space. Distances don't change in eye space.
  3. You need to transform it into a linear space anyway. Doing lighting, particularly with attentuation, in a non-linear space like post-projection spaces is... tricky. So you would have to provide normals and positions in some kind of linear space, so it may as well be eye space.
  4. It requires the fewest transforms. Eye space is the space right before the projection transform. If you have to reverse-transform to a linear space (deferred rendering, for example), eye space is the one that requires the fewest operations.
兔姬 2024-11-26 12:23:19

您不必向着色器提供相机矩阵并在那里进行灯光位置和方向转换。实际上,这样做效率相当低,因为您对每个顶点一次又一次地对相同的数字执行相同的操作。

只需变换 CPU 端的灯光​​位置和方向,并将容易变换的灯光参数提供给着色器即可。然而,眼睛空间中的光照计算仍然更加简洁,特别是在涉及法线贴图的情况下。但无论如何,您都必须将所有内容转换为视空间,因为法线不会通过透视变换进行转换(尽管顶点位置可以直接转换为剪辑空间)。

You don't have to supply the camera matrix to the shader and do the light position and direction transformation there. Actually it is rather inefficient to do it that way, since you're doing the very same operations on the same numbers again and again for each vertex.

Just transform the light position and direction CPU side and supply the readily transformed light parameters to the shader. However lighting calculations are still more concise in eye space, especially if normal mapping is involved. But you've to transform everything into eyespace anyway, as normals are not transformed by the perspective transform (though the vertex positions could be transformed into clip space directly).

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