Drawing and Rasterization
Drawing and Rasterization
Drawing and Rasterization
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Computer Graphics<br />
Subodh Kumar<br />
Dept of Computer Sc. & Engg.<br />
IIT Delhi
Pixel/Screen Coordinates
Pixel/Screen Coordinates
Pixel/Screen Coordinates<br />
X screen<br />
= 0 1 w-1
Pixel/Screen Coordinates<br />
X screen<br />
= 0 1 w-1<br />
Caution:<br />
In OpenGL this is X = -1, pixel centers are at int + .5
Polygon <strong>Rasterization</strong><br />
n Draw pixels “intersected” by polygon<br />
n But one color per pixel..<br />
n And 2D polygon?<br />
n Can iterate over rows (scan lines)<br />
n Find intersection of scan-lines with edges<br />
n All lines <strong>and</strong> edges?
Polygon <strong>Rasterization</strong><br />
n Draw pixels “intersected” by polygon<br />
n But one color per pixel..<br />
n And 2D polygon?<br />
n Can iterate over rows (scan lines)<br />
n Find intersection of scan-lines with edges<br />
n All lines <strong>and</strong> edges?
Triangle <strong>Rasterization</strong> Issues<br />
n Sliver<br />
problem
Triangle <strong>Rasterization</strong> Issues<br />
n Animating<br />
Slivers
Triangle <strong>Rasterization</strong> Issues<br />
n Shared Edge Ownership
Polygon <strong>Rasterization</strong> Rules<br />
n Draw pixel, if center inside polygon<br />
n What if it is at an edge?<br />
n Draw on left edge, Skip on right edge<br />
n Unless Horizontal<br />
n Then, draw a bottom edge, skip a top edge<br />
Find lo, hi y (scanline)<br />
For y=lo to hi:<br />
Find left extreme (of span)<br />
Find right extreme (of span)<br />
For left to right, draw pixel
Triangle <strong>Rasterization</strong>
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:<br />
n StartX = StartX previous + 1/m
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:<br />
n StartX = StartX previous + 1/m<br />
n Similarly find Right extreme
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:<br />
n StartX = StartX previous + 1/m<br />
n Similarly find Right extreme<br />
n Extrema are on two edges
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:<br />
n StartX = StartX previous + 1/m<br />
n Similarly find Right extreme<br />
n Extrema are on two edges<br />
n (sometimes at vertices)
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:<br />
n StartX = StartX previous + 1/m<br />
n Similarly find Right extreme<br />
n Extrema are on two edges<br />
n (sometimes at vertices)<br />
n Which two?
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:<br />
n StartX = StartX previous + 1/m<br />
n Similarly find Right extreme<br />
n Extrema are on two edges<br />
n (sometimes at vertices)<br />
n Which two?<br />
n Start with two at the lowest vertex
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:<br />
n StartX = StartX previous + 1/m<br />
n Similarly find Right extreme<br />
n Extrema are on two edges<br />
n (sometimes at vertices)<br />
n Which two?<br />
n Start with two at the lowest vertex<br />
n What if there are two lowest vertices?
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:<br />
n StartX = StartX previous + 1/m<br />
n Similarly find Right extreme<br />
n Extrema are on two edges<br />
n (sometimes at vertices)<br />
n Which two?<br />
n Start with two at the lowest vertex<br />
n What if there are two lowest vertices?<br />
n When an edge ends (at a vertex), replace edge
Triangle <strong>Rasterization</strong><br />
n For a scan line: find Left extreme<br />
n Remember from previous line:<br />
n StartX = StartX previous + 1/m<br />
n Similarly find Right extreme<br />
n Extrema are on two edges<br />
n (sometimes at vertices)<br />
n Which two?<br />
n Start with two at the lowest vertex<br />
n What if there are two lowest vertices?<br />
n When an edge ends (at a vertex), replace edge<br />
n What if both edges end?
General Polygon <strong>Rasterization</strong>
General Polygon <strong>Rasterization</strong><br />
n Multiple spans
General Polygon <strong>Rasterization</strong><br />
n Multiple spans<br />
G<br />
F<br />
I<br />
J<br />
A<br />
H<br />
C<br />
B<br />
D<br />
E
General Polygon <strong>Rasterization</strong><br />
n Multiple spans<br />
G<br />
F<br />
I<br />
J<br />
A<br />
H<br />
C<br />
B<br />
D<br />
E
General Polygon <strong>Rasterization</strong><br />
n Multiple spans<br />
G<br />
F<br />
I<br />
J<br />
A<br />
H<br />
C<br />
B<br />
D<br />
E
General Polygon <strong>Rasterization</strong><br />
n Multiple spans<br />
n Count parity<br />
G<br />
F<br />
I<br />
J<br />
H<br />
C<br />
D<br />
E<br />
A<br />
B
General Polygon <strong>Rasterization</strong><br />
n Multiple spans<br />
n Count parity<br />
n How do you count<br />
vertex intersections?<br />
G<br />
F<br />
I<br />
J<br />
A<br />
H<br />
C<br />
B<br />
D<br />
E
General Polygon <strong>Rasterization</strong><br />
n Multiple spans<br />
n Count parity<br />
n How do you count<br />
vertex intersections?<br />
n Divide into triangles<br />
G<br />
F<br />
I<br />
J<br />
A<br />
H<br />
C<br />
B<br />
D<br />
E
Triangle <strong>Rasterization</strong>
Triangle <strong>Rasterization</strong><br />
n Setup:<br />
n Find inverse of edge slopes<br />
n Find bounding box
Triangle <strong>Rasterization</strong><br />
n Setup:<br />
n Find inverse of edge slopes<br />
n Find bounding box<br />
n Rasterize:<br />
n Initialize left, right intersection<br />
n From Bounding Box bottom to Top:<br />
n Update left, right intersection<br />
n Round up or down<br />
n Fill span
Triangle <strong>Rasterization</strong><br />
n Setup:<br />
n Find inverse of edge slopes<br />
n Find bounding box<br />
n Rasterize:<br />
n Initialize left, right intersection<br />
n From Bounding Box bottom to Top:<br />
n Update left, right intersection<br />
n Round up or down<br />
n Fill span<br />
n Interpolate Z, Color?<br />
n Do in fragment shader<br />
n Fill span = create fragment
Triangle <strong>Rasterization</strong>, Really<br />
n Multiple rasterizers?<br />
n Blocked or Strided<br />
n Create tiles of fragments<br />
n In parallel, divide tiles into<br />
sub-tiles<br />
n Each sub-tile shaded<br />
together<br />
n Mask to turn off uncovered<br />
tiles
Triangle <strong>Rasterization</strong>, Really<br />
n Multiple rasterizers?<br />
n Blocked or Strided<br />
n Create tiles of fragments<br />
n In parallel, divide tiles into<br />
sub-tiles<br />
n Each sub-tile shaded<br />
together<br />
n Mask to turn off uncovered<br />
tiles
Triangle <strong>Rasterization</strong>, Really<br />
n Multiple rasterizers?<br />
n Blocked or Strided<br />
n Create tiles of fragments<br />
n In parallel, divide tiles into<br />
sub-tiles<br />
n Each sub-tile shaded<br />
together<br />
n Mask to turn off uncovered<br />
tiles
Triangle <strong>Rasterization</strong>, Really<br />
n Multiple rasterizers?<br />
n Blocked or Strided<br />
n Create tiles of fragments<br />
n In parallel, divide tiles into<br />
sub-tiles<br />
n Each sub-tile shaded<br />
together<br />
n Mask to turn off uncovered<br />
tiles<br />
Fully covered<br />
sub-tile<br />
Partially covered sub-tile
Multi-sampling<br />
n An attempt to increase resolution<br />
n Anti-aliasing: more on this later
Multi-sampling<br />
n An attempt to increase resolution<br />
n Anti-aliasing: more on this later
Multi-sampling<br />
n An attempt to increase resolution<br />
n Anti-aliasing: more on this later
Multi-sampling<br />
n An attempt to increase resolution<br />
n Anti-aliasing: more on this later
n Draw [X 0 , Y 0 ]<br />
Point <strong>Drawing</strong>
n Draw [X 0 , Y 0 ]<br />
Point <strong>Drawing</strong>
n Draw [X 0 , Y 0 ]<br />
Point <strong>Drawing</strong>
Point <strong>Drawing</strong><br />
n Draw [X 0 , Y 0 ] of a certain size
Point <strong>Drawing</strong><br />
n Draw [X 0 , Y 0 ] of a certain size
Point <strong>Drawing</strong><br />
n Draw [X 0 , Y 0 ] of a certain size
Point <strong>Drawing</strong><br />
n Draw [X 0 , Y 0 ] of a certain size
Point <strong>Drawing</strong><br />
n Draw [X 0 , Y 0 ] of a certain size
Line <strong>Drawing</strong><br />
n Draw a line<br />
n Start at [X 0 , Y 0 ]<br />
n End at [X 1 , Y 1 ]
Line <strong>Drawing</strong><br />
n Draw a line<br />
n Start at [X 0 , Y 0 ]<br />
n End at [X 1 , Y 1 ]
Line <strong>Drawing</strong><br />
n Draw a line<br />
n Start at [X 0 , Y 0 ]<br />
n End at [X 1 , Y 1 ]
<strong>Drawing</strong><br />
n Draw a line<br />
n Start at [X 0 , Y 0 ]<br />
n End at [X 1 , Y 1 ]
<strong>Drawing</strong><br />
n Draw line: [X 0 , Y 0 ] TO [X 1 , Y 1 ]<br />
n width: t<br />
Endcap?
<strong>Drawing</strong><br />
n Draw line: [X 0 , Y 0 ] TO [X 1 , Y 1 ]<br />
n width: t<br />
mitered<br />
rounded<br />
bevelled<br />
Endcap?
<strong>Drawing</strong><br />
n Draw line [1, 1] TO [8, 1]<br />
n 7 pixels long<br />
n eliminate last pixel<br />
1 8
Line <strong>Drawing</strong><br />
n Which pixels to draw<br />
n How thick is the line<br />
n How long is the line<br />
n How are the ends drawn<br />
n Do you allow overdraw<br />
n Which direction is the line drawn in<br />
n Find color <strong>and</strong> Z
Line <strong>Drawing</strong> Simplified
Line <strong>Drawing</strong> Simplified
Line <strong>Drawing</strong> Simplified
Line <strong>Drawing</strong> Simplified
Line <strong>Drawing</strong> Simplified<br />
n Draw pixel if<br />
n Line is close to it<br />
n ie, lies in the circle<br />
n Or, the diamond<br />
n Draw pixel if:<br />
n Line exits diamond<br />
n What if line does not exit<br />
the diamond?<br />
n What if line straddles or<br />
ends at a diamond edge
Line <strong>Drawing</strong> Simplified<br />
n Draw pixel if<br />
n Line is close to it<br />
n ie, lies in the circle<br />
n Or, the diamond<br />
n Draw pixel if:<br />
n Line exits diamond<br />
n What if line does not exit<br />
the diamond?<br />
n What if line straddles or<br />
ends at a diamond edge
Efficient Line <strong>Drawing</strong><br />
n Proceed in the direction of smaller slope<br />
n DDA: Digital Difference Analyzer<br />
n Y = mX + i<br />
n m(x+1) + i, m(x+2) + i …<br />
n Successively add m to y <strong>and</strong> round it<br />
n Bresenham’s midpoint algorithm<br />
n Which next pixels are the closest to the line<br />
n aX + bY + c = 0<br />
n Useful for curves as well
Bresenham’s Algorithm<br />
n If slope in [-1:1]<br />
n 3 possibilities
Bresenham’s Algorithm<br />
n If slope in [-1:1]<br />
n 3 possibilities
Bresenham’s Algorithm<br />
n If slope in [-1:1]<br />
n 3 possibilities
Bresenham’s Algorithm
Bresenham’s Algorithm<br />
n If slope in [-1:1]<br />
n 3 possibilities<br />
n If slope in [0:1]?<br />
n On which side of<br />
the next mid-point<br />
does the line lie?
Locate Mid-point<br />
n Y = mX + i<br />
n Y + = m (X+1) + i<br />
n > y+.5?<br />
n Consider:<br />
n F(x,y) = 0<br />
n ie, aX+bY+c = 0<br />
x,y<br />
x+1,y+.5<br />
p
Locate Mid-point<br />
n Y = mX + i<br />
n Y + = m (X+1) + i<br />
n > y+.5?<br />
n Consider:<br />
n F(x,y) = 0<br />
n ie, aX+bY+c = 0<br />
x,y<br />
x+1,y+.5<br />
p<br />
F(p) = 0 => Curve intersects p<br />
F(p) < 0 => Curve passes below p<br />
F(p) > 0 => Curve passes above p
Locate Mid-point<br />
n Y = mX + i<br />
n Y + = m (X+1) + i<br />
n > y+.5?<br />
n Consider:<br />
n F(x,y) = 0<br />
n ie, aX+bY+c = 0<br />
x,y<br />
x+1,y+.5<br />
p<br />
F(p) = 0 => Curve intersects p<br />
F(p) < 0 => Curve passes below p<br />
F(p) > 0 => Curve passes above p<br />
If a > 0
Locate Mid-point<br />
n d = a(x+1) + b(y+.5) + c<br />
n d next =<br />
n ie,<br />
n a(x+2) + b(y+.5) + c<br />
n Or, a(x+2) + b(y+1.5) + c<br />
n d + a<br />
n Or, d + a + b<br />
n How do you initialize?<br />
x,y<br />
x+1,y+.5
Locate Mid-point<br />
n d = a(x+1) + b(y+.5) + c<br />
n d next =<br />
n ie,<br />
n a(x+2) + b(y+.5) + c<br />
n Or, a(x+2) + b(y+1.5) + c<br />
n d + a<br />
n Or, d + a + b<br />
n How do you initialize?<br />
x,y<br />
x+1,y+.5
Locate Mid-point<br />
n d = a(x+1) + b(y+.5) + c<br />
n d next =<br />
n ie,<br />
n a(x+2) + b(y+.5) + c<br />
n Or, a(x+2) + b(y+1.5) + c<br />
n d + a<br />
n Or, d + a + b<br />
n How do you initialize?<br />
x,y<br />
x+1,y+.5
Initialize d<br />
n What if initial vertex coordinates are at<br />
pixel centers?<br />
n d initial = a(x 0 + 1) + b(y 0 +.5) + c<br />
n = ax 0 + by 0 + c + a + b/2<br />
n = a + b/2<br />
n Can we avoid that division?<br />
n Otherwise, can we snap?<br />
(x 0 , y 0 )