INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Renderer Implementation: Basics and Clipping David Carr Virtual Environments, Fundamentals Spring 2005 Feb-28-05 SMM009, Basics and Clipping 1 L Overview Preliminaries Clipping - Line + Cohen-Sutherland + Liang-Barsky + Cohen-Sutherland in 3D - Polygon, Sutherland-Hodgman Hidden surface removal - Back-face removal - Depth sorting - Painter s algorithm - Z-buffer algorithm - Octree methods Scan conversion - Digital difference analyzers - Bresenham s algorithm - Circles - Flood fill Antialiasing Feb-28-05 SMM009, Basics and Clipping 2 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Preliminaries Feb-28-05 SMM009, Basics and Clipping 3 L 1
Two Approaches Object oriented - Forward object by object to frame buffer - Follows pipeline model - Main drawback, cannot support global effects Image oriented - From pixel to object - Supports global effects - Main drawback, inverse functions are problematic Feb-28-05 SMM009, Basics and Clipping 4 L Four Major Tasks Modeling application program Geometric processing already studied Rasterization next two lectures Display automatic Modeling Feb-28-05 Geometric Processing Rasterization SMM009, Basics and Clipping Display 5 L 6 L Coordinate Transformations Object Eye/Camera Clip Normalized Device Coordinates Display Feb-28-05 SMM009, Basics and Clipping 2
From NDC Transformation to Window Coordinates x v = x v min + ( x " x min ) x v max " x v min x max " x min y v = y v min + ( y " y min ) y v max " y v min y max " y min If depth is to be preserved add z v = z v min + ( z " z min ) z " z v max v min z max " z min For OpenGL, standard clipping volume x v = x v min + ( x +1) x v max " x v min 2 Feb-28-05 SMM009, Basics and Clipping 7 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Clipping Feb-28-05 SMM009, Basics and Clipping 8 L Four general cases of a line segment - All inside AB - All outside CD - Partially in, one endpoint EF - Partially in, no endpoints GH Line Clipping in 2D Feb-28-05 SMM009, Basics and Clipping 9 L 3
Cohen-Sutherland Clipping For each end point (p 1, p 2 ) - Assign codes (c 1, c 2 ) according to the figure - If (c 1 c 2 ) == 0, all in draw line - Else if (c 1 & c 2 ) 0, all out discard line - Else divide line along one clipping window edge + Based on (c 1 & c 2 ) + Recursively repeat 1001 1000 1010 0001 0000 0010 0101 0100 0110 x = x min x = x max y = y max y = y min Feb-28-05 SMM009, Basics and Clipping 10 L Cohen-Sutherland Line Division In general, which edge first doesn t matter when a line crosses more than one. Against a vertical edge let x = x max or x = x min ( ) - Then, y new = y 1 + y " y 2 1 ( ( x 2 " x 1 ) x " x 1), x 1 # x 2 - Sort points so that x 1 is smallest (largest) when clipping from the left (right) - And, the new line segments are [(x 1,y 1 ) - (x,y new )] and [(x±1,y new ) - [(x 1,y 1 )] Similarly, for horizontal edges just exchange x and y Feb-28-05 SMM009, Basics and Clipping 11 L Cohen-Sutherland Example 1001 0001 0101 p 1 p 2 1000 1010 0000 0010 0100 0110 y = y max y = y min x = x min x = x max Feb-28-05 SMM009, Basics and Clipping 12 L 4
Cohen-Sutherland in Practice Advantages - Simple operations - Efficient + Especially when most segments don t cross the clipping window - Extendable to 3D Disadvantages - Recursive division of lines + Intersection computations early - Vertical and horizontal lines are a singularity when dividing lines - Edges require special handling with floating point coordinates Feb-28-05 SMM009, Basics and Clipping 13 L More Efficient Clipping Idea is to delay intersection calculations Base on parametric form of a line - For a line segment [p 1, p 2 ], the segment between them is given by: - x = x 1 + α(x 2 - x 1 ) - y = y 1 + α(y 2 - y 1 ) 0 α 1 Feb-28-05 SMM009, Basics and Clipping 14 L Liang-Barsky Clipping If the clipping window boundaries are: - x wmin, x wmax, y wmin, and y wmax Then, the clipping conditions can be expressed as: - x wmin x 1 + α(x 2 - x 1 ) x wmax - y wmin y 1 + α(y 2 - y 1 ) y wmax Which gives the following four inequalities αp k q k k = 1..4 + p 1 = (x 2 - x 1 ) q 1 = x 1 - x wmin + p 2 = (x 2 - x 1 ) q 2 = x wmax - x 1 + p 3 = (y 2 - y 1 ) q 3 = y 1 - y wmin + p 4 = (y 2 - y 1 ) q 4 = y wmax - y 1 Want to keep α 1, α 2 that represent the intersection of the extensions of the line with the clipping window Feb-28-05 SMM009, Basics and Clipping 15 L 5
Liang-Barsky Tests If p k = 0 - The line is parallel to the edge corresponding to k (p k = 0) - And, q k < 0 + Completely outside of the clipping window - And, q k 0 + Inside the corresponding border, must considered If p k < 0 - Infinite extension proceeds outside to inside - r = q k / p k, if r > α 2, then line is outside discard - α 1 = max(r,α 1 ) If p k > 0 - Infinite extension proceeds inside to outside - r = q k / p k, if r < α 1, then line is outside discard - α 2 = min(r,α 2 ) Feb-28-05 SMM009, Basics and Clipping 16 L Liang-Barsky Example P 2 y = y max p 1 = (x 2 - x 1 ), q 1 = x 1 - x wmin p 2 = (x 2 - x 1 ), q 2 = x wmax - x 1 y = y min P 1 x = x min p 3 = (y 2 - y 1 ), q 3 = y 1 - y wmin p 4 = (y 2 - y 1 ), q 4 = y wmax - y 1 α 1 = 0, α 2 = 1 x = x max Feb-28-05 SMM009, Basics and Clipping 17 L Liang-Barsky versus Cohen-Sutherland Liang-Barsky generally avoids computing multiple intersection points Liang-Barsky only uses one divide per test Cohen-Sutherland can repeatedly subdivide a line that is outside of the clipping region Feb-28-05 SMM009, Basics and Clipping 18 L 6
Extending Cohen-Sutherland to 3D Add codes for in-front-of and behind These compute with respect to the z values Proceed as before but now with a 6-bit code Feb-28-05 SMM009, Basics and Clipping 19 L Pipeline Clipping The above algorithms consider the clipping region as a box However, we could consider it to be four infinite lines giving four half-planes Thus, we could test each in succession Left Clipper Right Clipper Bottom Clipper Top Clipper Feb-28-05 SMM009, Basics and Clipping 20 L Polygon Clipping Clipping so far has only considered line segments or unfilled polygons However, we model with filled polygons Remember, convex and non-convex polygons - Clipping of non-convex polygons is difficult - One polygon can become many - Most packages require convex polygons Feb-28-05 SMM009, Basics and Clipping 21 L 7
Sutherland-Hodgman Polygon Clipping Pipeline-based algorithm Works on convex polygons - Can be extended to non-convex - Or, polygons can be tessellated first Idea - Walk around the polygon vertices in order (counterclockwise) - Pipeline clip against each half-plane Left Clipper Right Clipper Bottom Clipper - Generate new vertices based on the boundary lines Top Clipper Feb-28-05 SMM009, Basics and Clipping 22 L Sutherland-Hodgman, Four Cases v 2 v 1 v 1 ' v 2 out in Output: v 1 ',v 2 v 1 in in Output: v 2 v 1 ' v 2 v 1 v 1 in out v 2 out out Output: none ' Output: v 1 Feb-28-05 SMM009, Basics and Clipping 23 L Sutherland-Hodgman, Example v 3 v 4 & v 4 v 5 : in " in v 5 output : v 4 & v 5 v 5 v 1 : in " out v " output : v 1 # 5 v 4 v 1 {v 1,v 2,v 3,v 4,v 5 } v 1 v 2 : out " in output : v 1 #,v 2 " v 1 v 2 v 3 : in " in v output : v 3 Feb-28-05 2 SMM009, Basics and Clipping 24 L v 3 8
v 5 v 5 " : in # in output : v 5 " Sutherland-Hodgman, Example v 4 v 5 : out " in output : v # 4,v 5 " v 5 v 5 " v 4 v 4 v 3 v 4 : out " out output : none v 5 " v 1 " : in # in output : v 1 " { v 1 ",v 2,v 3,v 4,v 5, v 5 " } v 1 " v 2 : in # in output : v 2 " v 1 v 3 v 2 v 3 : in " out v v 2 Feb-28-05 2 SMM009, Basics and Clipping 25 L " v 2 output : # v 5 v 5 " : in # in output : v 5 " v 5 " Sutherland-Hodgman, Example v 5 " v 4 v " 4 v 5 : in # in output : v 5 v 5 " v 1 " : in # out output : v 1 " {v 2, v 2 ", v " 4,v 5, v 5 ", v 1 "} v 2 " v " 4 : out # in output : v 2 ", v " 4 v 1 " v 2 : out # out v 1" output : none " v 1 v Feb-28-05 2 SMM009, Basics and Clipping 26 L " " v 2 " v 2 v 2 v 2 " : out # out output : none v 5 v 5 " : out # out output : none v 5 " v 5 " v 5 " v 1 " : out # in output : v 5 ", v 1 " Sutherland-Hodgman, Example " " v 1 v 5 { v 2 ", v " 4,v 5, v 5 ", v 1 " } v 2 " v " 4 : in # out v 1 " v 2 " : in # in output : v 2 " " v 4 " " v 2 v " 4 v 5 : out # out output : none output : v 2 " " Feb-28-05 SMM009, Basics and Clipping 27 L " " " v 2 9
Sutherland-Hodgman, Result " " v 5 " " " v 2 " " v 1 " " v 2 Feb-28-05 SMM009, Basics and Clipping 28 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Hidden Surface Removal Feb-28-05 SMM009, Basics and Clipping 29 L Overview Only certain polygons are visible from a given viewpoint - Clipping removes those outside of the view - However, there may be polygons that are partially or fully hidden by other objects in the view Parts of objects that face away from the viewer do not contribute to scene - Back-face removal Part of objects that are hidden by opaque surfaces do not contribute to the scene - Hidden-surface removal - Visible-surface detection Feb-28-05 SMM009, Basics and Clipping 30 L 10
Back-Face Removal If a surface points away from the viewer A quick way to test is: - It s normal vector also points away v - If the angle θ between the normal n and the vector to the viewer v is -90 θ 90 - Then, the surface is visible. θ n But, - -90 θ 90 cos θ 0 n v 0 - If w1, w2, w3 are successive, non-collinear vertices in a right-hand system - n = (w2 -w1) (w3-w1) If one waits until the polygon is transformed into the standard OpenGL viewing volume then - ui = Twi, n = (u2 -u1) (u3-u1), and v = [ 0 0 1 0]T - So, we need only test that zn 0 Feb-28-05 SMM009, Basics and Clipping 31 L 32 L 33 L Hidden-Surface Removal Two approaches - Object-space, consider the objects pair-wise - Image-space, trace a ray back Feb-28-05 SMM009, Basics and Clipping Object Approaches Naïve comparison - Pick an object, compare it with the n-1 other objects, - Complexity O(n2) Depth sort - For each object find the nearest and furthest points - Sort by increasing depth of far point, break ties with near point - Complexity O(n lg(n)) with an efficient sort Feb-28-05 SMM009, Basics and Clipping 11
Painter s Algorithm A simple variant of depth sorting Back to front rendering Basic algorithm: - Sort the objects (polygons) by decreasing maximum depth - Render the objects in sorted order Complications: - Intersecting objects - Cyclic overlap Feb-28-05 SMM009, Basics and Clipping 34 L 35 L 36 L Dealing with the Complications We must keep track of extent (nearest point) For each surface, S, which overlaps the depth of the back surface S test: - Do their areas overlap? Is S completely behind? Is S completely in front Does S completely obscure S? If not we must, - Reorder surfaces, or - Compute intersections and divide surfaces Feb-28-05 SMM009, Basics and Clipping Z-Buffer Algorithm A simple algorithm that relies on an auxiliary memory - Was expensive at one time Algorithm: - Initialize the depth buffer to - - For each polygon + Compute the depth of each pixel as it is scan converted + If the depth is less that the current value in the depth buffer Replace the pixel Update the depth buffer with the pixel s depth Feb-28-05 SMM009, Basics and Clipping 12
Octree Methods If we divided the octree as on the right Then, to render - Do a post order traversal of the tree - Render if the corresponding pixel in the frame buffer is empty 6 4 5 0 1 7 3 2 0 1 2 3 Feb-28-05 SMM009, Basics and Clipping 37 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Scan Conversion Feb-28-05 SMM009, Basics and Clipping 38 L Line Equations The slope intercept form of a line is - y = mx + b - Where the slope m = y end " y 0 x end " x 0 - And, the intercept b = y 0 - mx 0 Can draw a line by evaluating y at every location of x However, this requires a multiplication and addition for every point And, for m > 1 there will be gaps Feb-28-05 SMM009, Basics and Clipping 39 L 13
Digital Difference Analyzers Consider m 1 - For m > 1, we can reverse x and y What if we draw in unit increments - x = x + 1 (y = y + m) - y = y + m (x = x + 1/m) Now, we can rapidly increment between x 0 (y 0 ) and x end (y end ) There are still some inefficiencies - Floating point computations - Rounding to set the right point Feb-28-05 SMM009, Basics and Clipping 40 L Bresenham s Algorithm Uses only integer operations - One addition per step Can be extended to circles and other curves Key ideas: - Step one pixel at a time - Compute a decision parameter, p, whose sign determines the next point Feb-28-05 SMM009, Basics and Clipping 41 L Bresenham s for Lines with Slope, 0 m 1 Consider the diagram to the right - An arbitrary point on a line segment is not usually on a display pixel y k - We need to draw the closest point, i.e., with the smaller of d lower or d upper Note, d lower < d upper iff d lower - d upper < 0 So, y k +1 d upper d lower = y " y k = m(x k +1) + b " y k d upper = (y k +1) " y = (y k +1) " m(x k +1) " b d lower " d upper = 2m(x k +1) " 2y k + 2b "1 x k +1 d lower Feb-28-05 SMM009, Basics and Clipping 42 L 14
Bresenham s for Lines, Continued d lower - d upper still contains a division which can be noninteger m = (y " y ) end 0 (x end " x 0 ) #x = (x end " x 0 ),#y = (y end " y 0 ) What if we let p k = Δx(d lower - d upper )? - Removes integer division - And, p k = 2x k "y # 2y k "x + 2"y + "x(2b #1) = 2x k "y # 2y k "x + c Feb-28-05 SMM009, Basics and Clipping 43 L Bresenham s, Δp, p 0 Now, we need to compute the differences from p k to p k+1 p k+1 = 2"yx k+1 # 2"xy k+1 + c p k = 2"yx k # 2"xy k + c p k+1 " p k = 2#y(x k+1 " x k ) " 2#x(y k+1 " y k ) But, x k+1 = x k +1 so, p k+1 " p k = 2#y " 2#x(y k+1 " y k ) and, p k+1 = p k + 2#y " 2#x(y k+1 " y k ) Note, y k+1 " y k = 0 or 1, depending on which pixel is closer and, p 0 = (2m(x 0 +1) " 2y 0 + 2b "1)#x, but b = y 0 " mx 0 = (2mx 0 + 2m " 2y 0 + 2y 0 " 2mx 0 "1)#x, and m = #y #x = (2m "1)#x = 2#y " #x Feb-28-05 SMM009, Basics and Clipping 44 L Bresenham s, 0 m 1 Input the end points and set the left one as (x 0,y 0 ) PutPixel(x 0,y 0 ) Compute: - Δx, Δy, 2Δy, & 2Δy - 2Δx - p 0 = 2Δy - Δx x k on the line test - If < 0, then PutPixel(x k +1,y k ) and p k+1 = p k + 2Δy - Else PutPixel(x k +1,y k +1) and p k+1 = p k + 2Δy - 2Δx Stop when x = x end Feb-28-05 SMM009, Basics and Clipping 45 L 15
Bresenham s Example, (21,11) ~ (30,18) k p k (x,y) 0 6 (21,11) 1 2 (22,12) 2-2 (23,12) 3 14 (24,13) 4 10 (25,14) 5 6 (26,15) Δx = 10, Δy = 8, 2Δy =16, 6 2 (27,16) 2Δy-2Δx = -4, 7-2 (28,16) p 0 = 2Δy-Δx = 6 8 14 (29,17) 9 10 (30,18) Feb-28-05 SMM009, Basics and Clipping 46 L Bresenham s, General Lines Consider the symmetry If -1 m 0, then everything is the same except one subtracts 1 from y instead of adding - Can also go in steps of -1 for x starting at x end If m > 1, then swap x and y Completing the algorithm is left as an exercise! Feb-28-05 SMM009, Basics and Clipping 47 L A circle has equation - (x-x c ) 2 + (y-y c ) 2 = r 2 Drawing Circles - So, y = y c ± r 2 " (x " x c ) 2 Drawing directly has two problems: - The points are not connected - We have to do square root calculations which are expensive Can use an algorithm similar to Bresenham s? Feb-28-05 SMM009, Basics and Clipping 48 L 16
Midpoint Circle Algorithm Two characteristics of a circle can be used to make any algorithm more efficient - A circle is symmetric + If (x,y) in the arc from the y-axis to the line y = x is on the circle, so are the eight combinations of x,y with different signs - A circle can be drawn at (0,0) and translated to (x c,y c ) when drawn So, let f circle (x,y) = x 2 + y 2 - r 2, then - f circle will be <0 inside, =0 on, and >0 outside of the circle Test for the midpoints between pixel positions Feb-28-05 SMM009, Basics and Clipping 49 L Midpoint Circle Start at (0,r) Assume, we ve just plotted (x k,y k ) We must determine if - (x k +1,y k ), or - (x k +1,y k -1) - Is closer Evaluate the decision parameter at the midpoint between these to pixels - If less than 0, it is inside the circle and is y k closer - Otherwise, y k -1 is closer Feb-28-05 SMM009, Basics and Clipping 50 L Midpoint Circle, p p k = f circle (x k +1, y k " 1 2 ) = (x k +1) 2 + (y k " 1 2 )2 " r 2 note: x k+1 +1 = x k + 2 " p k+1 = f (x k+1 +1, y k+1 # 1 2 ) = [(x k +1)+1]2 + (y k+1 # 1 2 )2 # r 2 or p k+1 " p k = (x k + 2) 2 " (x k +1) 2 + (y k+1 " 1 2 )2 " (y k " 1 2 )2 2 2 " p k+1 = p k + 2(x k +1)+ (y k+1 # y k ) # (yk+1 # y k )+1 So, "p ={ 2x k+1 +1 p k < 0 2x k+1 +1# 2y k+1 p k $ 0 note: 2x k+1 = 2x k + 2 and 2y k+1 = 2y k " 2 Finally, p 0 = f circle (1,r " 1 2 ) =1+ (r " 1 2 )2 " r 2 = 5 4 " r or 1" r for Integers Feb-28-05 SMM009, Basics and Clipping 51 L 17
Midpoint Circle Algorithm Input radius, r, and center (x c,y c ) Compute p 0 = 1 - r, PutPixel(0,r) for 4 symmetric points x k until x y - If p k < 0, then + Plot point is (x k +1,y k ) + Compute the 8 points and plot them translated to (x c,y c ) + 2x k+1 = 2x k + 2, p k+1 = p k + 2x k+1 +1 - Else + Plot point is (x k +1,y k -1) + Compute the 8 points and plot them translated to (x c,y c ) + 2x k+1 = 2x k + 2, 2y k+1 = 2y k - 2, p k+1 = p k + 2x k+1 +1-2y k+1 Feb-28-05 SMM009, Basics and Clipping 52 L Midpoint Circle Example, r=10 k p k (x,y) 2x k+1 2y k+1 0-9 (1,10) 2 20 1-6 (2,10) 4 20 2-1 (3,10) 6 20 3 6 (4,9) 8 18 4-3 (5,9) 10 18 5 8 (6,8) 12 16 6 5 (7,7) 14 Feb-28-05 SMM009, Basics and Clipping 53 L 14 Flood Filling an Area If we have an arbitrary shape in an otherwise empty frame buffer Flood fill will recursively fill the area - Draw the shape - Start at an interior point (x,y) - FloodFill(x,y) + If (x,y) is uncolored Color (x,y) FloodFill(x+1,y), FloodFill(x,y+1), FloodFill(x-1,y), FloodFill(x,y -1) + Else return Feb-28-05 SMM009, Basics and Clipping 54 L 18
INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Antialiasing Feb-28-05 SMM009, Basics and Clipping 55 L Problem Due to the finite resolution of the screen Line segments and object edges appear saw-toothed This is known as the jaggies Feb-28-05 SMM009, Basics and Clipping 56 L Two Methods to Solve Super-sampling - Divide each pixel into a number of smaller pixels - Draw the figure - Count the number of smaller pixels drawn in each big pixel - Set the intensity in the big pixel proportional to the percentage of drawn smaller pixels Area sampling - Set each of two adjacent pixels proportional to their percentage coverage - Assumes finite with lines - Pixels can be horizontal or vertical Feb-28-05 SMM009, Basics and Clipping 57 L 19
Supersampling, Considerations Requires extra memory We should probably use finite width lines - Slower performance - Better intensity values We need to consider background colors - If we use 3x3 small pixels - Then, with a red background and a blue line that touches 4 small pixels (5r + 4b) Pixel color = 9 May want to weigh cells differently 2 4 2 1 2 1 Feb-28-05 SMM009, Basics and Clipping 58 L 1 2 1 Area Sampling Considerations In principle: - Drawing rectangles for lines - Considering pixels as squares (or rectangles) - Computing the intersecting trapizode - Intensity ~ Area(intersection) / Area(pixel) Extra computation Feb-28-05 SMM009, Basics and Clipping 59 L Questions? Feb-28-05 SMM009, Basics and Clipping 60 L 20