3D 投影问题:透视除法后 Z 值不在 [1,-1] 中

发布于 2024-11-05 04:38:58 字数 858 浏览 0 评论 0原文

我正在尝试在光栅化 3D 点的过程中进行简单的透视投影。这是所有的矩阵和其他信息。所有矩阵都是行主矩阵。坐标系是右手坐标系。

相机位于 [0,0,-1],点位于 [0,0,0](矩阵运算时 w=1)

模型视图矩阵(凸轮矩阵的逆,即 tx = 0;ty = 0; tz = 1):

[1 0 0 tx]
[0 1 0 ty]
[0 0 1 tz]
[0 0 0 1 ]

透视矩阵:

[f/aspect,0,0,0]
0,f,0,0
0,0,-(near+far)/(near-far),2*far*near/(near-far)
0,0,1,0]

由于视口是正方形,因此长宽比等于 1。远 = 100,近 = 0.1 f = 1/tan(fovDegress*M_PI/360);

结果矩阵是:

1.94445, 0,        0,        0
0,       1.944445, 0,        0
0,       0,        1.020202, -2.020202
0,       0,        1,        0

现在我将模型视图矩阵,然后投影矩阵应用到点向量,然后我得到一个新点 Pv = {x,y,z,w} 然后我得到标准化坐标 x' = x/w ; y' = y/w;且 z' = z/w; 只要该点位于截锥体中,x' 和 y' 始终位于 [-1,1] 之间。但 z' 的情况却并非如此。当该点接近相机时,z' 值呈指数增加。当点位于 [0,0,0] 时,z' 值等于 -1。

现在,我需要剪辑一些线,所以我需要 z' 值在 [1,-1] 之间。我想知道我的程序出了什么问题。谢谢。

I'm trying to do a simple perspective projection in the process of rasterizing a 3D point. Here are all the matrices and other info. All Matrices are row major. The coordinate system is Right Handed.

The Camera is at [0,0,-1] and the point is at [0,0,0] (w=1 for matrix operations)

Model View Matrix(Inverse of Cam Matrix i.e, tx = 0;ty = 0; tz = 1):

[1 0 0 tx]
[0 1 0 ty]
[0 0 1 tz]
[0 0 0 1 ]

Perspective Matrix:

[f/aspect,0,0,0]
0,f,0,0
0,0,-(near+far)/(near-far),2*far*near/(near-far)
0,0,1,0]

aspect is equal to 1 since the viewport is square. Far = 100 and Near = 0.1
f = 1/tan(fovDegress*M_PI/360);

The resultant Matrix is:

1.94445, 0,        0,        0
0,       1.944445, 0,        0
0,       0,        1.020202, -2.020202
0,       0,        1,        0

Now I apply the Model View Matrix and then the Projection Matrix to the point vector and then I get a new point Pv = {x,y,z,w}
Then I get the normalized co-ordinates x' = x/w ; y' = y/w; and z' = z/w;
x' and y' always lie in between [-1,1] as long as the point is in the frustum. But the same isn't the case for z'. As the point approaches near the camera, the z' values increases exponentially. When the point is at [0,0,0] z' value equals -1.

Now, I need to clip some lines, so I need z' value to be in between [1,-1]. I am wondering what's wrong with my procedure. Thank you.

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

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

发布评论

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

评论(1

太阳哥哥 2024-11-12 04:38:58

您体验到的是透视深度映射的非线性。对于平截头体投影矩阵,这遵循距视点距离的 1/x 定律。

编辑:

刚刚仔细检查了你的矩阵:你的截锥体矩阵错了。正确的视锥体矩阵是

f/aspect, 0,                      0,                     0
0,        f,                      0,                     0
0,        0, -(far+near)/(far-near), 2*far*near/(far-near)
0,        0,                      1,                     0

如果接近原点,仍然会遇到被零除的情况。只需将向量 (0,0,0,1) 穿过那个东西,就会在剪辑空间中得到 (x=0,y=0,z=2*far*near/(far-near),w=0) 。然后同质除法 {x,y,z}/(w=0) ← 爆炸。

What you experiencing is the nonlinearity of perspectivic depth mapping. With a frustum projection matrix this follows a 1/x law with distance from the viewpoint.

EDIT:

Just double checked your matrices: You got your frustum matrix wrong. The correct frustum matrix is

f/aspect, 0,                      0,                     0
0,        f,                      0,                     0
0,        0, -(far+near)/(far-near), 2*far*near/(far-near)
0,        0,                      1,                     0

Still you'll run into a division by zero if you approach the origin. Just put the vector (0,0,0,1) through that thing, which results in (x=0,y=0,z=2*far*near/(far-near),w=0) in clip space. And then the homogenous division {x,y,z}/(w=0) ← blows up.

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