Lesson 7 3-D Graphics

Download Report

Transcript Lesson 7 3-D Graphics

Modeling of worlds
• A complicated object be decomposed into
simple objects and represented by hierarchical
model
• A world scene usually contain many objects
• If position each objects are relatively fixed, then
the world can be rendered by drawing each
object.
Eg. the buildings in a city
CP411 Hidden face removing
1
Dynamic world with many objects
• What if the objects are moving and their
relative positions are changing?
E.g. solar system, animations
• How do we remember and get the current
position of each object?
CP411 Hidden face removing
2
Method one
• Remember the current position of all vertices of the
object.
• E.g. when cube is moving remember the current
coordinates of each object
• Pro:
• Con:
• Works when an object has a few vertices
• Bad when an object contains many object
CP411 Hidden face removing
3
Method two
• Use the model coordinate system, ie. the (Ux, Uy, Uz, O)
or MC, the MC->WC transformation matrix to remember
the position.
• When the an object is moving to a new position, apply
the same transformation matrix to MC to get the new
MC. Then new MC represents the new MC->WC
transformation matrix and the object can be rendered
with CTM * MC
• Pro :
• Con:
CP411 Hidden face removing
4
MC normalization
• The vectors in MC are unit vectors and
orthogonal with each other.
• After a large number of transformations,
the MC begins to deform due to error
accumulation
• How to fix?
CP411 Hidden face removing
5
The drawing does not look right!
What happens?
A face drawn earlier is
overwritten by other faces drawn
later, even though it is closer to
the viewer than the others
How can we solve the problem?
CP411 Hidden face removing
6
Visibility and Hidden surface removing
1.
2.
3.
4.
Visibility and visibility issues
Back-face method (to be implemented)
Depth sort method (to be implemented)
Depth-buffer method
(implemented in OpenGL, to be used)
5.
6.
7.
8.
A-buffer method
Scan line method
Area-division method
BSP-tree method
CP411 Hidden face removing
7
1. Visibility
• Given a set of polygons, determine which
is visible at each pixel?
– Also called hidden surface removal
• Many approaches were invented. Two
major classes:
– Object-space method: computations that
operate on primitives
– Image-space method: computations at
the pixel level
CP411 Hidden face removing
8
Space and Depth
• All the spaces in the viewing pipeline
maintain depth, so we can work in any
space
– World, View and Canonical Screen
spaces might be used
– Depth can be updated on a per-pixel
basis as we scan convert polygons or
lines
CP411 Hidden face removing
9
Issues with visibility
• Efficiency
– it is slow to overwrite pixels, or scan convert things that cannot
be seen
• Accuracy
– answer should be right, and behave well when the viewpoint
moves
• Complexity
– object precision visibility may generate many small pieces of
polygon
– Must have technology that handles large, complex rendering
databases
In many complex worlds, few things are visible
• How much of the real world can you see at any moment?
CP411 Hidden face removing
10
2. Back Face method
• Back faces are the surfaces facing at opposite directions from the
viewpoint.
(ex, ey, ez)
Ax + By + Cz + D
=0
• A surface is a back face if the viewpoint is on the inside of the
surface, ie, Aex + Bey + Cez + D < 0.
• The test will even be simpler after the viewing transformation
CP411 Hidden face removing
11
How to detect if a face is a back face
P2 ( x2 , y2 , z2 )
N
P3 ( x3 , y3 , z3 )
P( x, y, z)
P1 ( x1 , y1 , z1 )
P0 ( x0 , y0 , z0 )
Pref ( xref , yref , zref )
Reference point
A face is a back face if
P0 ( xeye , yeye , zeye )
Eye position
N  Peye Pref  0
Using this N  Peye P  0 condition for back face detection in A3 will give
a better performance. Credits to Alasdair and Derek
CP411 Hidden face removing
12
N  P0 P1  P1 P2
 ( x1  x0 , y1  y0 , z1  z0 )  ( x2  x1 , y2  y1 , z2  z1 )
i
j
k
 x1  x0
y1  y0
z1  z0  (
x2  x1
y2  y1
z2  z1
y1  y0
y2  y1
z1  z0 z1  z0
,
z2  z1 z2  z1
x1  x0 x1  x0
,
x2  x1 x2  x1
y1  y0
y2  y1
)
 (( y1  y0 )( z2  z1 )  ( y2  y1 )( z1  z0 ),
( z1  z0 )( x2  x1 )  ( z2  z1 )( x1  x0 ),
( x1  x0 )( y2  y1 )  ( x2  x1 )( y1  y0 ))
Peye Pref  ( xref  xeye , yref  yeye , zref  zeye )
N  Peye Pref  [( y1  y0 )( z2  z1 )  ( y2  y1 )( z1  z0 )]( xref  xeye )
[( z1  z0 )( x2  x1 )  ( z2  z1 )( x1  x0 )]( yref  yeye )
[( x1  x0 )( y2  y1 )  ( x2  x1 )( y1  y0 )]( zref  zeye )
CP411 Hidden face removing
13
Back face detection algorithm with OpenGL
• If an object is opaque, back faces are obstructed by the object itself and
thus are not visible from the viewer.
• In default mode, OpenGL draws both front face and back face as filled
polygons. This is the reason why the preceding drawing of the cube did
not look right.
• OpenGL can be set to display front faces as filled polygons and back
faces as wire frames.
glPolygonMode( GL_FRONT, GL_FILL);
glPolygonMode( GL_BACK, GL_LINE);
• A more thorough solution is not to display the back faces at all.
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
CP411 Hidden face removing
14
2. Depth Sorting (Painters) Method
• Algorithm:
– Choose an order for the polygons based on some
choice (e.g. depth to a point on the polygon)
– Render the polygons in that order, deepest one first
• An example of a list-priority algorithm
– Sort polygons on depth of some point
– Render from back to front (modifying order on the fly)
– Rendering: For surface S with greatest depth
• If no overlap in depth with other polygons, scan convert
• Else, for overlaps in depth, test for overlaps in the image plane
– If none, scan convert and go to next polygon
• If S, S’ overlap in depth and in image plane, swap order and try
again
• If S, S’ have been swapped already, split and reinsert
CP411 Hidden face removing
15
The depth of a point
• The depth of a point is the z value of in
view coordinate system
• The z value d of P (the depth of P) is
equal to the length of
P
project of PeyeP on PeyePref
vy
Pref
d
vx
vz
Peye
CP411 Hidden face removing
16
How to calculate the Z-value
P2 ( x2 , y2 , z2 )
N
P3 ( x3 , y3 , z3 )
P( x, y, z)
P1 ( x1 , y1 , z1 )
P0 ( x0 , y0 , z0 )
d
P0 ( xeye , yeye , zeye )
Pref ( xref , yref , zref )
The z-value of P is equal to
d
Peye Pref  Peye P
| Peye Pref |
CP411 Hidden face removing
17
d


Peye Pref  Peye P
| Peye Pref |
( xref  xeye , yref  yeye , zref  zeye )  ( x  xeye , y  yeye , z  zeye )
( xref  xeye ) 2  ( yref  yeye ) 2  ( zref  zeye ) 2
( xref  xeye )( x  xeye )  ( yref  yeye )( y  yeye )  ( zref  zeye )( z  zeye )
( xref  xeye ) 2  ( yref  yeye ) 2  ( zref  zeye ) 2
CP411 Hidden face removing
18
• The depth of a polygon can be defined to be the depth of its center
• Overlap of polygons is a problem with painter’s algorithm
• Testing for overlaps: Start drawing when first condition is met:
–
–
–
–
x-extents or y-extents do not overlap
S is behind the plane of S’
S’ is in front of the plane of S
z
S and S’ do not intersect in the
image plane
S
S
S’
or
S’
S
S’
x
z
S’
S
x
S’
S
CP411 Hidden face removing
19
• Advantages:
– Filter anti-aliasing works fine
• Composite in back to front order with a sequence
of over operations
– No depth quantization error
• Depth comparisons carried out in high-precision
view space
• Disadvantages:
– Over-rendering
– Potentially very large number of splits - (n2)
fragments from n polygons
CP411 Hidden face removing
20
• This renders nearer
polygons over further
• Difficulty:
– works for some
important geometries
(2.5D - e.g. VLSI)
– doesn’t work in this
form for most
geometries - need at
least better ways of
determining ordering
Fails
zs
CP411 Hidden face removing
Which
point for
choosing
ordering?
xs
21
3. The depth-buffer (Z buffer) algorithm
• When a new surface is drawn, a pixel value will be updated only if
the part of the surface corresponding to the pixel is closer to the
viewer than the surface currently displayed by the pixel.
One pixel of the screen
• Suppose that the cyan face in the middle is drawn first. The pixel
corresponding to the line of sight shown is set to cyan. Next, when
the green face is drawn, the pixel will not be updated. Finally
when the red triangle is drawn, the pixel is set to red.
CP411 Hidden face removing
22
• For each pixel on screen, have at least two buffers
– Color buffer stores the current color of each pixel
• The thing to ultimately display
– Z-Buffer stores at each pixel the depth (distance) of the nearest
thing seen so far
Display
Processor
Frame
Buffer
Depth
Buffer
Video
Controller
Monitor
012...
Frame
Buffer
0
1
2
.
.
.
Depth
Buffer
CP411 Hidden face removing
23
• Initialize this buffer to a value corresponding to the furthest point
• As a polygon is filled in, compute the depth value of each pixel that
is to be filled
– if depth < z-buffer depth, fill in pixel color and new depth
– else disregard
CP411 Hidden face removing
24
The depth-buffer algorithm
For each pixel at (x, y) of the screen //Initialization
color( x, y) = background_color;
depth( x, y) = ;
Draw each surface one by one
For each pixel that covered by the projection of the surface
(suppose the pixel is at (x, y) )
z = the depth of the surface corresponding to the pixel;
if (z < depth( x, y) ) {
color( x, y) = the color of the surface;
depth( x, y) = z;
}
CP411 Hidden face removing
25
OpenGL Depth Buffer Functions
• OpenGL defines a depth buffer as its visibility algorithm
– To set up the depth buffer testing (in main() )
glutInitDisplayMode(GLUT_DEPTH);
– The enable depth testing:
glEnable(GL_DEPTH_TEST);
– To clear the depth buffer
glClear(GL_DEPTH_BUFFER_BIT);
– To clear color and depth:
–
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
– The comparison function can be specified:
glDepthFunc(…);
• For efficiency, we shall enable back-face culling as well.
CP411 Hidden face removing
26
glutSolidSphere( 1.0, 20, 20);
CP411 Hidden face removing
27
glutSolidTeapot( 1.5);
CP411 Hidden face removing
28
• Advantages:
– Simple and now ubiquitous in hardware
• A z-buffer is part of what makes a graphics card “3D”
– Computing the required depth values is simple
• Disadvantages:
– Over-renders - worthless for very large collections of polygons
– Depth quantization errors can be annoying
– Can’t easily do transparency or filtering for anti-aliasing
(Requires keeping information about partially covered polygons)
CP411 Hidden face removing
29
4. A-buffer
• Handles transparent surfaces and filter anti-aliasing
• At each pixel, maintain a pointer to a list of polygons
sorted by depth, and a sub-pixel coverage mask for each
polygon
– Matrix of bits saying which parts of the pixel are
covered
• Algorithm: When drawing a pixel:
– if polygon is opaque and covers pixel, insert into list,
removing all polygons farther away
– if polygon is transparent or only partially covers pixel,
insert into list, but don’t remove farther polygons
CP411 Hidden face removing
30
over
=
• Algorithm: Rendering pass
– At each pixel, traverse buffer using polygon colors
and coverage masks to composite:
• Advantage:
– Can do more than Z-buffer
– Coverage mask idea can be used in other visibility
algorithms
• Disadvantages:
– Not in hardware, and slow in software
– Still at heart a z-buffer: Over-rendering and depth
quantization problems
• But, used in high quality
rendering tools
CP411 Hidden face removing
31
5. Scan Line Algorithm
(Image Precision)
• Assume polygons do not intersect one another
– Except maybe at edges or vertices
• Observation: across any given scan line, the visible
polygon can change only at an edge
• Algorithm:
– fill all polygons simultaneously
– at each scan line, have all edges that cross scan line
in AEL (Active Edge List)
– keep record of current depth at current pixel - use to
decide which is in front in filling span
CP411 Hidden face removing
32
zs
zs
xs
Spans
xs
zs
Where polygons
overlap, draw
front polygon
xs
CP411 Hidden face removing
33
• Advantages:
– Simple
– Potentially fewer quantization errors (more bits
available for depth)
– Don’t over-render (each pixel only drawn once)
– Filter anti-aliasing can be made to work (have
information about all polygons at each pixel)
• Disadvantages:
– Invisible polygons clog AEL, ET (edge table)
– Non-intersection criteria may be hard to meet
CP411 Hidden face removing
34
Final exam questions
• What is the back-face method for hidden
surface removal?
• What is the painter’s method for hidden
surface removal?
• What is the difference between depth-sort
method and depth-buffer method?
CP411 Hidden face removing
35
CP411 Hidden face removing
36
6. Area Subdivision
•
Exploits area coherence: Small areas of an image are
likely to be covered by only one polygon
•
Three easy cases for determining what’s in front in a
given region:
1. a polygon is completely in front of everything else in that region
2. no surfaces project to the region
3. only one surface is completely inside the region, overlaps the
region, or surrounds the region
CP411 Hidden face removing
37
Warnock’s Area Subdivision
Start with whole image
If one of the easy cases is satisfied (previous slide), draw
what’s in front
Otherwise, subdivide the region and recurse
If region is single pixel, choose surface with smallest depth
CP411 Hidden face removing
38
Warnock’s Algorithm
2
3
3
2
3
3
2
2
3
3
3
1
3
3
3
2
2
•
3
1
1 1 1
3
3
2
•
3
•
3
3
2
2
CP411 Hidden face removing
Regions labeled with
case used to classify
them:
1) One polygon in front
2) Empty
3) One polygon inside,
surrounding or
intersecting
Small regions not
labeled
Note it’s a rendering
algorithm and a HSR
algorithm at the same
time
– Assuming you can
draw squares
39
Warnock’s Area Subdivision Method
• Advantages:
– No over-rendering
– Anti-aliases well - just recurs deeper to get sub-pixel
information
• Disadvantage:
– Tests are quite complex and slow
CP411 Hidden face removing
40
7. BSP-Trees (Object Precision)
• Construct a binary space partition tree
– Tree gives a rendering order
– A list-priority algorithm
• Tree splits 3D world with planes
– The world is broken into convex cells
– Each cell is the intersection of all the half-spaces of
splitting planes on tree path to the cell
• Also used to model the shape of objects, and in other
visibility algorithms
CP411 Hidden face removing
41
BSP Trees
Having a pre-built BSP tree will allow us to
get a correct depth order of polygons in
our scene for any point in space.
We will build a data structure based on the
polygons in the scene, that can be queried
with any point input to return an ordering
of those polygons.
Structure of a BSP Tree
• Each internal node has a +half space, a -half
space, and a list of objects contained entirely
within that plane (if any exist).
• Each leaf has a list of zero or one objects inside
it, and no sub-trees
• The size of a BSP tree is the total number of
objects stored in the leaves & nodes of the tree.
This can be larger than the number of objects in
our scene because of splitting.
The Big Picture
Assume that no objects in our space
overlap
Use planes to
recursively split our
object space, keeping
a tree structure of
these recursive splits.
Choose a Splitting Line
Choose a splitting plane, dividing our objects into
three sets – those on each side of the plane,
and those fully contained on the plane.
Choose More Splitting Lines
What do we do when an object (like object 1) is
divided by a splitting plane?
It is divided into two objects, one on each side
of the plane.
Split Recursively Until Done
When we reach a convex space containing
exactly zero or one objects, that is a leaf
node.
Continue
Continue
Finished
Once the tree is constructed, every root-toleaf path describes a single convex
subspace.
Querying the Tree
If an eye point is in the positive/negative
half-space of a plane, then everything in
the negative/positive half-space is
farther away -- so draw it first, using this
algorithm recursively
Then draw objects on the splitting plane,
and recurse into the positive/negative
half-space.
What Order Is Generated From
This Eye Point?
Q5, Q4, Q4’, Q3, Q1’, Q2, Q1
Summary
1. Back-face method
2. Depth sort method
3. Depth-buffer method
4.
5.
6.
7.
A-buffer method
Scan line method
Area-division method
BSP-tree method
CP411 Hidden face removing
53