CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I....
Transcript of CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I....
![Page 1: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/1.jpg)
CS3241: Computer Graphics
![Page 2: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/2.jpg)
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
![Page 3: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/3.jpg)
Keeping the z Values After Perspective Transformation
![Page 4: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/4.jpg)
Transformation/Viewing
![Page 5: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/5.jpg)
Scan Convert Algorithm But how do we know the order?
Sca Co e t go t
![Page 6: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/6.jpg)
Wrong Drawing Order
![Page 7: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/7.jpg)
Hidden Surface Removal (HSR)
![Page 8: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/8.jpg)
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
![Page 9: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/9.jpg)
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
![Page 10: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/10.jpg)
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
![Page 11: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/11.jpg)
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
![Page 12: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/12.jpg)
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
![Page 13: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/13.jpg)
S t A d S t B I t tiSet A and Set B Intersection
A ‐ BUnion
A B
![Page 14: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/14.jpg)
Type 1a: Depth Sort
Polygon A
Polygon B
Polygon A
![Page 15: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/15.jpg)
Type 1a: Depth Sort
Polygon A
Polygon A minus B
Polygon A
Polygon A in B
![Page 16: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/16.jpg)
Type 1a: Depth Sort
Polygon B minus A
Polygon B in A
![Page 17: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/17.jpg)
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
![Page 18: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/18.jpg)
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
![Page 19: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/19.jpg)
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)
![Page 20: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/20.jpg)
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
![Page 21: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/21.jpg)
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
![Page 22: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/22.jpg)
Type 1b: BSP‐treeA
B1 B2
A
B1
BC1C2
HA
B2
C2
C1
![Page 23: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/23.jpg)
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
![Page 24: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/24.jpg)
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
![Page 25: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/25.jpg)
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
![Page 26: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/26.jpg)
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
![Page 27: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/27.jpg)
Keeping the z Values After Perspective Transformation
![Page 28: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/28.jpg)
Type 2a: Z‐buffer Algorithm For every pixel, every polygon will (usually) have a different z values
Z
COP
Z2
Z1
![Page 29: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/29.jpg)
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));
}}
}
![Page 30: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/30.jpg)
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
![Page 31: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/31.jpg)
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 )
![Page 32: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/32.jpg)
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
![Page 33: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/33.jpg)
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
![Page 34: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/34.jpg)
Polygon ygNormal
![Page 35: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/35.jpg)
Back Face Culling
![Page 36: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/36.jpg)
![Page 37: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/37.jpg)
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
![Page 38: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/38.jpg)
Outline Effect A polygonal model Draw the outline
The division between polygon being “backfaceculled” and not culledculled and not culled
![Page 39: CS3241: Computer Graphicshcheng/CS3241/L06 - Hidden... · 2011-09-13 · Two Main Approaches I. Control the drawing orderof objects Sort the order of polygons according to “depth”](https://reader033.fdocuments.us/reader033/viewer/2022053012/5f0f59f27e708231d443baa2/html5/thumbnails/39.jpg)
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