CS3500 Computer Graphics Module: Scan Conversion: P. J. Narayanan
CS3500 Computer Graphics Module: Scan Conversion: P. J. Narayanan
CS3500
Modelling
Object Coords World Coords
View Orientation
Camera Coords
View Mapping
Normalized Coords
Clipping Viewport
Screen Coords
Object Txform
Camera Position
Camera Specs
Window Specs
CS3500
Scan Conversion
CS3500
Scan Conversion
1 0 1 0 1 0 1 0 1 0 1 0
CS3500
1 0 1 0
1 0 1 0
Scan Conversion
Incremental Algorithm
Function DrawLine(x1, y1, x2, y2, colour) x x2 x1, y y2 y1, slope y/x x x1, y y1 While (x < x2) WritePixel (x, round(y), colour) x x + 1, y y + slope EndWhile WritePixel (x2, y2, colour) EndFunction
CS3500
Scan Conversion
Points to Consider
If abs(slope) > 1, step through y values, adding inverse slopes to x at each step. Simple algorithm, easy to implement. Need oating point calculations (add, round), which are expensive. Can we do with integer arithmetic only? Yes: Bresenhams Algorithm We will study a simplied version of it called the Mid-Point Line Algorithm.
CS3500 Scan Conversion
CS3500
Scan Conversion
Pseudocode
Function DrawLine (l, m, i, j, colour) a j m, b (l i), x l, y m d 2a + b, E 2a, N E 2(a + b) While (x < i) WritePixel(x, y, colour) if (d < 0) // East d d + E , x x + 1 else // North-East d d + N E , x x + 1, y y + 1 EndWhile WritePixel(i, j, colour) EndFunction
CS3500 Scan Conversion
1 0 1 0 1 0 1 0 1 0 1 0
Choice between E and SE neighbours between the vertical and the 45 degree lines.
CS3500 Scan Conversion
Pseudocode
Function DrawCircle (r, colour) x 0, y r, d 1 r CirclePoints (x, y, colour) While (x < y) if (d < 0) // East d d + 2 x + 3, x x + 1 else // South-East d d + 2 (x y) + 5, x x + 1, y y 1 CirclePoints (x, y, colour) EndWhile EndFunction
CS3500 Scan Conversion
Eliminate Multiplication?
Current selection is E: What are the new s? = 2(x + 1) + 3 = E + 2 E = 2(x + 1 y) + 5 = SE + 2 SE Current selection is SE: What are the new s? = 2(x + 1) + 3 = E + 2 E = 2(x + 1 (y 1)) + 5 = SE + 4 SE if (d < 0) // East d d + E , E += 2, SE += 2, x++ else // South-East d d + SE, E += 2, SE += 4, x++, y = y 1
CS3500 Scan Conversion
Patterned Line
Represent the pattern as an array of booleans/bits, say, 16 pixels long. Fill rst half with 1 and rest with 0 for dashed lines. Perform WritePixel(x, y) only if pattern bit is a 1. if (pattern[i]) WritePixel(x, y) where i is an index variable starting with 0 giving the ordinal number (modulo 16) of the pixel from starting point.
CS3500 Scan Conversion
Shared Points/Edges
It is common to have points common between two lines and edges between two polygons. They will be scan converted twice. Sometimes harmful. Not efcient.
Solution: Treat the intervals closed on the left and open on the right. [xm, xM ) & [ym, yM ) Thus, edges of polygons on the top and right boundaries are not drawn.
CS3500 Scan Conversion
CS3500
Scan Conversion
Clipping
Often, many points map to outside the range in the normalized 2D space. Think of the FB as an innite canvas, of which a small rectangular portion is sent to the screen. Lets get greedy: draw only the portion that is visible. That is, clip the primitives to a clip-rectangle. Scissoring: Doing scan-conversion and clipping together.
CS3500 Scan Conversion
Clipping Points
Clip rectangle: (xm, ym) to (xM , yM ). For (x, y): xm x xM , ym y yM
Can use this to clip any primitives: Scan convert normally. Check above condition before writing the pixel. Simple, but perhaps we do more work than necessary. Analytically clip to the rectangle, then scan convert.
CS3500 Scan Conversion
Clipping Lines
CS3500
Scan Conversion
CS3500
Scan Conversion
Cohen-Sutherland Algorithm
Identify line segments that can be accepted trivially. Identify line segments that can be rejected trivially. For the rest, identify the segment that falls within the cliprectangle. For ease of this, assign outcodes to each of the 9 regions.
CS3500
Scan Conversion
Region Outcodes
Bits from left to right: y > yM y < ym x > xM x < xm
1001 yM 0001 0000 0010 1000 1010
CS3500
Scan Conversion
Overall Algorithm
Accept: Reject: code1 | code0 == 0 code1 & code0 != 0
Else, identify one of the boundaries crossed by the line segment and clip it to the inside. Do it in some order, say,
TOP, RIGHT, BOTTOM , LEFT . BOTTOM
= 0100,
LEFT
Scan Conversion
Whole Algorithm
0 code0 ComputeCode(x0, y0), code1 1 if (! (code1 | code0)) Accept and Return 2 if (code1 & code0) Reject and Return 3 code code1 ? code1 : code0 4 if (code & TOP) Intersect with yM line. 5 elsif (code & RIGHT) Intersect with xM line. 6 elsif (code & BOTTOM) Intersect with ym line. 7 elsif (code & LEFT) Intersect with xm line. 8 if (code == code1) Replace EndPoint1. 9 else Replace EndPoint0. 10 Goto step 1.
CS3500 Scan Conversion
CS3500
Scan Conversion
Discussion
Simple logical operations to check intersections etc. Not efcient, as external intersections are not eliminated. In the worst case, 3 intersections may be computed and then the line segment could be rejectd. 4 intersections may be computed before accepting a line segment.
CS3500
Scan Conversion
Clipping Polygons
Restrict drawing/lling of a polygon to the inside of the clip rectangle. A convex polygon remains convex after clipping. A concave polygon can be clipped to multiple polygons. Can perform by intersecting to the four clip edges in turn.
CS3500
Scan Conversion
An Example
CS3500
Scan Conversion
An Example
CS3500
Scan Conversion
Sutherland-Hodgman Algorithm
Input: A list of vertices v1, v2, , vn. Implied edges from vi to vi+1 and from vn to v1. Output: Another list of vertices giving the clipped polygon. Method: Clip the entire polygon to the innite line for each clip edge in turn. Four passes, the output of each is a partially clipped polygon used as input to the next. Post-processing to eliminate degenerate edges.
CS3500 Scan Conversion
Algorithm Detail
Process edges one by one and clip it to a line. Start with the edge E(vn, v1). Compare the current edge E(vi1, vi) with the current clip line. Clip it to lie within the clip rectangle. Repeat for the next edge E(vi, vi+1). Till all edges are processed. When processing E(vi1, vi), treat vi1 as the in vertex and vi as the out vertex.
CS3500 Scan Conversion
In Out
In Out
CS3500
Scan Conversion
Function SuthHodg()
p last(inVertexList) // Copy, not remove while (notEmpty(inVertexList)) s p, p removeNext(inVertexList) if (inside(p, clipBoundary)) if (inside(s, clipBoundary)) addToList(p, outVertexList) // Case 1 else i intersect(s, p, clipBoundary) // Case 4 addToList(i, outVertexList), addToList(p, outVertexList) elsif (inside(s, clipBoundary)) // Case 2 addToList(intersect(s, p, clipBoundary), outVertexList)
CS3500 Scan Conversion
Complete Algorithm
Invoke SuthHodg() 4 times for each clip edge as clipBoundary. The outVertexList after one run becomes the inVertexList for the next. Uses list data structures to implement polygons. Function inside() determines if a point is in the inside of the clip-boundary. We can dene it as being on the left when looking from rst vertex to the second. Can be extended to clip to any convex polygonal region!
CS3500 Scan Conversion
Filled Rectangles
Write to all pixels within the rectangle. Function FilledRectangle (xm, xM , ym, yM , colour) for xm x xM do for ym y yM do WritePixel (x, y. colour) EndFunction How about non-upright rectangles? General polygons?
CS3500
Scan Conversion
Filled Polygons
For each scan line, identify spans of the polygon interior. Strictly interior points only. For each scan line, the parity determines if we are inside or ouside the polygon. Odd is inside, Even is outside. Trick: End-points count towards parity enumeration only if it is a ymin point. Span extrema points and other information can be computed during scan conversion. This information is stored in a suitable data structure for the polygon.
CS3500 Scan Conversion
Parity Checking
D
F C E
CS3500
Scan Conversion
Edge Coherence
If scan line y intersects with an edge E, it is likely that y + 1 also does. (Unless intersection is the ymax vertex.) When moving from y to y + 1, the X-coordinate goes from x to x + 1/m. 1/m = (x2 x1)/(y2 y1) = x / y Store the integer part of x, the numerator (x) and the denominator (y) of the fraction separately. For next scan line, add x to numerator. If sum goes > y, increment integer portion, subtract y from numerator.
CS3500 Scan Conversion
Special Concerns
Fill only strictly interior pixels: Fractions rounded up when even parity, rounded down when odd. Intersections at integer pixels: Treat interval closed on left, open on right. Intersections at vertices: Count only ym vertex for parity. Horizontal edges: Do not count as ym!
CS3500
Scan Conversion
25
...
20 10 5
... ...
10, 15, 15, 5
CS3500
Scan Conversion
y = 15 y = 22 y = 27
AET
20, 9, 10, 15
32, 31, 5, 22
AET
30, 5, 0, 10
32, 32, 5, 22
AET
30, 5, 0, 10
32, 34, 5, 22
CS3500
Scan Conversion
Pattern Filling
A rectangular bit-map with the desired pattern can be used to ll the interior with a pattern. If pattern(i mod M, j mod N ), draw pixel, else ignore. i, j are row, col indices. Lower left corner at 0 and 0. M, N are the pattern height and width.
CS3500
Scan Conversion
Efcient algorithms required since scan conversion is done repeatedly. 2D Scan Conversion is all, even for 3D graphics.
CS3500 Scan Conversion