General scheme of a 2D CG application 2D Image Synthesis modeling Virtual world model Balázs Csébfalvi world defined in a 2D plane image synthesis Department of Control Engineering and Information Technology email: [email protected] Web: http://www.iit.bme.hu/~cseb Metaphor: 2D drawing or sketching 2 /25 Raster graphics systems 2D image synthesis 3 /25 Vectorization 4 /25 Modeling transformation v y TM r(t) βv r2 r1 v βv p u αu o rn αu [0,1]: 0, t1 , t2 , ... , tn , 1 r1 =r(0), r2 = r(t2), … , rn = r(1) x u [x, y] = o +αu + βv [x, y, 1] = [α, β, 1] 5 /25 ux uy vx vy ox oy TM 0 0 1 6 /25 1 Viewport transformation Composite transformation [X, Y, 1] = ([α, β, 1] TM ) TV [X, Y, 1] = [α, β, 1] (TM TV ) = [α, β, 1] TC n n TV X = (x-wx) vw/ww + vx Y = (y-wy) vh/wh + vy vw/ww 0 0 0 vh/wh 0 [X, Y, 1] = [x, y, 1] v -w v /w v -w v /w 1 x x w w y y h h n calculation of the viewport transformation T V for each object o • determine TM • TC = TM TV • for each point of object o: transformation by TC endfor 7 /25 Clipping n 8 /25 Clipping a segment by a half plane Due to the vectorization, clipping is performed on points, segments, and polygons xmax • Clipping points x > xmin , x1, y1 x < xmax , y > ymin, x2, y2 y < ymax n ymax n ymin xmin xi, yi n xmax x(t) = x1 + (x2 - x1)t, y(t) = y1 + (y2 - y1)t x = xmax • intersection point: • xmax = x1 + (x2 - x1)t ð t = (xmax-x1)/(x2-x1) xi = xmax yi = y1 + (y2 - y1) (xmax-x1)/(x2-x1) 9 /25 Cohen-Sutherland algorithm 10 /25 Cohen-Sutherland algorithm #define Code(p) ((p.x < xmin) + ((p.y < ymin)<<1) + \\ ((p.x > xmax)<<2) + ((p.y > ymax)<<3) 1001 1000 1100 0001 0000 0100 0011 0010 bool LineClip(Vector& p1, Vector& p2) { for( ; ; ) { int c1 = Code(p1), c2 = Code(p2); if (c1 == 0 && c2 == 0) return true; if ((c1 & c2)) return false; if ((c1 else if else if else if else if else if else if else if 0110 & 1)) ((c1 & ((c1 & ((c1 & ((c2 & ((c2 & ((c2 & ((c2 & 2)) 4)) 8)) 1)) 2)) 4)) 8)) p1 p1 p1 p1 p2 p2 p2 p2 = = = = = = = = IntersectX(p1, IntersectY(p1, IntersectX(p1, IntersectY(p1, IntersectX(p1, IntersectY(p1, IntersectX(p1, IntersectY(p1, p2, p2, p2, p2, p2, p2, p2, p2, xmin); ymin); xmax); ymax); xmin); ymin); xmax); ymax); } 11 /25 } 12 /25 2 Polygon Clipping n n Sutherland-Hodgman Polygon Clipping modification of line clipping goal: one or more closed areas n n display of a polygon processed by a lineclipping algorithm display of a correctly clipped polygon processing polygon boundary as a whole against each window edge output: list of vertices original polygon clip left clip right clip bottom clipping a polygon against successive window boundaries 13 /25 Sutherland-Hodgman Polygon Clipping n clip top 14 /25 Sutherland-Hodgman Polygon Clipping four possible edge cases out → in in → in output: V’1, V2 V2 in → out V’1 out → out no output successive processing of pairs of polygon vertices against the left window boundary clipping a polygon against the left boundary of a window, starting with vertex 1. Primed numbers are used to label the points in the output vertex list for this window boundary finished! 15 /25 Sutherland-Hodgman Algorithm: Combination of the 4 Passes 16 /25 Example n n n 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 17 /25 pipeline of boundary clippers to avoid intermediate vertex lists 1st clip: left 2 2’ 1’ 2” 3 3’ 1 2nd clip: bottom 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 [1’, 2, 2’, 2”] 18 /25 3 Sutherland-Hodgman Polygon Clipping n Sutherland-Hodgeman polygon clipping extraneous lines for concave polygons: • split into separate convex parts • final check of output vertex list or clipping the concave polygon with the SutherlandHodgeman clipper produces two connected areas PolygonClip(p[n] ð q[m]) m = 0; for( i=0; i < n; i++) { if (p[i] internal) { q[m++] = p[i]; if (p[i+1] is external) q[m++] = Intersect(p[i], p[i+1], borderline); } else { if (p[i+1] is internal) q[m++] = Intersect(p[i], p[i+1], borderline); } } } 19 /25 Rasterization 20 /25 Line drawing model transformation Pixels 10-50 nanosec / pixel clipping rasterization Line equation: y = mx + b Geometrical primitives: 0.1-1 microsec / primitive Line drawing pixel operations frame buffer x1 x2 for( x = x1; x <= x2; x++) { Y = m*x + b; y = Round( Y ); write( x, y ); } 21 /25 Incremental approach n n n 22 /25 DDA line drawing Calculate Y(X) • Y(X) = F( Y(X-1) ) = Y(X-1) + dY/dX Line drawing: Y(X) = mX + b • Y(X) = F( Y(X-1) ) = Y(X-1) + m Addition: fix-point arithmetic: y = Y 2T • T: error < 1 for the longest segment • N 2-T < 1: T > log2 N • round( y ): y+0.5 is truncated DDADrawLine(x1, y1, x2, y2) { m = (y2 - y1)/(x2 - x1) y = y1 + 0.5 FOR X = x1 TO x2 { Y = round(y) trunc(y) WRITE(X, Y, color) y = y+m } } 23 /25 24 /25 4 Bresenham’s Line Algorithm DDA line-drawing hardware n X X counter y register CLK x1 Faster than simple DDA • incremental integer calculations • adaptable to circles, other curves Y y = m.(xk + 1) + b Σ y1 Section of a display screen where a straight line segment is to be plotted, starting from the pixel at column 10 on scan line 11 m 25 /25 26 /25 Bresenham’s Line-Drawing Algorithm (1/4) Bresenham’s Line Algorithm Section of the screen grid showing a pixel in column xk on scan line yk that is to be plotted along the path of a line segment with slope 0<m<1 y = m.(xk + 1) + b dlower = y − yk = = m.(xk + 1) + b − yk dupper = (yk + 1) − y = = yk + 1 − m.(xk + 1) − b dlower − dupper = 2m.(xk + 1) − 2yk + 2b − 1 27 /25 28 /25 Bresenham’s Line-Drawing Algorithm (2/4) Bresenham’s Line-Drawing Algorithm (3/4) dlower − dupper = = 2m.(xk + 1) − 2yk + 2b − 1 current decision value: pk = ∆x.(dlower − dupper) = 2∆y. xk − 2∆x. yk + c m = ∆y/∆x next decision value: ∆x, ∆y: separations of endpoint positions decision parameter: pk = ∆x.(dlower − dupper) = = 2∆y. xk − 2∆x. yk + c has the same sign as (dlower − dupper) 29 /25 pk+1 = 2∆y.xk+1 − 2∆x. yk+1 + c + 0 + pk – 2∆y.xk + 2∆x.yk – c = starting decision = pkk + 2∆value: y − 2∆x..(yk+1 k+1 − yk k) p0 = 2∆y − ∆x 30 /25 5 Bresenham: Example Bresenham’s Algorithm (4/4) 1. store left line endpoint in (x0,y0) 2. plot pixel (x0,y0) 3. calculate constants ∆x, ∆y, 2∆y, 2∆y − 2∆x, and obtain p0 = 2∆y − ∆x 4. At each xk along the line, perform test: if pk <0 then plot pixel (xk +1,yk ); pk+1 = pk + 2∆y else plot pixel (xk +1,yk +1); pk+1 = pk + 2∆y − 2∆x 5. perform step 4 ∆x − 1 times. line from (20/10) to (30/18) 31 /25 Flood fill 2 2 1 2 1 02 1 1 32 /25 Specific area filling Flood(x, y) { if (pixel[x][y] is internal) { Write(x, y, color); Flood(x, y-1); Flood(x, y+1); starting point Flood(x-1, y); Flood(x+1, y); } } n n Internal point =BLUE Based on the geometrical representation • p1,p2,...,pn vertices • p1-p2, p2-p3,...,pn-p1edges 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 33 /25 Polygon filling 34 /25 Accelerated filling n p2 n Check only the active edges Incremental intersection point calculation Dx/ Dy p4 x1 y x4 x3 Dx/ Dy x2 y p1 p3 35 /25 x(y) x(y+1) 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=1024 ymax Dx/ Dy x1 next ymax Dx/ Dy x1 next ymin=1 ymin=0 37 /25 7
© Copyright 2026 Paperzz