7.1 Planar Shadows平面阴影
A simple case of shadowing occurs when objects cast shadows onto a planar surface.A few types of algorithms for planar shadows are presented in this section, each with variations in the softness and realism of the shadows.
当物体将阴影投射到平面上时,会出现一个简单的阴影情况。本节将介绍几种平面阴影算法,每种算法在阴影的柔和度和真实感方面都有所不同。
7.1.1 Projection Shadows投影阴影
In this scheme, the three-dimensional object is rendered a second time to create a shadow. A matrix can be derived that projects the vertices of an object onto a plane [162, 1759]. Consider the situation in Figure 7.3, where the light source is located at l, the vertex to be projected is at v, and the projected vertex is at p. We will derive the projection matrix for the special case where the shadowed plane is y = 0, and then this result will be generalized to work with any plane.
在这个方案中,三维物体被第二次渲染以产生阴影。可以导出将对象的顶点投影到平面上的矩阵[162,1759]。考虑图7.3中的情况,其中光源位于l处,要投影的顶点位于v处,投影的顶点位于p处。我们将推导投影平面为y = 0的特殊情况下的投影矩阵,然后将此结果推广到任何平面。
Figure 7.3. Left: A light source, located at l, casts a shadow onto the plane y = 0. The vertex v is projected onto the plane. The projected point is called p. The similar triangles are used for the derivation of the projection matrix. Right: The shadow is being cast onto a plane, π : n · x + d = 0.
图7.3。左图:位于l处的光源将阴影投射到平面y = 0上。顶点v被投影到平面上。投影点称为p。相似的三角形用于投影矩阵的推导。右图:阴影被投射到一个平面上,π : n x + d = 0。
We start by deriving the projection for the x-coordinate. From the similar triangles in the left part of Figure 7.3, we get
我们从推导x坐标的投影开始。从相似的三角形中 在图7.3的左边,我们得到
The z-coordinate is obtained in the same way: pz = (lyvz − lzvy)/(ly − vy), while the y-coordinate is zero. Now these equations can be converted into the projection matrix M:
z坐标的获取方式相同:pz =(lyvz-lzvy)/(ly-vy),而y坐标为零。现在这些方程可以转换成投影矩阵M:
It is straightforward to verify that Mv = p, which means that M is indeed the projection matrix.
验证Mv = p是很简单的,这意味着M确实是投影矩阵。
In the general case, the plane onto which the shadows should be cast is not the plane y = 0, but instead π : n · x + d = 0. This case is depicted in the right part of Figure 7.3. The goal is again to find a matrix that projects v down to p. To this end, the ray emanating at l, which goes through v, is intersected by the plane π. This yields the projected point p:
在一般情况下,阴影应该投射到的平面不是平面y = 0,而是π : n x + d = 0。这种情况描述在图7.3的右部。目标仍然是找到一个将v向下投影到p的矩阵。为此,从l发出的穿过v的射线与平面π相交。这产生了投影点p:
This equation can also be converted into a projection matrix, shown in Equation 7.4, which satisfies Mv = p:
这个等式也可以转换成投影矩阵,如等式所示 7.4,满足Mv = p:
As expected, this matrix turns into the matrix in Equation 7.2 if the plane is y = 0, that is, n = (0, 1, 0) and d = 0.
不出所料,如果平面是y = 0,即n = (0,1,0),d = 0,这个矩阵就变成了方程7.2中的矩阵。
To render the shadow, simply apply this matrix to the objects that should cast shadows on the plane π, and render this projected object with a dark color and no illumination. In practice, you have to take measures to avoid allowing the projected triangles to be rendered beneath the surface receiving them. One method is to add some bias to the plane we project upon, so that the shadow triangles are always rendered in front of the surface.
要渲染阴影,只需将此矩阵应用于应该在π平面上投射阴影的对象,并使用深色和无照明渲染该投影对象。在实践中,您必须采取措施避免投影的三角形被渲染到接收它们的表面之下。一种方法是给我们投影的平面增加一些偏差,这样阴影三角形总是呈现在表面的前面。
A safer method is to draw the ground plane first, then draw the projected triangles with the z-buffer off, then render the rest of the geometry as usual. The projected triangles are then always drawn on top of the ground plane, as no depth comparisons are made.
更安全的方法是先绘制地平面,然后绘制投影三角形 关闭z缓冲区,然后照常渲染几何体的其余部分。投影的三角形总是绘制在地平面的顶部,因为没有进行深度比较。
If the ground plane has a limit, e.g., it is a rectangle, the projected shadows may fall outside of it, breaking the illusion. To solve this problem, we can use a stencil buffer. First, draw the receiver to the screen and to the stencil buffer. Then, with the z-buffer off, draw the projected triangles only where the receiver was drawn, then render the rest of the scene normally.
如果地平面有一个界限,例如它是一个矩形,投影的阴影可能会落在它的外面,打破了幻觉。为了解决这个问题,我们可以使用模板缓冲区。首先,将接收器拖到屏幕和模板缓冲区。然后,关闭z缓冲区,仅在绘制接收器的位置绘制投影三角形,然后正常渲染场景的其余部分。
Another shadow algorithm is to render the triangles into a texture, which is then applied to the ground plane. This texture is a type of light map, a texture that modulates the intensity of the underlying surface (Section 11.5.1). As will be seen,this idea of rendering the shadow projection to a texture also allows penumbrae and shadows on curved surfaces. One drawback of this technique is that the texture can become magnified, with a single texel covering multiple pixels, breaking the illusion.
另一种阴影算法是将三角形渲染到纹理中,然后应用于地平面。这种纹理是一种光照贴图,一种调节基础表面强度的纹理(第11.5.1节)。正如将要看到的,这种将阴影投影渲染到纹理的想法也允许半影和曲面上的阴影。这种技术的一个缺点是纹理会被放大,一个纹理覆盖多个像素,打破了这种错觉。
If the shadow situation does not change from frame to frame, i.e., the light and shadow casters do not move relative to each other, this texture can be reused. Most shadow techniques can benefit from reusing intermediate computed results from frame to frame if no change has occurred.
如果阴影情况不会逐帧变化,即灯光和阴影投射器不会相对移动,则该纹理可以重复使用。如果没有发生变化,大多数阴影技术可以受益于逐帧重用中间计算结果。
All shadow casters must be between the light and the ground-plane receiver. If the light source is below the topmost point on the object, an antishadow [162] is generated,since each vertex is projected through the point of the light source. Correct shadows and antishadows are shown in Figure 7.4. An error will also occur if we project an object that is below the receiving plane, since it too should cast no shadow.
所有的阴影投射者必须在灯光和地平面接收器之间。如果光源低于物体上的顶点,则产生反阴影[162],因为每个顶点都通过光源的点投影。正确的阴影和反阴影如图7.4所示。如果我们投射一个在接收平面下面的物体,也会发生错误,因为它也不应该投射阴影。
Figure 7.4. At the left, a correct shadow is shown, while in the figure on the right, an antishadow appears, since the light source is below the topmost vertex of the object.
图7.4。在左边,显示了一个正确的阴影,而在右边的图中,出现了一个反阴影,因为光源在对象的最高顶点下面。
It is certainly possible to explicitly cull and trim shadow triangles to avoid such artifacts. A simpler method, presented next, is to use the existing GPU pipeline to perform projection with clipping.
显式剔除和修剪阴影三角形以避免这种伪影当然是可能的。接下来介绍的一种更简单的方法是使用现有的GPU管道来执行带有裁剪的投影。