Hidden Surface Removal 1 Hidden Surface Removal Suppose

  • Slides: 26
Download presentation
Hidden Surface Removal 1

Hidden Surface Removal 1

Hidden Surface Removal § Suppose that we have the polyhedron which has 3 totally

Hidden Surface Removal § Suppose that we have the polyhedron which has 3 totally visible surfaces, 4 totally invisible/hidden surfaces, and 1 partially visible/hidden surface. § Obviously, invisible/hidden surfaces do not contribute to the final image. § The procedure that distinguishes between visible surfaces from invisible/hidden surfaces is called visible-surface determination, which is often called hidden-surface removal. 2

Back Face Culling § Performance goals in real-time rendering § frame rate: 60 -72

Back Face Culling § Performance goals in real-time rendering § frame rate: 60 -72 frames/sec? § resolution: 1600 x 1200? § photorealism (if necessary): undistinguishable from real scene! § Unfortunately, there is no real upper limit on the scene complexity. § We should cull those portions of the scene that are not considered to contribute to the final image, and process only the rest of the scene. § The simplest is back-face culling, which distinguishes between front faces (faces towards the viewer) and back faces (faces away from the viewer). front faces back faces polygon normals 3

Back Face Culling (cont’d) § Back-face culling works as a preprocessing step for hidden

Back Face Culling (cont’d) § Back-face culling works as a preprocessing step for hidden surface removal, and is very powerful in that almost half of polygons of an object are discarded as back faces. § Especially, for a single convex polyhedron, the back-face culling does the entire job of hidden-surface removal. single convex polyhedron convex, but not a single polyhedron § Hidden-surface removal is applied to only the remaining front faces. 4

Face Normal § The normal of a triangle <p 1, p 2, p 3>

Face Normal § The normal of a triangle <p 1, p 2, p 3> is computed as v 1 X v 2 where v 1 is the vector connecting p 1 and p 2, and v 2 connects p 1 and p 3. § If the vertices of every polygon are ordered consistently (CCW or CW), we can make every polygon’s normal point out of the polyhedron. All mesh data used in this class have triangles of CCW vertex ordering. t 1 = (p, q, r) p s r q t 2 = (s, q, p) t 1’s normal = (q-p)X(r-p) t 2’s normal = (q-s)X(p-s) 5

Dot Product (revisited) § Recall the dot product of two vectors vi & vj.

Dot Product (revisited) § Recall the dot product of two vectors vi & vj. § If vi • vj = 0, vi & vj are orthogonal/perpendicular. § If vi • vj > 0, angle < 90 § If vi • vj < 0, angle > 90 y d=(-1, 0) a=(-1, 1) b=(0, 1) c=(1, 1) x a·d = (-1, 1)·(-1, 0) = 1 > 0 b·d = (0, 1)·(-1, 0) = 0 c·d = (1, 1)·(-1, 0) = -1 < 0 § The same applies to 3 D. 6

§ Dot Product for Back Face Determining if a polygon Culling is a front

§ Dot Product for Back Face Determining if a polygon Culling is a front face or a back face § § § Generate a vector C connecting COP and a vertex of the polygon. Take the dot product C • N of the vector C and the polygon’s normal N. If C • N > 0, it’s a back face. If C • N < 0, it’s a front face. If C • N = 0, it’s a face that is projected as an edge/silhouette on the screen. C 1 • N 1 = 0 N 1 C 1 N 2 COP C 2 • N 2 > 0 C 3 N 3 C 3 • N 3 < 0 7

Hidden Surface Removal § Suppose that we have two triangles in the viewport defined

Hidden Surface Removal § Suppose that we have two triangles in the viewport defined by (xmin, ymin, width, height) = (0, 0, 350, 250). § Consider a pixel at (241, 200). y=249 x=349 z=1 Pixel (241, 200) at 350 250 resolution viewport. It should be blue. DOP § The above scene should result in Fig-1, not Fig-2. Fig-1 Fig-2 § Note that Fig-1 does correct hidden-surface removal. 8

Z-buffer § The most popular hidden-surface removal algorithm uses z-buffer or depth-buffer. § It’s

Z-buffer § The most popular hidden-surface removal algorithm uses z-buffer or depth-buffer. § It’s called z-buffer algorithm, where we need z-buffer in addition to the frame buffer. Their sizes are the same. y=249 Pixel (241, 200) at 350 250 resolution screen. It should be blue. x=349 z=1 DOP z-buffer frame buffer 250 should be “blue” 350 9 ?

Z-buffer Algorithm § The frame buffer will have a color-value for each pixel. §

Z-buffer Algorithm § The frame buffer will have a color-value for each pixel. § The z-buffer will have a z-value for each pixel. § The z-buffer is initialized to the z-values of the back plane (in our case, 1). § Similarly, the frame buffer is initialized to the background color (in our case, white). ① initial states frame buffer ……………. : : w w w w …. . z-buffer ……………. : : -1 -1. . 10

Z-buffer Algorithm (cont’d) § Select polygons/triangles of a mesh in an arbitrary order. §

Z-buffer Algorithm (cont’d) § Select polygons/triangles of a mesh in an arbitrary order. § Project each triangle onto the z=0 plane, and draw it using the scan-line algorithm. § When drawing each polygon, if the polygon point at (x, y) has a bigger zvalue than the current value of the z-buffer, the point’s color & z-value replace the old ones in both buffers. y=201 y=200 y y=199 x 240 241 242 243 244 240. 4 242. 6 Let’s take the red triangle. For example, when processing y=200 scan line, x=[241, 242] will be filled. For the pixel (241, 200), its z-value a of the red triangle should be greater than (or equal to) the current z-buffer’s value -1. So, set frame-buffer[241, 200] to red, and set z-buffer[241, 200] to a. 11

Z-buffer Algorithm (cont’d) ② When scan line 200 is completed z-buffer frame buffer y=200

Z-buffer Algorithm (cont’d) ② When scan line 200 is completed z-buffer frame buffer y=200 ……………. : : w w …. . : w R R w …. . x=241 A point (242, 200, 0. 5) on the red triangle ……………. : : : -1 -1. . -1 0. 6 0. 5 -1. . y=200 x=241 ③ When the red triangle is completely processed ……………. : : w w …. . : w R R w …. . R R ……………. : : -1 -1. . : -1 0. 6 0. 5 -1. . : 0. 7 0. 6 0. 5 0. 4. . 12

Z-buffer Algorithm (cont’d) § When a triangle is completed, arbitrarily select the next triangle

Z-buffer Algorithm (cont’d) § When a triangle is completed, arbitrarily select the next triangle and repeat the scan-line algorithm. pixel (241, 200) y=202 y=201 y=200 y 240 241 242 243 244 x Let’s now take the blue triangle. For example, when processing y=200 scan line, x=[*, 241] will be filled. For the pixel (241, 200), its z-value b of the blue triangle should be greater than the current z-buffer’s value a. Then, set frame-buffer[241, 200] to blue, and set z-buffer[241, 200] to b. 241. 3 13

Z-buffer Algorithm (cont’d) ④ When the blue triangle is completely processed updated! y=200 …………….

Z-buffer Algorithm (cont’d) ④ When the blue triangle is completely processed updated! y=200 ……………. : : B B w w …. . : B B R w …. . R R x=241 ……………. : : 0. 8 0. 7 -1 -1. . : 0. 8 0. 7 0. 5 -1. . : 0. 7 0. 6 0. 5 0. 4. . y=200 x=241 § Note that we will have the same result if we process blue triangle first and then red triangle (unless they are partially transparent). 14

Depth Computation § A polygon has the plane equation Ax+By+Cz+D=0. § We can compute

Depth Computation § A polygon has the plane equation Ax+By+Cz+D=0. § We can compute A, B, C and D from the 3 vertices of the polygon. § (A, B, C) is the plane’s normal vector, and computed using cross product. (A, B, C) § The remaining unknown D is obtained by inserting any vertex out of p 1, p 2 and p 3 into Ax+By+Cz+D=0. § The depth value (z-value) at a pixel position (x, y) is computed by inserting (x, y) into the plane equation Ax+By+Cz+D=0. For example, at (x, y)=(241, 200), pixel (241, 200) 15

Incremental Depth Computation § Note that the z-buffer testing is integrated into the polygon-filling

Incremental Depth Computation § Note that the z-buffer testing is integrated into the polygon-filling scanline algorithm. § In a scan line, the scan-line algorithm processes pixels of the range [xmin, xmax] one by one from the left to the right. § Once the z-value z 1 at (x, y) is computed, the z-value z 2 at the next pixel (x+1, y) is computed incrementally using z 1 as follows: z 1 z 2 y x x+1 z 1 16

Discussions on Z-buffer Algorithm § Z-buffer algorithm is the standard algorithm for hidden-surface removal.

Discussions on Z-buffer Algorithm § Z-buffer algorithm is the standard algorithm for hidden-surface removal. § Why is the z-buffer algorithm so popular? § § simplest among hidden-surface removal algorithms easy for H/W implementation compatible with rendering pipeline architecture etc. § However, the z-buffer algorithm needs a separate memory for the zbuffer. The size of the z-buffer depends on the accuracy to which the depth value of each point (x, y) is to be stored, which is a function of the scene complexity. Suppose that 4 bytes are used for a pixel. Then, it’s as large as the frame buffer! § e. g. 1280 1024 4 > 4 MB 17

The Paiter’s algorithm • Sort the faces list on the basis of the extent.

The Paiter’s algorithm • Sort the faces list on the basis of the extent. far value • paint entire faces, in order from farthest to closest • Depth sort, back to front rendering 18

Depth Sort • If z overlapped • X, y overlapped? • All the vertices

Depth Sort • If z overlapped • X, y overlapped? • All the vertices of a face on one side of the plane defined by the other face? • If still overlap, Partition!!! 19

HSR using Binary-Space Partition Trees • BSP • the polygons are rearranged into BT

HSR using Binary-Space Partition Trees • BSP • the polygons are rearranged into BT and certain faces are split into pieces • the tree can traversed in a systematic way to draw the scene with HS properly removed. 20

Building a BSP Tree • 2 D example • 3 D example 21

Building a BSP Tree • 2 D example • 3 D example 21

Traverse the tree • • • If the eye is outside { draw all

Traverse the tree • • • If the eye is outside { draw all faces in the inside of F draw all faces in the outside of F } else{ { draw all faces in the outside of F draw all faces in the inside of F } 22

A 2 D Scene 23

A 2 D Scene 23

A BSP tree 24

A BSP tree 24

A 3 D Scene and its BSP tree 25

A 3 D Scene and its BSP tree 25

Traverse Order 26

Traverse Order 26