Docsity

Shadowing Techniques - Lecture Slides | CAP 4720, Study notes of Computer Graphics

Partial preview of the text

Download Shadowing Techniques - Lecture Slides | CAP 4720 and more Study notes Computer Graphics in PDF only on Docsity!

10/16/2008 1

Juraj Obert [email protected] http://graphics.cs.ucf.edu/cap4720/fall2008/

Shadowing techniques

 Two general approaches

 Shadow mapping

 Original algorithm  PCF  Variance shadow maps  …

 Stencil shadows

 Stencil pass  Stencil fail (also called John Carmack’s shadows ;)

10/16/2008 3

General idea

light source (^) shadow map

Shadow mapping

 Shadow mapping determines whether a fragment is in shadow by comparing two depth values of the same fragment:

 Depth of the fragment when rendered from a light’s point of view

 Depth of the fragment when rendered from camera’s point of view, but transformed using the light’s view and projection matrices

10/16/2008 7

Outputting depth

// pass these down from vertex shader varying vec3 positionEye; varying vec4 positionClip; void main() { // eye space depth gl_FragColor = vec4(vec3(1.0, 1.0, 1.0) * positionEye.z, 1.0); // clip space depth gl_FragColor = vec4(vec3(1.0, 1.0, 1.0) * positionClip.z / positionClip.w, 1.0); }

 Fragment shader

Depth buffer

10/16/2008 9

Homogenous coordinates in 2D

x

y

w

1

A (x, y, w)

A’(x/w, y/w, 1)

 Transformation of a point in 3D homogenous coordinates is in fact its projection on a plane w units away from origin

 This can be generalized to higher dimensions

Homogenous coordinates in 3D

 By generalizing the concept from previous

slide:

 4D homogenous to non-homogenous

 A (x, y, z, w) -> A’(x/w, y/w, z/w, 1)

 3D non-homogenous to homogenous

 A’(x’, y’, z’) -> A (x, y, z, 1)

10/16/2008 13

Shadow mapping shader code

overview

 Vertex shader

 Besides usual stuff:

 Compute vertex position in light space (projection space of the light, i.e. light’s modelview matrix times render target’s projection matrix)

Shadow mapping shader code

overview

 Fragment shader

 Step 1:

 Convert fragment’s light space position to texture space  Light space has same dimensions as general clip space (<-1, +1> x <-1, +1> x <-1, +1>)  Texture coordinates are in <0.0, 1.0> x <0.0, 1.0>

10/16/2008 15

Shadow mapping shader code

overview

 Fragment shader

 Step 2:

 Sample depthMap at computed coordinates  This will return fragment’s eye space (or clip space) distance as viewed from light’s location

if (sampled > computed) we’re in shadow else we’re not in shadow

Shadow mapping shader code

overview

 Fragment shader

 Step 3:

 Compare computed fragment’s light space z value with sampled z value

10/16/2008 19

Hard edges

Hard shadow edges in shadow mapping

Fixing hard edges

 Sample multiple depths around current pixel and

average them

 texelSizes needs to be passed in as uniform

variables from application and set according to

render target resolution

for (int u = -2; u < 2; u++) for (int v = -2; v < 2; v++) tex2D (baseUV + texelSizes*(u,v))

10/16/2008 21

Images

Softer edges

More images

Render target resolution dependency