投影 - 将3d转换为2d [英] Projection- Transforming 3d to 2d

查看:117
本文介绍了投影 - 将3d转换为2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题或好,我不知道如何将3d点与x,y,z值转换为2d点,
我必须绘制投影,其中我有x,y,z值但我不知道如何将它们转换为2d,因此我可以将它们移动到我的坐标轴上。




我一直在寻找维基和谷歌,howevever我不太确定哪个矩阵我们首先假设相机看着你的场景是以原点为中心的,并且看着 -z 方向。
然后:


  • 透视投影由以下公式给出:

    x'= x / z

    y'= y / z


  • 正射投影由下式给出:

    x'= x

    y'= y

    (即只放弃z组件)




现在您已经应用了上述步骤,您可能会获得(x',y')=( - 28.4,+ 134.5)。您现在需要根据屏幕分辨率和相机缩放比例和宽高比进行缩放和居中:例如,您可能需要乘以 Zoom 并添加 screen_center 给你的 x y 组件(注意:大多数图形渲染系统的 y 方向朝下,因此您可能需要交换 y 组件的符号)。您仍然可能最终得到负坐标或坐标大于您的画布大小的像素。

最后,如果您的相机不是,您可能会想知道该怎么做,指向 -z 或不在原点居中。对于后面的内容来说,很简单:只需在做其他事情之前将相机坐标减去所有3D点的组成部分。对于相机旋转,它实际上也很简单:只需要​​以相反的方式旋转相机,然后再进行其他操作。这意味着您需要将所有3D坐标乘以相机旋转矩阵的转置。这一步背后的想法是,移动相机与移动相反方向的点完全相同(并且发生旋转矩阵的倒数是该矩阵的转置)。

I have problem or well, I do not know how to transform 3d point with x,y,z values to 2d point, I have to draw projection, where I do have x,y,z values for points but I don't know how to transform them into 2d so I can move them onto my axis.

I have been looking around wiki and google, howevever I'm not quite sure which matrix transformations should I use to get wanted result.

解决方案

Let's first assume the camera looking at your scene is centered at the origin, and looking at the -z direction. Then:

  • a perspective projection is given by:
    x' = x/z
    y' = y/z

  • an orthographic projection is given by:
    x' = x
    y' = y
    (i.e., just discard the z component)

Now that you have applied the step above, you might obtain a point that is at (x',y') = (-28.4, +134.5). You now need to scale and center them based on your screen resolution and camera "zoom factor" and aspect ratio : for instance, you might want to multiply by Zoom and add screen_center to both your x and y components (beware: most graphics rendering systems have the y direction pointing down, so you might need to swap signs for the y component). You might still end up with pixels with negative coordinates or whose coordinates are greater than your canvas size. Just discard them : it means that they are outside of your view frustum.

Finally, you might wonder what to do if your camera is not pointing toward -z or not centered at the origin. For the later, it is simple: just subtract the camera coordinates to the component of all your 3D points prior to doing anything else. For the camera rotation, it is also actually quite easy : you just need to rotate your points in the inverse way your camera is rotated prior to doing anything else as well. That just means that you need to multiply all your 3D coordinates by the transpose of the camera rotation matrix. The idea behind this step is that moving the camera around is exactly the same as moving your points in the reverse direction (and it happen that the inverse of a rotation matrix is the transpose of that same matrix).

这篇关于投影 - 将3d转换为2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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