Scan Converson & Shadng Thomas Funkhouser Prnceton Unversty C0S 426, Fall 1999 3D Renderng Ppelne (for drect llumnaton) 3D Prmtves 3D Modelng Coordnates Modelng Transformaton 3D World Coordnates Lghtng 3D World Coordnates Vewng Transformaton 3D Camera Coordnates Projecton Transformaton 2D Screen Coordnates Clppng 2D Screen Coordnates Vewport Transformaton 2D Image Coordnates Scan Converson 2D Image Coordnates Image P 2 P 1 Scan Converson & Shadng P 3 1
Overvew Scan converson Fgure out whch pxels to fll Shadng Determne a color for each flled pxel Scan Converson Render an mage of a geometrc prmtve by settng pxel colors vod SetPxel(nt x, nt y, Color rgba) Example: Fllng the nsde of a trangle P 1 P 2 P 3 2
Scan Converson Render an mage of a geometrc prmtve by settng pxel colors vod SetPxel(nt x, nt y, Color rgba) Example: Fllng the nsde of a trangle P 1 P 2 P 3 Trangle Scan Converson Propertes of a good algorthm Symmetrc Straght edges Antalased edges No cracks between adjacent prmtves MUST BE FAST! P 1 P 2 P 3 P 4 3
Trangle Scan Converson Propertes of a good algorthm Symmetrc Straght edges Antalased edges No cracks between adjacent prmtves MUST BE FAST! P 1 P 2 P 3 P 4 Smple Algorthm Color all pxels nsde trangle vod ScanTrangle(Trangle T, Color rgba){ for each pxel P at (x,y){ f (Insde(T, P)) SetPxel(x, y, rgba); } } P 1 P 2 P 3 4
Insde Trangle Test A pont s nsde a trangle f t s n the postve halfspace of all three boundary lnes Trangle vertces are ordered counter-clockwse Pont must be on the left sde of every boundary lne L 1 P L 3 L 2 Insde Trangle Test Boolean Insde(Trangle T, Pont P) { for each boundary lne L of T { Scalar d = L.a*P.x + L.b*P.y + L.c; f (d < 0.0) return FALSE; } return TRUE; } L 1 L 3 L 2 5
Smple Algorthm What s bad about ths algorthm? vod ScanTrangle(Trangle T, Color rgba){ for each pxel P at (x,y){ f (Insde(T, P)) SetPxel(x, y, rgba); } } P 1 P 2 P 3 Trangle Sweep-Lne Algorthm Take advantage of spatal coherence Compute whch pxels are nsde usng horzontal spans Process horzontal spans n scan-lne order Take advantage of edge lnearty Use edge slopes to update coordnates ncrementally dy dx 6
Trangle Sweep-Lne Algorthm vod ScanTrangle(Trangle T, Color rgba){ for each edge par { ntalze x L, x R ; compute dx L /dy L and dx R /dy R ; for each scanlne at y for (nt x = x L ; x <= x R ; x++) SetPxel(x, y, rgba); x L += dx L /dy L ; x R += dx R /dy R ; } } dx L dy L dx R dy R x L x R Polygon Scan Converson Fll pxels nsde a polygon Trangle Quadrlateral Convex Star-shaped Concave Self-ntersectng Holes What problems do we encounter wth arbtrary polygons? 7
Polygon Scan Converson Need better test for ponts nsde polygon Trangle method works only for convex polygons L 5 L 4 L 5 L 4 L 1 L 1 L 3B L 2 L 3 L 2 L 3A Convex Polygon Concave Polygon Insde Polygon Rule What s a good rule for whch pxels are nsde? Concave Self-Intersectng Wth Holes 8
Insde Polygon Rule Odd-party rule Any ray from P to nfnty crosses odd number of edges Concave Self-Intersectng Wth Holes Polygon Sweep-Lne Algorthm Incremental algorthm to fnd spans, and determne nsdeness wth odd party rule Takes advantage of scanlne coherence x L x R Trangle Polygon 9
Polygon Sweep-Lne Algorthm vod ScanPolygon(Trangle T, Color rgba){ sort edges by maxy make empty actve edge lst for each scanlne (top-to-bottom) { nsert/remove edges from actve edge lst update x coordnate of every actve edge sort actve edges by x coordnate for each par of actve edges (left-to-rght) SetPxels(x, x +1, y, rgba); } } Hardware Scan Converson Convert everythng nto trangles Scan convert the trangles 10
Hardware Antalasng Supersample pxels Multple samples per pxel Average subpxel ntenstes (box flter) Trades ntensty resoluton for spatal resoluton P 1 P 2 P 3 Overvew Scan converson Fgure out whch pxels to fll Shadng Determne a color for each flled pxel 11
! " Shadng How do we choose a color for each flled pxel? Each llumnaton calculaton for a ray from the eyepont through the vew plane provdes a radance sample» How do we choose where to place samples?» How do we flter samples to reconstruct mage? Emphass on methods that can be mplemented n hardware Angel Fgure 6.34 Ray Castng Smplest shadng approach s to perform ndependent lghtng calculaton for every pxel When s ths unnecessary? I = I E + K A I AL + ( K D ( N L ) I + K S ( V R ) n I ) 12
# Polygon Shadng Can take advantage of spatal coherence Illumnaton calculatons for pxels covered by same prmtve are related to each other I = I E + K A I AL + ( K D ( N L ) I + K S ( V R ) n I ) Polygon Shadng Algorthms Flat Shadng Gouraud Shadng Phong Shadng 13
Polygon Shadng Algorthms Flat Shadng Gouraud Shadng Phong Shadng Flat Shadng What f a faceted object s llumnated only by drectonal lght sources and s ether dffuse or vewed from nfntely far away I = I E + K A I AL + ( K D ( N L ) I + K S ( V R ) n I ) 14
$ % & Flat Shadng One llumnaton calculaton per polygon Assgn all pxels nsde each polygon the same color N Flat Shadng Objects look lke they are composed of polygons OK for polyhedral objects Not so good for ones wth smooth surfaces 15
Polygon Shadng Algorthms Flat Shadng Gouraud Shadng Phong Shadng Gouraud Shadng What f smooth surface s represented by polygonal mesh wth a normal at each vertex? Watt Plate 7 I = I E + K A I AL + ( K D ( N L ) I + K S (V R ) n I ) 16
' Gouraud Shadng Method 1: One lghtng calculaton per vertex Assgn pxels nsde polygon by nterpolatng colors computed at vertces Gouraud Shadng Blnearly nterpolate colors at vertces down and across scan lnes 17
Gouraud Shadng Smooth shadng over adjacent polygons Curved surfaces Illumnaton hghlghts Soft shadows ( ) * Mesh wth shared normals at vertces Watt Plate 7 Gouraud Shadng Produces smoothly shaded polygonal mesh, + Pecewse lnear approxmaton Need fne mesh to capture subtle lghtng effects Flat Shadng Gouraud Shadng 18
Polygon Shadng Algorthms Flat Shadng Gouraud Shadng Phong Shadng Phong Shadng What f polygonal mesh s too coarse to capture llumnaton effects n polygon nterors? I = I E + K A I AL + ( K D ( N L ) I + K S ( V R ) n I ) 19
- Phong Shadng Method 2: One lghtng calculaton per pxel Approxmate surface normals for ponts nsde polygons by blnear nterpolaton of normals from vertces Phong Shadng Blnearly nterpolate surface normals at vertces down and across scan lnes 20
Polygon Shadng Algorthms Wreframe Gouraud Flat Phong Watt Plate 7 Shadng Issues Problems wth nterpolated shadng: 2 1 0 /. Polygonal slhouettes Perspectve dstorton Orentaton dependence (due to blnear nterpolaton) Problems at T-vertces Problems computng shared vertex normals 21
3 4 5 6 7 8 9 : Summary 2D polygon scan converson Pant pxels nsde prmtve Sweep-lne algorthm for polygons Polygon Shadng Algorthms Flat Less expensve Gouraud Phong Ray castng More accurate Key deas: Samplng and reconstructon Spatal coherence 22