Modeling of worlds A complicated object be decomposed into simple objects and represented by...
-
Upload
damian-bradley -
Category
Documents
-
view
215 -
download
0
Transcript of Modeling of worlds A complicated object be decomposed into simple objects and represented by...
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 cityCP411 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
CP411 Hidden face removing 6
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 7
Visibility and Hidden surface removing
1. Visibility and visibility issues
2. Back-face method (to be implemented)
3. Depth sort method (to be implemented)
4. Depth-buffer method (implemented in OpenGL, to be used)
5. A-buffer method
6. Scan line method
7. Area-division method
8. BSP-tree method
CP411 Hidden face removing 8
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 9
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 10
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
databasesIn many complex worlds, few things are visible
• How much of the real world can you see at any moment?
CP411 Hidden face removing 11
2. Back Face method• Back faces are the surfaces facing at opposite directions from the
viewpoint.
• 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
Ax + By + Cz + D
= 0
(ex, ey, ez)
CP411 Hidden face removing 12
Eye positionReference point
0 ( , , )eye eye eyeP x y z( , , )ref ref ref refP x y z
0 0 0 0( , , )P x y z
1 1 1 1( , , )P x y z
2 2 2 2( , , )P x y z
3 3 3 3( , , )P x y z
N
How to detect if a face is a back face
0eye refN P P ������������� �
( , , )P x y z
A face is a back face if
0eyeN P P ������������� �
Using this condition for back face detection in A3 will give a better performance. Credits to Alasdair and Derek
CP411 Hidden face removing 13
0 1 1 2
1 0 1 0 1 0 2 1 2 1 2 1
1 0 1 0 1 0 1 0 1 0 1 01 0 1 0 1 0
2 1 2 1 2 1 2 1 2 1 2 12 1 2 1 2 1
1 0 2 1 2 1 1 0
1
( , , ) ( , , )
( , , )
(( )( ) ( )( ),
(
N P P PP
x x y y z z x x y y z z
i j ky y z z z z x x x x y y
x x y y z zy y z z z z x x x x y y
x x y y z z
y y z z y y z z
z
�������������������������� ��
0 2 1 2 1 1 0
1 0 2 1 2 1 1 0
1 0 2 1 2 1 1 0
1 0 2 1 2 1 1 0
)( ) ( )( ),
( )( ) ( )( ))
( , , )
[( )( ) ( )( )]( )
[( )( ) ( )( )](
eye ref ref eye ref eye ref eye
eye ref ref eye
z x x z z x x
x x y y x x y y
P P x x y y z z
N P P y y z z y y z z x x
z z x x z z x x y
��������������
������������� �
1 0 2 1 2 1 1 0
)
[( )( ) ( )( )]( )
ref eye
ref eye
y
x x y y x x y y z z
CP411 Hidden face removing 14
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 15
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 16
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 project of PeyeP on PeyePref
Peye
Pref
vz vx
vy
P
d
CP411 Hidden face removing 17
0 ( , , )eye eye eyeP x y z( , , )ref ref ref refP x y z
0 0 0 0( , , )P x y z
1 1 1 1( , , )P x y z
2 2 2 2( , , )P x y z
3 3 3 3( , , )P x y z
N
How to calculate the Z-value
| |eye ref eye
eye ref
P P P Pd
P P
����������������������������
��������������The z-value of P is equal to
( , , )P x y z
d
CP411 Hidden face removing 18
2 2 2
| |
( , , ) ( , , )
( ) ( ) ( )
( )( ) ( )( ) ( )(
eye ref eye
eye ref
ref eye ref eye ref eye eye eye eye
ref eye ref eye ref eye
ref eye eye ref eye eye ref eye eye
P P P Pd
P P
x x y y z z x x y y z z
x x y y z z
x x x x y y y y z z z z
����������������������������
��������������
2 2 2
)
( ) ( ) ( )ref eye ref eye ref eyex x y y z z
CP411 Hidden face removing 19
• 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– S and S’ do not intersect in the
image plane
SS’
S
S’or
z
x
SS’
z
x
SS’
SS’
CP411 Hidden face removing 20
• 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 21
• 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
zs
xs
Fails
Which point for choosing ordering?
CP411 Hidden face removing 22
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.
• 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.
One pixel of the screen
CP411 Hidden face removing 23
• 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
0
2
0
1 2
1
. . .
.
.
.
FrameBuffer
VideoController
MonitorDisplayProcessor
Depth Buffer
DepthBuffer
Frame Buffer
CP411 Hidden face removing 24
• 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 25
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 26
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 27
glutSolidSphere( 1.0, 20, 20);
CP411 Hidden face removing 28
glutSolidTeapot( 1.5);
CP411 Hidden face removing 29
• 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 30
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 31
• 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
over =
CP411 Hidden face removing 32
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 33
zs
xs
zs
xs
zs
xs
Spans
Where polygons overlap, draw front polygon
CP411 Hidden face removing 34
• 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
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
CP411 Hidden face removing 37
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 38
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 39
Warnock’s Algorithm • Regions labeled with case used to classify them:1) One polygon in front2) Empty3) 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
2 2 2
2222
2
2
3
3
3
3 33
3
3
3
3
3
333
3
3
1
1 1 11
CP411 Hidden face removing 40
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 41
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
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-to-leaf 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. A-buffer method
5. Scan line method
6. Area-division method
7. BSP-tree methodCP411 Hidden face removing 53