130 likes | 138 Views
This article explains the scan-conversion algorithm used in the graphics pipeline for rendering triangles, including perspective transformation, frustum clipping, and pixel coverage. It also highlights why scan-conversion is faster than ray-casting.
E N D
Graphic pipeline • Scan-conversion algorithm (high level) • Pixels covered by a triangle’s projection • Why scan-conversion is faster than ray-casting • Conversion of points and vectors to screen coordinates
Scan-conversion algorithm (high level) For each triangle T do For each pixel P covered by the projection of T { If (T is in front of previously visited triangles seen through P) { Update the color of P}} The “in front of” test is accomplished by storing the distance to the nearest triangle encountered so far along ray(E,P) in the zbuffer Z[P] The color is computed through linear interpolation of the colors at the vertices of the triangle
Graphic Pipeline • Model transforms • Vertex lighting • Viewing transform • Frustum clipping • Perspective transform • Slope computation • Scan-conversion
Conversion screen coordinates Screen center O, horizontal axis unit vector I, viewing direction K. • Compute the (x,y,z) coordinates of a point Q in the screen coordinate system Q x=OQ•I y=OQ•J z=OQ•K J I K O
Clipping • What does it do? • Trims each triangle to the portion that projects onto the screen • Why? • To avoid processing pixels that do not exist • How? • Could (should?) do this during scan-conversion • clip the range of L to the vertical range of the screen • clip the pixel range [S..E] to the horizontal range of the screen • Usually done as a separate preprocessing stage • In model space (before perspective transform) • Intersect the triangle the viewing frustum • Intersection of 5 or 6 half-spaces • Or in image space (after perspective) • Using axis-aligned half-spaces
M Clipping a triangle by a half-space • Compute the intersection of triangle (A,B,C) with half-space H(Q,N) defined by {P:N•PQ>0} • If (N•AQ)(N•BQ)<0 • Insert intersection point M • Binary search or • Division • t:= N•AQ / N•BA • M:=A+tAB • Repeat for other 2 edges • Discard bad vertices (B) • Triangulate • How complex can the result be? • Block/plane intersection : convex hexagon • Intersect it with a triangle C B A N Q Could we get 10 vertices?
Perspective • Transform the model so that the (x,y) of each vertex correspond to its projection on the screen. • But if we set z to 0, we will not know what is hidden We need a perspective transform that preserves depth order!
Pixels covered by a triangle’s projection • Their center must fall inside the projection of the triangle • We need only to worry about the pixels inside the axis aligned rectangle around the projection of T • Could test each one of them against the triangle (expensive) • Could update linear inequality of the 3 clipping half-spaces • F(x+1,y) = f(x,y) + constant • Usually done in scan-line • Establish leading and trailing pixels • Using slope of edges
S P E L A naïve 3D graphics rasterization process Clear z-buffer Z[*,*] and back frame buffer I[*,*] For each triangle (A,B,C) do Lit A, B, C using normals, surface attributes, lighting parameters Transform vertices {A’=dA/(d+A.z); B’=dC/(d+B.z); C’=dC/(d+C.z)} Compute slopes for c (color), z (depth), and leading/trailing edges For each scanline L covered by triangle (A’,B’,C’) do Compute pixel range [S..E] by interpolation on leading/trailing edges Compute z and c at pixel (S,L, by interpolation on the leading edge For each pixel P in [S..E] do Compute z and c using interpolation If z < Z[P,L] then {Z[P,L]:=z; I[P,L]:=c} Swap back and front frame buffer to show image
Rasterization costs • Per vertex • Lighting calculations • Perspective transformation • Slopes calculation • Per pixel • Interpolations of z, c (and leading trailing edges, amortized) • Read, compare, right to update the z-buffer and frame buffer
Ray-casting / scan-conversion similarities • Ray-casting: • For each pixel P do • For each triangle T projecting over P do • Decide if the triangle is visible • Compute reflected color and store in the pixel • Scan-conversion: • For each triangle T do • For each pixel P covered by the projection of T do • Decide if the triangle is visible • Compute reflected color and store in the pixel
Scan-conversion is faster than ray-casting • Ray casting consider all ray-triangle pairs. • Scan-conversion only considers pixels covered by each triangle. • 10,000 times less work if you have 10x10 pixel triangles • Ray-casting requires multiplications and divisions for selecting which triangles cover a pixel and for computing the corresponding depth. • Scan-conversion uses a constant number of multiplications and division per triangle (to compute vertex projections and slopes), but only additions to test which pixels are covered and to compute the color and depth for each pixel.