使用透视相机矩阵将 3D 点投影到 2D 屏幕空间

发布于 2024-09-18 02:26:27 字数 887 浏览 8 评论 0原文

我正在尝试使用透视相机矩阵将一系列 3D 点投影到屏幕上。我没有世界空间(或将其视为单位矩阵),并且我的相机没有相机空间(或将其视为单位矩阵),我的对象空间确实有一个 4x4 矩阵。

我将对象矩阵乘以通过以下方法生成的相机透视矩阵:

Matrix4 createPerspectiveMatrix( Float fov, Float aspect, Float near, Float far )
{
    Float fov2 = (fov/2) * (Math.PI/180);
    Float tan = Math.tan(fov2);
    Float f = 1 / tan;

    return new Matrix4 ( 
        f/aspect, 0, 0, 0,
        0, f, 0, 0,
        0, 0, -((near+far)/(near-far)), (2*far*near)/(near-far),
        0, 0, 1, 0 
    );
}

然后将我的点 [x, y, z, 1] 并将其乘以透视矩阵和对象矩阵的乘积。

下一部分是我感到困惑的地方,我很确定我需要在 -1 和 1 或 0 和 1 的范围内获得这些点,并且在具有第一组值的情况下,然后我将这些点分别乘以屏幕坐标 x 和 y 值的屏幕宽度和高度,或者将这些值乘以屏幕高度/2 和宽度/2,并将相同的值添加到相应的点。

任何一步步告诉我如何实现这一点或者我可能在其中任何一个方面出错的地方将受到极大的赞赏! :D

向大家致以最诚挚的问候!

PS 在恒等/平移矩阵的示例中,我的模型中的矩阵格式是:

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

I am attempting to project a series of 3D points onto the screen using a perspective camera matrix. I do not have world space (or consider it being an identity matrix) and my camera does not have camera space (or consider it an identity matrix), I do have a 4x4 matrix for my object space.

I am taking the object matrix and multiplying it by the camera perspective matrix, generated with the following method:

Matrix4 createPerspectiveMatrix( Float fov, Float aspect, Float near, Float far )
{
    Float fov2 = (fov/2) * (Math.PI/180);
    Float tan = Math.tan(fov2);
    Float f = 1 / tan;

    return new Matrix4 ( 
        f/aspect, 0, 0, 0,
        0, f, 0, 0,
        0, 0, -((near+far)/(near-far)), (2*far*near)/(near-far),
        0, 0, 1, 0 
    );
}

I am then taking my point [x, y, z, 1] and multiplying that by the resulting multiplication of the perspective matrix and object matrix.

The next part is where i'm getting confuzzled, I'm pretty sure that I need to get these points within the ranges of either -1 and 1, or 0 and 1, and, in the case of having the first set of values, I would then multiply the points by the screen width and height for the screen coordinates x and y value respectivly or multiply the values by the screen height/2 and width/2 and add the same values to the respective points.

Any step by step telling me how this might be achieved or where I might be going wrong with any of this would be massivly appreciated!! :D

Best regards everyone!

P.S. In the example of a identity/translation matrix, my matrix format in my model is:

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

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

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

发布评论

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

评论(2

方觉久 2024-09-25 02:27:44

对于诸如此类的问题,我会推荐一个很好的资源:
http://scratchapixel.com/lessons/3d-高级课程/透视和正交投影矩阵/
它确实涉及投影矩阵的细节。它们是更多关于相机、构建相机光线等主题的课程。您需要进行一些挖掘。

For questions such as this one I would suggest an excellent resources:
http://scratchapixel.com/lessons/3d-advanced-lessons/perspective-and-orthographic-projection-matrix/
It really goes in the detail of the projection matrix. They are more lessons on the topic of camera, constructing camera rays, etc. You will need to do some digging.

洛阳烟雨空心柳 2024-09-25 02:27:30

你的问题是你忘记执行透视划分。

透视除法意味着将点的 x、y 和 z 分量除以其 w 分量。
这是将您的点从同质 4D 空间转换为标准化设备坐标系 (NDCS) 所必需的,其中每个分量 x、y 或 z 落在 -1 和 1 之间,或者0 和 1。

在此转换之后,您可以进行视口转换(将点乘以屏幕宽度、高度等)。

Foley 的书(计算机图形学:C 语言的原理与实践)对这种转换管道有很好的看法,您可以在这里看到:

http://www.ugrad.cs.ubc.ca/~cs314/notes/pipeline.html

Your problem is that you forget to perform the perspective division.

Perspective division means that you divide x, y and z component of your point by its w component.
This is required for transforming your point from Homogeneous 4D Space to Normalized Device Coordinates System (NDCS) in which each component x, y or z falls between -1 and 1, or 0 and 1.

After this transformation, you can do your viewport transformation (multiply points by screen width, heigth etc).

There's a good view of this transformation pipeline in Foley's book (Computer Graphics: Principles and Practice in C), you can see it here:

http://www.ugrad.cs.ubc.ca/~cs314/notes/pipeline.html

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