Upload
others
View
18
Download
0
Embed Size (px)
Citation preview
OUTPUT PRIMITIVES
CEng 477Computer Graphics
METU, 2004
Output Primitives
● Graphic SW and HW provide subroutines to describe a scene in terms of basic geometric structures called output primitives.
● Output primitives are combined to form complex structures
● Simplest primitives– Point (pixel)– Line segment
Scan Conversion
● Converting output primitives into frame buffer updates. Choose which pixels contain which intensity value.
● Constraints– Straight lines should appear as a straight line– primitives should start and end accurately– Primitives should have a consistent brightness
along their length– They should be drawn rapidly
Line Drawing
● Simple approach:sample a line at discrete positions at one coordinate from start point to end point, calculate the other coordinate value from line equation.
If m>1 , increment y and find xIf m≤1, increment x and find y
y=m xb x=1m
ybm
m=ystart−yend
x start−xend
Digital Differential Analyzer
● Simple approach: too many floating point operations and repeated calculations
● Calculate from for a value
yk1 yk x
y=m x
x= ym
yk1=ykm for x=1, 0m1
x k1=xk1m
for y=1, m1
Bresenham's Line Algorithm
● DDA: Still floating point operations
xk+1
xk
yk
yk+1
yd
2
d1
y=m xk1b⇒ d1=y−yk=m xk1b−yk
d2=yk1−y=yk1−m xk1−b
d1−d2=2 m xk1−2 yk2 b1
Assume∣m∣1If already at xk , yk , choices:xk1, yk if d1≤d2
xk1, yk1 if d1d2
define pk= x d1−d 2=2 y xk−2 x ykc
c=2 y x 2 b−1 Independent from pixel position
m= y x
=yend−ystart
xend−x start
If d1d 2 ⇒ pk0 ⇒ Choose yk
Else Choose yk1
for x0 :
c=2 y x 2 b−1=2 y x 2 y0−2 x0
y x
− x
=2 y2 x y0−2 y x0− x
xk1=xk1 ⇒ pk1=pk2 y−2 x yk1−ykyk1−yk=0 If yk is choosen ,yk1−yk=1 If yk1 is choosen
at step k1 :pk1 = 2 y x k1−2 x yk1c
pk1−pk = 2 y xk1−xk−2 x yk1−yk
● Algorithm:
p0=2 y x0−2 x y0c=2 y x0−2 x y02 y2 x y0−2 y x0− xp0=2 y− xx0, y0 knownpk1=pk2 y−2 x yk1−yk
draw x0, y0pk 2 y− x ; xk x0
while xkxend
xk1 xk1if pk≤0 choose yk
yk1 yk ; pk1 pk2 yelse choose yk1
yk1 yk1 ; pk1 pk2 y−2 xdraw xk1 , yk1xk xk1pk=pk1
Circle Generation
● Computationally complex● Non uniform spacing● Polar coordinates:
x−x02y−y0
2=r2
unit steps in x ⇒ y=y0∓r2−x−x02
x=r cosxc
y=r sin yc
● Fixed angular step size to have equally spaced points
xk=r cos xk1=r cosd yk=r sin yk1=r sin d
xk1 = r coscos d −r sinsin d = xk cos d −yk sin d
yk1 = r sincos d r cossin d = yk cos d −yk sin d
fixed d so compute cos d and sin d initially
● Computation can be reduced by considering symmetry of circles:
● Still too complex,multiplications,trigonometric calculations
● Bresenham's circle generation algorithm involves simple integer operations
● Midpoint Circle Algorithm generates the same pixels
(x,y)
(x,-y)(-x,-y)
(-x,y)
(-y,-x) (y,-x)
(y,x)(-y,x)
Midpoint Circle Algorithm
● Consider the second octant.Increment x , decide on y
● Assume that
select which of 2 pixelsare closer to the circleby evaluating the functionat E and SE.
x
y
P=x p , y p E
SE
M ME
MSE
previouspixel
choicesfor
currentpixel
choices for the
next pixel
f x , y=x2y2−r2={ 0 if on the circle 0 if outside the circle choose SE0 if inside the circle choose E
pk1=pkxk24 xk4yk1
2 −yk114−xk
2−2 xk−1−yk2yk−
14
pk1−pk=xk112yk1−12
2
−r2−xk12−yk−122
r2
pk= f xk1, yk−12=xk12yk−
122
−r2
pk1= f xk11, yk1−12=xk112yk1−
122
−r2
pk1=pkxk24 xk4yk1
2 −yk114−xk
2−2 xk−1−yk2yk−
14
pk1=pk2 xk3yk12 −yk
2−yk1−yk
If E is chosen pk0 :yk1=yk ⇒ pk1=pk2 xk3
If SE is chosen pk0 :yk1=yk−1 ⇒ pk1=pk2 xk3−2 yk11
=pk2 xk−2 yk5
x0=0 ; y0=r ; p0= f 0,r−12=1r−
12
2
−r2=54−r
All increments are integer, rounding 54
will give 1 so,
p0=1−r
● Algorithm:drawoctants x0, r pk 1−r ; xk 0 ; yk rwhile xkyk
if pk0 choose E yk1 yk ; pk1 pk2 xk3
else choose SEyk1 yk−1 ; pk1 pk2 xk−2 yk5
xk1 xk1drawoctants xk1 , yk1xk xk1
yk yk1
pk=pk1
x=0 ; y=10 ; r=10 plot (0,10)
pk=1−10=−9 choose E plot 1,10
pk=−923=−4 choose E plot 2,10
pk=−443=3 choose SE plot 3,9
pk=36−185=−4 choose E plot 4,9
pk=−483=7 choose SE plot 5,8
pk=710−165=6 choose SE plot 6,7
pk=612−145=9 choose SE plot 7,6
if pk0 choose E yk1 yk ; pk1 pk2 xk3
else choose SEyk1 yk−1 ; pk1 pk2 xk−2 yk5
Ellipse Generation
● Similar to circle generation with mid-point. Inside test.
● Different formula for points up to the tangent y=x , slope<1.(0,b) to tangent: increment x find ytangent to (a,0): decrement y find x
● Mid-point algorithm is applicable to other polynomial equations:– Parabola, Hyperbola
y=x
a
b
x2
a2y2
b2=1
Filled Area Primitives
● Two basic approaches to area filling on raster systems:– Determine the overlap intervals for scan lines
that cross the area (scan-line)– Start from an interior position and point
outward from this point until the boundary condition reached (fill method)
● Scan-line: simple objects, polygons, circles,..● Fill-method: complex objects, interactive fill.
Scan-line Polygon Fill
● For each scan-line:
– Locate the intersection of the scan-line with the edges (y=ys)
– Sort the intersection points from left to right.
– Draw the interiors intersection points pairwise. (a-b), (c-d)
● Problem with corners. Same point counted twice or not?
a b c d
● a,b,c and d are intersected by 2 line segments each.
● Count b,d twice but a and d once. Why?
a
b
c
d
a
b
c
d
2
1
2
2
2
2
1
● Solution:Make a clockwise or counter-clockwise traversal on edges.Check if y is monotonically increasing or decreasing.If direction changes, double intersection, otherwise single intersection.
Scan-line Polygon Filling (coherence)
● Coherence: Properties of one part of a scene are related with the other in a way that can it be used to reduce processing of the other.
● Scan-lines adjacent to each other.Intersection of scan-lines are close to each other (like scan conversion of a line)
● Intersection points with scan lines:
xk1=round xk1m
xk xk1
yk1
yk
● Instead of floating point operations, use integer operations:
m= y x
xk1=xk x y
counter0for each scan-linecounter counter xwhile counter yx x1countercounter− y
● Example:m = 8/5
scanline counter x 0 0 0 1 5 0 2 10 2 1 3 7 1 4 12 4 2 5 9 1 3
Efficient Polygon Fill
● Make a (counter)clockwise traversal and shorten the single intersection vertices.
● Generate a sorted edge table on the scan-line axis. Each edge has an entry in smaller y valued corner point.
● Each entry keeps a linked list of all connected edges:
– x value of the point
– y value of the end-point
– Slope of the edge
A
BD
C
E
E'
F
F'
yF' xA 1/mAF' yB xA 1/mAB
yE' xF 1/mFE'
yD xC 1/mCD yB xC 1/mCB
yD xE 1/mED
Scan line 01
● Start with the smallest scan-line
● Keep an active edge list:
– Update the current x value of the edge based on m value
– Add the lists in the current table entry based on their x value
– Remove the completed edges
– Draw the intermediate points of pairwise elements of the list.
● Example:A,B,C,D,E,F,A Polygon: (30,10),(24,32),(20,22),(16,34)(8,26),(12,16),(30,10)E'=(20,25), F'=(12,15)
Edge Table: Y E1 E2 10 [15,30,-3] [32,30,-3/11] 16 [25,12,-2/5] 22 [34,20,-1/3] [32,20,2/5] 26 [34,8,1]
Y S1 S1 S2 S210 30 3011 27 29.7312 24 29.4513 21 29.1814 18 28.9115 15 28.6416 12 28.3617 11.6 28.0918 11.2 27.8219 10.8 27.5520 10.4 27.2721 10 2722 9.6 20 20 26.7323 9.2 19.67 20.4 26.4524 8.8 19.33 20.8 26.1825 8.4 19 21.2 25.9126 8 18.67 21.6 25.6427 9 18.33 22 25.3628 10 18 22.4 25.0929 11 17.67 22.8 24.8230 12 17.33 23.2 24.5531 13 17 23.6 24.2732 14 16.67 24 2433 15 16.3334 16 16
Boundary Fill Algorithm● Start at a point inside a continuous arbitrary shaped
region and paint the interior outward toward the boundary. Assumption: boundary color is a single color
● (x,y): start point; b:boundary color, fill: fill color
void boundaryFill4(x,y,fill,b) {cur = getpixel(x,y)if (cur != b) AND (cur != fill) {
setpixel(x,y,fill);boundaryFill4(x+1,y,fill,b);boundaryFill4(x-1,y,fill,b);boundaryFill4(x,y+1,fill,b);boundaryFill4(x,y-1,fill,b);
}}
● 4 neighbors vs 8 neighbors: depends on definition of continuity. 8 neighbor: diagonal boundaries will not stop
● Recursive, so slow. For large regions with millions of pixels, millions of function calls.
● Stack based improvement: keep neighbors in stack
● Number of elements in the stack can be reduced by filling the area as pixel spans and pushing only the pixels with pixel transitions.
● Check the neighbor pixels as filling the area line by line
● If pixel changes from null to boundary or null when scan-line finishes, push the pixel information on stack.
● After a scan-line finishes, pop a value from stack and continue processing.
11
2 3
1
2
34
5
6
7 8910
1
12
Stack after scan line
Flood-Fill
● Similar to boundary fill. Algorithm continues while the neighbor pixels have the same color.
● void FloodFill4(x,y,fill,oldcolor) {cur = getpixel(x,y)if (cur == oldcolor) {
setpixel(x,y,fill);FloodFill4(x+1,y,fill,oldcolor);FloodFill4(x-1,y,fill,oldcolor);FloodFill4(x,y+1,fill,oldcolor);FloodFill4(x,y-1,fill,oldcolor);
}}
Character Generation
● Typesetting fonts:– Bitmap fonts: simple, not scalable.– Outline fonts: scalable, flexible,
more complex process●
0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 00 0 1 1 0 1 1 00 1 1 0 0 0 1 10 1 1 0 0 0 1 10 1 1 1 1 1 1 10 1 1 1 1 1 1 10 1 1 0 0 0 1 10 1 1 0 0 0 1 10 1 1 0 0 0 1 10 1 1 0 0 0 1 10 0 0 0 0 0 0 0
Pixelwise on/of information
Points and tangents of the boundary
Attributes of Output Primitives
● Line attributes:
– Line typedotted, dashed, ...
– Line width
– Line caps and joinmiter, round, bevel
– Line color
– Line brush
● Area Fill Attributes:
– Solid
– Pattern
– Gradient
● Character Attributes
– Font, bold, italic, underlined, outline, shadowshadow
– Spacing abcdef abcdef abcdef abcdef– Direction
normal
–
down
Text
o
n arb
itrary baseline path
rota
ted
slant
ed
Antialiasing
● CG generated shapes: limited resolution sampling of original shapes with infinite resolution
● Loose information, Aliasing: Jagging, stairway effects.
● Use higher resolutions.Expensive, no limit.
● Another solution:Antialiasing: use intensity levels.
● Supersampling: sample at higher resolution.
● Count the superpixels at each pixel. Give an intensity based on that value.
● Aliased vs. Antialiased.
● Filtering techniques:Giving a larger weight to center pixel and lower weight to neighbor pixels. Weight mask or volume based filters.
1 2 1
2
121
2 4
Pixel WeightingMask
Box filter Cone filter Gaussian filter