如何在 2D 中绘制透视校正网格 [英] How to draw a Perspective-Correct Grid in 2D

查看:21
本文介绍了如何在 2D 中绘制透视校正网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它在图像/照片的顶部定义了一个真实世界的矩形,当然在 2D 中它可能不是矩形,因为您是从某个角度看它的.

问题是,假设矩形需要在其上绘制网格线,例如如果它是 3x5,那么我需要从 1 边到 3 边绘制 2 条线,从 2 边到 4 边绘制 4 条线.

现在我将每条线分成等距的部分,以获得所有网格线的起点和终点.然而,矩形的角度越大,这些线就越不正确",因为离你越远的水平线应该靠得更近.

有人知道我应该搜索的算法的名称吗?

是的,我知道您可以在 3D 中执行此操作,但是对于此特定应用程序,我仅限于 2D.

解决方案

这是解决方案.

基本思想是您可以通过对角连接角来找到矩形的透视正确中心".两条结果线的交点是您的透视正确中心.从那里您将矩形细分为四个较小的矩形,然后重复该过程.次数取决于您想要的准确程度.您可以细分到略低于一个像素的大小,以获得有效的完美视角.

然后在您的子矩形中,您只需应用标准的未校正的纹理"三角形或矩形或其他任何东西.

您可以执行此算法,而无需遇到构建真实"3d 世界的复杂麻烦.如果您确实建模了真实的 3d 世界,但您的文本三角形没有在硬件中进行透视校正,或者您需要一种高性能的方法来获得透视正确的平面,而无需逐像素渲染技巧,那么这也很有用.>

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

The problem is, say that the rectangle needs to have grid lines drawn on it, for example if it is 3x5 so I need to draw 2 lines from side 1 to side 3, and 4 lines from side 2 to side 4.

As of right now I am breaking up each line into equidistant parts, to get the start and end point of all the grid lines. However the more of an angle the rectangle is on, the more "incorrect" these lines become, as horizontal lines further from you should be closer together.

Does anyone know the name of the algorithm that I should be searching for?

Yes I know you can do this in 3D, however I am limited to 2D for this particular application.

解决方案

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.

这篇关于如何在 2D 中绘制透视校正网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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