Binghamton University EngiNet State University of New York EngiNet Thomas J. Watson School of Engineering and Applied Science WARNING All rights reserved. No Part of this video lecture series may be reproduced in any form or by any electronic or mechanical means, including the use of information storage and retrieval systems, without written approval from the copyright owner. 2007 The Research Foundation of the State University of New York CS 460/560 Computer Graphics Professor Richard Eckert April 25, 2007 General Hidden Surface Removal Algorithms Z-Buffer Depth Sort 1
Hidden Surface Removal Determination of surfaces not visible to the viewer Many different techniques Back face culling, for single objects only Z-Buffer Depth Sort Z-Buffer Hidden Surface Removal Algorithm Basic Idea: At a given pixel we want to plot color of closest surface that projects to that pixel We re looking for minimum zv Use a buffer (array) parallel to the frame buffer Store minimum values of zv One for every pixel Called the Z-Buffer Z-Buffer Technique Applied to a Polygon Mesh Initialize Z-Buffer and Frame Buffer Look at each polygon Look at each point (xs,ys) projected to by the polygon Compute zv of the point on the polygon If zv is closer than value stored at [x,y] in Z-Buffer Replace value in Z-Buffer with zv Update corresponding element in frame buffer with color of the polygon Z-Buffer Algorithm Applied to Convex Polygons Data Structures: For each polygon Polygon color Polygon vertex coordinates: xs, ys, and zv Note mixed coordinates Edge table (xmin, ymin, zmin, xmax, ymax, zmax) Active edge list (AEL) with active edges intersected by current scanline sorted on xs (See scanline polygon fill notes) Other Data Structures Frame Buffer FBuf[x][y] Will store the color of each pixel (x,y) Z-Buffer ZBuf[x][y] Will store the zv distance of point on closest polygon that projects to pixel (x,y) on screen Initialize each element of FBuf[][] to background color Initialize each element of ZBuf[][] to infinity (largest possible value) 2
The Algorithm For each polygon For each scanline y spanning the polygon Get left & right active edges from AEL Get x,z coordinates of endpoints from edge table Compute scanline/edge intersection pts (xl,zl,xr,zr ) (Use x-y & z-y interpolation) For (x=xl toxr) Compute z by z-x interpol. If (z < ZBuf [x,y]) ZBuf [x,y] = z FBuf [x,y] = polygon color Double Interpolation We know (from Edge Table): lower/upper vertices of left active edge: (x0,y0,z0) and (x1,y1,z1) lower/upper vertices of right active edge: (x2,y2,z2) and (x3,y3,z3) We also know y of current scanline x-y Interpolation: Find x coords of intersection pts (xl,xr) Left Edge: xl-x0 y -y0 ------- = ------- x1-x0 y1-y0 Solving for xl: xl = (x1-x0)*(y-y0)/(y1-y0) + x0 Similarly for xron right edge: xr = (x3-x2)*(y-y2)/(y3-y2) + x2 z-y Interpolation Find z coordinates of intersection points of scan line (y) with left and right edges Done the same way as x-y interpolation x coordinates replaced by z coordinates Results: zl = (z1-z0)*(y-y0)/(y1-y0) + z0 zr = (z3-z2)*(y-y2)/(y3-y2) + z2 z-x Interpolation Find z value on polygon at pixel x on current scanline (y) Interpolate between the left and right edge intersection points: z-zl x-xl -------- = -------- zr-zl xr-xl Solving for z: z = (zr-zl)*(x-xl)/(xr-xl) + zl Speeding up the Algorithm Do interpolations incrementally Get new values from old values by adding correct increments xl,xr,zl,zr (in the outer loop) z (in the inner loop) Avoids multiplications and divisions inside algorithm loops 3
Z-Buffer Performance Outer loop repeats for each polygon Complex scenes have more polygons So complex scenes should be slower But:-- More polygons usually means smaller polygons So inner loops (y and x) are faster For most real scenes, performance is approximately independent of scene complexity Disadvantage of Z-Buffer Memory requirements Z-Buffer is at least as big as the frame buffer For best results, need floating point or doubles for z values Example 1000 X 1000 resolution screen Assume 8 bytes to store a double 8 Megabytes required for Z-Buffer But memory has become cheap Z-Buffer used very commonly now Often implemented in hardware Depth Sort Hidden Surface Removal (Newell, Newell, & Sanchez) Depth Sort Hidden Surface Removal Algorithm Basic Idea Order polygons according to how far away they are from the observer Then paint them into the picture, farthest first, closest last (Painter s Algorithm) So: Farthest surfaces are entered into frame buffer first, nearest last Means far surfaces are painted over by near surfaces Details of Depth Sort 1. Remove back faces (preprocessing) 2. Decompose remaining polygons into triangles (preprocessing) Depth determination easiest for triangles 3. Sort triangles in depth order 4. Apply Painter s algorithm to display sorted triangles farthest first, closest last 3. Sorting in Depth Order For each triangle: Create a linked list containing pointers to all triangles in front of it And a counter of all triangles in back of it 4
4. Applying Painter s Algorithm Repeat: Index through triangles & draw any whose counter is 0 (those that have no triangles behind them farthest away) Decrement counter of all triangles in list of drawn triangle (# in back has been reduced by 1) Mark counter of drawn triangle as finished -- Don t want to draw it again Example Details of Depth Sort Initialize all triangle counters to 0 For i = 1 to n-1 For j = i+1 to n If triangles i and j OVERLAP If triangle j is IN FRONT of triangle i Add triangle j to i s list ctr[j]++ Else Add triangle i to j s list ctr[i]++ [If no overlap, all ctrs=0, so all triangles painted] Determination of OVERLAP 1. Mini-max test: look at enclosing rectangles If rectangles don t overlap, triangles don t Easy test for no overlap: xmax1<xmin2 or xmax2<xmin1 or ymax1<ymin2 or ymax2<ymin1 But if rectangles overlap, triangles may not 5
So check for intersections between edge pairs x = x1 + (x2-x1)t y = y1 + (y2-y1)t x = x3 + (x4-x3)s y = y3 + (y4-y3)s Solve for s,t 0<=s<=1 and 0<=t<=1 ==> they intersect which means triangles overlap But one triangle could be contained in the other Test for containment D & A must be on SAME SIDE of BC Same for E and F Repeat for AB C with D, E, F And for AC B with D, E, F SAME SIDE Test Point (x,y) Line (x1,y1) to (x2,y2) If Point lies on Line: (y-y1)/(x-x1) = (y2-y1)/(x2-x1) Define f(x,y)=(x-x1)(y2-y1)-(y-y1)(x2-x1) f(x,y) = 0 ==> Point lies on line f(x,y) < 0 ==> Point lies on one side f(x,y) > 0 ==> Point lies on other side Determination of IN FRONT 1. Mini-max test If all vertex zv s of triangle i < all vertex zv s of triangle j: then triangle i is in front of triangle j zv(i),max < zv(j),min If Mini-max Test is Inconclusive Find a point with same x,y on projection of overlapping triangles Triangle with smaller z is in front Here triangle i is in front of triangle j Special Problem Cases Interpenetrating triangles Cyclicly overlapping triangles Decompose triangles into smaller ones 6
Disadvantage of Depth Sort Performance depends on number of polygons More polygons, slower Not good for complex scenes Illumination, Reflection, Shading The Phong Model Illumination, Reflection, Shading Need to display surfaces in natural colors Colors observed if we really saw the scene How do they get those colors? Observed Colors Depend on: Light sources in scene Material properties of object surfaces How light interacts with those surfaces Reflection, Transmission, Absorption Need an Illumination/Reflection model Light Sources Approximate with two types: 1. Ambient (non-directional, diffuse, background light) Take as a constant Non-directional Grossly approximates multiply -reflected light Global reflection 2. Light Sources Approximate with a series of point sources Directional Interaction of Light with Surfaces Absorption Transmission Reflection Diffuse Nondirectional Dull, chalky surfaces No highlights Specular Directional Mirror-like surfaces Highlights Material Properties Incident light is reflected to different degrees Depends on physical (material) properties of reflecting surface This gives intrinsic color to materials Approximate by giving 3 diffuse reflection coefficients Fractions of red, green blue reflected kr, kg, kb (0 <= k <= 1) 0 means no reflection in that color band 1 means 100% reflection in that band 7
Phong Illumination/Reflection Model Assume all illumination comes from: Ambient Light Point sources Diffuse reflection of Ambient light Reflection from Point sources: Some is reflected diffusely Some is reflected specularly Reflection of Ambient Light I = k d *I a I = intensity of ambient light reflected k d = ambient reflection coefficient Actually 3 values of k d : kr, kg, kb (So this is really three equations) I a = Intensity of ambient light in scene Ia, kr, kg, kb are adjustable parameters FinalPhong Model Result (Single Light Source) Three color intensity equations: I(r,g,b) = Ambient + Point Diffuse + Point Specular I(r,g,b) =kd(r,g,b)*ia (ambient) + Ip*kd(r,g,b)*(N. L) (diffuse from point source) + Ip*ks*(R. V) n (specular from pt. Source) It can be shown that R = 2*(N. L)N - L where N and L are unit vectors Note that specular term has no color dependency (First approximation) If viewer moves, specular term must be recomputed 8