简体   繁体   English

将 3D 点投影到 2D 屏幕坐标

[英]Project 3D point to 2D screen coordinates

I'm trying to project a point from 3D world space to 2D screen coordinates.我正在尝试将一个点从 3D 世界空间投影到 2D 屏幕坐标。 I have almost everything worked out but I'm having a hard time actually projecting the point.我几乎已经解决了所有问题,但我很难真正表达出这一点。

The setup is as follows:设置如下:

Pitch = 90 (all the way up, looking down) Pitch = 90(一直向上,向下看)

Yaw = 0 (facing north)偏航 = 0(朝北)

Roll = 0 (as normal)滚动 = 0(正常)

I currently am casting a ray from the camera position through the center of the viewport all the way to the end of the frustum, when debugging the ray the values are 0, -1, 0 which means it's facing down so this seems to be correct.我目前正在从相机位置投射一条光线穿过视口的中心一直到视锥体的末端,调试光线时,值是 0、-1、0,这意味着它朝下,所以这似乎是正确的. Now what I'm trying to achieve is to project the fixed camera point (point on the middle of the ray) from 3D to 2D.现在我想要实现的是将固定的相机点(光线中间的点)从 3D 投影到 2D。

When rotating the camera the projected point should be at the center of the viewport, which it isn't unfortunately.旋转相机时,投影点应位于视口的中心,不幸的是,这并非如此。

The current output:当前输出:

Viewport 800 x 600视口 800 x 600

projectTo2D: vector: Vector4f: 0.0 0.0 500.0 1.0
World matrix:
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0

View matrix:
0.99999994 0.0 0.0 0.0
0.0 -4.371139E-8 -1.0 2.1855694E-5
0.0 1.0 -4.371139E-8 -500.0
0.0 0.0 0.0 1.0

Projection matrix:
-0.75 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0000999 -0.10000999
0.0 0.0 1.0 0.0

Projection world view:
-0.74999994 0.0 0.0 0.0
0.0 -4.371139E-8 -1.0 2.1855694E-5
0.0 1.0000999 -4.3715755E-8 -500.14996
0.0 1.0 -4.371139E-8 -500.0

HDC: Vector4f: 0.0 -499.99997 -500.15 -500.00003
NDC: Vector4f: -0.0 0.9999999 1.0002999 1.0
Viewport coordinates: 400.0, 599.99994

Firstly, you are projecting a plain 3d vector.首先,您正在投影一个普通的 3d 矢量。 Doing so, you ignore the translation and perspective parts of the matrix.这样做,您将忽略矩阵的平移和透视部分。 Use a 4d vector with 1 as the w -component.使用1作为w分量的 4d 向量。

Secondly, you are missing the w-clip:其次,您缺少 w-clip:

transform(matrix, vector);

vector.x /= vector.w;
vector.y /= vector.w;
vector.z /= vector.w;
vector.w /= vector.w;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM