Download presentation
Presentation is loading. Please wait.
1
Shadow Mapping RTR Team 2009 0
2
1 Why Shadows?
3
2 RTR Team 2009
4
3 Why Shadows? Shadows...... make a scene look more three-dimensional... tell us about the positions of objects relative to each other... tell us where the light comes from... should really be there RTR Team 2009
5
4 Shadow Determination Several techniques, e.g. Shadow Mapping Shadow Volumes Let’s take a closer look at shadow mapping 2 pass algorithm fast on today’s GPUs relatively easy to implement RTR Team 2009
6
5 Shadow Mapping Overview 1 st pass: we assume the light source has a “view frustum” just like a camera render scene from light source’s position save depth values only we end up with a shadow (depth-) map 2 nd pass: render scene as usual, but transform vertices to light space, too for each fragment, compare rasterized fragment depth to previously stored depth (read it from shadow map) z fragment > z from_shadow_map => fragment lies in shadow both fragments must be in light space!!! RTR Team 2009
7
6 Scene – “Meta” View Eye Light Source RTR Team 2009
8
7 Scene – Light Source View RTR Team 2009
9
8 Scene – Light Source View (Depth Only) RTR Team 2009
10
9 Scene – Eye View RTR Team 2009
11
10 Shadowed Fragment Eye View “Meta“ View RTR Team 2009
12
11 Shadowed Fragment “Meta“ View Fragment Distance Distance from Shadow Map Eye View RTR Team 2009
13
12 Lit Fragment “Meta“ View Eye View RTR Team 2009
14
13 Lit Fragment “Meta“ View Fragment Distance Distance from Shadow Map RTR Team 2009 Eye View
15
14 Coordinate Systems Eye Light World Model RTR Team 2009
16
15 Coordinate Systems Eye Light World Model RTR Team 2009
17
16 Transforming to World Space Eye Light World Model RTR Team 2009
18
17 Transforming to Light Space Eye Light World Model Light (V light ) rendering from the light source‘s point of view RTR Team 2009
19
18 Transforming to Eye Space Eye Light World Model rendering from the eye‘s point of view RTR Team 2009
20
19 1 st pass: Create Shadow Map Create an FBO // create the texture we'll use for the shadowmap glGenTextures(1, &shadow_tex_ID); glBindTexture(GL_TEXTURE_2D, shadow_tex_ID); glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, SM_width, SM_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); glGenFramebuffers(1, &shadow_FBO); glBindFramebuffer(GL_FRAMEBUFFER, shadow_FBO); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadow_tex_ID, 0); glDrawBuffer(GL_NONE); // essential for depth-only FBOs!!! glReadBuffer(GL_NONE); // essential for depth-only FBOs!!! // then, just before rendering glBindFramebuffer(GL_FRAMEBUFFER, shadow_FBO); // create the texture we'll use for the shadowmap glGenTextures(1, &shadow_tex_ID); glBindTexture(GL_TEXTURE_2D, shadow_tex_ID); glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, SM_width, SM_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); glGenFramebuffers(1, &shadow_FBO); glBindFramebuffer(GL_FRAMEBUFFER, shadow_FBO); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadow_tex_ID, 0); glDrawBuffer(GL_NONE); // essential for depth-only FBOs!!! glReadBuffer(GL_NONE); // essential for depth-only FBOs!!! // then, just before rendering glBindFramebuffer(GL_FRAMEBUFFER, shadow_FBO); RTR Team 2009
21
20 1 st pass: Create Shadow Map The “view” matrix must be set to V light Note: No projection matrix used up to now! but light-”camera” involves another projection! Turn off all effects when rendering to the shadow map No textures, lighting, etc. RTR Team 2009
22
21 2 nd pass: Render from Eye’s POV Transform vertices to eye space and project as usual p‘ = P cam * V cam * M * p RTR Team 2009
23
22 2 nd pass: Render from Eye’s POV Also transform vertices to light space and project one possibility: Use V cam -1, the inverse view matrix of the camera p proj_lightspace = (P light * V light * V cam -1 ) * V cam * M * p save as texture coordinates for accessing shadow map Note: the light‘s projection matrix may be different from the eye‘s projection matrix RTR Team 2009
24
23 2 nd pass: Render from Eye’s POV one last issue... p lightspace is in the interval [-1…..+1] shadow map coordinates in range [0….+1] scaling/translation necessary (*0.5, +0.5) SM texcoords = (M translate *M scale *P light *V light *V cam -1 )* V cam * M * p RTR Team 2009
25
Shadow Mapping: Vertex Shader texture_matrix = (M translate *M scale *P light *V light *V cam -1 ) #version 140 uniform mat4 M; // model matrix uniform mat4 V_cam; // view matrix for the camera uniform mat4 P_cam; // projection matrix for the camera uniform mat4 texture_matrix; in vec4 vertex; // from the application out vec4 SM_tex_coord; // pass on to the FS void main(void) { // standard transformation gl_Position = P_cam * V_cam * M * vertex; // shadow texture coords in projected light space SM_tex_coord = texture_matrix * V_cam * M * vertex; } #version 140 uniform mat4 M; // model matrix uniform mat4 V_cam; // view matrix for the camera uniform mat4 P_cam; // projection matrix for the camera uniform mat4 texture_matrix; in vec4 vertex; // from the application out vec4 SM_tex_coord; // pass on to the FS void main(void) { // standard transformation gl_Position = P_cam * V_cam * M * vertex; // shadow texture coords in projected light space SM_tex_coord = texture_matrix * V_cam * M * vertex; } 24 RTR Team 2009
26
Shadow Mapping: Fragment Shader Fragment Shader: #version 140 uniform sampler2D shadow_map; in vec4 SM_tex_coord; // passed on from VS out vec4 fragment_color; // final fragment color destination void main(void) { // note the perspective division! vec3 tex_coords = SM_tex_coord.xyz/SM_tex_coord.w; // read depth value from shadow map float depth = texture(shadow_map, tex_coords.xy).r; float inShadow = (depth < tex_coords.z) ? 1.0 : 0.0; // do something with that value... } #version 140 uniform sampler2D shadow_map; in vec4 SM_tex_coord; // passed on from VS out vec4 fragment_color; // final fragment color destination void main(void) { // note the perspective division! vec3 tex_coords = SM_tex_coord.xyz/SM_tex_coord.w; // read depth value from shadow map float depth = texture(shadow_map, tex_coords.xy).r; float inShadow = (depth < tex_coords.z) ? 1.0 : 0.0; // do something with that value... } 25 RTR Team 2009
27
Add Offset to polygons when rendering shadow map Artifacts glPolygonOffset(1.1, 4.0);// these values work well 26 RTR Team 2009
28
Decrease ambient term Filter shadow map... Artifacts 27 RTR Team 2009
29
GPU can do depth compare in hardware and also PCF! Filter shadow map glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 28 RTR Team 2009
30
Shadow Mapping: Fragment Shader Fragment Shader for Hardware PCF: #version 140 uniform sampler2DShadow shadow_map; in vec4 SM_tex_coord; // passed on from VS out vec4 fragment_color; // final fragment color destination void main(void) { float shadow = textureProj(shadow_map, SM_tex_coord); // do something with that value... } #version 140 uniform sampler2DShadow shadow_map; in vec4 SM_tex_coord; // passed on from VS out vec4 fragment_color; // final fragment color destination void main(void) { float shadow = textureProj(shadow_map, SM_tex_coord); // do something with that value... } 29 RTR Team 2009
31
References www.opengl.org/registry http://www.opengl.org/registry/doc/glspec31undep.20090528.pdf http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.40.07.pdf 30 RTR Team 2009
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.