如何撤消相机变换和透视? [英] How to undo camera transformation and perspective?

查看:37
本文介绍了如何撤消相机变换和透视?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在世界原点定位一个 3d 对象,这样当我移动相机或改变它的视野时,它不会改变它的位置.我试过这样做

I am trying to orient a 3d object at the world origin such that it doesn't change its position wrt camera when I move the camera OR change its field of view. I tried doing this

Object Transform = Inverse(CameraProjectionMatrix)

如何取消透视分割,因为当我改变 fov 时,对象会受到它的影响

How do I undo the perspective divide because when I change the fov, the object is affected by it

详细的样子

origin(0.0, 0.0, 0.0, 1.0f);
projViewInverse = Camera.projViewMatrix().inverse();
projectionMatrix = Camera.projViewMatrix();
projectedOrigin = projectionMatrix * origin;
topRight(0.5f, 0.5f, 0.f);
scaleFactor = 1.0/projectedOrigin.z();
scale(scaleFactor,scaleFactor,scaleFactor);
finalMatrix = projViewInverse * Scaling(w) * Translation(topRight);

推荐答案

如果您使用 gfx 管道,其中位置 (w=1.0) 和向量 (w=0.0)像这样转换为 NDC:

if you use gfx pipeline where positions (w=1.0) and vectors (w=0.0) are transformed to NDC like this:

(x',y',z',w') = M*(x,y,z,w) // applying transforms
(x'',y'') = (x',y')/w'      // perspective divide

其中 M 是所有 4x4 同质变换矩阵按顺序相乘的结果.如果你想回到原来的(x,y,z),你需要知道w',它可以从z 计算出来.该等式取决于您的投影.在这种情况下,您可以这样做:

where M are all your 4x4 homogenyuous transform matrices multiplied in their order together. If you want to go back to the original (x,y,z) you need to know w' which can be computed from z. The equation depends on your projection. In such case you can do this:

w' = f(z')             // z' is usually the value encoded in depth buffer and can obtained
(x',y') = (x'',y'')*w' // screen -> camera
(x,y) = Inverse(M)*(x',y',z',w') // camera -> world

然而,只有当您知道 z' 并且可以从中导出 w' 时,才能使用它.所以通常所做的(如果我们不能)是通过 (x'',y'') 从相机焦点投射光线,并在与相机所需的垂直距离处停止.对于透视投影,您可以将其视为三角形相似度:

However this can be used only if you know the z' and can derive w' from it. So what is usually done (if we can not) is to cast ray from camera focal point through the (x'',y'') and stop at wanted perpendicular distance to camera. For perspective projection you can look at it as triangle similarity:

所以对于每个你想要变换的顶点,你需要它在 znear 平面(屏幕)上的投影 x'',y'' 位置,然后只需缩放 x'',y''code>x'',y'' 与相机焦点的距离比 (*z1/z0).现在我们只需要焦距z0.这取决于您使用的投影矩阵类型.当您在相机坐标系中时,我通常会遇到 2 个版本,然后点 (0,0,0) 要么是焦点,要么是 znear 平面.然而,投影矩阵可以是任何因此焦点位置也可以变化......

So for each vertex you want to transform you need its projected x'',y'' position on the znear plane (screen) and then just scale the x'',y'' by the ratio between distances to camera focal point (*z1/z0). Now all we need is the focal length z0. That one dependss on the kind of projection matrix you use. I usually encounter 2 versions when you are in camera coordinate system then point (0,0,0) is either the focal point or znear plane. However the projection matrix can be any hence also the focal point position can vary ...

现在,当您必须处理纵横比时,第一种方法在内部将其处理为 M 内部.第二种方法需要在转换前应用纵横比校正的倒数.所以直接应用在 x'',y''

Now when you have to deal with aspect ratio then the first method deals with it internally as its inside the M. The second method needs to apply inverse of aspect ratio correction before conversion. So apply it directly on x'',y''

这篇关于如何撤消相机变换和透视?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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