为什么我的正交投影矩阵不起作用?

发布于 2025-01-02 20:31:02 字数 968 浏览 1 评论 0原文

我正在尝试使用正交投影渲染位于平面 (z=0) 中的一些四边形。在阅读正交投影矩阵的公式?后,我确实设置了投影矩阵?但我想我做错了什么。

我的第一次尝试是以下矩阵。 W 和 H 是我所需视口的宽度和高度,在以下示例中为 640x640:

 ˹ 2/w 0   0  0 ˺
 | 0   2/h 0  0 |
 | 0   0   0  0 |
 ˻ 0   0   0  1 ˼

这是我渲染左上角位于 (0,0,0) 的四边形时获得的结果:

No good

因为它是垂直翻转的,所以我将矩阵更改为:

 ˹ 2/w  0   0  0 ˺
 | 0   -2/h 0  0 |
 | 0    0   0  0 |
 ˻ 0    0   0  1 ˼

我得到:

better


然后我尝试移动我的四边形,结果出乎意料。我希望 3D 空间的 X 和 Y 与视口的 X 和 Y 相匹配。然而,当我沿 X 轴移动四边形时,发生了以下情况:

ouch


沿 Y 轴移动同样产生了意想不到的结果:

ouch ouch


如何修复矩阵,使 3D 空间的 X 轴和 Y 轴与视口的 X 轴和 Y 轴匹配?非常感谢。

I am trying to render some quads that are located in the plane (z=0) with an orthogonal projection. I did set up a projection matrix after reading Formula for a orthogonal projection matrix? but I think I did something wrong.

My first attempt was the following matrix. W, and H are the width and height of my desired viewport which is 640x640 in the following examples :

 ˹ 2/w 0   0  0 ˺
 | 0   2/h 0  0 |
 | 0   0   0  0 |
 ˻ 0   0   0  1 ˼

Here is what I obtained when I rendered a quad whose topleft corner was in (0,0,0) :

No good

Since it was flipped vertically I changed my matrix to :

 ˹ 2/w  0   0  0 ˺
 | 0   -2/h 0  0 |
 | 0    0   0  0 |
 ˻ 0    0   0  1 ˼

I obtained :

better


Then I tried to move my quad around and the results were unexpected. I wanted the X and Y of the 3D space to match those of the viewport. However here is what happened when I moved the quad along the X axis :

ouch


Moving along the Y axis gave equally unexpected results :

ouch ouch


How can I fix my matrix so that the X and Y axis of the 3D space match those of the viewport ? Many thanks in advance.

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

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

发布评论

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

评论(2

留蓝 2025-01-09 20:31:02

这个投影矩阵实际上是正确的。问题出在四元转换代码中,抱歉弄乱了。

This projection matrix is actually correct. The problem was in the quad translation code, sorry for messing up.

蓦然回首 2025-01-09 20:31:02

你错过了远近剪辑。试试这个

2/viewWidth    0              0             0
0             -2/viewHeight   0             0
0              0              1/(far-near)  -near/(far-near)
0              0              0             1

You missing the far near clipping. Try this

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