30/9/2008Lecture 21 Computer Graphics Assistant Professor Dr. Sana’a Wafa Al-Sayegh 2 nd Semester...

32
30/9/2008 2 Lecture 1 Computer Graphics Assistant Professor Dr. Sana’a Wafa Al-Sayegh 2 nd Semester 2008-2009 ITGD3107 University of Palestine

Transcript of 30/9/2008Lecture 21 Computer Graphics Assistant Professor Dr. Sana’a Wafa Al-Sayegh 2 nd Semester...

30/9/2008 Lecture 2 1

Computer Graphics

Assistant Professor

Dr. Sana’a Wafa Al-Sayegh

2nd Semester 2008-2009

ITGD3107

University of Palestine

30/9/2008 Lecture 2 2

Chapter 3

Output Primitives

line, circle , Ellipse algorithms and others

ITGD3107 Computer Graphics

30/9/2008 Lecture 2 3

Output Primitives• What Kind of Math do We Need?

• Drawing Lines, Circles, Ellipses Filled Polygons and Characters

• Ideal Line, simple line

• Vector Generation

• DDA Algorithm

• Bresenham algorithm

• Circle Drawing algorithms

• Ellipse Drawing algorithms

• Representing Characters

• Drawing Filled Polygons

30/9/2008 Lecture 2 4

What Kind of Math do We Need?Cartesian Coordinates• Typically modeling space is floating point, screen space is integer

screen coordinates are measured top to bottom, based on raster scan

(0,0)

x, y Cartesian grid Integer Grid

30/9/2008 Lecture 2 5

Example 3D PrimitivesPolyhedron

Sphere

Polyline

Patch

30/9/2008 Lecture 2 6

Drawing Lines

• For horizontal, vertical and diagonal lines all pixels lie on ideal line: special case

• For lines at arbitrary angle, pick pixels closest to ideal line (Bresenham’s midpoint “scan conversion” algorithm)

• For thick lines, use multiple pixels in each column or fill a rotated rectangle

Sampling continuous line on discrete grid introduces sampling errors: “jaggies”

30/9/2008 Lecture 2 7

Towards the Ideal Line• We can only do a discrete approximation

• Illuminate pixels as close to the true path as possible, consider bi-level display only– Pixels are either lit or not lit

30/9/2008 Lecture 2 8

What is an ideal line• Must appear straight and continuous

– Only possible axis-aligned and 45o lines

• Must interpolate both defining end points

• Must have uniform density and intensity– Consistent within a line and over all lines

• Must be efficient, drawn quickly– Lots of them are required!!!

30/9/2008 Lecture 2 9

Simple Line

Based on slope-intercept algorithm from algebra:

y = mx + b

Simple approach:

increment x, solve for y

Floating point arithmetic required

30/9/2008 Lecture 2 10

Does it Work?

It seems to work okay for lines with a slope of 1 or less,

but doesn’t work well for lines with slope greater than 1 – lines become more discontinuous in appearance and we must add more than 1 pixel per column to make it work.

Solution? - use symmetry.

30/9/2008 Lecture 2 11

Modification

OR, increment along x-axis if dy<dx else increment along y-axis

30/9/2008 Lecture 2 12

Vector Generation

• There are two vector generation algorithm:• DDA = Digital Differential Analyser

– finite differences

• Bresenham’s Algorithm

30/9/2008 Lecture 2 13

DDA algorithm

• DDA = Digital Differential Analyser– finite differences

• Treat line as parametric equation in t :

)()(

)()(

121

121

yytyty

xxtxtx

),(

),(

22

11

yx

yxStart point

End point

30/9/2008 Lecture 2 14

DDA Algorithm

• Start at t = 0

• At each step, increment t by dt

• Choose appropriate value for dt

• Ensure no pixels are missed:– Implies: and

• Set dt to maximum of dx and dy

)()(

)()(

121

121

yytyty

xxtxtx

dt

dyyy

dt

dxxx

oldnew

oldnew

1dt

dx 1dt

dy

30/9/2008 Lecture 2 15

DDA algorithmline(int x1, int y1, int x2, int y2)

{float x,y;int dx = x2-x1, dy = y2-y1;int n = max(abs(dx),abs(dy));float dt = n, dxdt = dx/dt, dydt = dy/dt;

x = x1;y = y1;while( n-- ) {

point(round(x),round(y));x += dxdt;y += dydt;}

}

n - range of t.

30/9/2008 Lecture 2 16

DDA algorithm

• Still need a lot of floating point arithmetic.– 2 ‘round’s and 2 adds per pixel.

• Is there a simpler way ?

• Can we use only integer arithmetic ?– Easier to implement in hardware.

30/9/2008 Lecture 2 17

Bresenham (mid-point) algorithm

• Choose between 2 pixels at each step based upon the sign of a decision variable.

• Update the decision variable based upon which pixel is chosen.

• Start point is simply first endpoint (x1,y1).

• Need to calculate the initial value for d

30/9/2008 Lecture 2 18

Bresenham algorithmm<1

void MidpointLine1(int x1,y1,x2,y2)

{

int dx=x2-x1;

int dy=y2-y1;

int d=2*dy-dx;

int increE=2*dy;

int incrNE=2*(dy-dx);

x=x1;

y=y1;

WritePixel(x,y);

while (x < x2) {if (d<= 0) {

d+=incrE;x++

} else {d+=incrNE;x++;y++;

}WritePixel(x,y);

}}

30/9/2008 Lecture 2 19

Bresenham algorithmm>=1

void MidpointLine2(int x1,y1,x2,y2)

{

int dx=x2-x1;

int dy=y2-y1;

int d=2*dx-dy;

int increE=2*dx;

int incrNE=2*(dx-dy);

x=x1;

y=y1;

WritePixel(x,y);

while (y < y2) {if (d<= 0) {

d+=incrE;y++;

} else {d+=incrNE;x++;

y++;}WritePixel(x,y);

}}

30/9/2008 Lecture 2 20

Drawing Circles and Ellipses

circle outline

filled ellipse

30/9/2008 Lecture 2 21

Circle drawing.

• Can also use Bresenham to draw circles.• Use 8-fold symmetry

Choices forNext pixel

M

E

SE

PreviousPixel

Choices forCurrent pixel

30/9/2008 Lecture 2 22

Circle drawing.

• First method is:

222 )()( ryyxx cc

22 )( xxryy cc

rxc rxc • Where x value steps from

to

30/9/2008 Lecture 2 23

Circle drawing.

• Second method is:

)cos(rxx c

)sin(ryy c

30/9/2008 Lecture 2 24

Circle drawing.

30/9/2008 Lecture 2 25

Example: Midpoint Circle algorithm:

30/9/2008 Lecture 2 26

Ellipse Drawing

)cos(xc rxx

)sin(yc ryy

30/9/2008 Lecture 2 27

Midpoint Ellipse Algorithm

30/9/2008 Lecture 2 28

Example: Midpoint Ellipse Algorithm

30/9/2008 Lecture 2 29

Example: Midpoint Ellipse Algorithm

30/9/2008 Lecture 2 30

Drawing Characters• For characters defined by small bitmap

– selectively write pixels of refresh buffer corresponding to “true” bits of character bitmap

– Outline fonts: defined in terms of (mathematical) drawing primitives (lines, arcs, etc).

30/9/2008 Lecture 2 31

Representing Characters

30/9/2008 Lecture 2 32

Drawing Filled Polygons

1. Find intersection of scan line with polygon edges

2. Sort intersections by increasing x

3. Fill the polygon between pairs of intersections (spans)