Projection- Transforming 3d to 2d

后端 未结 3 596
北海茫月
北海茫月 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:30

    I would highly recommend using an existing graphics package to do this rather trying to write your own libraries. I don't know what language you are working in but OpenGL is an open source graphics engine that can be used for 3D rendering and has cross-language comparability so it might be the place to start.

    If you insist on doing it by hand there is very good example code in the answer to this question.

    0 讨论(0)
  • 2021-02-14 22:50

    You only need to take camera angle into account if you intend to rotate your shape in 3 dimensions. If you have a firm understanding of linear algebra and trigonometry, it's worth the extra effort since it makes your program more flexible, but if your not too string in mathematics I would recommend the following solution.

    What you need to be able to do to project a 3D image into a 2D plain is create and equation that will map.

    (x,y,z) -> (x',y')
    

    You can do this by defining three mappings form a 3D point to a 2D point.

    (1,0,0) -> (  1,  0)
    (0,1,0) -> (  0,  1)
    (0,0,1) -> (-.7,-.7)
    

    I used (-.7,-.7) for the z access because that point is approximately 1 unit from the origin and half way between the x and y access.

    After you have these three points you have enough information to calculate any arbitrary point x,y,z.

    (x,y,z) -> (1*x - .7*z, 1*y - .7*z)
    

    In computer graphics the origin of a grid is not in the center of the screen but instead in the top left hand corner. In order use the equation we just generated in our program we must define an offset to move the origin to the center of the screen. We will call this offset point(Ox, Oy).

    With the offset our equation becomes the following.

    (x,y,z) -> (Ox + 1*x - .7*z, Oy + 1*y - .7*z)
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题