31
OUTPUT PRIMITIVES CEng 477 Introduction to Computer Graphics METU, 2007

CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

  • Upload
    lyphuc

  • View
    227

  • Download
    3

Embed Size (px)

Citation preview

Page 1: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

OUTPUT PRIMITIVES

CEng 477Introduction to Computer Graphics

METU, 2007

Page 2: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 3: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic
Page 4: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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.

Page 5: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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.

Page 6: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 7: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 8: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

OpenGL Point Functions

● glBegin (GL_POINTS);

glVertex2i(50, 100);

glVertex2i(75, 150);

glVertex2i(100, 200);

glEnd();

Page 9: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

OpenGL Line Functions

● glBegin (GL_LINES);

glVertex2iv(p1);

glVertex2iv(p2);

glVertex2iv(p3);

glVertex2iv(p4);

glVertex2iv(p5);

glEnd();

p5

p3

p1

p4p2

Page 10: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

OpenGL Line Functions

● glBegin (GL_LINE_STRIP);

glVertex2iv(p1);

glVertex2iv(p2);

glVertex2iv(p3);

glVertex2iv(p4);

glVertex2iv(p5);

glEnd();

p5

p3

p1

p4p2

Page 11: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

OpenGL Line Functions

● glBegin (GL_LINE_LOOP);

glVertex2iv(p1);

glVertex2iv(p2);

glVertex2iv(p3);

glVertex2iv(p4);

glVertex2iv(p5);

glEnd();

p5

p3

p1

p4p2

Page 12: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 13: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 14: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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?

Page 15: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 16: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 17: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 18: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

Example from the textbook

● Using Bresenham’s algorithm digitize the line with endpoints (20,10) and (30,18)

Page 19: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

Example continued...

20 21 3022 25

10

18

Page 20: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

Plotted pixels

20 21 3022 25

10

18

Page 21: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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?

Page 22: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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.

Page 23: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 24: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

● 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

Page 25: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

● 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)

Page 26: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 27: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 28: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 29: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 30: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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

Page 31: CEng 477 Introduction to Computer Graphics METU, …ocw.metu.edu.tr/.../lecturenotes_2007/week2_outputprimitives.pdfIntroduction to Computer Graphics. METU, 2007. Recap: The basic

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