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

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

问题描述

我尝试使用透视相机矩阵将一系列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,远)/(近远)),(2 *远*近)/(近远),
0,0,1,0
)。
}

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



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



任何一步一步告诉我这可能是什么实现或在哪里,我可能会错误与任何这将被massivly赞赏! :D



最适合所有人!



在身份/翻译矩阵的例子中,我的模型中的矩阵格式是:

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

解决方案

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



透视除法意味着您将点的x,y和z分量除以其w分量。
将点从均匀4D空间转换为标准化设备坐标系(NDCS),其中每个分量x,y或z在-1



此变换后,您可以进行视口转换(按屏幕宽度,高度等乘以点)。



在Foley的书(Computer Graphics:Principles and Practice in 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天全站免登陆