General scheme of a 2D CG application 2D Image Synthesis Balázs Csébfalvi modeling image synthesis Virtual world model world defined in a 2D plane Department of Control Engineering and Information Technology email: cseb@iit.bme.hu Web: http://www.iit.bme.hu/~cseb Metaphor: 2D drawing or sketching 2 /25 Raster graphics systems 2D image synthesis 3 /25 4 /25 Vectorization Modeling transformation v y v r(t) T M βv αu u r 2 rn βv p o r [0,]: 0, t, t 2,..., t n, r =r(0), r 2 = r(t 2 ),, r n = r() αu [x, y] = o +αu + βv u [x, y, ] = [α, β, ] u x u y 0 v x v y 0 o x o y x 5 /25 T M 6 /25
Viewport transformation Composite transformation [X, Y, ] = ([α, β, ] T M ) T V [X, Y, ] = [α, β, ] (T M T V ) = [α, β, ] T C calculation of the viewport transformation T V for each object o X = (x-w x ) v w /w w + v T x V Y = (y-w y ) v h /w h + v v y w /w w 0 0 0 v h /w h 0 [X, Y, ] = [x, y, ] v x -w x v w /w w v y -w y v h /w h 7 /25 determine T M T C = T M T V for each point of object o: transformation by T C endfor 8 /25 Clipping Due to the vectorization, clipping is performed on points, segments, and polygons Clipping points x > x min, x < x max, y > y min, y min x min y max x max y < y max 9 /25 Clipping a segment by a half plane x max x i, y i x, y x 2, y 2 x(t) = x + (x 2 - x )t, y(t) = y + (y 2 - y )t x = x max intersection point: x max = x + (x 2 - x )t t = (x max -x )/(x 2 -x ) x i = x max y i = y + (y 2 - y ) (x max -x )/(x 2 -x ) 0 /25 Cohen-Sutherland algorithm Cohen-Sutherland algorithm 00 000 00 000 0000 000 00 000 00 /25 #define Code(p) ((p.x < xmin) + ((p.y < ymin)<<) + \\ ((p.x > xmax)<<2) + ((p.y > ymax)<<3) bool LineClip(Vector& p, Vector& p2) { for( ; ; ) { int c = Code(p), c2 = Code(p2); if (c == 0 && c2 == 0) return true; if ((c & c2)) return false; if ((c & )) p = IntersectX(p, p2, xmin); else if ((c & 2)) p = IntersectY(p, p2, ymin); else if ((c & 4)) p = IntersectX(p, p2, xmax); else if ((c & 8)) p = IntersectY(p, p2, ymax); else if ((c2 & )) p2 = IntersectX(p, p2, xmin); else if ((c2 & 2)) p2 = IntersectY(p, p2, ymin); else if ((c2 & 4)) p2 = IntersectX(p, p2, xmax); else if ((c2 & 8)) p2 = IntersectY(p, p2, ymax); 2 /25 2
Polygon Clipping modification of line clipping goal: one or more closed areas Sutherland-Hodgman Polygon Clipping processing polygon boundary as a whole against each window edge output: list of vertices display of a polygon processed by a lineclipping algorithm display of a correctly clipped polygon 3 /25 original polygon clip left clip right clip bottom clipping a polygon against successive window boundaries clip top 4 /25 Sutherland-Hodgman Polygon Clipping four possible edge cases out fi in output: V, V 2 in fi in V 2 in fi out out fi out V no output successive processing of pairs of polygon vertices against the left window boundary 5 /25 Sutherland-Hodgman Polygon Clipping clipping a polygon against the left boundary of a window, starting with vertex. Primed numbers are used to label the points in the output vertex list for this window boundary finished! 6 /25 Sutherland-Hodgman Algorithm: Combination of the 4 Passes The polygon is clipped against each of the 4 borders separately, that would produce 3 intermediate results. By calling the 4 tests recursively, (or by using a clipping pipeline) every result point is immediately processed on, so that only one result list is produced 7 /25 Example pipeline of boundary clippers to avoid intermediate vertex lists 3 st clip: left Processing the vertices of the polygon through a boundary-clipping pipeline. After all vertices are processed through the pipeline, the vertex list for the clipped polygon is [, 2, 2, 2 ] 2 2 3 2 2 nd clip: bottom 8 /25 3
Sutherland-Hodgman Polygon Clipping Sutherland-Hodgeman polygon clipping extraneous lines for concave polygons: split into separate convex parts or final check of output vertex list clipping the concave polygon with the Sutherland- Hodgeman clipper produces two connected areas 9 /25 PolygonClip(p[n] q[m]) m = 0; for( i=0; i < n; i++) { if (p[i] internal) { q[m++] = p[i]; if (p[i+] is external) q[m++] = Intersect(p[i], p[i+], borderline); else { if (p[i+] is internal) q[m++] = Intersect(p[i], p[i+], borderline); 20 /25 Rasterization Line drawing model transformation clipping Geometrical primitives: 0.- microsec / primitive Line equation: y = mx + b Line drawing Pixels 0-50 nanosec / pixel rasterization pixel operations frame buffer x x2 for( x = x; x <= x2; x++) { Y = m*x + b; y = Round( Y ); write( x, y ); 2 /25 22 /25 Incremental approach Calculate Y(X) Y(X) = F( Y(X-) ) = Y(X-) + dy/dx Line drawing: Y(X) = mx + b Y(X) = F( Y(X-) ) = Y(X-) + m Addition: fix-point arithmetic: y = Y 2 T T: error < for the longest segment N 2 -T < : T > log 2 N round( y ): y+0.5 is truncated DDA line drawing DDADrawLine(x, y, x2, y2) { m = (y2 - y)/(x2 - x) y = y + 0.5 FOR X = x TO x2 { Y = round(y) trunc(y) WRITE(X, Y, color) y = y+m 23 /25 24 /25 4
DDA line-drawing hardware Bresenham s Line Algorithm X Y Faster than simple DDA incremental integer calculations adaptable to circles, other curves X counter y register y = m. (x k + ) + b CLK x y m Σ Section of a display screen where a straight line segment is to be plotted, starting from the pixel at column 0 on scan line 25 /25 26 /25 Bresenham s Line Algorithm Section of the screen grid showing a pixel in column x k on scan line y k that is to be plotted along the path of a line segment with slope 0<m< 27 /25 Bresenham s Line-Drawing Algorithm (/4) y = m. (x k + ) + b d lower = y y k = = m. (x k + ) + b y k d upper = (y k + ) y = = y k + m. (x k + ) b d lower d upper = 2m. (x k + ) 2y k + 2b 28 /25 Bresenham s Line-Drawing Algorithm (2/4) Bresenham s Line-Drawing Algorithm (3/4) d lower d upper = = 2m. (x k + ) 2y k + 2b m = y/ x x, y: separations of endpoint positions decision p k = x. (d lower d upper ) = parameter: = 2 y. x k 2 x. y k + c has the same sign as (d lower d upper ) 29 /25 current decision value: p k = x. (d lower d upper ) = 2 y. x k 2 x. y k + c next decision value: p = 2 y. x k+ 2 x. k+ y k+ + c + 0 + p k 2Dy. x k + 2Dx. y k c = starting decision = p value: k + 2 y 2 x. (y k+ y k ) p 0 = 2 y x 30 /25 5
Bresenham s Algorithm (4/4). store left line endpoint in (x 0,y 0 ) 2. plot pixel (x 0,y 0 ) 3. calculate constants x, y, 2 y, 2 y - 2 x, and obtain p 0 = 2 y - x 4. At each x k along the line, perform test: if p k <0 then plot pixel (x k +,y k ); p k+ = p k + 2 y else plot pixel (x k +,y k +); p k+ = p k + 2 y - 2 x 5. perform step 4 x - times. Bresenham: Example line from (20/0) to (30/8) 3 /25 32 /25 Flood fill 2 2 2 02 starting point Internal point =BLUE Flood(x, y) { if (pixel[x][y] is internal) { Write(x, y, color); Flood(x, y-); Flood(x, y+); Flood(x-, y); Flood(x+, y); 33 /25 Specific area filling Based on the geometrical representation p,p2,...,pn vertices p-p2, p2-p3,...,pn-pedges Internal pixels cast a ray into the infinity the number of intersection points with the edges has to be odd infinity : the border line of the viewport after the clipping 34 /25 Polygon filling p2 Accelerated filling Check only the active edges Incremental intersection point calculation y p4 x x4 x3 x2 Dx/Dy Dx/Dy p p3 y 35 /25 x(y) x(y+) x(y+2) 36 /25 6
List of active edges AET: active edge table AEL ymax Dx/Dy x next ymax Dx/Dy x next ymin=024 ymax Dx/Dy x next ymax Dx/Dy x next ymin= ymin=0 37 /25 7