Projection- Transforming 3d to 2d

后端 未结 3 680
北海茫月
北海茫月 2021-02-14 21:51

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 kn

3条回答
  •  半阙折子戏
    2021-02-14 22:52

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

提交回复
热议问题