cs3241: computer graphics - comp.nus.edu.sghcheng/cs3241/l06 - hidden... · two main approaches i....
TRANSCRIPT
Where are we? All the objects are set up in the scene
And they are polygonal models A camera (view point) is set After all the transformation (including the
ti t f ti )perspective transformation) Transform all the polygon vertices into the screen coordinates
Two Main ApproachesI. Control the drawing order of objects
Sort the order of polygons according to “depth” Draw polygons from furthest to nearest View dependent
The depth order changes when the viewpoint changes The depth order changes when the viewpoint changes
II. Draw objects in a random order Then for each pixel drawn by a new objectThen for each pixel drawn by a new object
If there is already some object drawn, decide if the new object should overwrite it or not
Various HSR Algorithms Type 1:
Depth SortingWeiler Atherton Algorithm Weiler‐Atherton Algorithm
Binary Space‐Partitioning (BSP) Tree Type 2:yp
Z‐buffer Warnock's AlgorithmR i (i h l f i ) Ray‐casting (in the lecture of ray tracing)
…. etc. All of them are performed after perspective transformation All of them are performed after perspective transformation
Except Ray‐casting
Type 1: Controlling Drawing Order However, sometime there is no possible correct order:
P R
Q Q
P
S h b k h l So, we have to break the polygons
Type 1a: Depth Sort For every pairs of polygons, check
If their bounding boxes (2D) overlap each otherh l Compute their intersections/overlapping areas
(If there is no intersections, they are not affecting each other)
If not, these two polygons are not affecting each otherIf not, these two polygons are not affecting each other
A B A
B
A B
A B
B occludes A partially
A occludes B partially
A & B are non-overlapping
B occludes A completely
Type 1a:Depth Sort Weiler‐Atherton Algorithm Computing intersections of polygons A generic polygon clipping algorithm (detail omitted) A generic polygon clipping algorithm (detail omitted) For two polygons and B, we can compute
the union of A and B the intersection of A and B A minus B B minus A B minus A
Type 1a: Depth Sorting Drawing order:
Determine which one to draw firstl “ ” l “ ” Polygon “A in B” or Polygon “B in A”
In this case, draw “A in B” first then draw “B in A” if polygon B is transparent
If B is not transparent, we can ignore or skip drawing “A in B” directly
Do not need to care about “A‐B” or “B‐A”Do not need to care about A B or B A If they do not intersect other polygons in the screen
Type 1a: Depth Sorting Advantages
Handle transparency Especially good if p y g
The order is predictable No need for polygon clipping
Disadvantages Need special care for penetrating polygons
Computational expensivel d i i n polygons produce nC2 intersections
i.e. O(n2) And for each pair of polygon, we could
produce O(m2) if each polygon has m edgesp ( ) p yg g
Type 1b: Binary Space Partitioning Subdivide the entire space by a binary tree
Each internal node is a division of a partition/space Each leaf is a part of the space with only one polygon
Di id i Divide into two stepsI. PreparationII Rendering A
B
II. RenderingC
(Use a bird’s eye view for illustration)(Use a bird s eye view for illustration)
Type 1b: BSP‐tree Preparation Initialization
Let S = the entire space Pick any polygon, say Polygon A Let HA be the hyperplane that
t i A d di id S i t t contains A and divides S into two subspace S1 and S2
A
B
C
HA
Type 1b: BSP‐tree Build a node by polygon A Break every polygon in S by HA
Build the two children of A by the two subspaces S1 and S2R il h b Repeat until the subspace contains only 1 polygon
A
B1
BC
HA
B2A
B1
Type 1b: BSP‐tree Renderingype b S t ee e de gA
B1 B2
Depending on the viewpoint p Start from the root
C1C2
For each node there is one polygon and two sub‐spaces in the two children
A
B1
B
the two children1. Draw the sub‐tree that does
NOT contains pD th l f th d
HA
B2
C2
C1
2. Draw the polygon of the node3. Draw the sub‐tree that
contains p
AType 1b: BSP‐treeB1 B2
For example, for the following viewpoints, their drawing order will be
C1C2
drawing order will be p1 : B1, A, C1, B2 , C2
p : C B C A B
A
B1
B
p2: C1, B2, C2, A, B1
p2
HA
B2
C2
C1
p1
Type 1b: BSP‐tree Advantage:
Once the tree is computed, the tree can handle all i i t i ffi i tviewpoints, i.e. efficient
Handle transparency Indeed it’s a stand format ( BSP files) to store the Indeed, it s a stand format (.BSP files) to store the environment for many games E.g. Quake, Half‐life, Call of Duty, etc
Disadvantages Cannot handle moving/changing environmentsP i i i l Preprocessing time is long
Type 2a: Z‐buffer Algorithm When we draw a pixel on the screen
It is from a pixel by the scan conversion algorithm We may store the color onto the color buffer (memory) We may store the color onto the color buffer (memory) The choices are:
To overwrite the original values already there, or Do not write anything, i.e. ignoring the current pixel
The main idea of z‐buffer algorithm: Allocate another buffer to store the z values of all the Allocate another buffer to store the z values of all the pixel drawn
Note: When we perform all the transformation, we keep the z values of the vertices of all polygons
Type 2a: Z‐buffer Algorithm For every pixel, every polygon will (usually) have a different z values
Z
COP
Z2
Z1
Z‐buffer Algorithm// initialization
for (y = 0; y < YMAX; y++) {for (x = 0; x < XMAX; x++) {
WritePixel (x, y, BACKGROUND_VALUE );WriteZ (x,y, BACKGROUND_Z );
}}// going through each polygon
for (each polygon) {for (each pixel in polygon’s projection) {
pz = polygon’s z-value at pixel coords (x,y);if (pz < ReadZ(x,y){ /* new point is not farther */
WriteZ (x, y, pz);WritePixel (x,y, polygon’s color at (x,y));
}}
}
9 9 9 9 9 9 9 9 99 9 9 9 9 9 9 9 9
5 5 5 5 5 5 5 55 5 5 5 5 5 5
5 5 5 5 5 5 5 5 95 5 5 5 5 5 5 9 99 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 99 9 9 9 9 9 9 9 99 9 9 9 9 9 9 9 9
5 5 5 5 5 5 55 5 5 5 5 55 5 5 5 55 5 5 5+ =
5 5 5 5 5 5 5 9 95 5 5 5 5 5 9 9 95 5 5 5 5 9 9 9 95 5 5 5 9 9 9 9 9
9 9 9 9 9 9 9 9 99 9 9 9 9 9 9 9 99 9 9 9 9 9 9 9 9
5 5 55 55
5 5 5 9 9 9 9 9 95 5 9 9 9 9 9 9 95 9 9 9 9 9 9 9 9
Initial Screen z‐buffer(Let 9 be the maximum z value)
5 5 5 5 5 5 5 5 95 5 5 5 5 5 5 9 95 5 5 5 5 5 9 9 9 2 3 4 5 6 7 8
5 5 5 5 5 5 5 5 95 5 5 5 5 5 5 9 92 3 4 5 5 5 8 9 9
5 5 5 5 5 9 9 9 95 5 5 5 9 9 9 9 95 5 5 9 9 9 9 9 9
2 3 4 5 6 7 82 3 4 5 6 7 8
+ 2 3 4 5 5 7 8 9 92 3 4 5 6 7 8 9 95 5 5 9 9 9 9 9 9
=
5 5 9 9 9 9 9 9 95 9 9 9 9 9 9 9 9
5 5 9 9 9 9 9 9 95 9 9 9 9 9 9 9 9
Type 2a: Z‐buffer Algorithm Advantages
No pre‐sorting is necessary, no explicit intersection/object‐object comparisons are required, i.e.
Fast and simple Fast and simple Handles cyclic and penetrating polygons Can make use of ∆z to speed up in the incremental SCA
Disadvantagesg Can handle only opaque objects
Well… can somehow fix it partially… A single point sampling process, i.e. severe aliasing.
E g two polygons on the same plane but with different shapes E.g. two polygons on the same plane but with different shapes Problem in a sub‐pixel level
The z‐buffer's finite precision does not provide adequate resolution for scene with small objects separated far apart (in z) and for intersections of distant objects. (i.e. 2 pts close together may become touching each other due to rounding ) ( .e. pts c ose toget e ay beco e touc g eac ot e due to ou d g )
Type 2b: Warnock's Algorithm Subdivide the screen until
The area only contains one l polygon, or
The area is one pixel big For the one pixel big area For the one pixel big area
Determine which polygon to draw by intersecting a line to all the polygons in that pixel
Speed‐up Enhancements1. Skipping (eliminating) objects outside the view frustum
Many many methods…k f ll2. Back face culling
For each polygon, define one side is “outside” and the other “inside”inside
Only draw the polygon if the “outside” face is facing the eye by testing if the dot product of the two vectors V and N is greater than zero V: eye position minus a point on the polygon N: normal vector of the polygon
N N: normal vector of the polygon
V
OpenGL Z‐buffer (depth buffer)
Setup:glEnable(GL DEPTH TEST);glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE);
Every time clearing the screenglClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
Back face culling Setup SetupglEnable(GL_CULL_FACE);
glCullFace(GL_FRONT); or glCullFace(GL_BACK);
Th d ll l i i l k i l k i d Then draw all polygons in an anti‐clockwise or a clockwise order
Outline Effect A polygonal model Draw the outline
The division between polygon being “backfaceculled” and not culledculled and not culled
Conclusion This all COMPUTATIONAL GEOMETRY! Z‐buffer is not superior,
but most “user friendly”y Most suitable for hardware implementation
Other algorithms are more suitable for software implementationp
Performance depends on The number of polygons Size of the output Size of the output
Those who are interested: Quake’s HSR techniques:
http://downloads gamedevnet/pdf/gpbb/gpbb66 pdf http://downloads.gamedev.net/pdf/gpbb/gpbb66.pdf