Simple Shadow Mapping Tutorial with OpenGL

Simple Shadow Mapping Tutorial with OpenGL

Jie Tan

DALAB

Introduction

Shadow mapping is a classical but widely-used technique to render shadows. It is introduced by Lance Williams in 1978 in “Casting curved shadows on curved surfaces”. Nowadays, this shadowing method is integrated in a myriad of offline renderers such as Pixar’s Renderman and real-time game engines.

 

What is Shadow Mapping and how it works?

There is one simple way to determine whether something/someplace is in shadow: If a point cannot be illuminated from the light source but can be seen from the camera, it is in shadow. Consider a simple scene lit by only one spot light, see the figure above. The blue occluder cast a shadow on the ground. While the point A can be seen from the camera’s position directly, it cannot be illuminated from the light’s position because the blue occluder blocks the ray at point B. Thus point A is in shadow. It is a perfect analogy between “camera-see” and “light-illuminate”. Put in another word, point A is not illuminated by the light source is analogue to point A cannot be seen (is not visible) by the camera placed in the same position.

 

We have already known how the visibility is determined in the pipeline: Z-buffering. From the analysis above, it is straightforward to come into a solution with current graphics hardware. The solution of the shadowing problem can be decomposed into several steps:

1)      Determine the visibility of the whole scene from the light source (Place the camera at light’s position, draw the whole scene and copy the Z-buffer into a texture named shadow map).

2)      From every point that is visible to the camera, transform that point from camera coordinate to the texture coordinate of shadow map and then compare its depth with the shadow map.

3)      If the depth of transformed point is smaller than the value in shadow map, render it with normal light settings (NOT in shadow). Otherwise, render that point without any lights (in shadow).

 

Transformations from camera coordinates to texture coordinates of the shadow map

Before comparing the depth values between camera space and that stored in the shadow map, a transformation is necessary to convert them into the same coordinate system. Let’s have a close look at the associated coordinate systems. Refer to the figure below:

The shadow map is a snapshot of the light’s viewport, which store the Z-Buffer when drawing the scene with camera placed at light’s position. It means that the depth values are in light’s clip space (after the model-view transformation and projection transformation). On the other hand, the depth of the points visible from the camera is in camera’s eye space (after model-view transformation only and before the projection). The transformation from camera coordinate system to texture coordinate of the shadow map consists of several matrices:

where

T is the transformation matrix, B is the bias matrix, Pl is the light’s projection matrix and Vl, Vc are the light’s and camera’s view matrices respectively.

Note: Since the value stored in a texture (shadow map) ranges from 0 to 1 and the coordinates in canonical coordinate system (multiplied by ) ranges from -1 to 1, a bias matrix B performs scaling and translation to normalized the transformed coordinates within range [0, 1]. It is in the form:

Instead of using texture matrix to perform the transformation T, we can adopt simpler techniques: automatic texture coordinate generation by calling glTexGen* and glTexGen*v. (Refer to the Chapter 9 in “OpenGL Programming Guide”)

For example,

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

glTexGenfv(GL_S, GL_EYE_PLANE, p);

will generate texture coordinate s by following equations:

where pi is the four elements of array p, and (xe, ye, ze, we) is the point location in camera coordinate system. With some matrix manipulations, it is easy to verify that the following codes are equivalent to applying the matrix T to the camera coordinate. And it finishes the transformation from camera coordinates to texture coordinates of the shadow map.

 

     static matrix44 biasMatrix(vector4(0.5f, 0.0f, 0.0f, 0.0f),

                                vector4(0.0f, 0.5f, 0.0f, 0.0f),

                                 vector4(0.0f, 0.0f, 0.5f, 0.0f),

                                 vector4(0.5f, 0.5f, 0.5f, 1.0f));    //bias from [-1, 1] to [0, 1]

matrix44 textureMatrix = biasMatrix * shadowLightProjectionMatrix * shadowLightViewMatrix;

textureMatrix.transpose();

     //Set up texture coordinate generation.

     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

     glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix[0]);

     glEnable(GL_TEXTURE_GEN_S);

 

     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

     glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix[1]);

     glEnable(GL_TEXTURE_GEN_T);

 

     glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

     glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix[2]);

     glEnable(GL_TEXTURE_GEN_R);

 

     glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

     glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix[3]);

     glEnable(GL_TEXTURE_GEN_Q);

 

The generated coordinate <s, t, r, q> is the transformed coordinate in texture coordinate system of the shadow map. <s, t> is used to locate the texel in the shadow map and r is the transformed depth value to be compared with the shadow map.

 

Render Pass and Pseudo-code

To make each render pass simple enough to understand and implement, we will use three passes to finish the whole rendering process:

1)      Generate the shadow map: place the camera with the same position and orientation as the light source, render the whole scene, and copy the depth buffer to a texture.

2)      Render the shadow: Adjust the light intensity to be dark and render the whole scene. In case that the whole shadow region is of the same flat black/gray color, we use the dim diffuse light source to mimic the shadow effect.

3)      Render the illuminated part of the scene: Adjust the light intensity to normal level, bind the shadow map texture, transform the camera coordinates to the texture coordinates and compare texture coordinate r with the values d stored in shadow map. If r <= d, draw the pixel. Otherwise, leave the pixel unchanged (implemented with alpha test).

 

Pseudo-code in Pass 1:

         PushState(ViewPort);

         PushState(ViewMatrix);

         PushState(ProjectionMatrix);

         SetViewport(0, 0, shadowMapWidth, shadowMapHeight);

         SetTransformMatrix(VIEW_MATRIX, shadowLightViewMatrix);

         SetTransformMatrix(PROJECTION_MATRIX, shadowLightProjectionMatrix);

         glShadeModel(GL_FLAT);

         glColorMask(0, 0, 0, 0);

         DrawScene();

         PopState(RS_ProjectionMatrix);

         PopState(RS_ViewMatrix);

         PopState(RS_ViewPort);

 

SetViewport() is to setup the size of the shadow map. Although openGL 2.0 does not restrict the texture size to be 2n anymore, to be conservative, set the size of shadow map to be 2n is still the best choice. SetTransformMatrix() is to set the position and direction of the light source. For simplicity, we assume that the light source is always directional light or spot light. The view matrix of light can be set by calling glLookAt() and the projection matrix can be set by calling glOrtho() or glPerspective() for directional light and spot light respectively. Setting the shading model to flat and disable frame buffer write is to speed up this pass since only depth buffer makes sense to the following passes.

 

Pseudo-code in Pass 2:

         glBindTexture(GL_TEXTURE_2D, shadowMapTexture);

         glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONET, 0, 0, shadowMapWidth, shadowMapHeight, 0);

         glShadeModel(GL_SMOOTH);

         glColorMask(1, 1, 1, 1);

         Clear(DEPTH_BUFFER);

         setupDimLight();

         DrawScene();

 

 

Pseudo-code in Pass 3:

matrix44 textureMatrix = biasMatrix * shadowLightProjectionMatrix * shadowLightViewMatrix;

         textureMatrix.transpose();

              //Set up texture coordinate generation.

         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

         glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix[0]);

         glEnable(GL_TEXTURE_GEN_S);

        

         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

         glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix[1]);

         glEnable(GL_TEXTURE_GEN_T);

    

         glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

         glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix[2]);

         glEnable(GL_TEXTURE_GEN_R);

 

         glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

         glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix[3]);

         glEnable(GL_TEXTURE_GEN_Q);

 

         glEnable(GL_TEXTURE_2D);

         Bind & enable shadow map texture

         glBindTexture(GL_TEXTURE_2D, shadowMapTexture);

         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);

         //Shadow comparison should be true (ie not in shadow) if r<=texture

         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);

         //Shadow comparison should generate an INTENSITY result

         glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);

         //Set alpha test to discard false comparisons

         glAlphaFunc(GL_GEQUAL, 0.99f);

         glEnable(GL_ALPHA_TEST);

         setupNormalLight();

         DrawScene();

         glDisable(GL_TEXTURE_2D);

         glDisable(GL_TEXTURE_GEN_S);

         glDisable(GL_TEXTURE_GEN_T);

         glDisable(GL_TEXTURE_GEN_R);

         glDisable(GL_TEXTURE_GEN_Q);

         glDisable(GL_ALPHA_TEST);

 

We use two extensions of openGL implementation in pass three in order to compare the depth value with the shadow map. ARB_depth_texture provides a new texture format to hold the depth buffer. By using DEPTH_COMPONENT as both the format and internal format of a texture, we get a single-channel texture of the same precision as the depth buffer. As with RGB textures, we can use glCopyTex[Sub]Image2D to copy data from the frame buffer into the texture, this information will be copied from the depth buffer rather than the color buffer. ARB_shadow provides the automatic shadow comparison mentioned above. Rather than write possibly large amounts of code to create and initialize function pointers for the extensions, we will use the extension loading library “Glee”, by Ben Woodhouse. It can be downloaded from http://elf-stone.com/glee.php. You can check whether your graphics card support such extensions by checking whether both GLEE_ARB_depth_texture and GLEE_ARB_shadow are true.

 

Sample Results

 

 

Shortcomings of Shadow Mapping

Shadow mapping has several notorious shortcomings. First of all, classical shadow mapping cannot generate hard shadows. Even worse, the edge of the shadow is aliased, especially when the shadow map’s resolution is not high enough. Various research papers have presented a variety of solutions. For example, “Percentage Closer Filtering” (or PCF) is able to produce slightly soft shadow edges. “Parallel-Split Shadow Maps” is capable of alleviate the aliasing problem.

The second problem of shadow mapping is self-shadowing. Remember that we need to compare the depth value at every point visible to the camera with the texels stored on the shadow map? Because of the precision of float-point depth value, the comparison cannot always get the right answer if two values are close enough. This will cause the nasty self-shadowing effect. Adding a small epsilon to the bias matrix can partially solve the problem, but it will bring another artifact that the shadow creeps out from under the occluder. So determining an appropriate bias value requires careful tuning. I introduce interested readers to refer to the “Summed-Area Variance Shadow Maps” which can solve the problems of classical shadow mapping in an elegant way.

 

References

[1]Paul's Projects - Shadow Mapping Tutorial, http://www.paulsprojects.net/tutorials/smt/smt.html

[2]Real-time Rendering 2nd ed., Tomas Akennine-Moller and Eric Haines, AK PETER, LTD.

[3]Summed-Area Variance Shadow Maps, Andrew Lauritzen, GPU Gems 3, Addison Wesley

[4]Parallel-Split Shadow maps on Programmable GPUs, Fan Zhang, Hanqiu Sun, Oskari Nyman, GPU Gems 3, Addison Wesley

[5]OpenGL Programming Guide, 5th ed. Dave Shreiner, Mason Woo, Jackie Neider and Tom Davis

 

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值