Image Synthesis

49
computer graphics & visualization Image Synthesis Shadows / Transparency

description

Image Synthesis. Shadows / Transparency. Inter-Object realism. Covers different kinds of interactions between objects Increasing realism in the scene Relationships between objects easier to understand Shadows, Reflections, Transparency. Shadow techniques . - PowerPoint PPT Presentation

Transcript of Image Synthesis

Page 1: Image Synthesis

computer graphics & visualization

Image Synthesis

Shadows / Transparency

Page 2: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Inter-Object realism• Covers different kinds of interactions between

objects– Increasing realism in the scene– Relationships between objects easier to understand

• Shadows, Reflections, Transparency

Page 3: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow techniques In illuminated scenes objects cast shadows on other

objects– Important visual cue for object positioning– Scene without shadows doesn’t match reality

Page 4: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow techniques Classification of shadow techniques

– How to account for interaction between light, shadow casters and shadowed objects

Projective Shadows Shadow Volumes Shadow Maps

Page 5: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Projective shadows Projective shadows

– Shadows are projected copies of the shadowing objects onto shadowed surface

– Good for complex objects casting shadows onto simple surfaces

Objects Objects & projected objects Shadows

Page 6: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Projective shadows Projection of shadow casters onto planes

– Shadow projection is a projective mapping of the object onto the shadowed plane

• Directional or positional lights can be handled– Shadow projection transform S is added to the end of the modelview

matrix• S: render the scene from the light source position with the shadowed plane

as the image plane

Ax+By+Cz+D=0

L+t(P-L)=(x,y,z)T

P

L

Page 7: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Projective shadowsMulti-pass algorithm

– Draw the surface to be shadowed– Render the object twice

• Render the object with original matrix, colors and textures as specified for that object

• Disable texturing and lighting• Render the object as its shadow with appended matrix S and the color of the

shadow

Page 8: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Projective shadowsProblems of projective shadows

– Projection must be clipped to shadowed surface– Solution: clip the projected polygon to the surface using the

stencil buffer

Draw polygon setting the

stencil buffer

Draw shadows where stencil is

setUse stencil as

mask

Page 9: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Projective shadowsProblems of projective shadows

– Shadow color modulates receivers color• Receiver should not just become darker

– Solution:• Render unlit surface again where stencil is set due to

shadow projection – Shadow shows color of textured surface

Page 10: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Projective shadows• Problems of projective shadows

– Z-Fighting: Projected object has „same“ depth values as shadowed plane

z-fighting correct

Page 11: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Projective shadows• Problems of projective shadows

– In complex scenes multiple shadow projections are needed

• #Matrices depends on #ShadowedSurfaces and #Lights• Objects have to be rendered multiple times

– #PotentiallyShadowedSurfaces

– Need to find surfaces that are in shadow– Only works well on planar surfaces– For complex objects clipping has to be performed

against every face

Page 12: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Projective shadowsProjective shadows in complex scenes

Shadow clipped to multiple surfaces

Culling of non-shadowed surfaces

Modulate surface with shadow color

Page 13: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow volumesShadow volumes

– Relies on a geometric representation of the spatial region that is shadowed by an object

– Shadow polyhedra is generated in advance• Project shilouette edges of the object onto shadowed

surface and connect points• Finding shilouettes not a simple task

– Shadow algorithm now involves the stencil test and the depth test

Page 14: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow volumes• When is a point in shadow?• Shadow volume algorithm [Crow 1977]

– Intersect ray from eye with shadow volume– Start with 0, invert at every intersection point– If 0, point not in shadow

– If eye in shadow, start with 1

Page 15: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow volumes• Shadow volume geometry

Without shadows Shadow volume Final scene

Page 16: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow volumes• Algorithm

– Render the unlit scene and clear the stencil buffer– Disable depth write and color write but leave depth test

enabled– Render front-facing polygons of the shadow volume and

increment the stencil where they pass the depth test– Render back-facing polygons of the shadow volume and

decrement the stencil where they pass the depth test

• Result– Stencil value classifies fragments as shadowed or unshadowed

• Shadowed pixels end up with non-zero stencil

Page 17: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Unshadowedobject

+ ---+ +

Shadow Volume Count = +1+1+1-1-1-1 = 0Illuminated (Behind Shadow Volumes)

Page 18: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Shadowedobject

+ -+ +

Shadow Volume Count = +1+1+1-1 = 2

Shadowed (inside Shadow Volumes)

Page 19: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Shadowedobject

Shadow Volume Count = 0

Illuminated (before Shadow Volumes)

Page 20: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

zero

zero

+1+1+2

+2+3

Near clipplane

Far clipplane

Missed shadow volume intersection due to near clip plane clipping; leads to mistaken count

Problems

Page 21: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow volumes ZFail• Algorithm

– Render the unlit scene and clear the stencil buffer– Disable depth write and color write but leave depth test

enabled– Render back-facing polygons of the shadow volume and

increment the stencil where they fail the depth test– Render front-facing polygons of the shadow volume and

decrement the stencil where they fail the depth test

• Result– Stencil value classifies fragments as shadowed or unshadowed

• Shadowed pixels end up with non-zero stencil

Page 22: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow volumesOpenGL implementation (Zfail)glDisable(GL_LIGHTING);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LESS);glDepthMask(0);glColorMask(0,0,0,0);glStencilMask(255);glEnable(GL_CULL_FACES);

glCullFace(GL_FRONT);glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);for(i=0;i<numPolysInScene; i++) renderShadowVolumeForPolygon(i);

glCullFaces(GL_BACK);glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);for(i=0;i<numPolysInScene; i++) renderShadowVolumeForPolygon(i);

Page 23: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow volumesProblems of shadow volumes

– Huge number of shadow geometries have to be generated, stored and rendered

• Shadow volumes are in general too costly to be applied for complex scene

• Volumes have to be re-generated whenever the light source moves relatively to object

• Using simplified geometries can help

Page 24: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Hardware shadow volumes: Zfail• Render scene to initialize depth buffer

– Depth values indicate the closest visible fragments• Use a stencil enter/leave counting approach

– Draw shadow volume twice using face culling• 1st pass: render back faces and increment when depth test fails• 2nd pass: render front faces and decrement when depth test

fails– Don’t update depth or color

• Pixel’s stencil is non-zero if pixel in shadow, and zero if illuminated

• Problem now is far clipping plane

Page 25: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Hardware shadow volumes: Zfail• Conclusion: shadow volume approach requires

clipping against near or far plane to be avoided• Avoiding near plane clipping

– Not really possible– Objects can always be behind you

• Avoiding far plane clipping– Perspective make it possible to render at infinity

Page 26: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Hardware shadow volumes: Zfail• Recall: glFrustum projection matrix

0100

200

020

002

NearFarNearFar

NearFarNearFar

BottomTopBottomTop

BottomTopNear

LeftRightLeftRight

LeftRightNear

P

Page 27: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Hardware shadow volumes: ZfailProjection matrix with infinite Far

01002100

020

002

lim

NearBottomTopBottomTop

BottomTopNear

LeftRightLeftRight

LeftRightNear

Far infPP

Page 28: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Hardware shadow volumes: ZfailWhat happens to vertex at infinity?

– Most distant vertex is (0,0,-D,0) where D>0• OpenGL is looking along negative z

– Transform (0,0,-D,0) to window space

– Vertex is still in valid depth range; it is not clipped

15.05.05.05.0

DD

wzzc

cw

Page 29: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Hardware shadow volumesShadow volume must be closedThree sets of polygons close the shadow volume

– Possible silhouette edges extruded to infinity away from the light position L

– All of the occluder’s back-facing (w.r.t. the light) triangles projected away from the light to infinity

– All of the occluder’s front-facing (w.r.t. the light) triangles are “slightly” projected away from the light

Use homogeneous vector differences to project vertex V to infinity along the light source direction

– V-L represent direction heading away from the light– Has w coordinate equal to zero

Page 30: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Hardware shadow volumes• Example

Page 31: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Hardware shadow volumesAlgorithm using programmable vertex shader

– Specify for each edge a shadow edge by duplicating edge vertices

• Create so-called zero-area quads

– Test for silhouette edge, front/back facing triangles– Extrude respective vertices to infinity in the vertex shader

Page 32: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Hardware shadow volumesAlgorithm using DirectX10Graphics

– Includes a geometry shader• Sits behind the vertex shader• Provides functionality to amplify/deamplify primitives

– For shadow volumes: duplicate silhouette edges and extrude to infinity

Page 33: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow mapsShadow maps – a two-pass approach

– Render the scene from the light source position using a virtual image plane

– Grab the depth buffer and store it as a shadow map– Before rendering a fragment from the camera

• Project it towards the light source onto the shadow map • Discard fragment if distance to light source is less than entry in shadow map

SceneShadow map

Page 34: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow mapsImplementation

– Generate homogeneous texture coordinates (s,t,r,q) as light space coordinates (x,y,z,w)

e

e

e

e

wzyx

matrixlookat

vieweyeinverse

matrixlookatviewlight

Matrixprojectionfrustumlight

qrts

)()()(12/12/12/12/12/12/1

Texture matrix to be issued

Page 35: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Steps to Shadow Mapping

Scene from Eye Scene from Light Depth from Light

Projected Depth Depth Comparison Final Scene

Page 36: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow maps• Algorithm

– Render scene from light source– Read depth buffer and store as 2D texture map

• Use glCopyTexImage2D with glReadBuffer(GL_DEPTH_BUFFER)

– Render lit/textured scene• Use shadow map as projective texture• Compare r/q texture coordinate with shadow map entry• Alternatively employ hardware support

– Does depth comparison as texture filtering operation – Shadow map fragment color is 0 or 1

• Modulate fragment color with shadow map color

Page 37: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow mapsPros and cons of shadow maps

– General method for computing all shadows in a scene– Representation of shadows is independent of scene complexity– But, artifacts due to discrete sampling of the shadow map and

quantization of depth values

4096x40961024x1024

Page 38: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow texturesShadow textures

– Render shadow on the receiver and load it as shadow texture

• Light source and shadow object dependent

Unshadowed Scene Shadow Texture Texture mapped floor

Page 39: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Shadow techniquesSoft shadows

– Similar to motion blur– Assume area light source

• Jitter light position within area• Render from jittered positions• Combine results in the

accumulation buffer

Use decreasing scale with increasing distance to center of area

Page 40: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

TransparencyHow to handle non-opaque (semi-transparent) objects

– Multiple fragments contribute to a pixel– Visibility order of fragments is important– Depth buffer can be used for hidden surface

removal but not for semi-transparent objects

Page 41: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Transparency See-through objects

Object order affects final color Blending non commutative 1C1 + (1-1)0C0 0C0 + (1-0)1C1

Depth test discards objects

Page 42: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

TransparencyGeneral solution

– Visibility sort all semi-transparent objects in the scene

– Render opaque objects first and update the depth values

– Render semi-transparent objects in visibility order• Apply depth test but do not alter depth values

– Use alpha-blending in order to obtain linear combination of src and dst color

Page 43: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

TransparencyCorrect transparency by sorting and alpha-blending

Page 44: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Depth peeling• Recall: standard depth test gives nearest fragment at each

pixel without imposing any order• But: there is also a second, third … fragment, depth test

does not provide a means to render the nth nearest surface.

• Depth peeling solves this problem:– With n passes over a scene, n layers deeper into the scene are

generated– E.g. with 2 passes, the nearest and second nearest surfaces are

extracted– Both the depth and RGBA information is obtained for each

layer

Page 45: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Depth peelingExample

Page 46: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Depth peeling• The method peels different depth layers in front-to-back

order

• Drawback: as many rendering passes as objects depth complexity (maximum number of surface points mapping to one fragment)

Page 47: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Depth peelingHow to determine the depth complexity

– Render the scene without depth test and increment the stencil bit (or entries in a texture render target using additive blend) whenever a fragment is generated

– „Reduce“ the buffer to obtain the maximum• In each pass, maximum of 4 texels is computed and stored into 1

texel of output texture

Reduce m x m region in fragment shader1 pass: render quad that covers 1/4 pixels2 pass: render quad that covers 1/16 pixels…

Page 48: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Depth peelingAlgorithm:

– First pass: render as normal - depth test gives us nearest surface

– Second pass:Use depth buffer computed in the first pass to “peel away” depths less than or equal to nearest depths from the first pass

All other depths (for fragments passing the first test) are resolved by the standard depth test

– The second pass generates depths of the second nearest surface, which are used to peel away the first and second nearest surfaces in the third pass

Page 49: Image Synthesis

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Depth peelingDepth peeling issues

– Depth peeling needs two depth tests• In the nth pass a test with the (n-1)th nearest depths• The standard depth test

– The first test can be done in a fragment shader• Therefore, in each pass the depth values are rendered into a

texture render target– The depth test remains active even if rendering is to such a target

• In the upcoming pass, a shader reads the depth value from this target and discard fragments with depth values less than this value

• All surviving fragments are passed through the standard test– Use 32 Bit float texture render target for highest accuracy