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

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

问题描述

我在尝试预测了一系列的3D点到使用透视相机矩阵屏幕。我没有世界空间(或认为它是单位矩阵)和我的相机不具备摄像头空间(或认为这是一个单位矩阵),我有一个4×4矩阵为我的对象空间。

我正在采取目标矩阵和它由照相机透视矩阵相乘,以下面的方法产生:

  Matrix4 createPerspectiveMatrix(浮动FOV,浮动方面,浮附近,浮远)
{
    浮fov2 =(FOV / 2)*(Math.PI / 180);
    浮法棕褐色= Math.tan(fov2);
    浮动F = 1 /棕褐色;

    返回新Matrix4(
        的f /方面中,0,0,0,
        0中,f,0,0,
        0,0, - ((近远+)/(近远)),(2 * *远近)/(近远),
        0,0,1,0
    );
}
 

我然后取我的点[X,Y,Z,1]和乘以由所得的立体矩阵和对象的矩阵乘法。

接下来的部分是在这里我得到confuzzled,我是pretty的肯定,我需要是-1和1或0和1的范围内得到这些点,并在的情况下,具有第一组值,然后我会在屏幕宽度和高度的屏幕坐标x和y的值respectivly乘以点或通过屏幕高度/ 2和宽/乘以2的值,并添加相同的值到相应的点。

一步地告诉我这是如何实现或者我可能与任何此类脚麻是massivly AP preciated任何一步! :D

最好的问候大家!

P.S。在一个身份/翻译矩阵的例子中,在我的模型我的矩阵格式是:

  [1,0,0,TX,
 0,1,0,TY,
 0,0,1,TZ,
 0,0,0,1]
 

解决方案

您的问题是,你忘了执行透视除法。

透视除法意味着你把你的观点的x,y和z分量以其w的组成部分。 这是必需的改变​​你的观点,从的齐四维空间的到的归一化设备坐标系统的(NDCS),其中每个组件X,Y或Z -1到1之间,或下降0和1

这个改造后,就可以(通过屏幕宽度,heigth等乘点)做你的视口变换。

有这种转换管道的Foley的书一个很好的观点(计算机图形学:原理与C语言练习),你可以在这里看到:

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

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 ]

解决方案

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

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

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