0% found this document useful (0 votes)
6 views71 pages

GDC2K ShadowsTransparencyFog

The document is a tutorial on advanced OpenGL game development, focusing on techniques such as stencil testing, reflections, shadows, transparency, and fog. It explains the mechanics of stencil testing, including its operations and API, and discusses various shadow rendering techniques, emphasizing the importance of shadows in creating realistic scenes. Additionally, it covers practical applications of these techniques, such as planar reflections and shadow volumes.

Uploaded by

Shruti Tripathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views71 pages

GDC2K ShadowsTransparencyFog

The document is a tutorial on advanced OpenGL game development, focusing on techniques such as stencil testing, reflections, shadows, transparency, and fog. It explains the mechanics of stencil testing, including its operations and API, and discusses various shadow rendering techniques, emphasizing the importance of shadows in creating realistic scenes. Additionally, it covers practical applications of these techniques, such as planar reflections and shadow volumes.

Uploaded by

Shruti Tripathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

GDC Tutorial:

Advanced OpenGL Game Development

Reflections, Shadows,
Transparency, and Fog

March 8, 2000
Mark J. Kilgard
Graphics Software Engineer
NVIDIA Corporation
Quick Tutorial on
Stencil Testing
An extra test for fine -grain pixel control
fine-grain
•• Standard
Standard OpenGL
OpenGL and
and DirectX
DirectX 66 feature
feature
•• Per-pixel test
Per-pixel test similar
similar to
to depth
depth buffering
buffering
–– Tests
Tests fragment
fragment against
against pixel’s
pixel’s stencil
stencil value,
value, rejects
rejects
fragments
fragments that
that fail
fail
–– Also,
Also, can
can modify
modify pixel’s
pixel’s stencil
stencil buffer
buffer value
value based
based on
on
stencil/depth
stencil/depth test
test results
results
•• Hardware
Hardware accelerates
accelerates stencil
stencil testing
testing
–– Typically
Typically free
free when
when depth
depth testing
testing too
too
Stencil Testing in the
Fragment Pipeline
Fragment Pixel
+ Scissor Alpha
Ownership
Test Test
Associated Test
Data

Depth Stencil
Test Test

Blending Dithering Logic Op Frame


buffer
Stencil Testing
Similar to Depth Testing but
•• Compares
Compares current
current reference
reference value
value to
to pixel’s
pixel’s stencil
stencil buffer
buffer
value
value
•• Same
Same comparison
comparison functions
functions as
as depth
depth test:
test:
–– NEVER,
NEVER, ALWAYS
ALWAYS
–– LESS,
LESS, LEQUAL
LEQUAL
–– GREATER,
GREATER, GEQUAL
GEQUAL
–– EQUAL,
EQUAL, NOTEQUAL
NOTEQUAL
•• Stencil
Stencil bit
bit mask
mask controls
controls comparison
comparison
–– ((
(( ref
ref &
& mask
mask )) op
op (( svalue
svalue &
& mask
mask ))
Stencil Operations
Way stencil buffer values are controlled
•• Stencil
Stencil side
side effects
effects of
of
–– Stencil
Stencil test
test fails
fails
–– Depth
Depth test
test fails
fails
–– Depth
Depth test
test passes
passes
•• Possible
Possible operations
operations
–– Increment,
Increment, Decrement
Decrement (saturates)
(saturates)
–– Increment,
Increment, Decrement
Decrement (wraps,
(wraps, DX6
DX6 option)
option)
–– Keep,
Keep, Replace,
Replace, Zero,
Zero, Invert
Invert
Stencil Write Mask
Controlling stencil buffer updates
•• Bit
Bit mask
mask for
for controlling
controlling write
write back
back of
of stencil
stencil value
value to
to the
the
stencil
stencil buffer
buffer
•• Applies
Applies to
to the
the clear
clear too!
too!

•• Stencil
Stencil compare
compare & & write
write masks
masks allow
allow stencil
stencil values
values to
to be
be
treated
treated as
as sub -fields
sub-fields
OpenGL Stencil API
Stencil -related Routines
Stencil-related Routines
•• glEnable/glDisable(GL_STENCIL_TEST);
glEnable/glDisable(GL_STENCIL_TEST);
•• glStencilFunc(function,
glStencilFunc(function, reference,
reference, mask);
mask);
•• glStencilOp(stencil_fail,
glStencilOp(stencil_fail,
depth_fail,
depth_fail, depth_pass);
depth_pass);
•• glStencilMask(mask);
glStencilMask(mask);
•• glClear(…
glClear(… || GL_STENCIL_BUFFER_BIT);
GL_STENCIL_BUFFER_BIT);
Requesting a
Stencil Buffer
Have to request one to use one!
•• IfIf using
using stencil,
stencil, request
request sufficient
sufficient bits
bits of
of stencil
stencil
•• Implementations
Implementations may
may support
support from
from zero
zero to
to 32
32 bits
bits of
of stencil
stencil
•• 8,
8, 4,
4, or
or 11 bit
bit are
are common
common possibilities
possibilities
•• Easy
Easy for
for GLUT
GLUT programs:
programs:

glutInitDisplayMode(GLUT_DOUBLE
glutInitDisplayMode(GLUT_DOUBLE || GLUT_RGB
GLUT_RGB ||
GLUT_DEPTH || GLUT_STENCIL
GLUT_DEPTH );
GLUT_STENCIL);
glutCreateWindow(“stencil
glutCreateWindow(“stencil example”);
example”);
Stencil Performance
Cheaper than you think
•• With
With today’s
today’s 32 -bit graphics
32-bit graphics accelerator
accelerator modes,
modes, 24 -bit depth
24-bit depth
and
and 88-bit
-bit stencil
stencil packed
packed in
in same
same memory
memory word
word
•• TNT2
TNT2 and
and GeForce
GeForce are
are examples
examples
•• Performance
Performance implication:
implication:

ifif using
using depth
depth testing,
testing, stenciling
stenciling is
is at
at
NO
NO PENALTY
PENALTY
Planar Reflections
Dinosaur reflected in the floor

Easy hack.
Draw dino twice,
second time has
glScalef(1, -1, 1)
to reflect through
the floor.
Confining the Reflection
Reflection should be limited to reflective surface

Bad Good, stencil


used to limit reflection.
How to do it
Draw 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 mirrors
Reflection idea can be applied repeatedly

Requires more bits of stencil.


Note bogus illumination.
A Magician reveals the
Trick
A Bird’s Eye View
Shadows
Important visual cue
•• All
All real
real world
world scenes
scenes have
have shadows
shadows
–– Lack
Lack of
of shadows
shadows == big
big clue
clue that
that an
an image
image is
is computer
computer
generated!
generated!
•• Occlusion
Occlusion from
from light’s
light’s point -of-view instead
point-of-view instead of
of viewer’s
viewer’s
–– Cue
Cue for
for light -object-object relationships
light-object-object relationships
•• Artistic
Artistic effect
effect
–– Soft
Soft vs.
vs. hard
hard feel,
feel, dramatic
dramatic or
or spooky
spooky feel
feel
Interactive
Lighting Models
Typically lack shadow support
•• So -called “local”
So-called “local” lighting
lighting models
models ignore
ignore occlusion
occlusion of
of light
light
sources
sources that
that create
create shadows
shadows
–– Except
Except trivial
trivial self -shadowing (i.e.,
self-shadowing (i.e., clamping •N )
clamping LL•N)
•• Global
Global lighting
lighting models
models account
account for
for shadows
shadows
–– Examples:
Examples: radiosity,
radiosity, ray
ray tracing
tracing
–– But
But too
too expensive
expensive for
for interactive
interactive use
use
•• Interactive
Interactive shadow
shadow algorithms
algorithms typically
typically operate
operate independent
independent
of
of local
local lighting
lighting models
models
Interactive Shadow
Simplications
True shadows are expensive, cut corners
•• Simple
Simple geometry
geometry tricks
tricks
–– Projection
Projection
–– Volume
Volume intersection
intersection
•• Pre -compute static
Pre-compute static shadow
shadow results
results where
where possible
possible
•• Make
Make simplifying
simplifying assumptions
assumptions
–– Treat
Treat area
area light
light sources
sources as
as points
points
•• Exploit
Exploit hardware
hardware features
features such
such as
as stencil
stencil
Interactive Shadow
Rendering Techniques
Different approaches, different tradeoffs
•• Planar
Planar projected
projected shadows
shadows
•• Stenciled
Stenciled shadow
shadow volumes
volumes
•• Pre -computed shadow
Pre-computed shadow textures
textures (lightmaps)
(lightmaps)
•• Shadow
Shadow textures
textures
•• Shadow
Shadow maps
maps
Planar Projected
Shadows
So -called “fake” shadows
So-called
•• Uses
Uses projective
projective geometry
geometry trick
trick
•• Flattens
Flattens 3D
3D model
model into
into ground
ground plane
plane
•• Shadow
Shadow effect
effect only
only can
can be
be applied
applied to
to planes
planes
•• Supports
Supports either
either positional
positional or
or directional
directional lights
lights
Projected Planar Shadow
Example
Projected Planar
Shadows
Classic computer graphics trick
•• Given
Given
–– Ground
Ground plane
plane equation,
equation, Ax
Ax ++ By
By ++ Cz
Cz ++ D
D == 00
–– Light
Light position
position (x,
(x, y,
y, z,
z, w)
w)
•• Construct
Construct projective
projective transform
transform that
that “squishes”
“squishes” vertices
vertices into
into
ground
ground plane
plane based
based on
on light
light position
position
–– Concatenate
Concatenate this
this with
with view
view transform
transform
•• Rendering
Rendering 3D
3D object
object creates
creates shadow -like pile
shadow-like pile of
of polygons
polygons in
in
ground
ground plane
plane
Projected Planar
Shadow Matrix
4x4 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 Planar
Shadow Issues
•• Shadow
Shadow must
must be
be cast
cast on
on infinite
infinite planes
planes
–– Stencil
Stencil can
can limit
limit shadow
shadow to
to finite
finite planar
planar region
region
•• “Pile
“Pile of
of polygons”
polygons” creates
creates ZZ-fighting
-fighting artifacts
artifacts
–– Polygon
Polygon offset
offset fixes
fixes this,
this, but
but disturbs
disturbs ZZ values
values
–– Stencil
Stencil testing
testing is
is better
better solution
solution
•• Difficult
Difficult to
to blend
blend shadow
shadow with
with ground
ground texture
texture
–– Just
Just blending
blending creates
creates “double
“double blends”
blends”
–– But
But stencil
stencil testing
testing can
can eliminate
eliminate “double
“double blends”
blends”
–– Specular
Specular highlights
highlights can
can show
show in
in shadow
shadow
Planar Projected
Shadow Artifacts
Bad Good

extends off
ground region

Z fighting double blending


The Shadow Volume
Concept
Volumetric shadows, not just planar
•• A
A single
single point
point light
light source
source splits
splits the
the world
world in
in two
two
–– shadowed
shadowed regions
regions
–– unshadowed
unshadowed regions
regions
•• A
A shadow
shadow volume
volume is
is the
the boundary
boundary between
between these
these shadowed
shadowed
and
and unshadowed
unshadowed regions
regions
•• First
First described
described by
by [Crow
[Crow 77]
77]
Visualizing
Shadow Volumes
Occluder
Occluder and
and light
light projects
projects aa shadow
shadow volume
volume
Shadow Volume
Result
Objects within the volume are shadowed
Shadow Volume
Algorithm
High -level view
High-level view of the
the algorithm
algorithm
•• Given
Given the
the scene
scene and
and aa light
light source
source position,
position, determine
determine the
the
shadow
shadow volume
volume (harder
(harder than
than itit sounds)
sounds)
•• Render
Render the
the scene
scene in
in two
two passes
passes
–– Draw
Draw scene
scene with
with the
the light
light enabled
enabled,,
updating
updating only
only fragments
fragments in in unshadowed
unshadowed region
region
–– Draw
Draw scene
scene with
with the
the light
light disabled
disabled,,
updated
updated only
only fragments
fragments in in shadowed
shadowed region
region
•• But
But how
how to
to control
control update
update of
of regions?
regions?
2D Cutaway of a
Shadow Volume
surface outside
shadowing shadow volume
object (illuminated)
light
source shadow
volume
(infinite extent)

eye
position surface inside
partially
shadowed shadow volume
object (shadowed)
Tagging Pixels as
Shadowed or Unshadowed
Using stencil testing
•• High -level algorithm
High-level algorithm does
does not
not say
say how
how to
to update
update only
only either
either pixels
pixels
in
in or
or out
out of
of the
the shadow
shadow volume!
volume!
•• The
The stenciling
stenciling approach
approach
–– Clear
Clear stencil
stencil buffer
buffer to
to zero
zero and
and depth
depth buffer
buffer to
to 1.0
1.0
–– Render
Render scene
scene to
to leave
leave depth
depth buffer
buffer with
with closest
closest Zs
Zs
–– Render
Render shadow
shadow volume
volume into
into frame
frame buffer
buffer with
with depth
depth testing
testing but
but
without
without updating
updating color
color and
and depth, but inverting
depth, but inverting aa stencil
stencil bit
bit
–– This
This leaves
leaves stencil
stencil bit
bit set
set within
within shadow!
shadow!
Stencil Inverting of
Shadow Volume
Why it works
shadowing
light object
source
two inverts, left zero

one invert, left one

eye zero inverts, left zero


position
Visualizing Stenciled
Shadow Volume Tagging
Shadowed scene Stencil buffer

red = stencil value of 1


green = stencil value of 0
Computing
Shadow Volumes
Harder than you might think
•• Easy
Easy for
for aa single
single triangle,
triangle, just
just project
project out
out three
three infinite
infinite
polygons
polygons from
from the
the triangle,
triangle, opposite
opposite the
the light
light position
position
•• But
But shadow
shadow volume
volume polygons
polygons should
should not
not intersect
intersect each
each other
other
for
for invert
invert trick
trick to
to work
work
–– This
This makes
makes things
things hard
hard
•• For
For complex
complex objects,
objects, projecting
projecting object’s
object’s 2D
2D silhouette
silhouette is
is aa
good
good approximation
approximation (flat
(flat objects
objects are
are easy)
easy)
•• Static
Static shadow
shadow volumes
volumes can
can be
be pre -compiled
pre-compiled
For Shadow Volumes
With Intersecting Polygons
Use a stencil enter/leave counting approach
•• Draw
Draw shadow
shadow volume
volume twice
twice using
using face
face culling
culling
–– 1st
1st pass:
pass: render
render front
front faces
faces and
and increment
increment when
when depth
depth
test
test passes
passes
–– 2nd
2nd pass:
pass: render back faces
render back faces and
and decrement
decrement when
when depth
depth
test
test passes
passes
•• This
This two -pass way
two-pass way is
is more
more expensive
expensive than
than invert
invert
•• And
And burns
burns more
more fill
fill rate
rate drawing
drawing shadow
shadow volumes
volumes
•• Inverting
Inverting is
is better
better ifif no
no polygon
polygon intersections
intersections
Why Increment/Decrement
Works
Example in 2D

light shadowing object


source

zero +1
zero

+2 +2
eye +1 +3
position
Shadow Volume
Issues
Practical considerations [Bergeron 86]
•• IfIf eye
eye is
is in
in shadow
shadow volume,
volume, need
need to
to determine
determine this
this and
and flip
flip
enabled
enabled & & disabled
disabled lighting
lighting passes
passes
•• Shadow
Shadow volume
volume only
only as
as good
good as
as its
its tessellation
tessellation
–– Shadows
Shadows tend
tend to
to magnify
magnify limited
limited tessellation
tessellation of
of curved
curved
surfaces
surfaces
•• Must
Must cap
cap the
the shadow
shadow volume’s
volume’s intersection
intersection with
with the
the near
near
clipping
clipping plane
plane
•• Open
Open models
models and
and non -planar polygons
non-planar polygons
Reconstructing Shadow
Volume From a Depth Buffer
Very clever idea [McCool 98]
•• Render
Render scene
scene from
from light
light source
source with
with depth
depth testing
testing
•• Read
Read back
back the
the depth
depth buffer
buffer
•• Use
Use computer
computer vision
vision techniques
techniques to
to reconstruct
reconstruct the
the shadow
shadow
volume geometry
volume geometry from
from the
the depth buffer image
depth buffer image
•• Very
Very reasonable
reasonable results
results for
for complex
complex scenes
scenes
Multiple Lights and
Shadow Volumes
Requires
Requires still
still more
more rendering
rendering passes,
passes,
but
but can
can work!
work!

Shadows from different


light sources overlap
correctly
Shadow Volumes from
Area Light Sources
Make soft shadows
•• Shadow
Shadow volumes
volumes work
work for
for point
point light
light sources
sources
•• Area
Area light
light sources
sources are
are common
common and
and make
make soft
soft shadows
shadows
•• Model
Model an
an area
area light
light source
source as
as aa collection
collection of
of point
point light
light
sources
sources [Brotman
[Brotman & & Badler
Badler 84]
84]
•• Use
Use accumulation
accumulation buffer
buffer or
or additive
additive blending
blending to
to accumulate
accumulate
soft
soft shadow
shadow
•• Linear
Linear cost
cost per
per shadow
shadow volume
volume sample
sample
Soft Shadow Example
Eight samples (more would be better)

Note the banding


artifacts
Combined
Shadow Approaches
Two Approaches at Once
just logo shadow volume

logo lacks shadow


on the teapot

just planar projected


teapot lacks
shadows
shadow
on the floor
combined
approach
Pre-computed
Shadow Textures
Lightmaps are static shadow textures
•• Compute
Compute lightmaps
lightmaps off-line with
off-line with radiosity
radiosity solver
solver
–– local
local lighting
lighting models
models evaluated
evaluated on
on tight
tight grid
grid can
can work
work well
well
too
too
•• Lightmaps
Lightmaps with
with soft
soft shadows
shadows cancan be
be built
built faster
faster with
with
convolution
convolution approach,
approach, accelerated
accelerated using
using the
the Fast
Fast Fourier
Fourier
Transform
Transform [Soler
[Soler &
& Silion
Silion 98]
98]
•• Pre -computed shadow
Pre-computed shadow textures
textures work
work well
well for
for building
building interior
interior
with
with lots
lots of
of diffuse
diffuse surfaces
surfaces
Rendering
Soft Shadow Textures
Fast soft shadows [Heckbert & Herf 96]
•• Render
Render aa shadow
shadow texture
texture
–– For
For each
each shadowed
shadowed polygon,
polygon, project
project all
all light -source-occluding
light-source-occluding
polygons
polygons into
into the
the shadowed
shadowed polygon’s
polygon’s plane
plane
** Similar
Similar to
to projected
projected planar
planar shadows
shadows
** Model
Model area
area light
light sources
sources as
as multiple
multiple jittered
jittered points
points
** Use
Use additive
additive blending
blending to
to accumulate
accumulate all
all contributions
contributions
•• Copy
Copy resulting
resulting image
image to
to the
the shadow
shadow texture
texture
•• Draw
Draw polygon
polygon in
in final
final scene
scene with
with shadow
shadow texture
texture
Rendered
Soft Shadow Texture Example
Shadow Texture and Final Result

Shadow texture, accumulation Final resulting scene, shadow


of nine jittered occluder texture modulated with
projections wooden floor
Hardware
Shadow Mapping
Shadowing with a depth map texture
•• Software
Software version
version described
described by
by [Reeves
[Reeves 89]
89]
•• Requires
Requires hardware
hardware support
support [Segal,
[Segal, et.al.
et.al. 92]
92] for
for
–– A
A “depth
“depth texture”
texture” (see
(see SGIX_depth_texture)
SGIX_depth_texture)
–– A
A “depth
“depth compare”
compare” texture
texture filter
filter operator
operator
(see
(see SGIX_shadow)
SGIX_shadow)
•• Uses
Uses eye -linear texgen
eye-linear texgen to
to generate
generate texture
texture coordinates
coordinates
(s/q,t/q,r/q)
(s/q,t/q,r/q) in
in light’s
light’s coordinate
coordinate space
space
Hardware
Shadow Mapping Algorithm
Two
Two pass
pass approach
approach
•• First,
First, render
render scene
scene from
from light’s
light’s point-of-view with
point-of-view with depth
depth testing
testing
–– Copy
Copy the
the depth
depth buffer
buffer to
to aa “depth
“depth texture”
texture”
•• Second,
Second, render
render scene
scene from
from eye’s
eye’s point -of-view as
point-of-view as follows
follows
–– Configure
Configure eye -linear texgen
eye-linear texgen to to generate
generate (s,t,r,q)
(s,t,r,q) coordinates
coordinates in
in light’s
light’s
coordinate
coordinate space
space (“r”
(“r” is
is zz-planar
-planar distance
distance to
to the
the light)
light)
–– Configure
Configure special
special shadow
shadow texture
texture filter
filter operator
operator
–– Texture
Texture filter
filter tests
tests ifif “r”
“r” is
is less
less than
than depth
depth texture
texture texel
texel
–– Texture
Texture color
color is
is one
one ifif tests
tests passes,
passes, zero
zero ifif test
test fails
fails
–– Modulate
Modulate fragment
fragment color
color with
with one
one or
or zero
zero texture
texture
Hardware
Shadow Mapping Issues
Why it works and problems
•• Shadow
Shadow map
map texturing
texturing is
is effectively
effectively aa second
second occlusion
occlusion test
test
done
done with
with respect
respect to
to the
the light
light
–– Modulate
Modulate with
with zero
zero ifif inside
inside shadow
shadow
–– Modulate
Modulate with
with one
one ifif outside
outside shadow
shadow
•• Traditional
Traditional mipmap
mipmap texture
texture filtering
filtering not
not suited
suited for
for filtering
filtering aa
depth
depth map
map
•• Explicit
Explicit hardware
hardware support
support currently
currently limited
limited to
to InfiniteReality
InfiniteReality
and
and RealityEngine
RealityEngine
Transparency
See -through objects
See-through
•• Multiple
Multiple objects
objects contribute
contribute to
to pixel’s
pixel’s final
final color
color
•• Order
Order of
of objects
objects relative
relative to
to view
view affects
affects color
color
•• Unsorted
Unsorted depth
depth buffering
buffering insufficient
insufficient for
for hidden
hidden surface
surface
removal
removal
•• Internal
Internal structure
structure of
of object
object geometry
geometry can
can become
become visible
visible
•• With
With caveats,
caveats, alpha
alpha blending
blending enables
enables transparency
transparency
Some Blending is
Commutative
Order independent blends
•• Filter
Filter blending
blending
glBlendFunc(GL_DST_COLOR,
glBlendFunc(GL_DST_COLOR, GL_ZERO)
GL_ZERO)
•• Additive
Additive blending
blending
glBlendFunc(GL_ONE,
glBlendFunc(GL_ONE, GL_ONE)
GL_ONE)
•• But
But transparency
transparency blending
blending is
is non -commutative
non-commutative
glBlendFunc(GL_SRC_ALPHA,
glBlendFunc(GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA)
GL_ONE_MINUS_SRC_ALPHA)
Transparency Ordering
Rendering order matters

Orange sphere Blue cone Depth testing


in front of blue in front of orange can mess up transparency.
cone. sphere. WRONG: Overlap
Overlap should be Overlap should be should
more orange. more blue. be blended!
Sorting for
Transparency
Ensuring proper blending ordering
•• Draw
Draw all
all opaque
opaque objects
objects first
first
•• When
When transparent
transparent object
object do
do not
not intersect,
intersect,
back -to-front sort
back-to-front sort of
of transparent
transparent objects
objects works
works
–– Inexact
Inexact sort
sort may
may be
be acceptable
acceptable
•• When
When transparent
transparent objects
objects intersect,
intersect, must
must subdivide
subdivide and
and sort
sort
intersecting
intersecting primitives
primitives
Sorting Optimizations
Transparency shortcuts
•• Take
Take advantage
advantage of
of face
face culling
culling
–– Draw
Draw back
back faces,
faces, then
then draw
draw front
front faces
faces
–– Works
Works for
for non -overlapping closed
non-overlapping closed convex
convex objects
objects
•• Disable
Disable depth
depth testing
testing
–– Prevents
Prevents front
front faces
faces from
from hiding
hiding back
back faces
faces
–– For
For closed,
closed, convex,
convex, constant -colored objects,
constant-colored objects, order
order of
of
front
front and
and back
back face
face drawing
drawing does
does not
not matter
matter
Screen Door
Transparency Example
Stipple simulates semi -opacity
semi-opacity
Screen Door
Transparency Implementation
Polygon Stipple
•• OpenGL
OpenGL supports
supports 32x32
32x32 binary
binary stipple
stipple pattern
pattern
•• Percentage
Percentage of
of 1’s
1’s in
in screen
screen door
door pattern
pattern should
should match
match the
the
object’s
object’s opacity
opacity percentage
percentage
•• Different
Different objects
objects should
should use
use distinct
distinct patterns
patterns
•• Limitations
Limitations
–– Polygon
Polygon stipple
stipple only
only applies
applies to
to polygons
polygons
–– Stipple
Stipple pattern
pattern tied
tied to
to window
window origin
origin
Screen Door Transparency
With Alpha Textures
Alternative to polygon stipple
•• Place
Place screen
screen door
door pattern
pattern in
in alpha
alpha texture
texture
•• Generate
Generate window -space texture
window-space texture coordinates
coordinates
–– Enable GL_EYE_LINEAR texgen
Enable GL_EYE_LINEAR texgen with
with texgen
texgen plane
plane equations
equations
mimicking
mimicking the
the projection
projection &
& viewport
viewport transforms
transforms
–– Projective
Projective texturing’s
texturing’s division
division by
by “q”
“q” will
will mimic
mimic vertex
vertex coordin ate’s
coordinate’s
division
division by
by “w”
“w”
•• Use
Use alpha
alpha testing
testing to
to discard
discard fragments
fragments based
based on
on screen
screen door
door
alpha
alpha texture
texture
•• Applies
Applies to
to both
both lines
lines and
and polygons
polygons
Multiple Levels of
Screen Door Transparency
Opacity control

18% 25%

56% 25%

93% 25%

Right image shows no sense of transparency between objects!


Screen door transparency is defeated if multiple objects
share the same stipple pattern.
Sub-pixel
Screen Door Transparency
Multisample masking
SGIS_multisample extension
•• SGIS_multisample extension supports
supports multiple
multiple color
color
samples
samples per
per pixel
pixel
–– SGI
SGI high -end only
high-end only feature
feature for
for antialiasing
antialiasing
glSampleMaskSGIS specifies
•• glSampleMaskSGIS specifies aa multisample
multisample sub-pixel
sub-pixel
screen
screen door
door pattern
pattern
–– enabled
enabled via
via glEnable(GL_SAMPLE_MASK_SGIS)
glEnable(GL_SAMPLE_MASK_SGIS)
•• Because
Because sub -pixel, stipple
sub-pixel, stipple pattern
pattern is
is not
not visible
visible
•• Limited
Limited number
number of
of samples
samples (4
(4 or
or 88 typical)
typical)
Atmospheric Effects
Fog, Haze, Smoke
•• As
As light
light travels
travels through
through an
an atmosphere
atmosphere
–– Some
Some light
light is
is extinguished
extinguished (absorbed)
(absorbed)
–– Some
Some light
light may
may also
also be
be added
added due
due to
to scattering
scattering
•• An
An atmospheric
atmospheric model
model tells
tells how
how the
the atmosphere
atmosphere affects
affects the
the
appearance
appearance of
of objects
objects viewed
viewed through
through the
the atmosphere
atmosphere
Fog Example
Common in Games and Visual Simulation
OpenGL’s Built-in
Fog Model
Assumptions
•• Uniform
Uniform fog
fog color
color (“color”
(“color” of
of the
the atmosphere)
atmosphere)
•• Uniform
Uniform fog
fog density
density throughout
throughout scene
scene
•• Fog
Fog is
is essentially
essentially aa weighted
weighted blend
blend
C = f Crr + (1 - f) Cff
where
where C Crr == pre -fog fragment
pre-fog fragment color,
color, C
Cff == uniform
uniform fog
fog color,
color,
and
and ff == fog
fog factor
factor
•• Fog
Fog factor
factor computed
computed per -fragment based
per-fragment based on
on
eye -distance of
eye-distance of given
given fragment
fragment
Computing the
Fog Factor
Based on Beer’s Law
•• Fog
Fog factor
factor has
has an
an exponential
exponential drop -off with
drop-off with the
the distance
distance from
from
the
the viewer
viewer

f = e--(d
(d z)
z)

•• Intuition:
Intuition: Clarity
Clarity is
is 100%
100% atat 00 meters
meters,, but at xx ++ 10
but at 10 meters
meters is
is
99%
99% of of the
the clarity at xx meters
clarity at meters
at 30
–– at 30 meters
meters,, the
the clarity
clarity is (0.99 ×× 0.99
is (0.99 0.99 ×× 0.99)%
0.99)%
–– continuous
continuous version
version of
of this
this idea
idea is
is an
an exponential
exponential drop -off
drop-off
Standard
OpenGL Fog Models
Fog factor equations
2
GL_EXP2 f = e-(d z)

f GL_EXP f = e-(d z)

Zeye

Given the fog model (EXP2 or EXP), fog density (d),


and Z eye-distance (z), compute fog factor (f)
OpenGL Fog
Limitations
•• Emissive
Emissive objects
objects are
are over-attenuated
over-attenuated
–– Headlights
Headlights should
should break
break through
through the
the fog,
fog, but
but don’t
don’t
•• OpenGL
OpenGL permits
permits implementations
implementations to to use
use window
window ZZ (a
(a planar
planar
distance)
distance) instead
instead of
of the
the radial
radial eye -distance
eye-distance
–– Objects
Objects at
at sides
sides of
of the
the view
view frustum
frustum under -fogged
under-fogged
•• Real
Real world
world fog
fog is
is not
not so
so uniform
uniform
–– Fog
Fog varies
varies with
with altitude,
altitude, patchy
patchy fog
fog
Planar Eye-distance Fog
Artifacts
Why they occur

B planar
eye-distance

A
radial
eye
eye-distance
position view
frustum

Points A and B have the same Z planar distance


but different radial distances.
Texture Fog Methods
Textures to encode fog
•• 1D,
1D, 2D,
2D, or
or 3D
3D texture
texture as
as fog
fog lookup
lookup table
table
–– Map
Map eye
eye position
position to
to texture
texture coordinates
coordinates with
with GL_EYE_LINEAR
GL_EYE_LINEAR
texgen
texgen
–– Use
Use luminance
luminance texture
texture with GL_BLEND texture
with GL_BLEND texture environment
environment and
and
set
set fog
fog color in texture
color in texture environment
environment color
color
–– Or
Or an
an RGBA
RGBA texture
texture with GL_DECAL mode
with GL_DECAL mode can
can encode
encode the
the fog
fog
factor
factor in
in A
A and
and the
the fog
fog color
color in
in RGB
RGB
OpenGL Fog Coordinate
Extension
User-supplied per
User-supplied -vertex fog distance
per-vertex
•• Normally,
Normally, OpenGL
OpenGL computes
computes the
the fog
fog distance
distance automatically
automatically
•• EXT_fog_coord
EXT_fog_coord extension
extension allows
allows application
application to
to supply
supply the
the
fog
fog distance
distance at
at each
each vertex
vertex via
via glFogCoord1fEXT
glFogCoord1fEXT
–– OpenGL
OpenGL interpolates
interpolates fog
fog distance
distance
–– and
and computes
computes the
the fog
fog factor
factor per-fragment using
per-fragment using OpenGL
OpenGL
fog
fog mode
mode (exponentiates)
(exponentiates)
A More Complex
Fog Model
Height-based fog
Height-based
•• Described
Described by
by [Perlin
[Perlin 85,
85, Legakis
Legakis 98]
98]
•• Height-based fog
Height-based fog factor
factor equation
equation
b
f=e --∫∫ α
α (t) dt
a(t) dt

•• The
The extension rate α
extension rate α (density)
(density) is
is integrated
integrated over
over the
the optical
optical
path
path from
from point
point AA to
to point
point B
B
–– This
This integral
integral is
is the
the optical
optical depth
depth
Assume α
•• Assume α varies
varies solely
solely with
with height
height (altitude)
(altitude)
Height-based Fog
Scenario
Consider the optical depth from A to B

Y2 A = eye position
C B
B = fogged position
∆Y
C = position above A
Y1 at height of B
A

D1 D1
∆D

∆D = √ (Bx - Ax)2 + (Bz - Az)2


Height-based
Optical Depth
Nice solution, good for glFogCoord
b  ∆D • α(Y1) , ∆Y = 0
∫ a α(t) dt =  Y2
 √ 1 + ( ∆D / ∆Y )2 •, ∫ α(y) dy , ∆Y ≠ 0
Y1

Y2 Y2 Y1
∫ α(y) dy = ∫ α(y) dy -
-∞
∫ α(y) dy
Y1 -∞

This can be implemented as two 1D table lookups.


Height-based
Fog Examples
Foggy city normal uniform fog

layered fog layered fog

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy