1 10/24/2015 13:01 uml graphics ii 91.547 shadows session 4

29
1 05/15/22 10:28 UML Graphics II 91.547 Shadows Session 4

Upload: chloe-harvey

Post on 20-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

104/21/23 16:48

UML

Graphics II 91.547

Shadows

Session 4

Page 2: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

204/21/23 16:48

UML

The General Lighting Situation

Proj. Plane

COP

Light Source

Objects

Page 3: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

304/21/23 16:48

UML

The Simplified OpenGL Model

Proj. Plane

COP

Light Source

Objects

For each object (or primitive) For each light source enabled Compute color at pixel based on:

Source properties Angle, distance to source

Angle to observer (COP) Material properties

Page 4: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

404/21/23 16:48

UML

Vectors Used byPhong Lighting Model

n

l r

v

p is an arbitrary point on a surface.n is the unit normal to the surface at p.l is a unit vector in the direction of the light source.r is a unit vector in the direction of a perfectly (specular) reflectionv is a unit vector in the direction of the viewer.

p

COP

Page 5: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

504/21/23 16:48

UML

The Complete Phong Model

I 1

a bd cd 2(kdLd (l n) ksLs (r v) ) kaLa

Distance term Ambient term

Diffuse termSpecular term

Page 6: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

604/21/23 16:48

UML

Rendered with Phong Model

Page 7: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

704/21/23 16:48

UML

Rendered with Ray Tracing

Page 8: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

804/21/23 16:48

UML

Information provided by shadows:Where are the balls?

Page 9: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

904/21/23 16:48

UML

Where are the balls?Shadows show relative position.

Page 10: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1004/21/23 16:48

UML

Shadows: How do we get them?*

1. Integration of shadow calculation with scan-line hiddensurface removal

2. Projection and clipping

3. Shadow volumes

4. Shadow z-buffer

5. Recursive ray tracing

6. Radiosity

*Classification due to Bergeron, 1986, quoted in Watt & Watt

Page 11: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1104/21/23 16:48

UML

Projecting shadows

Light Source

ShadowingObject (polygon)

Plane of projection

Page 12: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1204/21/23 16:48

UML

Why is this model a simplification?

Finite sizelight source

Shadowingobject

Umbra

Penumbra

Page 13: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1304/21/23 16:48

UML

Projecting shadows

Light Source

ShadowingObject (polygon)

Plane of projection:

x

y

z

L

L

L

1

x

y

z

S

S

S

1

ax by cz d 0

Projected point.

Page 14: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1404/21/23 16:48

UML

Calculating the projection matrix

a x x b y y c z z dL L L( ) ( ) ( ) 0

x

y

z

L

L

L

1

0

0

0

1

Light source moved to origin.

p p S

pS

Page 15: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1504/21/23 16:48

UML

Calculating the projection matrix

a x x b y y c z z d

ax by cz ax by cz d

ax by cz dax by cz

dotax by cz

S L S L S L

S S S L L L

L L L

S S S S S S

( ) ( ) ( )

( ) ( )

0

0

Substituting parametric expression for projection line into plane equation:

dot

dot

dot

a b c

0 0 0

0 0 0

0 0 0

0

Substituting back into line equation gives the following transformation:

Page 16: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1604/21/23 16:48

UML

Calculating the projection matrix:

1 0 0

0 1 0

0 0 1

0 0 0 1

0 0 0

0 0 0

0 0 0

0

1 0 0

0 1 0

0 0 1

0 0 0 1

x

y

z

dot

dot

dot

a b c

x

y

z

L

L

L

L

L

L

dot ax bx cx dx

ay dot by cy dy

az bz dot cz dz

a b c dot d

L L L L

L L L L

L L L L

Performing the matrix multiplication gives:

Transform to frame where light is at origin, then transform back:

Page 17: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1704/21/23 16:48

UML

Projection shadow algorithm

1. Render all objects

2. For each light sourceFor each shadowing object

For each shadowed planeProject shadowing object onto planeClip to dimension of shadowed polygonRender the shadow

Page 18: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1804/21/23 16:48

UML

The Clipping Issue

Light

Shadow clipped at this boundary

Plane 1

Plane 2

Page 19: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

1904/21/23 16:48

UML

Using the stencil buffer

Color Planes Z-bufferPlanes

StencilPlanes

glEnable(GL_STENCIL_TEST);

Fragments will be drawn only if the stencil test passes.

Page 20: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2004/21/23 16:48

UML

Using the stencil buffer, contd.

void glStencilFunct(Glenum func, Glint ref, Gluint mask);

func is one of:

GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL

compares the stencil buffer value with the reference value.

Void glStencilOp(Glenum fail, Glenum zfail,Glenum zpass);

specifies the action to take when a fragment passes or fails the stencil test. Arguments are:

GL_KEEP, G_ZERO, GL_REPLACE,GL_INCR, GL_DECR, GL_INVERT

Page 21: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2104/21/23 16:48

UML

Projection ShadowsCode example.

120 goto 240

Page 22: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2204/21/23 16:48

UML

Issues with projection shadows

0 N x N problem0 Difficult to shadow onto anything other than flat surfaces

- Polygonal objects ok, but have to project onto the plane of each polygon and clip to its edges.

0 Difficult to show shadow’s color- Shadow is a “squashed” version of object doing the

shadowing, not the object being shadowed.

Page 23: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2304/21/23 16:48

UML

Shadow Volumes

Light Source

Shadow volume

Page 24: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2404/21/23 16:48

UML

Determining whether a fragment is from an object in a shadow volume.

COP

ShadowVolume

ShadowVolume

Page 25: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2504/21/23 16:48

UML

Determining whether a fragment is from an object in a shadow volume.

COP

ShadowVolume

ShadowVolume

For each fragment: Number of front facing shadow volume polygons minusnumber of back facing shadow volume polygons (that pass depth test)= zero if object is not in shadow.

Page 26: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2604/21/23 16:48

UML

Shadow Volumes:The algorithm (for a single light source)

1. Enable color buffer and depth buffer for writing. Enabledepth testing

2. Set attributes for drawing in shadow. Turn off light source.

3. Render the entire scene.

4. Compute the polygons enclosing the shadow volume.

5. Disable color and depth buffer for writing. Leave depth test.

6. Clear the stencil buffer to zero.

7. Set the stencil function to always pass

Page 27: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2704/21/23 16:48

UML

Shadow Volumes:The algorithm (for a single light source)

8. Set the stencil operation to increment if depth passes

9. Turn on back face culling

10. Render the shadow volumes

11. Set the stencil operation to decrement if depth passes

12. Turn on front face culling

13. Render the shadow volumes

14. Set the stencil function to test for zero and the stencil operation to do nothing.

15. Enable the color and depth buffer for writing.

16. Turn on the light and render the entire scene.

Page 28: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2804/21/23 16:48

UML

Shadow volume issues:

0 Difficult if the shadowing object is a complex shape. - Can project a separate shadow volume from each polygon

but this is very inefficient- Ideally, shadow volume should be generated from

vertices forming silhouette of the object. This is a non-trivial computation (see Watt & Watt for one algorithm).

0 If polygon renderer does not handle adjacent polygons correctly, the stencil count can get mixed up resulting in grossly incorrect shadows.

Page 29: 1 10/24/2015 13:01 UML Graphics II 91.547 Shadows Session 4

2904/21/23 16:48

UML

Shadow Volume code example.

120 goto 240240 goto 120

Let’s see ifthey are

still awake