Click here to load reader
Upload
vokien
View
212
Download
0
Embed Size (px)
Citation preview
1
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 1
Raster Graphics Algorithms
scan conversionLine rasterizationBresenham’s Midpoint line algorithmMidpoint circle algorithmMidpoint ellipse algorithm and moreFilled primitives
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 2
Overview of Graphics Pipeline
3D scene database
traverse geometricmodel transform to
world space transform toeye space
clip
transform to 2Dscreen space rasterize
2D image(frame-buffer values)
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 3
Frame-buffer Model
Raster Display: 2D array of picture elements (pixels)Pixel can be set (grayscale/color)Window coordinates: pixels centered at integers
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 4
2D Scan Conversion
Geometric Primitives:2D: point, line, polygon, circle, …3D: point, line, polyhedron, sphere, …
Problem: Primitives are continuous, screen is discrete
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 5
2D Scan Conversion (2)
Solution: compute discrete approximation
Scan Conversion:Algorithms for efficient generation of the samples comprising this approximation
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 6
Line Rasterization
Scan converting 2D line segmentsInput: segment end points as integer values:(x1, y1) , (x2, y2)
Output: set of pixels lighten up
2
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 7
Line Rasterization: Basic Math Review
x
y
P2 = (x2, y2)
P1 = (x1, y1)
P = (x, y)
1
1
12
12
xx
yy
xx
yySlope
−−=
−−=
Solving for y
bmxy
or
yxxx
yyx
xx
yyy
+=
+−−−+
−−= )( 11
12
12
12
12
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 8
Line Rasterization: Basic Math Review
Length of line segment between P1 and P2
Midpoint of a line segment between P1 and P2
Two lines are perpendicular iff
Parametric form of line equation:
212
212 )()( yyxxL −+−=
)2
,2
( 2121 yyxxPmid
++=
12
1
mm −=
)(
)(
121
121
yytyy
xxtxx
−+=−+=
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 9
Basic Line Algorithms
Must:
Computer integer coordinates of pixels which lie on or near a line
Be efficientCreate visually satisfactory images:
lines should appear straightlines should terminate accurately
lines should have constant densitylines’ density should be independent of length and angle
Always be defined
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 10
A Naïve Algorithm
increment x in each step
Compute y as a function of xRound y (why?)Set Pixel ( x, Rnd(y(x)) )
dx
dy
xx
yym
bmxy
or
yxxx
yyx
xx
yyy
=−−=
+=
+−−−+
−−=
12
12
1112
12
12
12 )(
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 11
A Naïve Algorithm (2)
Problems:
Computing y value is expensive, for every x:1 floating point multiplication
1 floating point addition
1 call to rounding function
Exception around vertical lines
Gets disconnected (strung out) for m >1
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 12
Basic Incremental Algorithm
Also called: Digital Differential Analyzer (DDA)
Based on parametric equation of line:
Compute ∆x and ∆y so that one of them is 1 and the other is less than 1Start from (x1, y1)in each step, increment x1 by ∆x and y1 by ∆y
Set Pixel (Rnd(x), Rnd(y))
3
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 13
Basic Incremental Algorithm (2)
void DDA (int x1, int y1, int x2, int y2) {int length;float x, y, dx, dy;if abs(x2-x1) > abs(y2-y1)
length = abs(x2-x1)else
length = abs(y2-y1)
dx = (x2-x1)/length;dy = (y2-y1)/length;
x = x1;y = y1;for (int i = 0; i <=length; i++) {
setpixel(rnd(x), rnd(y))x += dx; y += dy;
}}
Creates good lines, but still there are problems
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 14
Bresenham’s Midpoint Algorithm
Keynote: At each step along the line, there are only two choicesfor the next pixel to be filled in.
The set of which two pixels should be filled in depends on the slop of the line
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 15
Bresenham’s Midpoint Algorithm (2)
For a line with a slop between 0 and 1, the two possible pixels that might be filled in, are the E and NE neighbors
What are the possible next pixels for a line with:-1 < slop <0slop > 1
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 16
Bresenham’s Midpoint Algorithm (3)
Let’s consider 0 < slope < 1
We can determine the next pixel by putting a decision point half-way between the two candidate points:
The next pixel is selected based on which side of the decision point, the “true line” is on, so we can define a decision variable (d).
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 17
Bresenham’s Midpoint Algorithm (4)
In each step of the algorithm:
determine the coordinates of current based on the decision variable computed in previous step
compute decision variable for next step
So, How to define decision variable (d)?
xp xp+1 xp+2
yp
yp+1
choices forcurrent step(based on the decisionvariable computed inthe previous step)
yp+1/2
yp+3/2
previouspixel
choices fornext step(based on the decisionvariable that will be computedin the current step)
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 18
Bresenham’s Midpoint Algorithm (5)
xp xp+1 xp+2
yp
yp+1yp+
1/2
yp+3/2
previouspixel
dxBdxydyxyxF
Bxdx
dyBmxy
...),( +−=
+=+=
F (x, y) = 0 : [x, y] is on the lineF (x, y) > 0 : [x, y] is below the lineF (x, y) < 0 : [x, y] is on top of the line
decision variable: d = F (xm, ym)where [xm, ym] is the coordinates of the midpoint
e.g. the value of d in current step:d = F(xp+1, yp+1/2) = (xp+1).dy – (yp+1/2).dx+B.dx
4
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 19
Bresenham’s Midpoint Algorithm (6)
xp xp+1 xp+2
yp
yp+1yp+
1/2
yp+3/2
previouspixel
dold = (xp+1).dy – (yp+1/2).dx+B.dx
)
dydd
dxBdxydydyxd
dxBdxydyxyxFd
Enextdcase
oldnew
ppnew
ppppnew
old
+=
++−++=
++−+=++=
=⇒<
.).(]).1[(
.).().2(),2(
01
2
1
2
1
2
1
case 1) dold < 0: next = Ecase 2) dold > 0: next = NE
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 20
Bresenham’s Midpoint Algorithm (7)
xp xp+1 xp+2
yp
yp+1yp+
1/2
yp+3/2
previouspixel
dold = (xp+1).dy – (yp+1/2).dx+B.dx
)
)(
.]).[(]).1[(
.).().2(),2(
02
2
1
2
3
2
3
dxdydd
dxBdxdxydydyxd
dxBdxydyxyxFd
NEnextdcase
oldnew
ppnew
ppppnew
old
−+=
+++−++=
++−+=++=
=⇒>
case 1) dold < 0: next = Ecase 2) dold > 0: next = NE
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 21
Bresenham’s Midpoint Algorithm (8)
dxdyd
yxFdxdyyxF
dxBdxdxydydyx
dxBdxydyxyxFd
initial
initial
21
0021
00
2
100
21
0021
00
0),(),(
.].[].[
.).().1(),1(
−=⇒
=−+=
++−+=
++−+=++=x0
y0
First midpoint
So far, We computed an incremental function forthe decision variable.
What about the initial decision value?
Oh, Noooo!!! floating point
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 22
Bresenham’s Midpoint Algorithm (9)
dxdyd
dxdydd
dydd
initial
oldNEnew
oldEnew
−×=
−×+=
×+=
2
)(2
2
)(
)(
How to get ride of the ½ fraction?
Solution: We only need the sign of d, so multiply d by 2!
☺
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 23
Bresenham’s Midpoint Algorithm (Finally the code!!!)
void MidpointLine(int x0, int y0, int x1, int y1){
int dx = x1 – x0;int dy = y1 – y0;int d = 2 * dy – dx // initial value of dint x = x0;int y = y0;int incE = 2 * dy; // increment when choose Eint incNE = 2 * (dy – dx); // increment when choose NE
SetPixel (x, y); // the start pixel
while (x < x1) {if (d <= 0) { // choose E
d += incE ;x ++;
} else {d += incNE; // choose NEx++;y++;
}SetPixel (x, y);
}}
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 24
Lines with more general slopes?
1) dx>dy
2) dy > dx
4) -dx > dy
3) dy > -dx
5) -dx > -dy
6) -dy > -dx
8) dx > -dy
7) -dy > dx
x
y
5
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 25
Lines with more general slopes? (2)
Solution 1: compute the proper decision variable for each 8 octants: e.g. o2): N and NE
o5): W and SW
mmm… no, thanks! we have already had enough trouble for that one octant!
Solution 2: convert all octants to octant1same Midpoint algorithmconvert to original octant in SetPixele.g. o2): Midpoint (y, x)
SetPixel (x, y)
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 26
Scan Converting Circles
222
222
),( RyxyxFd
Ryx
−+===+
x
y
R
F (x, y) = 0 : [x, y] is on the circleF (x, y) > 0 : [x, y] is outside the circleF (x, y) < 0 : [x, y] is inside the circle
Assume center of circle is at (0, 0):
What if center is not at (0, 0)?
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 27
Scan Converting Circles (2)
Take advantage of symmetries to minimize cases (slopes) and amount of curve drawingOnly one eighth of a circle needs to be computed!
void CirclePoints (x, y, xcenter, ycenter)
{
SetPixel (x + xcenter, y + ycenter);
SetPixel (y + xcenter, x + ycenter);
SetPixel (-x + xcenter, y + ycenter);
SetPixel (y + xcenter, -x + ycenter);
SetPixel (-x + xcenter, -y + ycenter);
SetPixel (-y + xcenter, -x + ycenter);
SetPixel (x + xcenter, -y + ycenter);
SetPixel (-y + xcenter, x + ycenter);
}
slope = -1
slope = 0
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 28
Midpoint Circle Algorithm
dold = F(xp+1, yp-½)= (xp +1)2 + (yp- ½)2 - R2
)
32
)(]32)1[(
)()2(),2(
01
222
12
22212
21
++=
−−++++=
−−++=−+=
=⇒<
poldnew
pppnew
ppppnew
old
xdd
Ryxxd
RyxyxFd
Enextdcase
-1< slope < 0:
case 1) dold < 0: next = Ecase 2) dold > 0: next = SE
E
SE
M ME
MSE
PreviousPixel
Choices forCurrent Pixel
Choices fornext Pixel
xp
yp
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 29
Midpoint Circle Algorithm (2)
dold = F(xp+1, yp-½)= (xp +1)2 + (yp- ½)2 - R2
case 1) dold < 0: next = Ecase 2) dold > 0: next = SE
E
SE
M ME
MSE
PreviousPixel
Choices forCurrent Pixel
Choices fornext Pixel
xp
yp
522
]22)[(]32)1[(
)()2(),2(
0)2
22212
22232
23
+−+=
−+−−++++=
−−++=−+=
=⇒>
ppoldnew
ppppnew
ppppnew
old
yxdd
Ryyxxd
RyxyxFd
SEnextdcase
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 30
Midpoint Circle Algorithm (3)
E
M ME
FirstPixel
Choices forCurrent Pixel
x0
y0
Rd
Ryx
Ryyxx
RyxyxFd
initial
initial
−=
⇒==−+−+++=
−−++=−+=
4
5
00
241
02
002
0
222
10
202
100
,0
][]12[
)()1(),1(
6
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 31
Midpoint Circle Algorithm (4)
Rd
dddd
dd
Rd
yxdd
xdd
initial
ppoldSEnew
poldEnew
−=
>⇔><⇔<−=
−=
+−+=
++=
1'
!?!d' usenot why so
00' and 00'
'int
522
32
41
4
5
int
)(
int
)(
4434421
321
Floating Point: to be or not to be?
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 32
Midpoint Circle Algorithm (5)
+=+=
⇒−−++⇒
+=+=
⇒++⇒
+−+=
++=
4
2,
2
2
522
32
deltaSE int
)(
deltaE int
)(
oldnew
oldnew
oldnew
oldnew
ppoldSEnew
poldEnew
deltaSEdeltaSE
deltaEdeltaEyxSE
deltaSEdeltaSE
deltaEdeltaExE
yxdd
xdd
4434421
321
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 33
Midpoint Circle Algorithm (6)
void MidpointCircle(int xcenter, int ycenter, int R){
int x = 0;int y = R;int d = 1 – R // initial value for decision variableint deltaE = 3;int deltaSE = -2*R + 5;
CirclePoints (x, y, xcenter, ycenter);
while (y > x) { // slope is between -1 and 0if (d < 0) { // Select E
d+= deltaE;deltaE += 2;deltaSE += 2;
} else { // Select SEd += deltaSE;deltaE += 2;deltaSE += 4;y--;
}x++;CirclePoints (x, y, xcenter, ycenter);
} // while}
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 34
Scan Conversion Ellipses
222222
222222
),( bayaxbyxFd
bayaxb
−+===+
x
y
b
F (x, y) = 0 : [x, y] is on the ellipseF (x, y) > 0 : [x, y] is outside the ellipseF (x, y) < 0 : [x, y] is inside the ellipse
What if not centered at (0, 0)?
a
(x, y)
(x, -y)
(-x, y)
(-x, -y)
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 35
Midpoint Ellipse Algorithm
⇒>>
⇒>>
+=∂∂+
∂∂=
−+=
2region 22:G(F) G(F)
1region 22:G(F) G(F)
j 2i 2)(
)(
Gradient
:slope test the tomethodAnother
),(
22ji
22ij
22
222222
yaxb
xbya
yaxbFGradient
jy
Fi
x
FFGradient
bayaxbyxF
y
x
Region 1
Region 2
gradient vector
G(F)j =G(F)i
gradient vector
i
j
G(F)j >G(F)i
G(F)i >G(F)j
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 36
Midpoint Ellipse Algorithm (2)
−+=⇒
−+=⇒⇒⇒
−+=⇒
−+=⇒⇒++⇒
)2,(
)2,(--y2region
),2(
),2(x1region
2
3
21
2
3
2
1
ppnew
ppnew
ppnew
ppnew
yxFdSE
yxFdS
yxFdSE
yxFdE
y
x
Region 1
Region 2
SE
E
SES
xp xp+1
yp
yp-1
7
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 37
Midpoint Ellipse Algorithm (3)
The rest is more straightforward:
calculate dnew as a function of dold
calculate the initial values
then we have two main loops:
while a2y > b2x, scan along x++while y > 0, scan along y--
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 38
Rotated Ellipses?
d1
d2
C1
C2
P
KyCPyxCPxyCPyxCPxyxF
KyCPyxCPxyCPyxCPx
KCPdistCPdist
Kdd
−−+−+−+−=
=−+−+−+−
=+=+
22
22
21
21
22
22
21
21
21
21
)()()()(),(
)()()()(
),(),(
(constant)
Pleeese, stop right here!!!
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 39
Why stop? – We want more!
General procedure to develop a midpoint method for an arbitrary shape:
formulate the shape (parabola, sinus, …)
determine the decision variable
determine the scanning path(s) so that the decision in each step is limited
try to formulate the equations as incremental and avoid floatingpoint operations as much as possible, or at least convert them to easier ones (mult to add, sin to mult, …)
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 40
Filled Primitives – Rectangle (Box)
for (x = x1; x <= x2; x++)for (y = y1; y <= y2; y++)
SetPixel (x,y, fillcolor);
?
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 41
Filled Primitives – Circle (Dist)
void CirclePointsFill (x, y, xcenter, ycenter){int i;for (i = x+center; i <= -x + center; i++) {
SetPixel (i,y+center, fillcolor);SetPixel (i,-y+center, fillcolor);
}for (i = y+center; i <= -y + center; i++) {
SetPixel (i,x+center, fillcolor);SetPixel (i,-x+center, fillcolor);
}}
Sept 29-Oct 1, 2003 CMPT-361 : Hamid Younesy 42
Filled Primitives – Triangle
The most frequently used primitive in 3D graphics!
Just, Keep track of two Bresenham lines in each step
any optimization?
yup: skip steps when y does not change!