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、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值