Computer Graphics Computer Graphics Computer Graphics Computer Graphics Computer Graphics Computer Graphics Computer Graphics Computer Graphics
Subodh
Subodh KumarKumar Dept of Computer Sc. &
Dept of Computer Sc. & EnggEngg.. IIT Delhi
IIT Delhi
Pixel/Screen Coordinates Pixel/Screen CoordinatesPixel/Screen Coordinates Pixel/Screen Coordinates Pixel/Screen Coordinates Pixel/Screen CoordinatesPixel/Screen Coordinates Pixel/Screen Coordinates
Xscreen = 0 1 w-1
Caution:
In OpenGL this is X = 0, pixel centers are at int + .5
Drawing Drawing Drawing Drawing Drawing DrawingDrawing Drawing
Draw a lineDraw a line
Start at [XStart at [X00, Y, Y00]]
End at [XEnd at [X11, Y, Y11]]
Drawing Drawing Drawing Drawing Drawing DrawingDrawing Drawing
Draw a lineDraw a line
Start at [XStart at [X00, Y, Y00]]
End at [XEnd at [X11, Y, Y11]]
Drawing Drawing Drawing Drawing Drawing DrawingDrawing Drawing
Draw line: [XDraw line: [X00, Y, Y00] TO [X] TO [X11, Y, Y11]]
mitered
bevelled
rounded
Endcap?
Drawing Drawing Drawing Drawing Drawing DrawingDrawing Drawing
Draw line [1, 1] TO [8, 1]Draw line [1, 1] TO [8, 1]
7 pixels long7 pixels long
Line Drawing Line DrawingLine Drawing Line Drawing Line Drawing Line Drawing Line Drawing Line Drawing
Which pixels to drawWhich pixels to draw
How thick is the lineHow thick is the line
How long is the lineHow long is the line
How are the ends drawnHow are the ends drawn
Do you allow overdrawDo you allow overdraw
Which direction is the line drawn inWhich direction is the line drawn in
What color should they beWhat color should they be
Similar issues exist for drawing other Similar issues exist for drawing other shapes
shapes
Line Drawing Simplified Line Drawing Simplified Line Drawing Simplified Line Drawing Simplified Line Drawing Simplified Line Drawing SimplifiedLine Drawing Simplified Line Drawing Simplified
Draw pixel ifDraw pixel if
Line is close to itLine is close to it
ieie, lies in the circle, lies in the circle
Or, the diamondOr, the diamond
Draw pixel if:Draw pixel if:
Line exits diamondLine exits diamond
What if line does not exit What if line does not exit the diamond?
the diamond?
What if line straddles or What if line straddles or ends at a diamond edge ends at a diamond edge
Perturb/Snap End Perturb/Snap End Perturb/Snap End Perturb/Snap End Perturb/Snap End Perturb/Snap End Perturb/Snap End
Perturb/Snap End---pointspointspointspointspointspointspointspoints
… …
……
Efficient Line Drawing Efficient Line DrawingEfficient Line Drawing Efficient Line Drawing Efficient Line Drawing Efficient Line DrawingEfficient Line Drawing Efficient Line Drawing
Proceed in the direction of smaller slopeProceed in the direction of smaller slope
DDA: Digital Difference AnalyzerDDA: Digital Difference Analyzer
Y = Y = mmXX + i+ i
mm(x+1) + i, (x+1) + i, mm(x+2) + i (x+2) + i ……
Successively add Successively add mm to y and round itto y and round it
Bresenham’Bresenham’ss midpoint algorithmmidpoint algorithm
Which next pixels are the closest to the lineWhich next pixels are the closest to the line
aXaX + + bYbY + c = 0+ c = 0
Useful for curves as wellUseful for curves as well
Bresenham Bresenham Bresenham Bresenham Bresenham Bresenham Bresenham
Bresenham’’’’’’’’ssssssss AlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithm
If slope in [If slope in [--1:1]1:1]
3 possibilities3 possibilities
Bresenham Bresenham Bresenham Bresenham Bresenham Bresenham Bresenham
Bresenham’’’’’’’’ssssssss AlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithm
If slope in [If slope in [--1:1]1:1]
3 possibilities3 possibilities
If slope in [0:1]?If slope in [0:1]?
On which side of On which side of the next mid
the next mid--point point does the line lie?
does the line lie?
Locate Mid Locate Mid Locate Mid Locate Mid Locate Mid Locate MidLocate Mid
Locate Mid---pointpointpointpointpointpointpointpoint
Y = Y = mmXX + i+ i
yy = = m m (x+1) + i(x+1) + i
> y+.5?> y+.5?
Consider:Consider:
F(x,y) = 0F(x,y) = 0
ieie aX+bY+caX+bY+c = 0= 0
F(F(pp) = 0 => Curve intersects ) = 0 => Curve intersects pp
F(F(pp) < 0 => Curve passes below ) < 0 => Curve passes below pp
F(F(pp) > 0 => Curve passes above ) > 0 => Curve passes above pp
x,y
x+1,y+.5
If a > 0 If a > 0
Locate Mid Locate Mid Locate Mid Locate Mid Locate Mid Locate MidLocate Mid
Locate Mid---pointpointpointpointpointpointpointpoint
d = a(x+1) + b(y+.5) + cd = a(x+1) + b(y+.5) + c
ddprevprev = =
ax + b(y+.5) + cax + b(y+.5) + c
Or, ax + b(yOr, ax + b(y--.5) + c.5) + c
ieie,,
d d –– aa
Or, d Or, d –– a a –– bb
How do you initialize d?How do you initialize d?
x,y
x+1,y+.5
Initialize d Initialize d Initialize d Initialize d Initialize d Initialize d Initialize d Initialize d
What if initial vertex coordinates are at What if initial vertex coordinates are at pixel centers?
pixel centers?
ddinitialinitial = a(x= a(x00 + 1) + b(y+ 1) + b(y00+.5) + c+.5) + c
= ax= ax00 + by+ by00 + c + a + b/2+ c + a + b/2
= a + b/2= a + b/2
Can we avoid that division?Can we avoid that division?
Otherwise, can we snap?Otherwise, can we snap?
(x(x00, y, y00))
Polygon Polygon Polygon Polygon Polygon Polygon Polygon
Polygon RasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterization RulesRulesRulesRulesRulesRulesRulesRules
Draw pixel, if center inside polygonDraw pixel, if center inside polygon
Find intersection of scanFind intersection of scan--lines with edgeslines with edges
All edges?All edges?
Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues
Sliver Sliver problem problem
Animating Animating Slivers
Slivers
Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues
Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues Triangle Rasterization Issues
Shared Edge OwnershipShared Edge Ownership
Polygon Polygon Polygon Polygon Polygon Polygon Polygon
Polygon RasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterization RulesRulesRulesRulesRulesRulesRulesRules
Draw pixel, if center inside polygonDraw pixel, if center inside polygon
What if it is at an edge?What if it is at an edge?
Draw on left edge, Skip on right edgeDraw on left edge, Skip on right edge
Unless HorizontalUnless Horizontal
Then, draw a bottom edge, skip a top edgeThen, draw a bottom edge, skip a top edge
Find lo, hi y (scanline) For y=lo to hi:
Find left extreme (of span) Find right extreme (of span) For left to right, draw pixel
Triangle Triangle Triangle Triangle Triangle Triangle Triangle
Triangle RasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterization IIIIIIII
Find Left extreme:Find Left extreme:
= = rasterizerasterize left lineleft line
= remember left line= remember left line
StartXStartX = = StartXStartXpreviousprevious+ 1/+ 1/mm
Similarly find Right extremeSimilarly find Right extreme
ExtremaExtrema are edges (sometimes at vertices)are edges (sometimes at vertices)
Which two?Which two?
Start with two at the lowest vertexStart with two at the lowest vertex
What is there are two lowest verticesWhat is there are two lowest vertices
When an edge ends (at a vertex), replace edgeWhen an edge ends (at a vertex), replace edge
General Polygon General Polygon General Polygon General Polygon General Polygon General Polygon General Polygon
General Polygon RasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterization
Multiple spansMultiple spans
Count parityCount parity
How do you count How do you count
vertex intersections?
vertex intersections?
Divide into trianglesDivide into triangles
B C
D
E F
G
I H
J
A
Triangulation TriangulationTriangulation Triangulation Triangulation Triangulation Triangulation Triangulation
Draw horizontal line at each Draw horizontal line at each vertex
vertex
Assume no two vertices have Assume no two vertices have the same Y
the same Y
Perturb if assumption not truePerturb if assumption not true
Draw diagonals of trapezoidsDraw diagonals of trapezoids
Decomposed into monotone Decomposed into monotone polygons
polygons
Triangulate monotone Triangulate monotone polygon
polygon
O(nO(n lglg n)n)
Triangulating Monotone Triangulating Monotone Triangulating Monotone Triangulating Monotone Triangulating Monotone Triangulating Monotone Triangulating Monotone Triangulating Monotone
Polygons Polygons Polygons Polygons Polygons PolygonsPolygons Polygons
Form diagonalsForm diagonals
If vertex visibleIf vertex visible
Otherwise, proceed to next Otherwise, proceed to next vertex
vertex
O(O(nn))
Edge Equations Edge Equations Edge Equations Edge Equations Edge Equations Edge EquationsEdge Equations Edge Equations
Edge equation (line equation)Edge equation (line equation)
Defines two Defines two halfhalf--spacesspaces::
Triangle Evaluation Triangle Evaluation Triangle Evaluation Triangle Evaluation Triangle Evaluation Triangle EvaluationTriangle Evaluation Triangle Evaluation
Triangle = intersection of three Triangle = intersection of three positive half
positive half--spaces:spaces:
A1x + B1y + C1< 0 A2x + B
2y + C
2 < 0
Ax3 + B 3 y + C 3
< 0
A1x + B1y + C1> 0 A 3
x + B 3 y + C 3
> 0 A
2x + B
2y + C
2 > 0
Evaulate Evaulate Evaulate Evaulate Evaulate Evaulate Evaulate
Evaulate Edge EquationsEdge EquationsEdge EquationsEdge EquationsEdge EquationsEdge EquationsEdge EquationsEdge Equations
Plug in pixelsPlug in pixels’’ x,yx,y into the equationinto the equation
If If F(x,yF(x,y) > 0, draw) > 0, draw
+ +
+ - -
-
Triangle Triangle Triangle Triangle Triangle Triangle Triangle
Triangle RasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterization
Setup:Setup:
Find edge slopesFind edge slopes
Find bounding boxFind bounding box
RasterizeRasterize::
Initialize left, right intersectionInitialize left, right intersection
From Bounding Box bottom to Top:From Bounding Box bottom to Top:
Update left, right intersectionUpdate left, right intersection
Round up or downRound up or down
Fill spanFill span
Interpolate Z, Color?Interpolate Z, Color?
Do in fragment Do in fragment shadershader
Fill span = create fragmentFill span = create fragment
Update Intersection Update Intersection Update Intersection Update Intersection Update Intersection Update Intersection Update Intersection Update Intersection
If y > swapIf y > swap--vertexvertex--yy
Replace one slopeReplace one slope
Replace one xReplace one xyy-1-1
xxyy = x= xy-y-11+ 1/m = x+ 1/m = xyy--11 + N/D + N/D
If y = swapIf y = swap--vertexvertex--yy
Replace one slopeReplace one slope
Replace one xReplace one xyy-1-1
Triangle Triangle Triangle Triangle Triangle Triangle Triangle
Triangle RasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterizationRasterization, Really, Really, Really, Really, Really, Really, Really, Really
Multiple Multiple rasterizersrasterizers??
Blocked or Blocked or StridedStrided
Create tiles of fragmentsCreate tiles of fragments
In parallel, divide tiles into In parallel, divide tiles into subsub--tilestiles
Each sub-Each sub-tile shaded tile shaded together
together
Mask to turn off uncovered Mask to turn off uncovered tiles
tiles
Partially covered sub-tile
Fully covered sub-tile