GAM532 DPS932 – Week 8 Texture Shadow Implementation.

16
GAM532 DPS932 – Week 8 Texture Shadow Implementation

Transcript of GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Page 1: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

GAM532DPS932 – Week 8Texture Shadow Implementation

Page 2: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Depth Texture Shadows Basics

Render scene depth from each light

Render Scene from main camera for

each light

Compare depth from light’s depth target to each

fragment

Page 3: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Binding Light Camera Values

Render scene depth from each light

Bind each shadow camera to a depth target (no color

needed)

struct Light { float4 diffuse; float4 specular; float3 attenuation; float3 spot;};

Texture2D shadowTex : register(t0);SamplerState shadowSamp : register(s0);

Bind each depth target to a slot in the

shader

Page 4: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Bridging the Gap

Rendering scene from the main

camera

Read from texture bound to shadow camera

Must find a way to transform from one space to the next

Main Camera’s View Space

Shadow Camera’s Clip-To-Texture

Space

Page 5: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

What We Need To Find

Main Camera’s View Space Fragment Position

Shadow Camera’s View Space Fragment Position

Main Camera’s View Space Fragment Position

Main Camera’s View Space

Light Transformation

Inverse

FragPos * InvLightMat

Page 6: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Inversion of Orthographic Matrices

Origin Orthographic

Matrix

| 0 1 0 0 || 0 0 1 0 || -1 0 0 0 || -8 9 3 1 |

| 1 0 0 0 || 0 1 0 0 || 0 0 1 0 || 8 -9 -3 1 |

Inverse Translation

Matrix

Inverse Translation +

Identity

*| 0 0 -1 0 || 1 0 0 0 || 0 1 0 0 || -9 -3 -8 1 |

Inverse Orthographic

Matrix

=| 0 0 -1 0 || 1 0 0 0 || 0 1 0 0 || 0 0 0 1 |

Inverse Rotational Matrix

Transpose Rotation - Translation

Page 7: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

From View Space to Clip Space

struct Light { float4x4 shadowProjection; float4 diffuse; float4 specular; float3 attenuation; float3 spot;};

Texture2D shadowTex : register(t0);SamplerState shadowSamp : register(s0);

Shadow Camera’s View Space Fragment Position

Shadow Camera Projection

TransformationFragPos * shadowProjection

Page 8: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Clip Space To Texture Coordinate

Shadow Camera’s Clip Space Fragment Position

[ 23 12 29 31 ]

[ x/w y/w z/w w/w]

[ 0.74 -0.39 0.94 1 ]

-1

-1

1

1

Screen Coordinates

0

0 1

1

Texture Coordinates

[ 0.74 -0.39 0.94 ] X Y Depth

U V Depth

Screen Coordinates of Fragment with

Depth

[ 0.74 -0.39 0.94 ]

[ x/2+0.5 y/2+0.5 z ]

[ 0.87 0.31 0.94 ]

Texture Coordinates of Fragment with

Depth

Page 9: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Testing Depth

[ 0.87 0.31 0.94 ]

U V Depth

Texture Coordinates of Fragment with

Depth

0.87

0.31

Sample Depth from Shadow Texture

Is sampled depth lower than the calculated depth of the fragment?

Yes

No

The light hit a different object

first, shadow casted on fragment, fragment

is not lit

The light hit the surface of the

object, no shadow casted on fragment, fragment is lit

Page 10: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Adding All Lights Together

+ + =

Each light contributes to an object

Shadows only block one light

Single object can project many shadows

Page 11: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Final Result

Translucency effects easily added

Expensive (re-render scene for each light)

Self Shadowing

Dynamically changing Shadow

Detail limited by texture size

High Detail (texture limited)

Point lights are very expensive to cast shadows from (6 renders per light)

Page 12: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Shadow Texture Notes

Point lights require up to 6

shadow textures to map out (don’t do

it)

Point and spot lights radiate light in many directions

Directional lights work in one direct and require an

orthographic camera

Page 13: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Depth Fighting Shadow Acne

Shadow Acne No Acne

Floating point precision issue

0.94534 > 0.94522

0.945341458 > 0.945341456

0.945341458458??? > 0.945341458458???

0.945341458458??? + 0.0000001 >

0.945341458458???

Page 14: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Alias Reduction

Texture resolution can cause aliasing

Sampling adjacent texels and blending can soften shadows

float2 poissonDisk[4] = { float2(-0.94201624, -0.39906216), float2(0.94558609, -0.76890725), float2(-0.094184101, -0.92938870), float2(0.34495938, 0.29387760));

for (int i=0; i<4; i++) { if(texture2D(shadowTex, shadow.xy + poissonDisk[i]/700.0).z<ShadowCoord.z-bias ){ visibility-=0.2; }}

Page 15: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

Graphics Section Complete

Audio Next…

Page 16: GAM532 DPS932 – Week 8 Texture Shadow Implementation.

To Do• Begin work on your enhancement

• Begin work on OpenGL labs

• Prepare for Mid-term (for real this time, I’m not pushing back again)