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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧农业是一种结合了现代信息技术,包括物联网、大数据、云计算等,对农业生产过程进行智能化管理和监控的新模式。它通过各种传感器和设备采集农业生产中的关键数据,如大气、土壤和水质参数,以及生物生长状态等,实现远程诊断和精准调控。智慧农业的核心价值在于提高农业生产效率,保障食品安全,实现资源的可持续利用,并为农业产业的转型升级提供支持。 智慧农业的实现依赖于多个子系统,包括但不限于设施蔬菜精细化种植管理系统、农业技术资料库、数据采集系统、防伪防串货系统、食品安全与质量追溯系统、应急追溯系统、灾情疫情防控系统、农业工作管理系统、远程诊断系统、监控中心、环境监测系统、智能环境控制系统等。这些系统共同构成了一个综合的信息管理和服务平台,使得农业生产者能够基于数据做出更加科学的决策。 数据采集是智慧农业的基础。通过手工录入、传感器自动采集、移动端录入、条码/RFID扫描录入、拍照录入以及GPS和遥感技术等多种方式,智慧农业系统能够全面收集农业生产过程中的各种数据。这些数据不仅包括环境参数,还涵盖了生长状态、加工保存、检验检疫等环节,为农业生产提供了全面的数据支持。 智慧农业的应用前景广阔,它不仅能够提升农业生产的管理水平,还能够通过各种应用系统,如库房管理、无公害监控、物资管理、成本控制等,为农业生产者提供全面的服务。此外,智慧农业还能够支持政府监管,通过发病报告、投入品报告、死亡报告等,加强农业产品的安全管理和质量控制。 面对智慧农业的建设和发展,存在一些挑战,如投资成本高、生产过程标准化难度大、数据采集和监测的技术难题等。为了克服这些挑战,需要政府、企业和相关机构的共同努力,通过政策支持、技术创新和教育培训等手段,推动智慧农业的健康发展。智慧农业的建设需要明确建设目的,选择合适的系统模块,并制定合理的设备布署方案,以实现农业生产的智能化、精准化和高效化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值