渲染无限大的平面 [英] Rendering infinitely large plane

查看:307
本文介绍了渲染无限大的平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想渲染一个平面,使其看起来好像在各个方向上都达到无穷大.我希望距离中的平面边界为地平线.

使用简单的网格不起作用-计算机无法渲染无限多个三角形.即使可能,照相机视锥也会切掉远处的多边形,并在平面边界和水平线之间形成间隙.

一种解决方法是数学计算地平线:在平面上找到点,这些点也位于无限远的平面上.将这些点与视口的两个角连接在一起,将创建一个梯形,该梯形表示要查找的平面.但是,这种方式无法正确地照亮飞机,无法施加纹理或需要精细三角剖分的其他任何东西.

解决方案

您可以使用标准栅格化管道绘制无限平面.它使用的齐次坐标可以像正则欧几里得点一样代表理想"点(也称为消失点或无穷大点),同样,设置将远平面置于无穷大的投影矩阵也是非常实用的.

一种简单的方法是在每个象限中使用一个三角形,如下所示:

vertices [x,y,z,w], for drawing an (x,y) coordinate plane, at (z==0):
  0: [ 0, 0, 0, 1 ]
  1: [ 1, 0, 0, 0 ]
  2: [ 0, 1, 0, 0 ]
  3: [-1, 0, 0, 0 ]
  4: [ 0,-1, 0, 0 ]

draw 4 triangles using indices:
  (0,1,2); (0,2,3); (0,3,4); (0,4,1)

如果您想要测试图案(例如无限棋盘),则必须处理以下事实:将三角形拉伸到无穷大会扭曲任何标准纹理.但是,您可以编写一个像素着色器,该着色器根据实际的3D点(即从世界空间(x,y,z)坐标使用xy)确定颜色,而完全忽略(扭曲的)纹理坐标.

您可以基于奇偶校验(对于棋盘格)在两种恒定颜色之间进行选择,也可以根据所选坐标的小数部分对纹理进行采样来平铺纹理.


请注意,对于x,y和z,OpenGL的剪辑空间均为[-1..1].您可以通过评估随着远剪辑距离f的增加而限制,来计算适当的投影矩阵不受限制:

clip coords: [x]  =  [ n/r             ]  * view coords [x]
             [y]     [     n/t         ]                [y]
             [z]     [          -1 -2n ]                [z]
             [w]     [          -1   0 ]                [w]

其中(如链接中):n是近剪切平面,r是近剪切平面的平截头体宽度的一半,t是近剪切平面的平截头体高度的一半. /p>

我尚未测试上面的矩阵,因此值得您花钱购买.另外请注意,当您接近无穷大时,深度值将失去其精度...

尽管,在更近距离处的精度可能会很好-例如,在任何给定距离处,(near:infinity)情况下的深度分辨率应比(near:far)情况下的深度分辨率小约10%. )的比例为(1:10).

I want to render a plane so that it looks as if it goes to infinity in all directions. I want the plane boundary in the distance to be the horizon.

Using a simple mesh does not work - the computer can't render infinitely many triangles. Even if this was possible, the camera frustum would cut out the distant polygons and create a gap between the plane boundary and the horizon.

A workaround is to compute the horizon mathematically: finding points on the plane, which also lie on the plane at infinity. Connecting these points and two corners of the viewport creates a trapezoid which represents the sought plane. However, this way the plane can not be lit properly, or applied a texture, or anything else which requires a fine triangulation...

解决方案

You can draw an infinite plane using the standard rasterization pipeline. The homogeneous coordinates it uses can represent "ideal" points (otherwise known as vanishing points or points at infinity) just as happily as regular Euclidean points, and likewise it is perfectly practical to set up a projection matrix which places the far plane at infinity.

A simple way to do this would be to use one triangle per quadrant, as follows:

vertices [x,y,z,w], for drawing an (x,y) coordinate plane, at (z==0):
  0: [ 0, 0, 0, 1 ]
  1: [ 1, 0, 0, 0 ]
  2: [ 0, 1, 0, 0 ]
  3: [-1, 0, 0, 0 ]
  4: [ 0,-1, 0, 0 ]

draw 4 triangles using indices:
  (0,1,2); (0,2,3); (0,3,4); (0,4,1)

If you want a test pattern (like an infinite checkerboard), you will have to deal with the fact that stretching your triangles to infinity will distort any standard texture. However, you can write a pixel shader that determines the color based on the actual 3D point (i.e., use x and y from the worldspace (x,y,z) coordinates), ignoring the (distorted) texture coords altogether.

You could choose between two constant colors based on parity (for a checkerboard), or tile a texture by sampling it based on the fractional part of your chosen coordinates.


Note that OpenGL's clip space is [-1..1] for each of x, y, and z. You can compute the appropriate projection matrix by evaluating the limits as far clip distance f increases without bound:

clip coords: [x]  =  [ n/r             ]  * view coords [x]
             [y]     [     n/t         ]                [y]
             [z]     [          -1 -2n ]                [z]
             [w]     [          -1   0 ]                [w]

Where (as in the link): n is the near clip plane, r is half the frustum width at the near clip plane, and t is half the frustum height at the near clip plane.

I have not tested the above matrix, so it's worth what you paid for it. Also be aware that the depth value will lose its precision as you approach infinity...

Although, the precision at closer distances will be likely be fine -- e.g., at any given distance, the depth resolution in the (near:infinity) case should be about 10% less than the case where the (near:far) ratio is (1:10).

这篇关于渲染无限大的平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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