1 computer graphics graphics output primitives by : dhaval shah

162
1 COMPUTER GRAPHICS Graphics Output Graphics Output Primitives Primitives By : Dhaval Sh

Upload: melvin-gregory

Post on 29-Dec-2015

254 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

1

COMPUTER GRAPHICS

Graphics Output PrimitivesGraphics Output Primitives

By :Dhaval Shah

Page 2: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Coordinate Frames Line Drawing Algorithms Line Drawing Algorithms: Order Dependence Circle Algorithms Ellipse Algorithms Pixel Addressing Area Fill Methods Plane Equations OpenGL Area Fill Vertex Arrays Pixel Array Primitives Character Handling

2

Page 3: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Coordinate Frame

In geometry, a coordinate frame is a system which uses one or more numbers, or coordinates, to uniquely determine the position of a point or other geometric element on a manifold such as Euclidean space

The use of a coordinate system allows problems in geometry to be translated into problems about numbers and vice versa; this is the basis of analytic geometry.

 Common coordinate systems are  Number line 1.2 Cartesian coordinate system ------------------> 1.3 Polar coordinate system 1.4 Cylindrical and spherical coordinate systems 1.5 Homogeneous coordinate system 1.6 Other commonly used systems

3

Page 4: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Screen Coordinate

Screen Coordinate System - This 2D coordinate system refers to the physical coordinates of the pixels on the computer screen, based on current screen resolution.

4Example :Diagram of Screen Coordinate system

Page 5: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Explanation

x, y coordinates are respectively the horizontal and vertical addresses of any pixel or addressable point on a computer display screen. The x coordinate is a given number of pixels along the horizontal axis of a display starting from the pixel (pixel 0) on the extreme left of the screen. The y coordinate is a given number of pixels along the vertical axis of a display starting from the pixel (pixel 0) at the top of the screen. Together, the x and coordinates locate any specific pixel location on the screen. x and y coordinates can also be specified as values relative to any starting point on the screen or any subset of the screen such as an image. On the Web, each clickable area of an image map is specified as a pair of and y coordinates relative to the upper left-hand corner of the image

5

Page 6: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

PointsPoints

The electron beam is turned on to illuminate the phosphor at the selected location (x, y) where

0 ≤ x ≤ maxx

0 ≤ y ≤ maxy

setpixel(x, y, intensity) – loads an intensity value into the frame-buffer at (x, y).

getpixel(x, y) – retrieves the current frame-buffer intensity setting at position (x, y).

6

(0,0)

(maxx,maxy)

CRT

Page 7: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

7

Page 8: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

LinesLines

Analog devises, such as a random-scan display or a vector plotter, display a straight line smoothly from one endpoint to another. Linearly varying horizontal and vertical deflection voltages are generated that are proportional to the required changes in the x and y directions to produce the smooth line.

8

Page 9: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Digital devices display a straight line by plotting discrete coordinate points along the line path which are calculated from the equation of the line.

Screen locations are referenced with integer values, so plotted positions may only approximate actual line positions between two specific endpoints.

A computed line position of (10.48, 20.51) will be converted to pixel position (10, 21). This rounding of coordinate values to integers causes lines to be displayed with a stairstep appearance (the “jaggies”).

Particularly noticeable on systems with low resolution.

To smooth raster lines, pixel intensities along the line paths must be adjusted.

9

Page 10: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Line Drawing AlgorithmsLine Drawing Algorithms

Cartesian equation:

y = mx + c

wherem – slope

c – y-intercept

x

y

xx

yym

12

12

10

x1

y1

x2

y2

Page 11: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

The Digital Differential Analyzer The Digital Differential Analyzer (DDA) Algorithm(DDA) Algorithm

mx

y

x y0 11 42 73 104 135 16

my

x 1

11

means that for a unit (1) change in x there is m-change in y.

i.e. y = 3x + 1 m = 3

means that for a unit (1) change in y there is 1/m change in x.

Do not usey = 3x + 1 to calculate y. Use m

Page 12: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

The DDA MethodThe DDA MethodUses differential equation of the line : mmIf slope |m| 1 then increment x in steps of 1 pixel and find corresponding y-values.If slope |m| 1 then increment y in steps of 1 pixel and find corresponding x-values.

12step through in x step through in y

Page 13: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

The DDA MethodThe DDA Method

13

Desired line

(xi,round(yi))

(xi,yi)

(xi+1,round(yi+m))

(xi+1,yi+m)

Page 14: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

if slope if slope mm 0 0

if ||mm| | 1 1

xi+1 = xi + 1

yi+1 = yi + m

14

if ||mm| | 1 1

yi+1 = yi + 1

xi+1 = xi + 1/m

Left

Right

Left

Right

Page 15: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Proceeding from right-endpoint to left-Proceeding from right-endpoint to left-endpointendpoint

if slope if slope mm 0 0

if ||mm| | 1 1

xi+1 = xi - 1

yi+1 = yi - m

15

if ||mm| | 1 1

yi+1 = yi - 1

xi+1 = xi - 1/m

Left

Right

Left

Right

Page 16: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

if slope if slope mm < 0 < 0

if ||mm| | 1 1

xi+1 = xi + 1

yi+1 = yi + m

16

if ||mm| | 1 1

yi+1 = yi - 1

xi+1 = xi - 1/m

Left

Right

Left

Right

Page 17: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Proceeding from right-endpoint to left-Proceeding from right-endpoint to left-endpointendpoint

if slope if slope mm 0 0

if ||mm| | 1 1

xi+1 = xi - 1

yi+1 = yi - m

17

if ||mm| | 1 1

yi+1 = yi + 1

xi+1 = xi + 1/m

Left

Right

Left

Right

Page 18: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Simple DDA Line Algorithm{Based on the parametric equation of a line}

Procedure DDA(X1,Y1,X2,Y2 :Integer);

Var Length, I :Integer;

X,Y,Xinc,Yinc :Real;

Begin

Length := ABS(X2 - X1);

If ABS(Y2 - Y1) > Length Then

Length := ABS(Y2-Y1);

Xinc := (X2 - X1)/Length;

Yinc := (Y2 - Y1)/Length;

X := X1;

Y := Y1;

DDA (digital differential analyzer) creates good lines but it is too time

consuming due to the round function and long operations on real values.

For I := 0 To Length Do

Begin

Plot(Round(X), Round(Y));

X := X + Xinc;

Y := Y + Yinc

End {For}

End; {DDA}

Page 19: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Bresenham Line AlgorithmBresenham Line AlgorithmA more efficient approach

Basis of the algorithm:

From start position decide A or B next

AA

BB

19

Start position

Page 20: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Bresenham Line AlgorithmBresenham Line Algorithm

For a given value of xone pixel lies at distance ti above the line, andone pixel lies at distance si below the line

20

True line

si

ti

Page 21: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Bresenham Line AlgorithmBresenham Line Algorithm

Decision parameter

di = (si - ti)

If di 0, then closest pixel is below true line (si smaller)

If di 0, then closest pixel is above true line (ti smaller)

We must calculate the new values for di as we move along the line.

21

Page 22: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Example:Example:)2or 5.0 (i.e. 0.5 (slope)gradientLet dxdy

dx

dy22

3dy2dydy

True line

Start pixel at (x0,y1)

4dy

At At xx11 : s1 = dy t1 = dx - dyd1 = (si - ti) = dy - (dx - dy) = 2dy - dxbut 2dy dx di 0 y stays the samehence next pixel is at (x1,y1)

At At xx22 : s2 = 2dy t2 = dx - 2dyd2 = (s2 – t2) = 2dy - (dx - 2dy) = 4dy - dxSuppose d2 0 y is incrementedhence next pixel is at (x2,y2)

At At xx33 : s3 = 3dy - dx t2 = 2dx - 3dyd3 = (s2 – t3) = 6dy - 3dx 0so y stays the samehence next pixel is at (x3,y2)

x1 x2 x3x4 x5

x0

y0

y1

y2

y3

y5

Page 23: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

In GeneralIn General For a line with gradient ≤ 1d0 = 2dy – dxif di 0 then yi+1 = yi

di+1 = di + 2dyif di ≥ 0 then yi+1 = yi + 1

di+1 = di + 2(dy – dx)

xi+1 = xi + 1

23

For a line with gradient 1d0 = 2dx – dyif di 0 then xi+1 = xi

di+1 = di + 2dxif di ≥ 0 then xi+1 = xi + 1

di+1 = di + 2(dx – dy)yi+1 = yi + 1

Note: For |m| ≤ 1 the constants 2dy and 2(dy-dx) can be calculated once,so the arithmetic will involve only integer addition and subtraction.

Page 24: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Example Example – Draw a line from (20,10) to (30,18)

19

18

17

16

15

14

13

12

11

10

20 21 22 23 24 25 26 27 28 29 30 31 32 24

(20,10)

(30,18)dx = 10dy = 8

initial decision d0 = 2dy – dx = 6Also 2dy = 16, 2(dy – dx) = -4

i di (xi+1,yi+1)

0 6 (21,11) 1 2 (22,12) 2 -2 (23,12) 3 14 (24,13) 4 10 (25,14) 5 6 (26,15) 6 2 (27,16) 7 -2 (28,16) 8 14 (29,17) 9 10 (30,18)

Page 25: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

void LineBres(int x0, int y0, int x1, int y1) // line for |m| < 1

{ int dx = abs(x1 – x0), dy = abs(y1 – y0); int d = 2 * dy – dx, twoDy = 2 * dy, twoDyMinusDx = 2

* (dy – dx); int x, y;

if (x0 > x1) { // determines which point to use as start, which as end

x = x1; y = y1; x1 = x0; } else { x = x0; y = y0; } setPixel(x,y);

while (x < x1) { x++; if (d < 0) d += twoDy; else { y++; d += twoDyMinusDx; } setPixel(x, y); }}

25

Page 26: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing

When an object is scan converted into the frame buffer, the input description is transformed to pixel coordinates.

So, the displayed image may not correspond exactly with the relative dimensions of the input object.

To preserve the specified geometry of world objects, we need to compensate for the mapping of mathematical input points to finite pixel area, we use one of the two ways:

Page 27: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing

1) Adjust the dimensions of displayed objects to account for the amount of overlap of pixel areas with the object boundaries.

(i.e. a rectangle with 40 cm width, will be displayed in 40 pixel)

2) Map world coordinates onto screen positions between pixels, so that we align objects boundaries with pixel boundaries instead of pixel centers.

Page 28: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing

Screen Grid Coordinates:

An alternative to addressing display positions in terms of pixel centers is to reference screen coordinates with respect to the grid of horizontal and vertical pixel boundary lines spaced one unit a part.

Page 29: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing

Screen coordinate position is then the pair of integer values identifying a grid intersection position between two pixels.

For example, the mathematical line path for a polyline with screen endpoints (0, 0), (5, 2), and (1,4) is shown beside.

Page 30: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing With the coordinate

origin at the lower left of the screen, each pixel area can be referenced by the integer grid coordinates of its lower left corner.

The following figure illustrates this convention for an 8 by 8 section of a raster, with a single illuminated pixel at screen coordinate position (4, 5).

Page 31: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing

In general, we identify the area occupied by a pixel with screen coordinates ( x, y) as the unit square with diagonally opposite corners at (x, y) and ( x + 1, y + 1 ).

This pixel addressing scheme has several advantages:

1. It avoids half-integer pixel boundary2. It facilitates precise object

representations.3. Simplifies the processing involved in

many scan conversion algorithms and in other raster procedures

Page 32: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing

Notes:

1)      The previous algorithms for drawing line, circle, …etc are still valid when applied to input positions expressed as screen grid coordinates.

2)      The decision parameter Pk is a measure of screen grid separation differences rather than separation differences from pixel centers.

Page 33: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing

A circle of radius 5 and center position (10, 10), for instance, would be displayed by the midpoint circle algorithm using screen grid coordinate positions.

But the plotted circle has a diameter of 11, To plot the circle with the defined diameter of 10, we can modify the circle algorithm to shorten each pixel scan line and each pixel column.

Page 34: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing

Page 35: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixel addressing

Page 36: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Order Dependency

Is this image correct?

Probably not

Polygons are rendered

in the order they pass

down the pipeline

Blending functions

are order dependent

Page 37: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Opaque and Translucent Polygons Suppose that we have a group of polygons

some of which are opaque and some translucent

How do we use hidden-surface removal? Opaque polygons block all polygons behind

them and affect the depth buffer Translucent polygons should not affect depth

buffer Render with glDepthMask(GL_FALSE) which makes

depth buffer read-only

Sort polygons first to remove order dependency

Page 38: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Clipping Operations

Clipping • Identify those portions of a picture that are

either inside or outside of a specified region of space.

Clip window • The region against which an object is to be

clipped.

• The shape of clip window

Applications of clipping

World-coordinate clipping

38

Page 39: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Clipping Operations

Viewport clipping • It can reduce calculations by allowing

concatenation of viewing and geometric transformation matrices.

Types of clipping • Point clipping • Line clipping • Area (Polygon) clipping • Curve clipping • Text clipping

Point clipping (Rectangular clip window)

39

Page 40: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Line Clipping

Possible relationships between line positions and a standard rectangular clipping region

40

Before clipping after clipping

Page 41: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Line Clipping

Possible relationships Completely inside the clipping window Completely outside the window Partially inside the window

Parametric representation of a line x = x1 + u(x2 - x1)

y = y1 + u(y2 - y1) The value of u for an intersection

with a rectangle boundary edge Outside the range 0 to 1 Within the range from 0 to 1

41

Page 42: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Cohen-Sutherland Line Clipping

Region code A four-digit binary code assigned to every line

endpoint in a picture.

Numbering the bit positions in the region code as 1 through 4 from right to left.

42

Page 43: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Cohen-Sutherland Line Clipping

Bit values in the region code • Determined by comparing endpoint

coordinates to the clip boundaries A value of 1 in any bit position: The point is in

that relative position.

Determined by the following steps: • Calculate differences between endpoint

coordinates and clipping boundaries.

• Use the resultant sign bit of each difference calculation to set the corresponding bit value.

43

Page 44: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Cohen-Sutherland Line Clipping The possible relationships:

• Completely contained within the window 0000 for both endpoints.

• Completely outside the window Logical and the region codes of both

endpoints, its result is not 0000.

• Partially

44

Page 45: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Splitting Concave Polygons

Identify a concave polygon • Calculating the cross product of successive edge

vectors.

• If the z component of some cross product is positive while others have a negative, it is concave.

45

Page 46: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Splitting Concave Polygons

Vector method Calculate the edge-vector cross product in a

counterclockwise order.

If any z component turns out to be negative The polygon is concave.

Split it along the line of the first edge vector in the cross-product pair.

46

Page 47: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Splitting Concave Polygons

47

Page 48: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Polygon Clipping48

Page 49: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Sutherland-Hodgeman Polygon Clipping

Processing the polygon boundary as a whole against each window edge

• Processing all polygon vertices against each clip rectangle boundary in turn

49

Page 50: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Sutherland-Hodgeman Polygon Clipping

Pass each pair of adjacent polygon vertices to a window boundary clipper

• There are four cases:

50

Page 51: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Sutherland-Hodgeman Polygon Clipping

Intermediate output vertex list • Once all vertices have been processed for

one clip window boundary, it is generated.

• The output list of vertices is clipped against the next window boundary.

• It can be eliminated by a pipeline of clipping routine.

Convex polygons are correctly clipped. If the clipped polygon is concave

• Split the concave polygon

51

Page 52: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Sutherland-Hodgeman Polygon Clipping

52

v1

v2

v3

'1v

'1v

'2v

''2v '

3v

Page 53: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Weiler-Atherton Polygon Clipping

Developed as a method for identifying visible surfaces • It can be applied with arbitrary polygon-

clipping region. • Not always proceeding around polygon

edges • Sometimes follows the window boundaries

For clockwise processing of polygon vertices • For an outside-to-inside pair of vertices,

follow the polygon boundary. • For an inside-to-outside pair of vertices,

follow the window boundary in clockwise direction.

53

Page 54: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Weiler-Atherton Polygon Clipping

54

Page 55: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Other Clipping

Curve clipping

• Use bounding rectangle to test for overlap with a rectangular clip window.

Text clipping

• All-or-none string-clipping

• All-or-none character-clipping

• Clip the components of individual characters

55

Page 56: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Circle Generating AlgorithmsCircle Generating Algorithms

Circles and ellipses are common components in many pictures.

Circle generation routines are often included in packages.

56

Page 57: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Circle EquationsCircle Equations

Polar form

x = rCos

y = rSin (r = radius of circle)

57

P=(rCos, rSin)

rSin)

rCos)x

y

r

Page 58: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Drawing a circleDrawing a circle

DisadvantagesDisadvantages To find a complete circle varies from 0° to

360°

The calculation of trigonometric functions is very slow.

58

= 0°while ( < 360°)

x = rCosy = rSinsetPixel(x,y) = + 1°

end while

Page 59: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Cartesian formCartesian form

Use Pythagoras theorem

x2 + y2 = r2

59

x

ry

y

x x

y

r 2 2,P x r x

Page 60: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Circle algorithmsCircle algorithms Step through x-axis to determine y-values60

Disadvantages:– Not all pixel filled in– Square root function is very slow

Page 61: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Circle AlgorithmsCircle Algorithms

Use 8-fold symmetry and only compute pixel positions for the 45° sector.

61

45°

(x, y)

(y, x)

(-x, y)

(y, -x)

(x, -y)(-x, -y)

(-y, x)

(-y, -x)

Page 62: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Bresenham’s Circle AlgorithmBresenham’s Circle Algorithm

General PrincipleGeneral Principle The circle function:

and

2 2 2( , )circlef x y x y r

62

Consider only 45° ≤ ≤ 90°

if (x,y) is inside the circle boundary

if (x,y) is on the circle boundary

if (x,y) is outside the circle boundary

0

( , ) 0

0circlef x y

Page 63: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Bresenham’s Circle AlgorithmBresenham’s Circle Algorithm

63

p1 p3

p2

D(si)D(ti)

After point p1, do we choose p2 or p3?

yi

yi - 1

xi xi + 1

r

Page 64: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Bresenham’s Circle AlgorithmBresenham’s Circle Algorithm

Define: D(si) = distance of p3 from circle

D(ti) = distance of p2 from circle

i.e. D(si) = (xi + 1)2 + yi2 – r2 [always

+ve]

D(ti) = (xi + 1)2 + (yi – 1)2 – r2 [always -ve]

Decision Parameter pi = D(si) + D(ti)so if pi < 0 then the circle is closer to p3 (point above) if pi ≥ 0 then the circle is closer to p2 (point below)

64

Page 65: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

The AlgorithmThe Algorithmx0 = 0y0 = rp0 = [12 + r2 – r2] + [12 + (r-1)2 – r2] = 3 – 2r

if pi < 0 thenyi+1 = yi

pi+1 = pi + 4xi + 6

else if pi ≥ 0 thenyi+1 = yi – 1pi+1 = pi + 4(xi – yi) + 10

Stop when xi ≥ yi and determine symmetry points in the other octants

65

xi+1 = xi + 1

Page 66: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

ExampleExample

10 9 8 7 6

5 4 3

2 1 0

0 1 2 3 4 5 6 7 8 9 10

66

i pi xi, yi

0 -17 (0, 10)

1 -11 (1, 10)

2 -1 (2, 10)

3 13 (3, 10)

4 -5 (4, 9)

5 15 (5, 9)

6 9 (6, 8)

7 (7,7)

r = 10

p0 = 3 – 2r = -17

Initial point (x0, y0) = (0, 10)

Page 67: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Advantages of Bresenham Advantages of Bresenham circlecircle

Only involves integer addition, subtraction and multiplication

There is no need for squares, square roots and trigonometric functions

67

Page 68: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Ellipse-Generating AlgorithmsEllipse-Generating Algorithms EllipseEllipse – A modified circle whose radius varies from

a maximum value in one direction (major axis) to a minimum value in the perpendicular direction (minor axis).

68

P=(x,y)F1

F2

d1

d2

The sum of the two distances d1 and d2, between the fixed positions F1 and F2 (called the foci of the ellipse) to any point P on the ellipse, is the same value, i.e.

d1 + d2 = constant

Page 69: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Ellipse PropertiesEllipse Properties Expressing distances d1 and d2 in terms of the focal

coordinates F1 = (x1, x2) and F2 = (x2, y2), we have:

Cartesian coordinates:

Polar coordinates:

2 2 2 21 1 2 2( ) ( ) ( ) ( ) constantx x y y x x y y

22

1c c

x y

x x y y

r r

69

ry

rx

cos

sinc x

c y

x x r

y y r

Page 70: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Ellipse AlgorithmsEllipse Algorithms

Symmetry between quadrants Not symmetric between the two octants of a

quadrant Thus, we must calculate pixel positions along the

elliptical arc through one quadrant and then we obtain positions in the remaining 3 quadrants by symmetry

70

(x, y)(-x, y)

(x, -y)(-x, -y)

rx

ry

Page 71: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Ellipse AlgorithmsEllipse Algorithms

Decision parameter:

2 2 2 2 2 2( , )ellipse y x x yf x y r x r y r r

0 if ( , ) is inside the ellipse

( , ) 0 if ( , ) is on the ellipse

0 if ( , ) is outside the ellipseellipse

x y

f x y x y

x y

71

1

Slope = -1

rx

ry2 2

2

2

2y

x

r xdySlope

dx r y

Page 72: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Ellipse AlgorithmsEllipse Algorithms

Starting at (0, ry) we take unit steps in the x direction until we reach the boundary between region 1 and region 2. Then we take unit steps in the y direction over the remainder of the curve in the first quadrant.

At the boundary

therefore, we move out of region 1 whenever

721

Slope = -1

rx

ry2

2 21 2 2y x

dyr x r y

dx

2 22 2y xr x r y

Page 73: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Midpoint Ellipse AlgorithmMidpoint Ellipse Algorithm

yi yi-1

xi xi+1 xi+2

73

Midpoint

Assuming that we have just plotted the pixels at (xi , yi).The next position is determined by:

12

2 2 2 2 2 212

1 ( 1, )

( 1) ( )

i ellipse i i

y i x i x y

p f x y

r x r y r r

If p1i < 0 the midpoint is inside the ellipse yi is closerIf p1i ≥ 0 the midpoint is outside the ellipse yi – 1 is closer

Page 74: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Decision Parameter (Region 1)Decision Parameter (Region 1)

At the next position [xi+1 + 1 = xi + 2]

OR

where yi+1 = yi

or yi+1 = yi – 1

74

11 1 1 2

2 2 2 2 2 211 2

1 ( 1, )

( 2) ( )

i ellipse i i

y i x i x y

p f x y

r x r y r r

2 2 2 2 2 21 11 1 2 21 1 2 ( 1) ( ) ( )i i y i y x i ip p r x r r y y

Page 75: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Decision Parameter (Region 1)Decision Parameter (Region 1)Decision parameters are incremented by:

Use only addition and subtraction by obtaining

At initial position (0, ry)

752 2

12 2 2

1 1

2 if 1 0

2 2 if 1 0y i y i

y i y x i i

r x r pincrement

r x r r y p

2 22 and 2y xr x r y

2

2 2

2 2 2 2 21 10 2 2

2 2 214

2 02 2

1 (1, ) ( )

y

x x y

ellipse y y x y x y

y x y x

r xr y r r

p f r r r r r rr r r r

Page 76: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Region 2Region 2Over region 2, step in the negative y direction and midpoint is taken between horizontal pixels at each step.

76

yi yi-1

xi xi+1 xi+2

Midpoint

Decision parameter:12

2 2 2 2 2 212

2 ( , 1)

( ) ( 1)

i ellipse i i

y i x i x y

p f x y

r x r y r r

If p2i > 0 the midpoint is outside the ellipse xi is closerIf p2i ≤ 0 the midpoint is inside the ellipse xi + 1 is closer

Page 77: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Decision Parameter (Region 2)Decision Parameter (Region 2)

At the next position [yi+1 – 1 = yi – 2]

OR

where xi+1 = xi

or xi+1 = xi + 1

77

11 1 12

2 2 2 2 2 211 2

2 ( , 1)

( ) ( 2)

i ellipse i i

y i x i x y

p f x y

r x r y r r

2 2 2 2 21 11 1 2 22 2 2 ( 1) ( ) ( )i i x i x y i ip p r y r r x x

Page 78: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Decision Parameter (Region 2)Decision Parameter (Region 2)

Decision parameters are incremented by:

At initial position (x0, y0) is taken at the last position selected in region 1

78

2 21

2 2 21 1

2 if 2 0

2 2 if 2 0x i x i

y i x i x i

r y r pincrement

r x r y r p

10 0 02

2 2 2 2 2 210 02

2 ( , 1)

( ) ( 1)

ellipse

y x x y

p f x y

r x r y r r

Page 79: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Midpoint Ellipse AlgorithmMidpoint Ellipse Algorithm1. Input rx, ry, and ellipse center (xc, yc), and obtain the

first point on an ellipse centered on the origin as

(x0, y0) = (0, ry)

2. Calculate the initial parameter in region 1 as

3. At each xi position, starting at i = 0, if p1i < 0, the next point along the ellipse centered on (0, 0) is (xi

+ 1, yi) and

otherwise, the next point is (xi + 1, yi – 1) and

and continue until

79

2 2 210 41 y x y xp r r r r

2 21 11 1 2i i y i yp p r x r

2 2 21 1 11 1 2 2i i y i x i yp p r x r y r

2 22 2y xr x r y

Page 80: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Midpoint Ellipse AlgorithmMidpoint Ellipse Algorithm4. (x0, y0) is the last position calculated in region 1.

Calculate the initial parameter in region 2 as

5. At each yi position, starting at i = 0, if p2i > 0, the next point along the ellipse centered on (0, 0) is (xi, yi – 1) and

otherwise, the next point is (xi + 1, yi – 1) and

Use the same incremental calculations as in region 1. Continue until y = 0.

6. For both regions determine symmetry points in the other three quadrants.

7. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc, yc) and plot the coordinate values x = x + xc , y = y + yc

80

2 2 2 2 2 210 0 022 ( ) ( 1)y x x yp r x r y r r

2 21 12 2 2i i x i xp p r y r

2 2 21 1 12 2 2 2i i y i x i xp p r x r y r

Page 81: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

ExampleExample

81

i pi xi+1, yi+1 2ry2xi+1 2rx

2yi+1

0 -332 (1, 6) 72 768

1 -224 (2, 6) 144 768

2 -44 (3, 6) 216 768

3 208 (4, 5) 288 640

4 -108 (5, 5) 360 640

5 288 (6, 4) 432 512

6 244 (7, 3) 504 384

rx = 8 , ry = 6

2ry2x = 0 (with increment 2ry

2 = 72)

2rx2y = 2rx

2ry (with increment -2rx2 = -128)

Region 1Region 1

(x0, y0) = (0, 6)2 2 21

0 41 332y x y xp r r r r

Move out of region 1 since2ry

2x > 2rx2y

Page 82: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

ExampleExample

6

5 4 3

2 1 0

0 1 2 3 4 5 6 7 882

i pi xi+1, yi+1 2ry2xi+1 2rx

2yi+1

0 -151 (8, 2) 576 256

1 233 (8, 1) 576 128

2 745 (8, 0) - -

Region 2Region 2

(x0, y0) = (7, 3) (Last position in region 1)1

0 22 (7 ,2) 151ellipsep f

Stop at y = 0

Page 83: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Mathematical equation of a line

p0

vt > 0

t < 0 t = 0

( ) , ( , )t t t 0p v

Page 84: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Line Equations

Let’s quickly review the equations involved in drawing lines

x

y

y0

yend

xendx0

Slope-intercept line equation:

bxmy where:

0

0

xx

yym

end

end

00 xmyb

Page 85: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Lines & Slopes The slope of a line (m) is defined by its start and end coordinates

The diagram below shows some examples of lines and their slopes

m = 0

m = -1/3

m = -1/2

m = -1

m = -2m = -4

m = ∞

m = 1/3

m = 1/2

m = 1

m = 2m = 4

m = 0

Page 86: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

A Very Simple Solution

We could simply work out the corresponding y

coordinate for each unit x coordinate

Let’s consider the following example:

x

y

(2, 2)

(7, 5)

2 7

2

5

Page 87: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

A Very Simple Solution (cont…)

1

2

3

4

5

0

1 2 3 4 5 60 7

Page 88: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

A Very Simple Solution (cont…) First work out m and b:

x

y

(2, 2)

(7, 5)

2 3 4 5 6 7

2

5

5

3

27

25

m

5

42

5

32 b

Now for each x value work out the y value:

5

32

5

43

5

3)3( y

5

13

5

44

5

3)4( y

5

43

5

45

5

3)5( y

5

24

5

46

5

3)6( y

Page 89: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

A Very Simple Solution (cont…)

Now just round off the results and turn on these pixels to draw our line

35

32)3( y

35

13)4( y

45

43)5( y

45

24)6( y

0 1 2 3 4 5 6 7 8

0

1

2

3

4

5

6

7

Page 90: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

A Very Simple Solution (cont…)

However, this approach is just way too slow

In particular look out for:

The equation y = mx + b requires the

multiplication of m by x Rounding off the resulting y coordinates

We need a faster solution

Page 91: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

91

.91.

Raster line

• For now, we consider only 1-pixel width lines.

NENE

EE

M1P (xp, yp)

RasterRaster

• The screen pixel mesh is represented in different forms

Page 92: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Display the line on a Raster Monitor

This process digitizes the line into a set of discrete integer positions that, in general, only approximates the actual line path.

the graphics system must first project the endpoints to integer screen coordinates and determine the nearest pixel positions along the line path between the two endpoints.

Reading from the frame buffer, the video controller plots the screen pixels. This process digitizes the line into a set of discrete integer positions that,

in general, only approximates the actual line path

The line color is loaded into the frame buffer at the corresponding pixel coordinates.

Page 93: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

What is a “pixel”

From a geometry point of view, a pixel is a point. Q: Where is (2,1)?

2

2

1

10 3 4

3

5

Q: What is a pixel? A square or a point?

Page 94: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

But when we think about images, a pixel is a rectangle.

Q: Where is (2,1)? A. The center of a pixel

1

2

0

10 3 4

2

Page 95: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Using Open-GL

Files: .h, .lib, .dll The entire folder gl is placed in the

Include directory of Visual C++ The individual lib files are placed in

the lib directory of Visual C++ The individual dll files are placed in

C:\Windows\System32

Page 96: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Using Open-GL (2)

Includes: <windows.h>

<gl/gl.h>

<gl/glu.h>

<gl/glut.h>

<gl/glui.h> (if used)

Include in order given. If you use capital letters for any file or directory, use them in your include statement also.

Page 97: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Using Open-GL (3)

Changing project settings: Visual C++ 6.0

Project menu, Settings entry

In Object/library modules move to the end of the line and add glui32.lib glut32.lib glu32.lib opengl32.lib (separated by spaces from last entry and each other)

In Project Options, scroll down to end of box and add same set of .lib files

Close Project menu and save workspace

Page 98: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Using Open-GL (3)

Changing Project Settings: Visual C++ .NET 2003

Project, Properties, Linker, Command Line

In the white space at the bottom, add glui32.lib glut32.lib glu32.lib opengl32.lib

Close Project menu and save your solution

Page 99: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Getting Started Making Pictures

Graphics display: Entire screen (a); windows system (b); [both have usual screen coordinates, with y-axis down]; windows system [inverted coordinates] (c)

Page 100: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Example: A Rectangle

moveTo(4, 4);//move to starting corner

lineTo(-2, 4); lineTo(-2, -2); lineTo(4, -2); lineTo(4, 4);

//close the square

Page 101: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Drawing Aligned Rectangles

glRecti (GLint x1, GLint y1, GLint x2, GLint y2); // opposite corners; filled with current color; later rectangles are drawn on top of previous ones

Page 102: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Aspect Ratio of Aligned Rectangles Aspect ratio = width/height

Page 103: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Area Fill

Different types of Polygons

Simple Convex

Simple Concave

Non-simple : self-intersecting

With holes

Convex Concave Self-intersecting

Page 104: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

PolygonsA polygon is a many-sided planar figure composed of vertices and edges. A polygon is bounded (finite area) and closed (includes boundary).

Vertices are represented by points (x,y).

Edges are represented as line segments which connect two points, (x1,y1) and (x2,y2).

P = { (xi , yi ) } i=1,n

E3

(x3,y3)

E2

(x2,y2)E1

(x1,y1)

Page 105: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Polygons: Complex vs Simple

• A simple polygon – edges only intersect a vertices, no coincident vertices

• A complex polygon – edges intersect and/or coincident vertices

A

B

C

C

EF

G

H

A B,E

C

D

F

A

B

C

A B

C

D

E

Page 106: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Simple Polygons: Convex and Concave

Convex Polygon - For any two points P1, P2 inside the polygon, all points on the line segment which connects P1 and P2 are inside the polygon.

– All points P = uP1 + (1-u)P2, u in [0,1] are inside the polygon provided that P1 and P2 are inside the polygon.

Concave Polygon - A polygon which is not convex.

Page 107: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Filling Concave Polygons

Fill the polygon 1 scanline at a time

Determine which pixels on each scanline are inside the polygon and set those pixels to the appropriate value.

Look only for those pixels at which changes occur.

Page 108: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Scan-Line Algorithm

For each scan-line:

-Find the intersections of the scan line with all edges of the polygon.

-Sort the intersections by increasing x-coordinate.

-Fill in all pixels between pairs of intersections.

Possible Problems

1. Horizontal edges ==> Ignore

2. Vertices ==> If local max or min, then count twice, else count once. (This is implemented by shortening one edge by one pixel.)

3. Calculating intersections is slow.

2

4

6

8

For scan-line number 7 the sorted list of x-coordinates is (1,3,7,9)

Therefore fill pixels with x-coordinates 1-3 and 7-9.

Page 109: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Edge Coherence-Not all polygon edges intersect each scanline.

-Many edges intersected by scanline yk will also be intersected by

yk+1

-Nearby pixels on given edge and span have similar coordinates, attributes and equation components

Page 110: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

“scan-line aligned” trapezoids

T1

T2

T3

T4

T5T6

-break up problem into simpler ”scan-line aligned” trapezoids- only two side edges: left, right

left edgeof T2

right edgeof T2

Page 111: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Edge Table

T1

T2

T3

T4

T5T6

-sort vertices by y coordinate building Edge Table (ET)

ymax

y=0A

B

CD

F

E

G

H

yD,C

yG

yE

yB

yH

yA AH AB

HG

BC

EF ED

GF

ET

not needed

Page 112: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Scan AET with AEL

T1

T2

T3

T4

T5T6

-scan up through ET and track Active Edge Table (AET). At each scan-line fill in span of pixel for each edge pair.

ymax

y=0A

CD

F

E

G

H

yG

yE

yB

yH

yA AH AB

HG

BC

EF ED

GF

ET AETk

(k = 1..10)

[(AH,AB)]

[(HG,AB)]

[(HG,BC)]

[(HG,EF),(DE,BC)]

[(GF,EF),(DE,BC)]

[(DE,BC)]

B

Page 113: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

How do we fill trapezoids?

-Need to generate pixel coordinates of left and right edges of edge pair-- (lx,ly) and (rx,ry)-Need to fill pixel in span between current

A

CD

F

E

G

H

B

A

CD

F

E

G

H

B

rx0, lx0

y0

rx2rx3

lx1,

lx2,

lx3

H

G

y1

y2

y3

rx1

Page 114: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Rasterize Edge-From y=mx + b we have:

yk+1= yk+1,

xk+1= xk+ 1 / m = xk+ Δx / Δy - fractions yuck!

-So split into integer and fraction and look at fraction numerator: xk+1= xk+ (Δx div Δy) + (Δx mod Δy) / Δy

integer fraction

xk = xik + xnk / Δ y, xi is integer component xn is x’s fraction’s numerator

xk=0

yk=0

yk

yk+1

xk xk+1 …

B

Page 115: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Rasterize Edge

-Base Case: xi = x0 , xn = 0-Iteration Case: xi is integer component, xn is fraction numerator

xi := xi + (Δx div Δy)xn := xn + (Δx mod Δy)if xn ≥ Δy then

xi := xi + 1 xn := xn – Δyendif

A=(6,1)

B=(13,4)

Δx=7Δy=3Δx div Δy= 2Δx mod Δy= 1 k y xi xn x (=xi+xn/Δy)

0 1 6 0 61 2 8 1 8 1/32 3 10 2 10 2/33 4 13 0 13 0/3(8,2)

(10,3)

Warning: H&B doesn’t handle Δx > Δy

Page 116: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Splitting Concave Polygons – Concept

Concave can be split into Convex Polygons – this simplifies Edge Tableand Active Edge Table in polygon rasterizer (only one scan-line trapezoid is active at a time!)

Identify: look for interior angle greater than 180º - cross product signs will differ

X

Y E1

E2

E3

E4

E5

E6

(E1 X E2)z > 0

(E2 X E3)z > 0

(E3 X E4)z < 0

(E4 X E5)z < 0

(E5 X E6)z > 0

(E6 X E1)z > 0

i j kx0 y0 z0x1 y1 z1

Page 117: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Splitting Concave Polygons

Edge Vector: Compute Edge Vectors (Ek = Vk+1- Vk) traversing polygon counter-clockwise.If cross product of consecutive edge is negative, then split polygon alongthe embedding line of first edge in cross product

Rotational: put successive edges on x-axis. If next edge has negative x coordinate split polygon on x-axis.

X

YE1

E2

E3

E4

E5

E6

X

Y E1

E2

E3

E4

E5

E6

XY

X

Y

Page 118: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Splitting Convex Polygon to Triangles

Triangle rasterizing is even simpler. Only 3 edges to “sort”. Only fill in 1 or 2 scan-line aligned triangles. Pretty much what all graphics hardware does.

Pick 3 vertices of convex polygon. Split off triangle. Repeat with remaining vertices. Continue until only 3 vertices left.

v0

v1

v2

v3

v4v0

v1

v2

v3

v4

v0

v1

v2

v3

v4

A) B) C)

Page 119: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

To make a realistic image...

...objects must be able to be combined

One of the most popular ways for combining objects...

...is with Boolean set operators

...using union, difference, and intersection

Boolean set operators are 3D equivalents of simple 2D

Building Realistic...Objects using solid modeling

Page 120: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Building Realistic...Objects using solid modeling

Two intersectingcubes

Unionoperation

Intersectionoperation

Differenceoperation:Bottom-Top

Differenceoperation:Top-Bottom

Page 121: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Using ordinary Boolean set operators, not all intersections form solid objects... ...they may instead form a plane, a line...

Building Realistic...Objects using solid modeling

Two intersecting cubesproducing a plane

Two intersecting cubesproducing a solid Two intersecting cubes

producing a line

Page 122: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

• Or...they may instead form a point or be null

Building Realistic...Objects using solid modeling

Two intersecting cubesproducing a point

Two cubes producing a null set

Page 123: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Using regularized Boolean set operators, only solid objects or null sets are formed...

...let's look at the same set of examples using regularized operators:

Building Realistic...Objects using solid modeling

Two intersecting cubesproducing a null set

Two intersecting cubesproducing a solid Two intersecting cubes

producing a null set

Page 124: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Building Realistic...Objects using solid modeling

Two intersecting cubesagain producing a null set

Two cubes producing a null set

Page 125: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Solid objects created with sweeps can be manipulated using Boolean set operations...

...by first converting the objects into boundary representations, spatial-partitioning representations, or constructive solid geometry

Building Realistic...Objects using solid modeling

Two simple sweeps of2D objects (triangles) How these objects

would look whenoverlapping

The result ofa union operation;it can no longerbe thought of asa simple sweep

Page 126: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Data structure

2 basic spatial data models exist

vector: based on geometry ofpointslines Polygons

raster: based on geometry ofgrid cells (images, bitmaps, DEMs)

Page 127: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Vector Data Model

Points: represent discrete point features

airports are point features

each point is stored as a coordinate pair

each point locationhas a record in thetable

Page 128: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Raster Data Model

origin is set explicitly

cell size is always known

cell references (row/column locations)are known

cell values are referencedto row/column location

values represent numerical phenomena orindex codes for non-numerical phenomena

Page 129: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Raster Data Model

• digital orthophoto

• digital elevation model (DEM)

A few different types of raster data

Page 130: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Normal equation of plane

The normal is perpendicular to any line in the plane

n

o

A R

ARn______________

0AR n______________

( ) 0 n r a

n r n a

(p,q,r)

(a,b,c)

(x,y,z)

px qy rz n aCartesian equation of a plane

px qy rz k

Page 131: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

6.1 Definitions

Definition 1: A nonzero vector x is an eigenvector (or characteristic vector) of a square matrix A if there exists a scalar λ such that Ax = λx. Then λ is an eigenvalue (or characteristic value) of A.

Note: The zero vector can not be an eigenvector even though A0 = λ0. But λ = 0 can be an eigenvalue.

Example:

Show x 2

1

isan eigenvector for A

2 4

3 6

Solution : Ax 2 4

3 6

2

1

0

0

But for 0, x 02

1

0

0

Thus, x isan eigenvector of A,and 0 isan eigenvalue.

Page 132: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

6.3 Eigenvectors

Example 1 (cont.):

00

41

41

123)1(:1 AI

0,1

4

,404

2

11

2121

ttx

x

txtxxx

x

00

31

31

124)2(:2 AI

0,1

3

2

12

ss

x

xx

To each distinct eigenvalue of a matrix A there will correspond at least one eigenvector which can be found by solving the appropriate set of homogenous equations. If λi is an eigenvalue then the corresponding eigenvector xi is the

solution of (A – λiI)xi = 0

Page 133: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

OpenGL Fill-Area Functions 1

glBegin(GL_POLYGON); // Specify what to draw, // a polygon

// Geometric info via vertices: glVertex*(); // 1 glVertex*(); // 2 ... // ...glEnd;

glVertex[234][isfd]

[234]: 2D, 3D, 4D[isfd]: integer, short, float, doubleFor instance: glVertex2i(100, 25);

H&B 4-4:81-82

Page 134: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

OpenGL Fill-Area Functions 2GL_POLYGON: convex polygon

Concave polygons give unpredictable results.1

2

34

5

1

3

45

H&B 4-8:94-99

Page 135: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

OpenGL Fill-Area Functions 3

GL_TRIANGLES: sequence of triangles

GL_TRIANGLE_STRIP:

GL_TRIANGLE_FAN:

1

2

3

4

56

7

8

9

1

2

3

4

5

6

7

8

1

23

4

5

6

H&B 4-8:94-99

Page 136: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

OpenGL Fill-Area Functions 4

GL_QUADS: sequence of quadrilaterals

GL_QUAD_STRIP: strip of quadrilaterals

1

2

34

5 6

78

9

1011

12

1

2

3

4

5

6

7

8

H&B 4-8:94-99

Page 137: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

OpenGL Display lists 2

// Straightforward

void drawRobot();

{ // lots of glBegin, glVertex, glEnd calls }

void drawScene(); {

drawRobot();

glTranslate3f(1.0, 0.0, 0.0);

drawRobot();

}

H&B 4-15 111-113

Page 138: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

OpenGL Display lists 3

void drawRobot();

{ // lots of glBegin, glVertex, glEnd calls }

int rl_id;

void init(); {

rl_id = glGenLists(1); // get id for list

glNewList(rl_id, GL_COMPILE); // create new list

drawRobot(); // draw your object

glEndList(); } // end of list

void drawScene(); {

glCallList(rl_id); // draw list

glTranslate3f(1.0, 0.0, 0.0);

glCallList(rl_id); // and again

}

H&B 4-15 111-113

Page 139: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

OpenGL Display lists 4

First, get an id. Either some fixed constant, or get a guaranteed unique one:

rl_id = glGenLists(1); // get id for list

Next, create a display list with this id:

glNewList(rl_id, GL_COMPILE); // create new list

drawing commands; // draw something

glEndList(); // end of list

Finally, “replay” the list. Change the list only when the scene is changed:

glCallList(rl_id); // draw list

H&B 4-15 111-113

Page 140: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Vertex Arrays

OpenGL provides a facility called vertex arrays that allows us to store array data in the implementation

Six types of arrays supported Vertices Colors Color indices Normals Texture coordinates Edge flags

We will need only colors and vertices

Page 141: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Initialization

Using the same color and vertex data, first we enableglEnableClientState(GL_COLOR_ARRAY);

glEnableClientState(GL_VERTEX_ARRAY);

Identify location of arraysglVertexPointer(3, GL_FLOAT, 0, vertices);

glColorPointer(3, GL_FLOAT, 0, colors);

3d arrays stored as floats data contiguous

data array

Page 142: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Sample code for drawing object

glDrawArrays(GL_TRIANGLES, 0, 3);

uniform vec3 triangleColor;

out vec4 outColor;

void main()

{

outColor = vec4(triangleColor, 1.0);

}

142

Page 143: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Definition: Bitmap

Bitmapped images:

Photographs, computer paintings, videos

Specific number of dots (or pixels) across and down (160 x 120, 800 x 600, 1400 x 1050)

Dots spread out or squeeze together as image size changes

Number of dots remains the same

Photoshop, Paint, QuickTime

Page 144: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

“Maps” of Dots

Page 145: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

“Maps” of Dots

Page 146: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

“Maps” of Dots

Page 147: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Bitmap Examples

Page 148: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Making Bitmaps Bigger

100%

(900 x 983 dpi)

500%200%

Page 149: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Raster Displays

Images are composed of arrays of pixels displayed on a raster device.

Two main ways to create images:

Scan and digitize an existing image.

Compute a value for each pixel procedurally.

The result may be stored as a pixmap, a rectangular array of color values.

Page 150: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Manipulating Pixmaps

Pixmaps may be stored in regular memory or in the frame buffer (off-screen or on-screen).

Rendering operations that draw into the frame buffer change the particular pixmap that is visible on the display.

Page 151: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixmap Operations: Copying

Page 152: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Pixmap Operations: Copying

glReadPixels () reads a portion of the frame buffer into memory.

glCopyPixels() copies a region in one part of the frame buffer into another region of the frame buffer.

glDrawPixels() draws a given pixmap into the frame buffer.

We can also copy a pixmap from memory to memory.

Page 153: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Scaling Pixmaps

glPixelZoom(float sx, float sy);

Sets scale factors in x and y

Any floating point values are allowed for sx and sy, even negative ones. The default values are 1.0.

The scaling takes place about the current raster position, pt.

Scale factors are applied to the image drawn from a pixmap, not to the pixmap.

Page 154: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Scaling Pixmaps (2)

Roughly, the pixel in row r and column c of the pixmap will be drawn as a rectangle of width sx and height sy screen pixels, with lower left corner at screen pixel (pt.x + sx * r, pt.y + sy * c).

More precisely, any screen pixels whose centers lie in this rectangle are drawn in the color of this pixmap pixel.

Page 155: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

CG Hardware System Overview

Components of a CG system

Raster displays

Display devices: crt, etc.

CG system architectures GPU

Frame buffers Addressability and resolution

“Scanning out” the image

Color look up tables

Input devices

Page 156: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Double Buffer

Use two system buffers instead of one

A process can transfer data to or from one buffer while the operating system empties or fills the other buffer

Page 157: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Bitmap Character raster

Each character represented (stored) as a 2-D array– Each element corresponds to a pixel in a rectangular “character cell”– Simplest: each element is a bit (1=pixel on, 0=pixel off)

0011100001101100110001101100011011111110110001101100011000000000

Page 158: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Stroke Character outline

Each character represented (stored) asa series of line segments– sometimes as more complex primitives Parameters needed to draw each stroke– endpoint coordinates for line segments

Page 159: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

Characters Characteristics

Characteristics of Bitmapped Characters• Each character in set requires same amount of

memory to store• Characters can only be scaled by integer scaling

factors • "Blocky" appearance• Difficult to rotate characters by arbitrary angles• Fast (BitBLT)Characteristics of Stroked Characters• Number of stokes (storage space) depends on complexity

of character• Each stroke must be scan converted more time to display• Easily scaled and rotated arbitrarily• – just transform each stroke

Page 160: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

160 1)Coordinate frame References•https://en.wikipedia.org/wiki/Coordinate_system•https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CCkQFjABahUKEwib5oWMzeLHAhXGvhQKHZx2C10&url=http%3A%2F%2Fads.harvard.edu%2Fbooks%2F1989fcm..book%2FChapter2.pdf&usg=AFQjCNHMzOT8o8AKNExtxCCdp7bYyjo68w•https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=7&cad=rja&uact=8&ved=0CD4QFjAGahUKEwib5oWMzeLHAhXGvhQKHZx2C10&url=http%3A%2F%2Fpeople.cs.clemson.edu%2F~dhouse%2Fcourses%2F405%2Fnotes%2Fcoord-systems.pdf&usg=AFQjCNEgrZ6nD0wHuZ5wEzEYk43GttdBSQ

References

2) Line drawing algorithm •https://en.wikipedia.org/wiki/Line_drawing_algorithm•http://www.slideshare.net/saikrishnatanguturu/computer-graphics-ver10•http://rosettacode.org/wiki/Bitmap/Bresenham's_line_algorithm•http://rosettacode.org/wiki/Bitmap/Bresenham's_line_algorithm

3)Circle and Ellipse drawing algorithm •http://groups.csail.mit.edu/graphics/classes/6.837/F98/Lecture6/circle.html•http://www.mathopenref.com/coordcirclealgorithm.html

Page 161: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

161Pixel Addressing references•https://edurev.in/studytube/Pixel-addressing--computer-graphics/57039bfd-8aca-454f-8f1e-a6cd5eeaced5_p•https://edurev.in/studytube/Pixel-addressing--computer-graphics/57039bfd-8aca-454f-8f1e-a6cd5eeaced5_p•https://edurev.in/studytube/Pixel-addressing--computer-graphics/57039bfd-8aca-454f-8f1e-a6cd5eeaced5_p

Area fill methods•https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&sqi=2&ved=0CB4QFjAAahUKEwjx2bv80uLHAhWkoNsKHYaPBBQ&url=http%3A%2F%2Fcomputer-graphics.se%2FTSBK07-files%2Fpdf%2FPDF09%2F9.pdf&usg=AFQjCNG5DfqOjDrFCyPjKkch3JTfOjeOug&bvm=bv.102022582,d.d24•https://en.wikipedia.org/wiki/Flood_fill•http://www.slideshare.net/avelraj/lecture-filling-algorithms•http://www.cs.uic.edu/~jbell/CourseNotes/ComputerGraphics/PolygonFilling.html

Plane Equations•http://escience.anu.edu.au/lecture/cg/surfaceModeling/examplePlane.en.html•http://www.idav.ucdavis.edu/education/GraphicsNotes/Planes/Planes.html

References

Page 162: 1 COMPUTER GRAPHICS Graphics Output Primitives By : Dhaval Shah

162References

OpenGL Area Fill•https://en.wikipedia.org/wiki/OpenGL•http://whatis.techtarget.com/definition/OpenGL-Open-Graphics-Library•http://graphics.wikia.com/wiki/OpenGL

Vertex array •http://math.hws.edu/graphicsbook/c3/s4.html•https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&cad=rja&uact=8&ved=0CDYQFjAEahUKEwjego2i1eLHAhXIPtsKHSdWAbU&url=http%3A%2F%2Fweb.engr.oregonstate.edu%2F~mjb%2Fcs553%2FHandouts%2FPerformance%2Fvertexarraysandbuffers.1pp.pdf&usg=AFQjCNEm2VuVMuh0HjbzrK8JDbV5_PjDRQ&bvm=bv.102022582,d.d24Pixel array•https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB4QFjAAahUKEwi2pM7_1eLHAhVigdsKHQeMAB8&url=http%3A%2F%2Fjozefg.ecs.fullerton.edu%2Fpublic%2FUL%2F463%2FLecture_Notes%2Fch03_Graphics_Output_Primitives.ppt&usg=AFQjCNF8x0GqzYmnssO92mw9QmogYgMtkw&bvm=bv.102022582,d.bGQ•https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&cad=rja&uact=8&ved=0CDcQFjAEahUKEwi2pM7_1eLHAhVigdsKHQeMAB8&url=http%3A%2F%2Fmerkez.ube.ege.edu.tr%2F~ozturk%2Fgraphics%2FLectures%2Flecture05-output-primitives.ppt&usg=AFQjCNEYmBE5yGWldIoUczNuGluBW8gs1Q&bvm=bv.102022582,d.bGQ