Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

13
Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20

Transcript of Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Page 1: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Advanced Computer GraphicsShadow Techniques

CO2409 Computer Graphics

Week 20

Page 2: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Lecture ContentsLecture Contents

1. Basic Shadows

2. Pre-calculated Shadows

3. Shadow Mapping

Page 3: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Basic ShadowsBasic Shadows

• Shadows in a scene to help resolve the relative positions of models– Naive mistake to think that shadows

happen automatically with lighting

• A couple of basic techniques have long been used:– Draw a “blob” under a model– Draw the model flattened (scaled to 0 in

Y) on the floor• Can project straight down or away from

the light

• Both methods require a flat floor to work correctly

Positions Ambiguous?

Shadows Resolve

Page 4: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Basic ShadowsBasic Shadows

• These methods are still frequently used, why?

• Basic methods are enough to resolve model positions• Very cheap techniques – good when many models

– More advanced techniques are complex and slow

• Advanced techniques often draw attention to themselves:– Sharp edges / too stark, problems in

complex cases

• In wide-open areas, shadows will tend to blur towards blobs in any case– Viewer may not notice / appreciate better

accuracy

Basic Shadows are still widely used

Page 5: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Pre-calculated ShadowsPre-calculated Shadows

• However, improved shadows can give a better sense of space and/or atmosphere

• Can pre-calculate light / shadows for the static models and lights in the scene

• Commonly used are static shadow maps / lightmaps– Light / shadow textures applied over

the main model textures

• The maps are pre-calculated– Using high-quality techniques

• Sometimes called baking the shadows / lighting Lightmass from Unreal 3

Tool to bake shadow / lightmaps

Page 6: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Static Shadows / Dynamic ModelsStatic Shadows / Dynamic Models

• Baked shadows work well for static environments– High quality, generated offline

• Dynamic models also need to be affected by these shadows

• So store samples of the static lighting at points where dynamic models will be– Both on the floor and in the air

• Model uses local static lighting samples to affect how it is lit

Page 7: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Dynamic Shadow MappingDynamic Shadow Mapping

• We also need to cast shadows from dynamic models

• Dynamic Shadow Mapping is an extension of render-to texture techniques used for shadows– Also called perspective shadow mapping (PSM)– Or more commonly, just shadow mapping

• The scene is rendered to a texture (a shadow map), from

the point of view of the light

• Then the scene is rendered normally, but each pixel first tested against shadow map– The pixel is not lit if it is in shadow

from the light

Page 8: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Shadow Mapping MethodShadow Mapping Method

• Create a render target texture for each light:– Each “pixel” in the texture is a single floating-point value

• Instead of four R,G,B & A values

– This is a floating-point texture

• Render the scene from the light’s point of view– Treat the light like a camera

• For each pixel, render the distance at that pixel only– Result is a like a depth buffer for the

scene from the light’s point of view– Sometimes called a light map or

(confusingly) a shadow map

Page 9: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Shadow Mapping MethodShadow Mapping Method

• After creating a textures for each light’s point of view, we next render the scene normally – Using an extra step to find shadowed areas

• In this main render step, we check the pixel’s visibility from each light before applying lighting to it:– Find the distance from the pixel we’re rendering to the light– Also find where this pixel would appear in the light’s depth map– Find the distance stored in the light’s depth map at that point– If the distance stored in the map is less than the actual distance

from pixel to light, then something is obscuring the pixel• The pixel in shadow from this light

– This condition determines whether the pixel gets a diffuse / specular contribution from this light or not

– Repeat for all lights on this pixel

Page 10: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Shadow Mapping DiagramShadow Mapping Diagram

Determining if the red pixel is in shadow from

the white light

It is – the troll’s head is in the way. The red pixel is hidden in the light map

Page 11: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Shadow Mapping Detail 1Shadow Mapping Detail 1

• To treat a light as a camera, we need a view & projection matrix for it– So we need: position, orientation, FOV and near / far clip planes

• A spotlight is the simplest case:– Already has a position and facing direction – can make view matrix– Has a field of view (FOV) for projection matrix

• A point light shines in all directions– We make six cameras pointing in each

of the world axis directions– A FOV of 90° for each one– This is an example of a cube map

Point Light = 6 Cameras

Page 12: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Shadow Mapping Detail 2Shadow Mapping Detail 2

• A directional light poses two problems:– No position as a camera. No FOV to use

• Solve first problem by “positioning” the directional light very far away – outside the scene

• Solve the second by using an orthogonal projection matrix

• Project vertices from 3D to 2D along parallel lines– Not towards camera point like

standard perspective projection– Don’t need FOV

Page 13: Advanced Computer Graphics Shadow Techniques CO2409 Computer Graphics Week 20.

Shadow Mapping IssuesShadow Mapping Issues

• Shadow mapping has two key problems:– Texels of shadow map may be visible, affects shadow quality – Polygons sometimes self-shadow

• Increase shadow map resolution for better quality– But lower performance, increased memory– Better to adapt resolution based on distance from viewer

• Good idea to blur or soften the shadows / map– Many methods here, e.g. Variance Shadow Mapping

• Self-shadowing resolved by tweaking calculated and compared depth– Other solutions illustrated in lab