ceng 477 introduction to computer graphics metu,...
TRANSCRIPT
OUTPUT PRIMITIVES
CEng 477Introduction to Computer Graphics
METU, 2007
Recap: The basic forward projection pipeline:
Model
Model
Model
M1
M2
M3
3D WorldScene
3D ViewScene
V
P Clip Normalize 2D/3D DeviceScene
2D ImageProjection
Rasterization
ModelingTransformations
ViewingTransformations
MCS
WCSVCS
NDCS DCSSCS
Screen vs. World coordinate systems
● Objects positions are specified in a Cartesian coordinate system called World Coordinate System which can be three dimensional and real-valued.
● Locations on a video monitor are referenced in integer screen coordinates. Therefore object definitions has to be scan converted to discrete screen coordinate locations to be viewed on a video monitor.
Specification of a 2D WCS in OpenGL
● glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (xmin, xmax, ymin, ymax);
● Objects that are
specified within these
coordinate limits will be
displayed within the
OpenGL window.
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
OpenGL Point Functions
● glBegin (GL_POINTS);
glVertex2i(50, 100);
glVertex2i(75, 150);
glVertex2i(100, 200);
glEnd();
OpenGL Line Functions
● glBegin (GL_LINES);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glEnd();
p5
p3
p1
p4p2
OpenGL Line Functions
● glBegin (GL_LINE_STRIP);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glEnd();
p5
p3
p1
p4p2
OpenGL Line Functions
● glBegin (GL_LINE_LOOP);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glEnd();
p5
p3
p1
p4p2
Line Drawing Algorithms
● 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 (slope-intercept line equation).
If m>1 , increment y and find x If m≤1, increment x and find y
Is this correct?startend
startend
xxyym
mby
mx
bmxy
−−
=
−=
+=1
or
Digital Differential Analyzer
● Simple approach: too many floating point operations and repeated calculations
● Calculate from for a valueyk 1 yk x
y m x
xy
m
yk 1 yk m for x 1, 0 m 1
xk 1 xk1m
for y 1, m 1
DDA
● Is faster than directly implementing y=mx+b. No floating point multiplications. We have floating point additions only at each step.
● But what about round-off errors?
● Can we get rid of floating point operations completely?
Bresenham's Line Algorithm
● DDA: Still floating point operations
xk+1xk
yk
yk +1
y=mx+b
dupper
dlower
upperlowerkk
upperlowerkk
kk
ddyx
ddyxyx
m
>++
≤+
≤
if )1,1(
if ),1(:choices ,),(at already If
1 Assume
122)1(2
)1(1)1()1(
)1(
−+−+=−⇒
−+−+=−+=−++=−=
⇒++=
byxmdd
bxmyyydybxmyyd
bxmy
kkdupperlower
kkkupper
kkklowerk
startend
startend
xxyy
xym
−−
=ΔΔ
=
position pixel fromt independen )12(2
22)( define
−Δ+Δ=
+Δ−Δ=−Δ=
bxyc
cxyyxddxp kkupperlowerk
xk+2 xk+3
1 choose else
choose 0 if
+
⇒<⇒<
k
kkupperlower
y
ypdd
)(2)(222
111
111
kkkkkk
kkk
yyxxxyppcyxxyp
−Δ−−Δ=−+⋅Δ−⋅Δ=
+++
+++
at step k+1:
)(221 111 kkkkkk yyxyppxx −Δ−Δ+=⇒+= +++
0 if pk was negative 1 if pk was positive
to calculate p0 at the starting pixel position (x0 ,y0 )
xypxyxxyycxxyyb
bxyccyxxyp
Δ−Δ=⇒Δ−Δ−Δ+Δ=⇒ΔΔ
−=
−Δ+Δ=+⋅Δ−⋅Δ=
2222
)12(222
00000
000
Input: two line end points (x0 ,y0 ) and (xend ,yend )draw (x0 ,y0 )pk ←2Δy-Δx; xk ←x0while xk <xend
xk+1 ←xk +1if pk ≤ 0 choose yk
yk+1 ←yk ; pk+1 ←pk +2Δyelse choose yk +1
yk+1 ←yk +1; pk+1 ←pk + 2Δy - 2Δxdraw (xk+1 ,yk+1 )xk ←xk+1pk ←pk+1
Bresenham’s Line-Drawing Algorithm
Example from the textbook
● Using Bresenham’s algorithm digitize the line with endpoints (20,10) and (30,18)
Example continued...
20 21 3022 25
10
18
Plotted pixels
20 21 3022 25
10
18
Circle Generation
● Circles can be approximated by a set of straight lines.
But, how many lines dowe need for an acceptablerepresentation?
How do we determine endpoints of lines?
Circle Drawing in OpenGL
● Routines for drawing circles or ellipses are not included in the OpenGL core library.
● GLU (OpenGL Utility) library has some routines for drawing spheres, cylinders, B- splines. Rational B-splines can be used to display circles and ellipses.
Circle Generation
● Computationally complex
● Non uniform spacing
● Polar coordinates:
x x 02 y y0
2 r 2
uni t steps in x y y0 r 2 x x 02
x r cos xc
y r sin yc
● Fixed angular step size to have equally spaced points
xk r cos xk 1 r cos dyk r sin yk 1 r sin d
xk 1 r cos cosd r sin sindxk cosd yk sind
yk 1 r sin cosd r cos sindyk cosd yksind
fixed d so compute cosd and sind initially
xk sin
● Computation can be reduced by considering symmetry of circles:
● Still too complex, multiplications, trigonometric calculations
● Bresenham's circle generation algorithm involves simple integer operations (comparing squares of pixel separation distances)
● Midpoint Circle Algorithm avoids squaring and generates the same pixels as Bresenhams’s algorithm.
(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
select which of 2 pixels, (xk +1,yk ) or (xk +1,yk -1) are closer to the circle by evaluating the circle
function at the midpoint.
x
y
pk 1 pk xk2 4 xk 4 yk 1
2 yk 114
xk2 2 xk 1 yk
2 yk14
pk 1 pk xk 1 1 2 yk 112
2
r 2 xk 1 2 yk12
2
r 2
pk f xk 1,yk12
xk 1 2 yk12
2
r 2
pk 1 f xk 1 1,yk 112
xk 1 1 2 yk 112
2
r 2
1 choose circle theinside if 0 choose circle theoutside if 0
1 choose circle on the if 0),( 222 −
⎪⎩
⎪⎨
⎧
<>
−=−+= k
k
k
yy
yryxyxf
1)()()1(2 122
11 +−−−+++= +++ kkkkkkk yyyyxpp
where yk+1 is either yk or yk –1 depending on the sign of pk .
if pk < 0 pk+1 = pk + 2xk + 3
if pk ≥ 0 pk+1 = pk + 2xk – 2yk + 5
computing p0 at (x0 ,y0 ) = (0,r)
r
rr
rfp
−=
−−+=
−=
45
)21(1
)21,1(
22
0
if r is integer p0 = 1–r
Midpoint Circle AlgorithmInput: radius r and circle center (xc ,yc )draw(0+xc ,r+yc ) (add xc and yc before plotting)pk ←1–r; xk ←0; yk ←r;while xk <yk
if pk < 0 choose ykyk+1 ←yk ; pk+1 ←pk +2xk +3
else choose yk –1yk+1 ←yk –1; pk+1 ←pk + 2xk – 2yk + 5
xk+1 ←xk +1draw (xk+1 +xc ,yk+1 +yc )xk ←xk+1 ; yk ←yk+1;pk ←pk+1
x 0 ; y 0 ; r 10 plot (0,10)
pk 1 10 9 choose E plot 1,10
pk 9 2 3 4 choose E plot 2,10
pk 4 4 3 3 choose SE plot 3,9
pk 3 6 18 5 4 choose E plot 4,9
pk 4 8 3 7 choose SE plot 5,8
pk 7 10 16 5 6 choose SE plot 6,7
pk 6 12 14 5 9 choose SE plot 7,6
if pk < 0 choose ykyk+1 ←yk ; pk+1 ←pk +2xk +3
else choose yk –1yk+1 ←yk –1; pk+1 ←pk + 2xk – 2yk + 5
yk
yk
yk
yk -1
yk -1
yk -1
yk -1
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 y tangent 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