使用透视相机矩阵将 3D 点投影到 2D 屏幕空间 [英] Projecting a 3D point to 2D screen space using a perspective camera matrix

查看:43
本文介绍了使用透视相机矩阵将 3D 点投影到 2D 屏幕空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 
    );
}

然后我将我的点 [x, y, z, 1] 乘以透视矩阵和对象矩阵的乘法结果.

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

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

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.

任何一步一步地告诉我这可能是如何实现的,或者我可能会在哪里出错,我们将不胜感激!!:D

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

向大家问好!

附言在恒等/平移矩阵的示例中,我的模型中的矩阵格式为:

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 ]

推荐答案

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

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

透视分割意味着您将点的 x、y 和 z 分量除以它的 w 分量.这是将您的点从 Homogeneous 4D Space 转换为 Normalized Device Coordinates System (NDCS) 所必需的,其中每个分量 x、y 或 z 落在 -1 和 1 之间,或者0 和 1.

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).

在 Foley 的书(Computer Graphics:Principles and Practice in C)中有一个关于这种转换管道的很好的观点,你可以在这里看到:

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

这篇关于使用透视相机矩阵将 3D 点投影到 2D 屏幕空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆