Shadow Mapping ChunFa Chang National Taiwan Normal University

  • Slides: 22
Download presentation
Shadow Mapping Chun-Fa Chang National Taiwan Normal University

Shadow Mapping Chun-Fa Chang National Taiwan Normal University

Shadow Map o o Using two textures: color and depth Relatively straightforward design using

Shadow Map o o Using two textures: color and depth Relatively straightforward design using pixel (fragment) shaders on GPUs.

Eye’s View Light’s View Depth/Shadow Map Image Source: Cass Everitt et al. , “Hardware

Eye’s View Light’s View Depth/Shadow Map Image Source: Cass Everitt et al. , “Hardware Shadow Mapping” NVIDIA SDK White Paper

Basic Steps of Shadow Maps 1. 2. 3. 4. Render the scene from the

Basic Steps of Shadow Maps 1. 2. 3. 4. Render the scene from the light’s point of view, Use the light’s depth buffer as a texture (shadow map), Projectively texture the shadow map onto the scene, Use “Tex. Gen” or shader Use “texture color” (comparison result) in fragment shading.

What’re in the Example Code? o A C++ class for storing matrix state: class

What’re in the Example Code? o A C++ class for storing matrix state: class Open. GL_Matrix_State { void Save_Matrix_State(); void Restore_Matrix_State(); void Set_Texture_Matrix(); } o A proxy rectangle for debug

(1) Rendering from Light’s View o o o Set the camera to the light

(1) Rendering from Light’s View o o o Set the camera to the light position. Viewport set to the same size as the texture. To avoid the floating point precision problem (casting its own shadow to a surface) , depth must be shifted: n n o gl. Polygon. Offset(. . . , . . . ); gl. Enable(GL_POLYGON_OFFSET_FILL); Shading could be turned off n We only care about the depth!

(2) Creation of Shadow Map (Texture) o o Draw the objects (from light’s view)

(2) Creation of Shadow Map (Texture) o o Draw the objects (from light’s view) To create a depth texture, use: n o gl. Tex. Image 2 D( GL_TEXTURE_2 D, 0, GL_DEPTH_COMPONENT, shadow. Map. Size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); Then use gl. Copy. Tex. Sub. Image 2 D() to copy the frame buffer to the depth texture.

(3) Generation of Texture Coordinates o When we render the scene again from the

(3) Generation of Texture Coordinates o When we render the scene again from the normal camera view: n n n We store the light’s view to the texture matrix. The texture matrix is then passed to the GLSL shaders. gl_Texture. Matrix[0] * vertex gives us the homogeneous coordinates in light space Divide by w to obtain the texture coordinates. Watch out! Must shift from [-1, 1] to [0, 1]

(4) Depth Comparison in Fragment Shader o Compare two depths: n n o o

(4) Depth Comparison in Fragment Shader o Compare two depths: n n o o Depth read from the shadow map Depth by transformation to the light space In the shadow if ____? _(your exercise)____ Set a darker color for shadowed surfaces

More GPU Programming and GPGPU Chun-Fa Chang National Taiwan Normal University

More GPU Programming and GPGPU Chun-Fa Chang National Taiwan Normal University

Calculator vs. Computer o o o What is the difference between a calculator and

Calculator vs. Computer o o o What is the difference between a calculator and a computer? Doesn’t a compute-r just “compute”? The Casio fx 3600 p calculated can be programmed (38 steps allowed).

Turing Machine o o o Can be adapted to simulates the logic of any

Turing Machine o o o Can be adapted to simulates the logic of any computer that could possibly be constructed. von Neumann architecture implements a universal Turing machine. Look them up at Wikipedia!

Simplified View Transform (& Lighting) o Rasterization The Data Flow: 3 D Polygons (+Colors,

Simplified View Transform (& Lighting) o Rasterization The Data Flow: 3 D Polygons (+Colors, Lights, Normals, Texture Coordinates…etc. ) à 2 D Polygons à 2 D Pixels (I. e. , Output Images)

Global Effects shadow multiple reflection translucent surface

Global Effects shadow multiple reflection translucent surface

Local vs. Global

Local vs. Global

How Does GPU Draw This?

How Does GPU Draw This?

Quiz o Q 1: A straightforward GPU pipeline give us local illumination only. Why?

Quiz o Q 1: A straightforward GPU pipeline give us local illumination only. Why? Hint: How is an object drawn? Do they consider the relationship with other objects? o Q 2: What typical effects are missing? Shadow, reflection, and refraction…

o Wait but I’ve seen shadow and reflection in games before… With Shadows Without

o Wait but I’ve seen shadow and reflection in games before… With Shadows Without Shadows

Faked Global Illumination o o Shadow, Reflection, BRDF…etc. In theory, real global illumination is

Faked Global Illumination o o Shadow, Reflection, BRDF…etc. In theory, real global illumination is not possible in current graphics pipeline: n n o Conceptually a loop of individual polygons. No interaction between polygons. Can this be changed by multi-pass rendering?

Case Study: Shadow Map o o Using two textures: color and depth Relatively straightforward

Case Study: Shadow Map o o Using two textures: color and depth Relatively straightforward design using pixel (fragment) shaders on GPUs.

Adding “Memory” to the GPU Computation o Modern GPUs allow: n n The usage

Adding “Memory” to the GPU Computation o Modern GPUs allow: n n The usage of multiple textures. Rendering algorithms that use multiple passes. Transform (& Lighting) Rasterization Textures