reflections, shadows, transparency, and fogdeveloper.download.nvidia.com/assets/gamedev/docs/... ·...
TRANSCRIPT
GDC Tutorial:Advanced OpenGL Game Development
March 8, 2000
GDC Tutorial:GDC Tutorial:Advanced OpenGL Game DevelopmentAdvanced OpenGL Game Development
March 8, 2000March 8, 2000
Reflections, Shadows,Transparency, and Fog
Mark J. KilgardGraphics Software EngineerNVIDIA Corporation
Quick Tutorial onStencil TestingAn extra test for fine-grain pixel control• Standard OpenGL and DirectX 6 feature
• Per-pixel test similar to depth buffering
– Tests fragment against pixel’s stencil value, rejects fragments that fail
– Also, can modify pixel’s stencil buffer value based on stencil/depth test results
• Hardware accelerates stencil testing
– Typically free when depth testing too
An extra test for fineAn extra test for fine--grain pixel controlgrain pixel control•• Standard OpenGL and DirectX 6 featureStandard OpenGL and DirectX 6 feature
•• PerPer--pixel test similar to depth bufferingpixel test similar to depth buffering
–– Tests fragment against pixel’s stencil value, rejects Tests fragment against pixel’s stencil value, rejects fragments that failfragments that fail
–– Also, can modify pixel’s stencil buffer value based on Also, can modify pixel’s stencil buffer value based on stencil/depth test resultsstencil/depth test results
•• Hardware accelerates stencil testingHardware accelerates stencil testing
–– Typically Typically freefree when depth testing toowhen depth testing too
Stencil Testing in theFragment Pipeline
PixelOwnership
Test
StencilTest
DepthTest
Blending Dithering Logic Op
ScissorTest
AlphaTest
Frame buffer
Fragment+
AssociatedData
Stencil TestingSimilar to Depth Testing but• Compares current reference value to pixel’s stencil buffer
value
• Same comparison functions as depth test:– NEVER, ALWAYS– LESS, LEQUAL– GREATER, GEQUAL– EQUAL, NOTEQUAL
• Stencil bit mask controls comparison– (( ref & mask ) op ( svalue & mask )
Similar to Depth Testing butSimilar to Depth Testing but•• Compares current reference value to pixel’s stencil buffer Compares current reference value to pixel’s stencil buffer
valuevalue
•• Same comparison functions as depth test:Same comparison functions as depth test:–– NEVER, ALWAYSNEVER, ALWAYS–– LESS, LEQUALLESS, LEQUAL–– GREATER, GEQUALGREATER, GEQUAL–– EQUAL, NOTEQUALEQUAL, NOTEQUAL
•• Stencil bit mask controls comparisonStencil bit mask controls comparison–– (( ref & mask ) op ( svalue & mask )(( ref & mask ) op ( svalue & mask )
Stencil OperationsWay stencil buffer values are controlled• Stencil side effects of
– Stencil test fails
– Depth test fails
– Depth test passes
• Possible operations– Increment, Decrement (saturates)
– Increment, Decrement (wraps, DX6 option)
– Keep, Replace, Zero, Invert
Way stencil buffer values are controlledWay stencil buffer values are controlled•• Stencil side effects ofStencil side effects of
–– Stencil test failsStencil test fails
–– Depth test failsDepth test fails
–– Depth test passesDepth test passes
•• Possible operationsPossible operations
–– Increment, Decrement (saturates)Increment, Decrement (saturates)
–– Increment, Decrement (wraps, DX6 option)Increment, Decrement (wraps, DX6 option)
–– Keep, Replace, Zero, InvertKeep, Replace, Zero, Invert
Stencil Write MaskControlling stencil buffer updates• Bit mask for controlling write back of stencil value to the
stencil buffer
• Applies to the clear too!
• Stencil compare & write masks allow stencil values to be treated as sub-fields
Controlling stencil buffer updatesControlling stencil buffer updates•• Bit mask for controlling write back of stencil value to the Bit mask for controlling write back of stencil value to the
stencil bufferstencil buffer
•• Applies to the clear too!Applies to the clear too!
•• Stencil compare & write masks allow stencil values to be Stencil compare & write masks allow stencil values to be treated as subtreated as sub--fieldsfields
OpenGL Stencil APIStencil-related Routines• glEnable/glDisable(GL_STENCIL_TEST);
• glStencilFunc(function, reference, mask);
• glStencilOp(stencil_fail,depth_fail, depth_pass);
• glStencilMask(mask);
• glClear(… | GL_STENCIL_BUFFER_BIT);
StencilStencil--related Routinesrelated Routines•• glEnable/glDisable(GL_STENCIL_TEST);glEnable/glDisable(GL_STENCIL_TEST);
•• glStencilFunc(function, reference, mask);glStencilFunc(function, reference, mask);
•• glStencilOp(stencil_fail,glStencilOp(stencil_fail,depth_fail, depth_pass);depth_fail, depth_pass);
•• glStencilMask(mask);glStencilMask(mask);
•• glClear(… | GL_STENCIL_BUFFER_BIT);glClear(… | GL_STENCIL_BUFFER_BIT);
Requesting aStencil BufferHave to request one to use one!• If using stencil, request sufficient bits of stencil
• Implementations may support from zero to 32 bits of stencil
• 8, 4, or 1 bit are common possibilities
• Easy for GLUT programs:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH | GLUT_STENCIL);
glutCreateWindow(“stencil example”);
Have to request one to use one!Have to request one to use one!•• If using stencil, request sufficient bits of stencilIf using stencil, request sufficient bits of stencil
•• Implementations may support from zero to 32 bits of stencilImplementations may support from zero to 32 bits of stencil
•• 8, 4, or 1 bit are common possibilities8, 4, or 1 bit are common possibilities
•• Easy for GLUT programs:Easy for GLUT programs:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH | GLUT_DEPTH | GLUT_STENCILGLUT_STENCIL););
glutCreateWindow(“stencil example”);glutCreateWindow(“stencil example”);
Stencil PerformanceCheaper than you think• With today’s 32-bit graphics accelerator modes, 24-bit depth
and 8-bit stencil packed in same memory word
• TNT2 and GeForce are examples
• Performance implication:
if using depth testing, stenciling is atNO PENALTY
Cheaper than you thinkCheaper than you think•• With today’s 32With today’s 32--bit graphics accelerator modes, 24bit graphics accelerator modes, 24--bit depth bit depth
and 8and 8--bit stencil packed in bit stencil packed in samesame memory wordmemory word
•• TNT2 and GeForce are examplesTNT2 and GeForce are examples
•• Performance implication:Performance implication:
if using depth testing, stenciling is atif using depth testing, stenciling is atNO PENALTYNO PENALTY
Planar ReflectionsDinosaur reflected in the floorDinosaur reflected in the floorDinosaur reflected in the floor
Easy hack.Draw dino twice,second time hasglScalef(1, -1, 1)to reflect throughthe floor.
Confining the ReflectionReflection should be limited to reflective surfaceReflection should be limited to reflective surfaceReflection should be limited to reflective surface
Good, stencilused to limit reflection.
Bad
How to do itDraw floor with a unique stencil valueDraw floor with a unique stencil valueDraw floor with a unique stencil value
Clear stencil to zero.Draw floor polygon and set stencil to one.Only draw reflection where stencil is one.
Recursive planar mirrorsReflection idea can be applied repeatedlyReflection idea can be applied repeatedlyReflection idea can be applied repeatedly
Requires more bits of stencil.Note bogus illumination.
A Magician reveals theTrickA Bird’s Eye ViewA Bird’s Eye ViewA Bird’s Eye View
ShadowsImportant visual cue• All real world scenes have shadows
– Lack of shadows = big clue that an image is computer generated!
• Occlusion from light’s point-of-view instead of viewer’s
– Cue for light-object-object relationships
• Artistic effect
– Soft vs. hard feel, dramatic or spooky feel
Important visual cueImportant visual cue•• All real world scenes have shadowsAll real world scenes have shadows
–– Lack of shadows = big clue that an image is computer Lack of shadows = big clue that an image is computer generated!generated!
•• Occlusion from light’s pointOcclusion from light’s point--ofof--view instead of viewer’sview instead of viewer’s
–– Cue for lightCue for light--objectobject--object relationshipsobject relationships
•• Artistic effectArtistic effect
–– Soft vs. hard feel, dramatic or spooky feelSoft vs. hard feel, dramatic or spooky feel
InteractiveLighting ModelsTypically lack shadow support• So-called “local” lighting models ignore occlusion of light
sources that create shadows
– Except trivial self-shadowing (i.e., clamping L•N)
• Global lighting models account for shadows
– Examples: radiosity, ray tracing
– But too expensive for interactive use
• Interactive shadow algorithms typically operate independent of local lighting models
Typically lack shadow supportTypically lack shadow support•• SoSo--called “local” lighting models ignore occlusion of light called “local” lighting models ignore occlusion of light
sources that create shadowssources that create shadows
–– Except trivial selfExcept trivial self--shadowing (i.e., clamping Lshadowing (i.e., clamping L•• NN))
•• Global lighting models account for shadowsGlobal lighting models account for shadows
–– Examples: radiosity, ray tracingExamples: radiosity, ray tracing
–– But too expensive for interactive useBut too expensive for interactive use
•• Interactive shadow algorithms typically operate independent Interactive shadow algorithms typically operate independent of local lighting modelsof local lighting models
Interactive ShadowSimplicationsTrue shadows are expensive, cut corners• Simple geometry tricks
– Projection
– Volume intersection
• Pre-compute static shadow results where possible
• Make simplifying assumptions
– Treat area light sources as points
• Exploit hardware features such as stencil
True shadows are expensive, cut cornersTrue shadows are expensive, cut corners•• Simple geometry tricksSimple geometry tricks
–– ProjectionProjection
–– Volume intersectionVolume intersection
•• PrePre--compute static shadow results where possiblecompute static shadow results where possible
•• Make simplifying assumptionsMake simplifying assumptions
–– Treat area light sources as pointsTreat area light sources as points
•• Exploit hardware features such as stencilExploit hardware features such as stencil
Interactive ShadowRendering TechniquesDifferent approaches, different tradeoffs• Planar projected shadows
• Stenciled shadow volumes
• Pre-computed shadow textures (lightmaps)
• Shadow textures
• Shadow maps
Different approaches, different tradeoffsDifferent approaches, different tradeoffs•• Planar projected shadowsPlanar projected shadows
•• Stenciled shadow volumesStenciled shadow volumes
•• PrePre--computed shadow textures (lightmaps)computed shadow textures (lightmaps)
•• Shadow texturesShadow textures
•• Shadow mapsShadow maps
Planar ProjectedShadowsSo-called “fake” shadows• Uses projective geometry trick
• Flattens 3D model into ground plane
• Shadow effect only can be applied to planes
• Supports either positional or directional lights
SoSo--called “fake” shadowscalled “fake” shadows•• Uses projective geometry trickUses projective geometry trick
•• Flattens 3D model into ground planeFlattens 3D model into ground plane
•• Shadow effect only can be applied to planesShadow effect only can be applied to planes
•• Supports either positional or directional lightsSupports either positional or directional lights
Projected Planar ShadowExample
Projected PlanarShadowsClassic computer graphics trick• Given
– Ground plane equation, Ax + By + Cz + D = 0
– Light position (x, y, z, w)
• Construct projective transform that “squishes” vertices into ground plane based on light position
– Concatenate this with view transform
• Rendering 3D object creates shadow-like pile of polygons in ground plane
Classic computer graphics trickClassic computer graphics trick•• GivenGiven
–– Ground plane equation, Ax + By + Cz + D = 0Ground plane equation, Ax + By + Cz + D = 0
–– Light position (x, y, z, w)Light position (x, y, z, w)
•• Construct projective transform that “squishes” vertices into Construct projective transform that “squishes” vertices into ground plane based on light positionground plane based on light position
–– Concatenate this with view transformConcatenate this with view transform
•• Rendering 3D object creates shadowRendering 3D object creates shadow--like pile of polygons in like pile of polygons in ground planeground plane
Projected PlanarShadow Matrix4x4 Projective Matrix4x4 Projective Matrix4x4 Projective Matrix
P - Lx A -Ly A -Lz A -Lw A
-Lx B P - Ly B -Lz B -Lw B
-Lx C -Ly C P - Lz C -Lw C
-Lx D -Ly D -Lz D P - Lw D
where P = Lx A + Ly B + Lz C + Lw D
Projected PlanarShadow Issues
• Shadow must be cast on infinite planes
– Stencil can limit shadow to finite planar region
• “Pile of polygons” creates Z-fighting artifacts
– Polygon offset fixes this, but disturbs Z values
– Stencil testing is better solution
• Difficult to blend shadow with ground texture
– Just blending creates “double blends”
– But stencil testing can eliminate “double blends”
– Specular highlights can show in shadow
•• Shadow must be cast on infinite planesShadow must be cast on infinite planes
–– Stencil can limit shadow to finite planar regionStencil can limit shadow to finite planar region
•• “Pile of polygons” creates Z“Pile of polygons” creates Z--fighting artifactsfighting artifacts
–– Polygon offset fixes this, but disturbs Z valuesPolygon offset fixes this, but disturbs Z values
–– Stencil testing is better solutionStencil testing is better solution
•• Difficult to blend shadow with ground textureDifficult to blend shadow with ground texture
–– Just blending creates “double blends”Just blending creates “double blends”
–– But stencil testing can eliminate “double blends”But stencil testing can eliminate “double blends”
–– Specular highlights can show in shadowSpecular highlights can show in shadow
Planar ProjectedShadow Artifacts
Bad Good
extends offground region
Z fighting double blending
The Shadow VolumeConceptVolumetric shadows, not just planar• A single point light source splits the world in two
– shadowed regions
– unshadowed regions
• A shadow volume is the boundary between these shadowed and unshadowed regions
• First described by [Crow 77]
Volumetric shadows, not just planarVolumetric shadows, not just planar•• A single point light source splits the world in twoA single point light source splits the world in two
–– shadowed regionsshadowed regions
–– unshadowed regionsunshadowed regions
•• A shadow volume is the boundary between these shadowed A shadow volume is the boundary between these shadowed and unshadowed regionsand unshadowed regions
•• First described by [Crow 77]First described by [Crow 77]
VisualizingShadow VolumesOccluder and light projects a shadow volumeOccluder and light projects a shadow volumeOccluder and light projects a shadow volume
Shadow VolumeResultObjects within the volume are shadowedObjects within the volume are shadowedObjects within the volume are shadowed
Shadow VolumeAlgorithmHigh-level view of the algorithm• Given the scene and a light source position, determine the
shadow volume (harder than it sounds)
• Render the scene in two passes
– Draw scene with the light enabled,updating only fragments in unshadowed region
– Draw scene with the light disabled,updated only fragments in shadowed region
• But how to control update of regions?
HighHigh--level view of the algorithmlevel view of the algorithm•• Given the scene and a light source position, determine the Given the scene and a light source position, determine the
shadow volume (harder than it sounds)shadow volume (harder than it sounds)
•• Render the scene in two passesRender the scene in two passes
–– Draw scene with the light Draw scene with the light enabledenabled,,updating updating onlyonly fragments in fragments in unshadowedunshadowed regionregion
–– Draw scene with the light Draw scene with the light disableddisabled,,updated updated onlyonly fragments in fragments in shadowedshadowed regionregion
•• But how to control update of regions?But how to control update of regions?
2D Cutaway of aShadow Volume
shadowingobject
shadowvolume(infinite extent)
partiallyshadowed object
lightsource
eyeposition surface inside
shadow volume(shadowed)
surface outsideshadow volume(illuminated)
Tagging Pixels asShadowed or UnshadowedUsing stencil testing• High-level algorithm does not say how to update only either pixels
in or out of the shadow volume!
• The stenciling approach
– Clear stencil buffer to zero and depth buffer to 1.0
– Render scene to leave depth buffer with closest Zs
– Render shadow volume into frame buffer with depth testing but without updating color and depth, but inverting a stencil bit
– This leaves stencil bit set within shadow!
Using stencil testingUsing stencil testing•• HighHigh--level algorithm does not say how to update only either pixels level algorithm does not say how to update only either pixels
in or out of the shadow volume!in or out of the shadow volume!
•• The stenciling approachThe stenciling approach
–– Clear stencil buffer to zero and depth buffer to 1.0Clear stencil buffer to zero and depth buffer to 1.0
–– Render scene to leave depth buffer with closest ZsRender scene to leave depth buffer with closest Zs
–– Render shadow volume into frame buffer with depth testing but Render shadow volume into frame buffer with depth testing but withoutwithout updating color and depth, but updating color and depth, but invertinginverting a stencil bita stencil bit
–– This leaves stencil bit set within shadow!This leaves stencil bit set within shadow!
Stencil Inverting ofShadow VolumeWhy it worksWhy it worksWhy it works
eyeposition
lightsource
shadowingobject
two inverts, left zero
one invert, left one
zero inverts, left zero
Visualizing StenciledShadow Volume TaggingShadowed scene Stencil buffer
red = stencil value of 1green = stencil value of 0
ComputingShadow VolumesHarder than you might think• Easy for a single triangle, just project out three infinite
polygons from the triangle, opposite the light position
• But shadow volume polygons should not intersect each other for invert trick to work
– This makes things hard
• For complex objects, projecting object’s 2D silhouette is a good approximation (flat objects are easy)
• Static shadow volumes can be pre-compiled
Harder than you might thinkHarder than you might think•• Easy for a single triangle, just project out three infinite Easy for a single triangle, just project out three infinite
polygons from the triangle, opposite the light positionpolygons from the triangle, opposite the light position
•• But shadow volume polygons should not intersect each other But shadow volume polygons should not intersect each other for invert trick to workfor invert trick to work
–– This makes things hardThis makes things hard
•• For complex objects, projecting object’s 2D silhouette is a For complex objects, projecting object’s 2D silhouette is a good approximation (flat objects are easy)good approximation (flat objects are easy)
•• Static shadow volumes can be preStatic shadow volumes can be pre--compiledcompiled
For Shadow VolumesWith Intersecting PolygonsUse a stencil enter/leave counting approach• Draw shadow volume twice using face culling
– 1st pass: render front faces and increment when depth test passes
– 2nd pass: render back faces and decrement when depth test passes
• This two-pass way is more expensive than invert
• And burns more fill rate drawing shadow volumes
• Inverting is better if no polygon intersections
Use a stencil enter/leave counting approachUse a stencil enter/leave counting approach•• Draw shadow volume twice using face cullingDraw shadow volume twice using face culling
–– 1st pass: render 1st pass: render frontfront faces and faces and incrementincrement when depth when depth test passestest passes
–– 2nd pass: render 2nd pass: render backback faces and faces and decrementdecrement when depth when depth test passestest passes
•• This twoThis two--pass way is more expensive than invertpass way is more expensive than invert
•• And burns more fill rate drawing shadow volumesAnd burns more fill rate drawing shadow volumes
•• Inverting is better if no polygon intersectionsInverting is better if no polygon intersections
Why Increment/DecrementWorksExample in 2DExample in 2DExample in 2D
shadowing objectlightsource
eyeposition
zero
zero
+1
+1+2 +2
+3
Shadow VolumeIssuesPractical considerations [Bergeron 86]• If eye is in shadow volume, need to determine this and flip
enabled & disabled lighting passes
• Shadow volume only as good as its tessellation
– Shadows tend to magnify limited tessellation of curved surfaces
• Must cap the shadow volume’s intersection with the near clipping plane
• Open models and non-planar polygons
Practical considerations [Bergeron 86]Practical considerations [Bergeron 86]•• If eye is in shadow volume, need to determine this and flip If eye is in shadow volume, need to determine this and flip
enabled & disabled lighting passesenabled & disabled lighting passes
•• Shadow volume only as good as its tessellationShadow volume only as good as its tessellation
–– Shadows tend to magnify limited tessellation of curved Shadows tend to magnify limited tessellation of curved surfacessurfaces
•• Must cap the shadow volume’s intersection with the near Must cap the shadow volume’s intersection with the near clipping planeclipping plane
•• Open models and nonOpen models and non--planar polygonsplanar polygons
Reconstructing ShadowVolume From a Depth BufferVery clever idea [McCool 98]• Render scene from light source with depth testing
• Read back the depth buffer
• Use computer vision techniques to reconstruct the shadow volume geometry from the depth buffer image
• Very reasonable results for complex scenes
Very clever idea [McCool 98]Very clever idea [McCool 98]•• Render scene from light source with depth testingRender scene from light source with depth testing
•• Read back the depth bufferRead back the depth buffer
•• Use computer vision techniques to reconstruct the shadow Use computer vision techniques to reconstruct the shadow volume volume geometrygeometry from the depth buffer from the depth buffer imageimage
•• Very reasonable results for complex scenesVery reasonable results for complex scenes
Multiple Lights andShadow VolumesRequires still more rendering passes,but can work!Requires still more rendering passes,Requires still more rendering passes,but can work!but can work!
Shadows from differentlight sources overlapcorrectly
Shadow Volumes fromArea Light SourcesMake soft shadows• Shadow volumes work for point light sources
• Area light sources are common and make soft shadows
• Model an area light source as a collection of point light sources [Brotman & Badler 84]
• Use accumulation buffer or additive blending to accumulate soft shadow
• Linear cost per shadow volume sample
Make soft shadowsMake soft shadows•• Shadow volumes work for point light sourcesShadow volumes work for point light sources
•• Area light sources are common and make soft shadowsArea light sources are common and make soft shadows
•• Model an area light source as a collection of point light Model an area light source as a collection of point light sources [Brotman & Badler 84]sources [Brotman & Badler 84]
•• Use accumulation buffer or additive blending to accumulate Use accumulation buffer or additive blending to accumulate soft shadowsoft shadow
•• Linear cost per shadow volume sampleLinear cost per shadow volume sample
Soft Shadow ExampleEight samples (more would be better)Eight samples (more would be better)Eight samples (more would be better)
Note the bandingartifacts
CombinedShadow ApproachesTwo Approaches at OnceTwo Approaches at OnceTwo Approaches at Once
just logo shadow volume
combinedapproach
just planar projectedshadowsteapot lacks
shadowon the floor
logo lacks shadowon the teapot
Pre-computedShadow TexturesLightmaps are static shadow textures• Compute lightmaps off-line with radiosity solver
– local lighting models evaluated on tight grid can work well too
• Lightmaps with soft shadows can be built faster with convolution approach, accelerated using the Fast Fourier Transform [Soler & Silion 98]
• Pre-computed shadow textures work well for building interior with lots of diffuse surfaces
Lightmaps are static shadow texturesLightmaps are static shadow textures•• Compute lightmaps offCompute lightmaps off--line with radiosity solverline with radiosity solver
–– local lighting models evaluated on tight grid can work well local lighting models evaluated on tight grid can work well tootoo
•• Lightmaps with soft shadows can be built faster with Lightmaps with soft shadows can be built faster with convolution approach, accelerated using the Fast Fourier convolution approach, accelerated using the Fast Fourier Transform [Soler & Silion 98]Transform [Soler & Silion 98]
•• PrePre--computed shadow textures work well for building interior computed shadow textures work well for building interior with lots of diffuse surfaceswith lots of diffuse surfaces
RenderingSoft Shadow TexturesFast soft shadows [Heckbert & Herf 96]• Render a shadow texture
– For each shadowed polygon, project all light-source-occluding polygons into the shadowed polygon’s plane
* Similar to projected planar shadows
* Model area light sources as multiple jittered points
* Use additive blending to accumulate all contributions
• Copy resulting image to the shadow texture
• Draw polygon in final scene with shadow texture
Fast soft shadows [Heckbert & Herf 96]Fast soft shadows [Heckbert & Herf 96]•• Render a shadow textureRender a shadow texture
–– For each shadowed polygon, project all lightFor each shadowed polygon, project all light--sourcesource--occluding occluding polygons into the shadowed polygon’s planepolygons into the shadowed polygon’s plane
** Similar to projected planar shadowsSimilar to projected planar shadows
** Model area light sources as multiple jittered pointsModel area light sources as multiple jittered points
** Use additive blending to accumulate all contributionsUse additive blending to accumulate all contributions
•• Copy resulting image to the shadow textureCopy resulting image to the shadow texture
•• Draw polygon in final scene with shadow textureDraw polygon in final scene with shadow texture
RenderedSoft Shadow Texture ExampleShadow Texture and Final ResultShadow Texture and Final ResultShadow Texture and Final Result
Shadow texture, accumulationof nine jittered occluderprojections
Final resulting scene, shadowtexture modulated withwooden floor
HardwareShadow MappingShadowing with a depth map texture• Software version described by [Reeves 89]
• Requires hardware support [Segal, et.al. 92] for
– A “depth texture” (see SGIX_depth_texture)
– A “depth compare” texture filter operator(see SGIX_shadow)
• Uses eye-linear texgen to generate texture coordinates (s/q,t/q,r/q) in light’s coordinate space
Shadowing with a depth map textureShadowing with a depth map texture•• Software version described by [Reeves 89]Software version described by [Reeves 89]
•• Requires hardware support [Segal, et.al. 92] forRequires hardware support [Segal, et.al. 92] for
–– A “depth texture” (see SGIX_depth_texture)A “depth texture” (see SGIX_depth_texture)
–– A “depth compare” texture filter operatorA “depth compare” texture filter operator(see SGIX_shadow)(see SGIX_shadow)
•• Uses eyeUses eye--linear texgen to generate texture coordinates linear texgen to generate texture coordinates (s/q,t/q,r/q) in light’s coordinate space(s/q,t/q,r/q) in light’s coordinate space
HardwareShadow Mapping AlgorithmTwo pass approach• First, render scene from light’s point-of-view with depth testing
– Copy the depth buffer to a “depth texture”
• Second, render scene from eye’s point-of-view as follows
– Configure eye-linear texgen to generate (s,t,r,q) coordinates in light’s coordinate space (“r” is z-planar distance to the light)
– Configure special shadow texture filter operator
– Texture filter tests if “r” is less than depth texture texel
– Texture color is one if tests passes, zero if test fails
– Modulate fragment color with one or zero texture
Two pass approachTwo pass approach•• First, render scene from light’s pointFirst, render scene from light’s point--ofof--view with depth testingview with depth testing
–– Copy the depth buffer to a “depth texture”Copy the depth buffer to a “depth texture”
•• Second, render scene from eye’s pointSecond, render scene from eye’s point--ofof--view as followsview as follows
–– Configure eyeConfigure eye--linear texgen to generate (s,t,r,q) coordinates in light’s linear texgen to generate (s,t,r,q) coordinates in light’s coordinate space (“r” is zcoordinate space (“r” is z--planar distance to the light)planar distance to the light)
–– Configure special shadow texture filter operatorConfigure special shadow texture filter operator
–– Texture filter tests if “r” is less than depth texture texelTexture filter tests if “r” is less than depth texture texel
–– Texture color is one if tests passes, zero if test failsTexture color is one if tests passes, zero if test fails
–– Modulate fragment color with one or zero textureModulate fragment color with one or zero texture
HardwareShadow Mapping IssuesWhy it works and problems• Shadow map texturing is effectively a second occlusion test
done with respect to the light
– Modulate with zero if inside shadow
– Modulate with one if outside shadow
• Traditional mipmap texture filtering not suited for filtering a depth map
• Explicit hardware support currently limited to InfiniteReality and RealityEngine
Why it works and problemsWhy it works and problems•• Shadow map texturing is effectively a second occlusion test Shadow map texturing is effectively a second occlusion test
done with respect to the lightdone with respect to the light
–– Modulate with zero if inside shadowModulate with zero if inside shadow
–– Modulate with one if outside shadowModulate with one if outside shadow
•• Traditional mipmap texture filtering not suited for filtering a Traditional mipmap texture filtering not suited for filtering a depth mapdepth map
•• Explicit hardware support currently limited to InfiniteReality Explicit hardware support currently limited to InfiniteReality and RealityEngineand RealityEngine
TransparencySee-through objects• Multiple objects contribute to pixel’s final color
• Order of objects relative to view affects color
• Unsorted depth buffering insufficient for hidden surface removal
• Internal structure of object geometry can become visible
• With caveats, alpha blending enables transparency
SeeSee--through objectsthrough objects•• Multiple objects contribute to pixel’s final colorMultiple objects contribute to pixel’s final color
•• Order of objects relative to view affects colorOrder of objects relative to view affects color
•• Unsorted depth buffering insufficient for hidden surface Unsorted depth buffering insufficient for hidden surface removalremoval
•• Internal structure of object geometry can become visibleInternal structure of object geometry can become visible
•• With caveats, alpha blending enables transparencyWith caveats, alpha blending enables transparency
Some Blending isCommutativeOrder independent blends• Filter blendingglBlendFunc(GL_DST_COLOR, GL_ZERO)
• Additive blendingglBlendFunc(GL_ONE, GL_ONE)
• But transparency blending is non-commutativeglBlendFunc(GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA)
Order independent blendsOrder independent blends•• Filter blendingFilter blendingglBlendFunc(GL_DST_COLOR, GL_ZERO)glBlendFunc(GL_DST_COLOR, GL_ZERO)
•• Additive blendingAdditive blendingglBlendFunc(GL_ONE, GL_ONE)glBlendFunc(GL_ONE, GL_ONE)
•• But transparency blending is nonBut transparency blending is non--commutativecommutativeglBlendFunc(GL_SRC_ALPHA, glBlendFunc(GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA)GL_ONE_MINUS_SRC_ALPHA)
Transparency OrderingRendering order mattersRendering order mattersRendering order matters
Orange spherein front of bluecone.Overlap should bemore orange.
Blue conein front of orangesphere.Overlap should bemore blue.
Depth testingcan mess up transparency.WRONG: Overlapshouldbe blended!
Sorting forTransparencyEnsuring proper blending ordering• Draw all opaque objects first
• When transparent object do not intersect,back-to-front sort of transparent objects works
– Inexact sort may be acceptable
• When transparent objects intersect, must subdivide and sort intersecting primitives
Ensuring proper blending orderingEnsuring proper blending ordering•• Draw all opaque objects Draw all opaque objects firstfirst
•• When transparent object do not intersect,When transparent object do not intersect,backback--toto--front sort of transparent objects worksfront sort of transparent objects works
–– Inexact sort may be acceptableInexact sort may be acceptable
•• When transparent objects intersect, must subdivide and sort When transparent objects intersect, must subdivide and sort intersecting primitivesintersecting primitives
Sorting OptimizationsTransparency shortcuts• Take advantage of face culling
– Draw back faces, then draw front faces
– Works for non-overlapping closed convex objects
• Disable depth testing
– Prevents front faces from hiding back faces
– For closed, convex, constant-colored objects, order of front and back face drawing does not matter
Transparency shortcutsTransparency shortcuts•• Take advantage of face cullingTake advantage of face culling
–– Draw back faces, then draw front facesDraw back faces, then draw front faces
–– Works for nonWorks for non--overlapping closed convex objectsoverlapping closed convex objects
•• Disable depth testingDisable depth testing
–– Prevents front faces from hiding back facesPrevents front faces from hiding back faces
–– For For closed, convex, constantclosed, convex, constant--coloredcolored objects, order of objects, order of front and back face drawing does not matterfront and back face drawing does not matter
Screen DoorTransparency ExampleStipple simulates semi-opacityStipple simulates semiStipple simulates semi--opacityopacity
Screen DoorTransparency ImplementationPolygon Stipple• OpenGL supports 32x32 binary stipple pattern
• Percentage of 1’s in screen door pattern should match the object’s opacity percentage
• Different objects should use distinct patterns
• Limitations
– Polygon stipple only applies to polygons
– Stipple pattern tied to window origin
Polygon StipplePolygon Stipple•• OpenGL supports 32x32 binary stipple patternOpenGL supports 32x32 binary stipple pattern
•• Percentage of 1’s in screen door pattern should match the Percentage of 1’s in screen door pattern should match the object’s opacity percentageobject’s opacity percentage
•• Different objects should use distinct patternsDifferent objects should use distinct patterns
•• LimitationsLimitations
–– Polygon stipple only applies to polygonsPolygon stipple only applies to polygons
–– Stipple pattern tied to window originStipple pattern tied to window origin
Screen Door TransparencyWith Alpha TexturesAlternative to polygon stipple• Place screen door pattern in alpha texture
• Generate window-space texture coordinates– Enable GL_EYE_LINEAR texgen with texgen plane equations
mimicking the projection & viewport transforms– Projective texturing’s division by “q” will mimic vertex coordinate’s
division by “w”
• Use alpha testing to discard fragments based on screen door alpha texture
• Applies to both lines and polygons
Alternative to polygon stippleAlternative to polygon stipple•• Place screen door pattern in alpha texturePlace screen door pattern in alpha texture
•• Generate windowGenerate window--space texture coordinatesspace texture coordinates–– Enable Enable GL_EYE_LINEARGL_EYE_LINEAR texgen with texgen plane equations texgen with texgen plane equations
mimicking the projection & viewport transformsmimicking the projection & viewport transforms–– Projective texturing’s division by “q” will mimic vertex coordinProjective texturing’s division by “q” will mimic vertex coordinate’s ate’s
division by “w”division by “w”
•• Use alpha testing to discard fragments based on screen door Use alpha testing to discard fragments based on screen door alpha texturealpha texture
•• Applies to both lines and polygons Applies to both lines and polygons
Multiple Levels ofScreen Door TransparencyOpacity controlOpacity controlOpacity control
18%
56%
93%
25%
25%
25%
Right image shows no sense of transparency between objects!Screen door transparency is defeated if multiple objectsshare the same stipple pattern.
Sub-pixelScreen Door TransparencyMultisample masking• SGIS_multisample extension supports multiple color
samples per pixel
– SGI high-end only feature for antialiasing• glSampleMaskSGIS specifies a multisample sub-pixel
screen door pattern
– enabled via glEnable(GL_SAMPLE_MASK_SGIS)
• Because sub-pixel, stipple pattern is not visible
• Limited number of samples (4 or 8 typical)
Multisample maskingMultisample masking•• SGIS_multisampleSGIS_multisample extension supports multiple color extension supports multiple color
samples per pixelsamples per pixel
–– SGI highSGI high--end only feature for antialiasingend only feature for antialiasing
•• glSampleMaskSGISglSampleMaskSGIS specifies a multisample subspecifies a multisample sub--pixel pixel screen door patternscreen door pattern
–– enabled via enabled via glEnable(GL_SAMPLE_MASK_SGIS)glEnable(GL_SAMPLE_MASK_SGIS)
•• Because subBecause sub--pixel, stipple pattern is not visiblepixel, stipple pattern is not visible
•• Limited number of samples (4 or 8 typical)Limited number of samples (4 or 8 typical)
Atmospheric EffectsFog, Haze, Smoke• As light travels through an atmosphere
– Some light is extinguished (absorbed)
– Some light may also be added due to scattering
• An atmospheric model tells how the atmosphere affects the appearance of objects viewed through the atmosphere
Fog, Haze, SmokeFog, Haze, Smoke•• As light travels through an atmosphereAs light travels through an atmosphere
–– Some light is extinguished (absorbed)Some light is extinguished (absorbed)
–– Some light may also be added due to scatteringSome light may also be added due to scattering
•• An atmospheric model tells how the atmosphere affects the An atmospheric model tells how the atmosphere affects the appearance of objects viewed through the atmosphereappearance of objects viewed through the atmosphere
Fog ExampleCommon in Games and Visual SimulationCommon in Games and Visual SimulationCommon in Games and Visual Simulation
OpenGL’s Built-inFog ModelAssumptions• Uniform fog color (“color” of the atmosphere)
• Uniform fog density throughout scene
• Fog is essentially a weighted blend
C = f Cr + (1 - f) Cfwhere Cr = pre-fog fragment color, Cf = uniform fog color, and f = fog factor
• Fog factor computed per-fragment based oneye-distance of given fragment
AssumptionsAssumptions•• Uniform fog color (“color” of the atmosphere)Uniform fog color (“color” of the atmosphere)
•• Uniform fog density throughout sceneUniform fog density throughout scene
•• Fog is essentially a weighted blendFog is essentially a weighted blend
C = f CC = f Crr + (1 + (1 -- f) Cf) Cffwhere where CCrr = pre= pre--fog fragment color, fog fragment color, CCff = uniform fog color, = uniform fog color, and and ff = fog factor= fog factor
•• Fog factor computed Fog factor computed perper--fragmentfragment based onbased oneyeeye--distance of given fragmentdistance of given fragment
Computing theFog FactorBased on Beer’s Law• Fog factor has an exponential drop-off with the distance from
the viewer
f = e-(d z)
• Intuition: Clarity is 100% at 0 meters, but at x + 10 meters is 99% of the clarity at x meters
– at 30 meters, the clarity is (0.99 × 0.99 ×××× 0.99)%
– continuous version of this idea is an exponential drop-off
Based on Beer’s LawBased on Beer’s Law•• Fog factor has an exponential dropFog factor has an exponential drop--off with the distance from off with the distance from
the viewerthe viewer
f = ef = e--(d z)(d z)
•• Intuition: Clarity is 100% at Intuition: Clarity is 100% at 0 meters0 meters, but at , but at x + 10 metersx + 10 meters is is 99% of the clarity at 99% of the clarity at xx metersmeters
–– at at 30 meters30 meters, the clarity is (0.99 , the clarity is (0.99 ×× 0.99 0.99 ×××××××× 0.99)%0.99)%
–– continuous version of this idea is an exponential dropcontinuous version of this idea is an exponential drop--offoff
StandardOpenGL Fog ModelsFog factor equationsFog factor equationsFog factor equations
GL_EXP2 f = e-(d z)
GL_EXP f = e-(d z)
2
f
Given the fog model (EXP2 or EXP), fog density (d),and Z eye-distance (z), compute fog factor (f)
Zeye
OpenGL FogLimitations• Emissive objects are over-attenuated
– Headlights should break through the fog, but don’t
• OpenGL permits implementations to use window Z (a planar distance) instead of the radial eye-distance
– Objects at sides of the view frustum under-fogged
• Real world fog is not so uniform
– Fog varies with altitude, patchy fog
LimitationsLimitations•• Emissive objects are overEmissive objects are over--attenuatedattenuated
–– Headlights should break through the fog, but don’tHeadlights should break through the fog, but don’t
•• OpenGL permits implementations to use window Z (a planar OpenGL permits implementations to use window Z (a planar distance) instead of the radial eyedistance) instead of the radial eye--distancedistance
–– Objects at sides of the view frustum underObjects at sides of the view frustum under--foggedfogged
•• Real world fog is not so uniformReal world fog is not so uniform
–– Fog varies with altitude, patchy fogFog varies with altitude, patchy fog
Planar Eye-distance FogArtifactsWhy they occurWhy they occurWhy they occur
eyeposition
planareye-distance
radialeye-distance
A
B
viewfrustum
Points A and B have the same Z planar distancebut different radial distances.
Texture Fog MethodsTextures to encode fog• 1D, 2D, or 3D texture as fog lookup table
– Map eye position to texture coordinates with GL_EYE_LINEARtexgen
– Use luminance texture with GL_BLEND texture environment and set fog color in texture environment color
– Or an RGBA texture with GL_DECAL mode can encode the fog factor in A and the fog color in RGB
Textures to encode fogTextures to encode fog•• 1D, 2D, or 3D texture as fog lookup table1D, 2D, or 3D texture as fog lookup table
–– Map eye position to texture coordinates with Map eye position to texture coordinates with GL_EYE_LINEARGL_EYE_LINEAR
texgentexgen
–– Use luminance texture with Use luminance texture with GL_BLENDGL_BLEND texture environment and texture environment and set fog color in set fog color in texture environment colortexture environment color
–– Or an RGBA texture with Or an RGBA texture with GL_DECALGL_DECAL mode can encode the fog mode can encode the fog factor in A and the fog color in RGBfactor in A and the fog color in RGB
OpenGL Fog CoordinateExtensionUser-supplied per-vertex fog distance• Normally, OpenGL computes the fog distance automatically
• EXT_fog_coord extension allows application to supply the fog distance at each vertex via glFogCoord1fEXT
– OpenGL interpolates fog distance
– and computes the fog factor per-fragment using OpenGL fog mode (exponentiates)
UserUser--supplied persupplied per--vertex fog distancevertex fog distance•• Normally, OpenGL computes the fog distance automaticallyNormally, OpenGL computes the fog distance automatically
•• EXT_fog_coord extension allows application to supply the EXT_fog_coord extension allows application to supply the fog distance at each vertex via fog distance at each vertex via glFogCoord1fEXTglFogCoord1fEXT
–– OpenGL interpolates fog distanceOpenGL interpolates fog distance
–– and computes the fog factor and computes the fog factor perper--fragmentfragment using OpenGL using OpenGL fog mode (exponentiates)fog mode (exponentiates)
A More ComplexFog ModelHeight-based fog• Described by [Perlin 85, Legakis 98]
• Height-based fog factor equation
f = e -∫∫∫∫ αααα(t) dt
• The extension rate αααα (density) is integrated over the optical path from point A to point B
– This integral is the optical depth
• Assume αααα varies solely with height (altitude)
HeightHeight--based fogbased fog•• Described by [Perlin 85, Legakis 98]Described by [Perlin 85, Legakis 98]
•• HeightHeight--based fog factor equationbased fog factor equation
f = e f = e --∫∫∫∫∫∫∫∫ αααααααα(t) dt(t) dt
•• The extension rate The extension rate αααααααα (density) is integrated over the optical (density) is integrated over the optical path from point A to point Bpath from point A to point B
– This integral is the optical depth
• Assume αααααααα varies solely with height (altitude)
ba
Height-based FogScenarioConsider the optical depth from A to BConsider the optical depth from A to BConsider the optical depth from A to B
A
C B
Y1
Y2
D1 D1∆D
A = eye positionB = fogged positionC = position above A
at height of B
∆D = √ (Bx - Ax)2 + (Bz - Az)2
∆Y
Height-basedOptical DepthNice solution, good for glFogCoordNice solution, good for glFogCoordNice solution, good for glFogCoord
∫ α(t) dt =∆D • α (Y1) , ∆Y = 0
√ 1 + ( ∆D / ∆Y )2 • , ∫ α(y) dy , ∆Y ≠ 0Y2
Y1
b
a
∫ α(y) dy = ∫ α(y) dy - ∫ α(y) dy Y2
Y1
Y2
-∞
Y1
-∞
This can be implemented as two 1D table lookups.
Height-basedFog ExamplesFoggy cityFoggy cityFoggy city normal uniform fog
layered fog layered fog