How to draw a Perspective-Correct Grid in 2D

后端 未结 9 1209
滥情空心
滥情空心 2020-12-25 08:43

I have an application that defines a real world rectangle on top of an image/photograph, of course in 2D it may not be a rectangle because you are looking at it from an angl

相关标签:
9条回答
  • 2020-12-25 09:25

    Given a rotation around the y axis, especially if rotation surfaces are planar, the perspective is generated by vertical gradients. These get progressively closer in perspective. Instead of using diagonals to define four rectangles, which can work given powers of two... define two rectangles, left and right. They'll be higher than wide, eventually, if one continues to divide the surface into narrower vertical segments. This can accommodate surfaces that are not square. If a rotation is around the x axis, then horizontal gradients are needed.

    0 讨论(0)
  • 2020-12-25 09:26

    What you need to do is represent it in 3D (world) and then project it down to 2D (screen).

    This will require you to use a 4D transformation matrix which does the projection on a 4D homogeneous down to a 3D homogeneous vector, which you can then convert down to a 2D screen space vector.

    I couldn't find it in Google either, but a good computer graphics books will have the details.

    Keywords are projection matrix, projection transformation, affine transformation, homogeneous vector, world space, screen space, perspective transformation, 3D transformation

    And by the way, this usually takes a few lectures to explain all of that. So good luck.

    0 讨论(0)
  • 2020-12-25 09:29

    Here's the solution.

    The basic idea is you can find the perspective correct "center" of your rectangle by connecting the corners diagonally. The intersection of the two resulting lines is your perspective correct center. From there you subdivide your rectangle into four smaller rectangles, and you repeat the process. The number of times depends on how accurate you want it. You can subdivide to just below the size of a pixel for effectively perfect perspective.

    Then in your subrectangles you just apply your standard uncorrected "textured" triangles, or rectangles or whatever.

    You can perform this algorithm without going to the complex trouble of building a 'real' 3d world. it's also good for if you do have a real 3d world modeled, but your textriangles are not perspective corrected in hardware, or you need a performant way to get perspective correct planes without per pixel rendering trickery.

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