expanded by jozef goetz chapter 3 graphics output primitives some slides adapted from benjamin lok,...

85
Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Post on 19-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Chapter 3

Graphics Output Primitives

Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Page 2: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Glossary

• CG API – Computer Graphics Application Programming Interface

• tbs – to be specified

• FoV – Field of View

• +’s – pluses, advanatges

• -’s – minuses, disadvantages

• Orthogonal to = Normal to = forms a 90o angle with

• Norm of a vector = length of the vector

• Are coplanar = there is a plane containing them

Page 3: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Glossary

• Aliasing is the jagged edges on curves and diagonal lines in a bitmap image.

• Anti-aliasing is the process of smoothing out those jaggies. – Graphics software programs have options for anti-

aliasing text and graphics. – Enlarging a bitmap image accentuates the effect of

aliasing.

Page 4: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 5: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Outline• 3-1Coordinate Reference Frames ,• 3-2Specifying a Two-Dimensional -World-Coordinate

Reference Frame in OpenGL• 3-3OpenGL Point Functions• 3-4OpenGL Line Functions• 3-5Line-Drawing Algorithms• 3-6Parallel Line Algorithms• 3-7Setting Frame-Buffer Values• 3-8OpenGL Curve Functions• 3-9Circle-Generating Algorithms• 3-10Ellipse-Generating Algorithms• 3-11Other Curves• 3-12Parallel Curve Algorithms• 3-13Pixel Addressing and ObjectGeometry

Page 6: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Outline• 3-14Fill-Area Primitives• 3-15Polygon Fill Areas• 3-16OpenGL Polygon Fill-AreaFunctions• 3-17OpenGL Vertex Arrays• 3-18Pixel-Array Primitives• 3-19OpenGL Pixel-Array Functions• 3-20Character Primitives• 3-21OpenGL Character Functions• 3-22Picture Partitioning• 3-23OpenGL Display Lists• 3-24OpenGL Display-Window ReshapeFunction• 3-25Summary

Page 7: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Basic Elements

• Geometry is the study of the relationships among objects in an n-dimensional space– In computer graphics, we are interested in objects

that exist in 3 dimensions

• Want a minimum set of primitives from which we can build more sophisticated objects

• Geometry provides a mathematical foundation for much of computer graphics.

Page 8: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Coordinate-Free Geometry

• When we learned simple geometry, most of us started with a Cartesian approach– Points were at locations in space p=(x,y,z)– We derived results by algebraic manipulations

involving these coordinates

• Physically, points exist regardless of the location of an arbitrary coordinate system

• Most geometric results are independent of the coordinate system

• Euclidean geometry: two triangles are identical if two corresponding sides and the angle between them are equal

Page 9: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Basic Elements

We will need:

• Three basic elements– Scalars– Vectors– Points

• Develop mathematical operations among them

• Define basic primitives:– Line segments– Polygons

Page 10: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Basic Elements

• Points are associated with locations of

• Vectors represent displacements between points or directions

• Points, vectors, and operators that combine them are the common tools for solving many geometric problems that arise in – Geometric Modeling, – Computer Graphics, – Animation, – Visualization, and – Computational Geometry.

Page 11: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3.1 COORDINATE REFERENCE FRAMES

To describe a picture, we first decide upon • A convenient Cartesian coordinate system, called the

world-coordinate reference frame, which could be either 2D or 3D.

• We then describe the objects in our picture by giving their geometric specifications in terms of positions in world coordinates.

• e.g., we define a straight-line segment with two endpoint positions, and a polygon is specified with a set of positions for its vertices.

– These coordinate positions are stored in the scene description along with other info about the objects, such as their color and their coordinate extents,

• which are the minimum and maximum x, y, and z values for each object. • A set of coordinate extents is also described as a bounding box for an

object. – For a 2D figure, the co ordinate extents are sometimes called an object's

bounding rectangle.

Page 12: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3.1 COORDINATE REFERENCE FRAMES

• Objects are then displayed by passing the scene description to the viewing routines,

• which identify visible surfaces

• and map the objects to the frame buffer positions and then on the video monitor.

• The scan-conversion algorithm stores info about the scene, such as color values, at the appropriate locations in the frame buffer, and the objects in the scene are displayed on the output device.

• locations on a video monitor • are referenced in integer screen coordinates, which• correspond to the integer pixel positions in the frame

buffer.

Page 13: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3.1 COORDINATE REFERENCE FRAMES

• E.g. scan-line algorithms for the graphics primitives use the coordinate descriptions to determine the locations of pixels– e.g. given the endpoint coordinates for a line segment, a

display algorithm must calculate the positions for those pixels that lie along the line path between the endpoints.

• Since a pixel position occupies a finite area of the screen, – the finite size of a pixel must be taken into account by the

implementation algorithms.

– for the present, we assume that each integer screen position references the center of a pixel area.

Page 14: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3.1 COORDINATE REFERENCE FRAMES

• Once pixel positions have been identified the color values must be stored in the frame buffer

• Assume we have available a low-level procedure of the form

setPixel (x, y); stores the current color setting into the frame buffer at integer position

(x, y), relative to the selected position of the screen-coordinate origin

getPixel (x, y, color); retrieve the current frame-buffer setting for a pixel location;• parameter color receives an integer value corresponding to the combined

RGB bit codes stored for the specified pixel at position (x, y).

• additional screen-coordinate information is needed for 3D scenes. For a two-dimensional scene, all depth values are 0.

Page 15: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3.1 Absolute and Relative Coordinate Specifications

• we have discussed are stated as absolute coordinate values

• some graphics packages also allow positions to be specified using relative coordinates– as an offset from the last position that was

referenced (called the current position)

Page 16: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-2 SPECIFYING A 2D WORLD-COORDINATE REFERENCE FRAME IN OpenGL

• World-coordinate limits for a display window, as specified in the glOrtho2D function.

• gluOrtho2D function specifies an orthogonal projection, we need also to be sure that the coordinate values are placed in the OpenGL projection matrix. – In addition, we could assign the identity matrix as the projection

matrix before defining the world-coordinate range. • This would ensure that the coordinate values were not accumulated with

any values we may have previously set for the projection matrix.

glMatrixMode (GL_PROJECTION); glLoadIdentity ( );glu0rtho2D (xmin, xmax, ymin, ymax);

– The display window will then be referenced by coordinates (xmin, ymin) at the lower-left corner and by coordinates (xmax, ymax) at the upper-right corner, as shown in Fig. 3-2.

Page 17: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 18: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-3 QpenGL POINT FUNCTIONS

• To specify the geometry of a point, we simply give a coordinate position in the world reference frame.

• Then along with other geometric descriptions is passed to the viewing routines. – Unless we specify other attribute values, OpenGL

primitives are displayed with a default size and color.

• The default color for primitives is white and the default point size is equal to the size of one screen pixel.

Page 19: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-3 QpenGL POINT FUNCTIONS• A glVertex function must be placed between a

glBegin function and a glEnd function. • The argument of the glBegin function is used

to identify the kind of output primitive that is to be displayed,

• For point plotting, the argument of the g1Begin function is the symbolic constant GL_POINTS.

• and glEnd takes no arguments.

glBegin (GL_POINTS);glVertex* ( );

glEnd ( ) ;

Page 20: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-3 QpenGL POINT FUNCTIONSglVertex* ( );

• state the coordinate values for a single position

• where the asterisk (*) indicates that suffix codes are required for this function.

• These suffix codes are used to identify the spatial dimension, 2, 3, or 4 (indicates a scaling factor for the Cartesian-coordinate values.)

– any (x, y) coordinate specification is equivalent to (x, y, 0) with h = 1

• the numerical data type– i (integer), s (short), f (float), and d (double)

• and a possible vector form for the coordinate specification– append a third suffix code: v (for "vector").

• the glVertex function is used in OpenGL to specify coordinates for any point position.

– In this way, a single function is used for point, line, and polygon specifications

Page 21: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-3 QpenGL POINT FUNCTIONS.glBegin (GL_POINTS);

glVertex2i (50, 100); glVertex2i (75, 150); glVertex2i (100, 200);

g1End ( ) ;

• Alternatively, we could specify the coordinate values for the preceding points in arrays such as

int pointl [ ] = {50, 100); int point2 [ ] = {75, 150); int point3 [ ] = {100, 200};

and call the OpenGL functions for plotting the three points as

glBegin (GL_POINTS);glVertex2iv (pointl); glVertex2iv (point2); glVertex2iv (point3);

glEnd ( ) ;

Page 22: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-3 QpenGL POINT FUNCTIONS

• And here is an example of specifying two point positions in a 3D world reference frame. In this case, we give the coordinates as explicit floating-point values.

glBegin (GL-POINTS);glVertex3f (-78.05, 909.72, 14.60);

glVertex3f (261.91, -5200.67, 188.33);

glEnd ( ) ;

Page 23: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-3 QpenGL POINT FUNCTIONS.class wcPt2D { public:

GLfloat x, y;};• Using this class definition, we could specify a 2D,

world-coordinate point position with the statements

wcPt2D pointPos;

pointPos.x = 120.75; pointPos.y = 45.30; glBegin (GL_POINTS);

glVertex2f (pointPos.x, pointPos.y); glEnd ( )

Page 24: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-4 OpenGL LINE FUNCTIONS • now we use a symbolic constant as the argument for the

g1Begin function that interprets a list of positions as the endpoint coordinates for line segments. – A set of straight-line segments between each successive pair of

endpoints in a list is generated using the primitive line constant GL_LINES

glBegin (GL_LINES):glVertex2iv(pl);glVertex2iv(p2);glVertex2iv(p3);glVertex2iv(p4);glVertex2iv(p5);

glEnd ( );

• Nothing is displayed if we do not list at least two coordinate positions

pl

p3

p2 p4

Page 25: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-4 OpenGL LINE FUNCTIONS. glBegin (GL_LINE_STRIP);

glVertex2iv (pl);glVertex2iv (p2);glVertex2iv (p3);glVertex2iv (p4);glVertex2iv (p5):

glEnd ( )• The first line segment in the polyline is

displayed between the first endpoint and the second endpoint;

• the second line segment is between the second and third endpoints; and so forth, up to the last line endpoint.

Page 26: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-4 OpenGL LINE FUNCTIONS

giBegin (GL_LINE_LOOP);glVertex2iv (pl);glVertex2iv (p2);glVertex2iv (p3);glVertex2iv (p4);glVertex2iv (p5);

glEnd ( ) ;

• produces a closed polyline.

• the last coordinate endpoint in the sequence is connected to the first coordinate endpoint of the polyline.

Page 27: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-5 LINE-DRAWING ALGORITHMA straight-line segment in a scene is defined by the

coordinate positions for the endpoints of the segment.

1. To display the line on a raster monitor, 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.

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

3. 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.

Page 28: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-5 LINE-DRAWING ALGORITHM• On raster systems, lines are plotted with pixels, and

step sizes in the horizontal and vertical directions are constrained by pixel separations.

• That is, we must "sample" a line at discrete positions and determine the nearest pixel to the line at sampled position. – Sampling is measuring the values of the function at

equal intervals

• Idea: A line is sampled at unit intervals in one coordinate and the corresponding integer values nearest the line path are determined for the other coordinate.

Page 29: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

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

Page 30: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

<Main Concept>

• In the raster line alg., – we sample at unit intervals and – determine the closest pixel position to the specified

line path at each step

Page 31: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

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

– What about anti-aliasing ?• Aliasing is the jagged edges on curves and diagonal lines in a

bitmap image.

• Anti-aliasing is the process of smoothing out those jaggies. – Graphics software programs have options for anti-aliasing text and

graphics.

– Enlarging a bitmap image accentuates the effect of aliasing.

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

Page 32: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Simple LineThe Cartesian slope-intercept equation for a straight line is :

y = mx + b

with m as the slope of the line and b as the y intercept.

Simple approach:

• increment x, solve for y

-‘s: Floating point arithmetic required

Page 33: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Does it Work?•It seems to work okay for lines with a slope m <= 1

•but doesn’t work well for lines with slope m >= 1

•lines become more discontinuous in appearance if we sample at unit x intervals

• and we must add more than 1 pixel per column to make it work.

Solution? - 1. use symmetry.

Page 34: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Modify algorithm per octant

OR 2. use: increment along x-axis if dy<dx (e.g. m<1)

else

increment along y-axis

We will use approach 2

Page 35: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Digital Differential Analyzer DDA alg. #include <stdlib.h> #include <math.h>

inline int round (const float a) { return int (a + 0.5); }

void lineDDA (int x0, int y0, int xEnd, int yEnd) { int dx = xEnd - x0, dy = yEnd - y0, steps, k; float xIncrement, yIncrement, x = x0, y = y0; // find max {dx, dy} if (fabs (dx) > fabs (dy)) // for m <=1 // for m >1 steps = fabs (dx); // # of x units // = steps // ------- = skipped else steps = fabs (dy); // # of y units // ------- // = steps xIncrement = float (dx) / float (steps); // dx = 1 // dx = 1/m or -1/m yIncrement = float (dy) / float (steps); // dy = m or –m // dy = 1

setPixel (round (x), round (y)); //stores the current color setting into the frame buffer // at integer position (x, y), for (k = 0; k < steps; k++) { x += xIncrement; // x += 1; // x = x + 1/m or -1/m y += yIncrement; // y = y + m or -m // y += 1; setPixel (round (x), round (y)); } }

Page 36: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

-’s of DDA algorithm• Still need a lot of floating point arithmetic

which is still time consuming.– 2 ‘round’s and 2 adds per pixel.

• Is there a simpler way ?

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

Page 37: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Bresenham’s line Algorithm

• we introduce an accurate and efficient raster line-generating algorithm, – that uses only incremental integer calculations.– in addition, Bresenham's line algorithm can be

adapted to display circles and other curves.

Page 38: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Observation on lines.while( n-- ) {draw(x,y);move right;if( below line )move up;}

Another approach will be applied:•decide which of two possible pixel positions is closer to the of a display screen at each sample step.

• e.g. starting from the left endpoint

Page 39: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

•To illustrate Bresenham's approach, we first consider the scan-conversion process for lines with positive m < 1.0. •Pixel positions along a line path are then determined by sampling at unit x intervals.

•Starting from the left endpoint (xo, yo) of a given line, we step to each successive column (x position) and plot the pixel whose scan-line y value is closest to the line path.

Figure 3-10 demonstrates the kth step in this process.

Assuming we have determined that the pixel at (x[k], y[k]) is to be displayed, •we next need to decide which pixel to plot in column x[k+1]? •Our choices are the pixels at positions (x[k+1], y[k]) and(x[k+1], y[k+1]).

Page 40: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Idea: test the sign of an integer parameter whose value is proportional to the difference between the vertical separations of the two pixel positions from the actual line path.

Page 41: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Bresenham’s Line Algorithm#include <stdlib.h>#include <math.h>

/* Bresenham line-drawing procedure for |m| < 1.0. */ void lineBres (int x0, int y0, int xEnd, int yEnd) { int dx = fabs (xEnd - x0), dy = fabs(yEnd - y0); int p = 2 * dy - dx; // calculate initial value of a decision parameter int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx); int x, y;

/* Determine which endpoint to use as start position. */ if (x0 > xEnd) { x = xEnd; y = yEnd; xEnd = x0; } else { x = x0; y = y0; } setPixel (x, y); // draw the first point

while (x < xEnd) { x++; if (p < 0) p += twoDy; // no y change else { y++; p += twoDyMinusDx; } setPixel (x, y); } }

•An implementation for slopes in the range 0 < m < 1.0 is given here.

•Endpoint pixel positions for the line are passed to this procedure, and pixels are plotted from the left endpoint to the right endpoint.

Page 42: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Bresenham’s Line AlgorithmTo illustrate the algorithm, we digitize the line with endpoints (20, 10) and (30, 18)This line has a slope of 0.8, with

dx =10, dy =8The initial decision parameter has the value:

p = 20dy – dx = 6and the increments for calculating successive decision parameters are

20dy = 16, 2dy – 2dx = -4

• We plot the initial point = (20, 10), and determine successive pixel positions along the line path from the decision parameter as:

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

•Pixel positions along the line path between endpoints (20, 10) and (30, 18), plotted with Bresenham's line algorithm.

Page 43: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Bresenham’s Line Algorithm • Bresenham's algorithm is generalized to lines with

arbitrary slope by considering the symmetry between the various octants and quadrants of the xy plane.

• For a line with positive m > 1.0, we interchange the roles of the x and y directions. – i.e. we step along the y direction in unit steps and

calculate successive x values nearest the line path. • Also, we could revise the program to plot pixels

starting from either endpoint. – If the initial position for a line with positive slope is the

right endpoint, both x and y decrease as we step from right to left

• For negative slopes, the procedures are similar, except that now one coordinate decreases as the other increases.

Page 44: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Displaying Polylines

• Implementation of a polyline procedure is accomplished by invoking a line drawing routine n - 1 times to display the lines connecting the n endpoints.

• Each successive call passes the coordinate pair needed to plot the next line section, where the first endpoint of each coordinate pair is the last endpoint of the previous section.

Page 45: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-6 PARALLEL LINE ALGORITHMS

• The line-generating algorithms so far determine pixel positions sequentially.

• Using parallel processing, we can calculate multiple pixel positions along a line path simultaneously by partitioning the computations among the various processors available. – An important consideration in devising a parallel

algorithm is to balance the processing load among the available processors.

Page 46: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-6 PARALLEL LINE ALGORITHMS

• I. Given processors, we can set up a parallel Bresenham line algorithm by subdividing the line path into partitions and simultaneously generating line segments in each of the subintervals.

• For a line with slope 0 < m < 1.0 and left endpoint coordinate position (x0, y0) , we partition the line along the positive x direction.

Page 47: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-6 PARALLEL LINE ALGORITHMS• The distance between beginning x positions of

adjacent partitions i.e. partition width

where

= width of the line

= # of processors• Numbering the partitions, and the processors, as 0,

1, 2, up to - 1, we calculate the starting x coordinate for the k-th partition as

Page 48: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-6 PARALLEL LINE ALGORITHMS

• The change in the y direction over each partition is calculated from the line slope m and partition width :

• At the k-th partition, the starting y coordinate is then

• the initial value for the decision parameter was

• so for each partition we get

Page 49: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-6 PARALLEL LINE ALGORITHMS• Each processor then calculates pixel positions over

its assigned subinterval using the preceding starting decision parameter value and the starting coordinates .

• Floating-point calculations can be reduced to integer arithmetic in the computations for starting values and by substituting and rearranging terms.

• We can extend the parallel Bresenham algorithm for m > 1.0 by partitioning the line in the y direction and calculating beginning x values for the partitions.

• For negative slopes, we increment coordinate values in one direction and decrement in the other.

Page 50: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-6 Another PARALLEL LINE ALGORITHMS

• II. Assign each processor to a particular group of screen pixels.

• each assigned processor to one pixel within some screen region (within the limits of the coordinate extents of the line) calculates pixel distances from the line path.

• III. Assign to each processor either a scan line or a column of pixels depending on the line slope.

• Each processor then calculates the intersection of the line with the horizontal row or vertical column of pixels assigned to that processor.

• For a line with slope abs (m) < 1.0, each processor simply solves the line equation for y, given an x column value.

• For a line with slope magnitude m > 1.0, the line equation is solved for x by each processor, given a scan line y value.

• Such direct methods, although slow on sequential machines, can be performed efficiently using multiple processors.

Page 51: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-8 OpenGL CURVE FUNCTIONS• Routines for generating basic curves, such as circles

and ellipses, are not included as primitive functions in the OpenGL core library.

• OpenGL Utility (GLU) has routines for three dimensional quadrics, such as spheres and cylinders, as well as routines for producing rational B-splines, – which are a general class of splines that include the simpler

Bezier curves– using rational B-splines, we can display circles, ellipses,

and other two-dimensional quadrics. • OpenGL Utility Toolkit (GLUT) has routines to

display some three-dimensional quadrics, such as spheres and cones, and some other shapes (chapter 8.)

Page 52: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-8 OpenGL CURVE FUNCTIONS

• Another method to generate a simple curve is to approximate it using a polyline.

• We just need to locate a set of points along the curve path and connect the points with straight-line segments.

• The more line sections we include in the polyline, the smoother the appearance of the curve (see Fig. 3-15, p103)

Page 53: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

3-8 OpenGL CURVE FUNCTIONS

• A third alternative is to write our own curve-generation functions based on the algorithms presented in the following sections. – We first discuss efficient methods for circle and

ellipse generation, and other conic sections, polynomials, and splines.

Page 54: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Testing for the side of a line.• Need a test to determine which side of a line a pixel

lies.• Write the line in implicit form:

0),( cbyaxyxF

• Easy to prove F<0 for points above the line, F>0 for points below.

Page 55: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Testing for the side of a line.

• Need to find coefficients a,b,c.• Recall explicit, slope-intercept form :

• So:

0),( cbyaxyxF

bxdx

dyybmxy so and

0..),( cydxxdyyxF

Page 56: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Decision variable.

PreviousPixel(xp,yp)

Choices forCurrent pixel

Choices forNext pixel

Evaluate F at point M

Referred to as decision variable

)2

1,1( pp yxFd

M

NE

E

Page 57: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Decision variable.

Evaluate d for next pixel, Depends on whether E or NE Is chosen :

If E chosen :

cybxayxFd ppppnew )2

1()2()

2

1,2(

But recall :

cybxa

yxFd

pp

ppold

)2

1()1(

)2

1,1(

So :

dyd

add

old

oldnew

M

E

NE

PreviousPixel

(xp,yp)

Choices forCurrent pixel

Choices forNext pixel

Page 58: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Decision variable.

If NE was chosen :

cybxayxFd ppppnew )2

3()2()

2

3,2(

So :

dxdyd

badd

old

oldnew

M

E

NE

PreviousPixel

(xp,yp)

Choices forCurrent pixel

Choices forNext pixel

Page 59: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Summary of mid-point algorithm• Choose between 2 pixels at each step based

upon sign of decision variable.

• Update decision variable based upon which pixel is chosen.

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

• Need to calculate initial value for d

Page 60: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Initial value of d.

2

)2

1()1()

2

1,1(

11

1111

bacbyax

cybxayxFdstart

But (x1,y1) is a point on the line, so F(x1,y1) =0

2/dxdydstart

Conventional to multiply by 2 to remove fraction doesn’t effect sign.

2),( 11

bayxF

Start point is (x1,y1)

Page 61: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Bresenham algorithmvoid MidpointLine(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);

}}

Page 62: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Bresenham was not the end!

2-step algorithm by Xiaolin Wu:

(see Graphics Gems 1, by Brian Wyvill)

Treat line drawing as an automaton , or finite state machine, ie. looking at next two pixels of a line, easy to see that only a finite set of possibilities exist.

The 2-step algorithm exploits symmetry by simultaneously drawing from both ends towards the midpoint.

Page 63: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Two-step Algorithm

Possible positions of next two pixels dependent on slope – current pixel in blue:

Slope between 0 and ½

Slope between ½ and 1

Slope between 1 and 2

Slope greater than 2

Page 64: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Circle drawing.• Can also use Bresenham to draw circles.

• Use 8-fold symmetry

Choices forNext pixel

M

E

SE

PreviousPixel

Choices forCurrent pixel

Page 65: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Circle drawing.• Implicit form for a circle is:

2 2 2) ( ) ( ) , (r y y x x y x fc c

)32(chosen is E If

)522(chosen is SE If

poldnew

ppoldnew

xdd

yxdd

• Functions are linear equations in terms of (xp,yp)

–Termed point of evaluation

Page 66: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Problems with Bresenham algorithm

• Pixels are drawn as a single line unequal line intensity with change in angle.

Pixel density = n pixels/mm

Pixel density = 2.n pixels/mm

Can draw lines in darker colours according to line direction.- Better solution : antialiasing !

Page 67: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Summary of line drawing so far.• Explicit form of line

– Inefficient, difficult to control.

• Parametric form of line.– Express line in terms of parameter t– DDA algorithm

• Implicit form of line– Only need to test for ‘side’ of line.– Bresenham algorithm.– Can also draw circles.

Page 68: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 69: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

PieChart p.164

#include <GL/glut.h>#include <stdlib.h>#include <math.h>

const GLdouble twoPi = 6.283185;

class scrPt {public: GLint x, y;};

GLsizei winWidth = 400, winHeight = 300; // Initial display window size.

void init (void){

glClearColor (1.0, 1.0, 1.0, 1.0);

glMatrixMode (GL_PROJECTION);gluOrtho2D (0.0, 200.0, 0.0, 150.0);

}

. // Midpoint routines for displaying a circle. Not working yet

. .

Page 70: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

PieChart p.164

void pieChart (void){ scrPt circCtr, piePt; GLint radius = winWidth / 4; // Circle radius.

GLdouble sliceAngle, previousSliceAngle = 0.0;

GLint k, nSlices = 12; // Number of Slices. GLfloat dataValues[12] = {10.0, 7.0, 13.0, 5.0, 13.0, 14.0, 3.0, 16, 5.0, 3.0, 17.0, 8.0}; GLfloat dataSum = 0.0; circCtr.x = winWidth / 2; // Circle center position. circCtr.y = winHeight / 2; circleMidpoint (circCtr, radius); // Call midpoint circle-plot routine.

for (k = 0; k < nSlices; k++) dataSum += dataValues[k];

for (k = 0; k < nSlices; k++) { sliceAngle = twoPi * dataValues[k] / dataSum + previousSliceAngle; piePt.x = circCtr.x + radius * cos (sliceAngle); piePt.y = circCtr.y + radius * sin (sliceAngle); glBegin (GL_LINES); glVertex2i (circCtr.x, circCtr.y); glVertex2i (piePt.x, piePt.y); glEnd ( ); previousSliceAngle = sliceAngle; }}

Page 71: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

void displayFcn (void){ glClear (GL_COLOR_BUFFER_BIT); // Clear display window.

glColor3f (0.0, 0.0, 1.0); // Set circle color to blue.

pieChart ( ); glFlush ( );}

void winReshapeFcn (int newWidth, int newHeight){ glMatrixMode (GL_PROJECTION); glLoadIdentity ( ); gluOrtho2D (0.0, GLdouble (newWidth), 0.0, GLdouble (newHeight));

glClear (GL_COLOR_BUFFER_BIT);

/* Reset display-window size parameters. */ winWidth = newWidth; winHeight = newHeight; }

void main (int argc, char** argv){ glutInit (&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (100, 100); glutInitWindowSize (winWidth, winHeight); glutCreateWindow ("Pie Chart");

init ( ); glutDisplayFunc (displayFcn); glutReshapeFunc (winReshapeFcn);

glutMainLoop ( );}

Page 72: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

DisplstReshape// 3DisplstReshape.cpp : Defines the entry point for the console application.//

#include "stdafx.h"

#include <GL/glut.h> #include <math.h> #include <stdlib.h>

const double TWO_PI = 6.2831853;

/* Initial display-window size. */ GLsizei winWidth = 400, winHeight = 400; GLuint regHex;

class screenPt { public: GLint x, y; };

static void init (void) { screenPt hexVertex, circCtr; GLdouble theta; GLint k;

/* Set circle center coordinates. */ circCtr.x = winWidth / 2; circCtr.y = winHeight / 2;

glClearColor (1.0, 1.0, 1.0, 0.0); // Display-window color = white.

Page 73: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

/* Set up a display list for a red regular hexagon. * Vertices for the hexagon are six equally spaced * points around the circumference of a circle. */ regHex = glGenLists (1); // Get an identifier for the display list. glNewList (regHex, GL_COMPILE); glColor3f (1.0, 0.0, 0.0); // Set fill color for hexagon to red. glBegin (GL_POLYGON); for (k = 0; k < 6; k++) { theta = TWO_PI * k / 6.0; hexVertex.x = circCtr.x + 150 * cos (theta); hexVertex.y = circCtr.y + 150 * sin (theta); glVertex2i (hexVertex.x, hexVertex.y); } glEnd ( ); glEndList ( ); }

void regHexagon (void) { glClear (GL_COLOR_BUFFER_BIT);

glCallList (regHex);

glFlush ( ); }

Page 74: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

void winReshapeFcn (int newWidth, int newHeight)

{ glMatrixMode (GL_PROJECTION); glLoadIdentity ( ); gluOrtho2D (0.0, (GLdouble) newWidth, 0.0, (GLdouble) newHeight);

glClear (GL_COLOR_BUFFER_BIT); }

void main (int argc, char** argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (100, 100); glutInitWindowSize (winWidth, winHeight); glutCreateWindow ("Reshape-Function & Display-List Example");

init ( ); glutDisplayFunc (regHexagon); glutReshapeFunc (winReshapeFcn);

glutMainLoop ( ); }

Page 75: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 76: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 77: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 78: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 79: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 80: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 81: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 82: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Page 83: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

Digital Differential Analyzer -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 -

Page 84: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

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

dx1

dt

dy

Page 85: Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester

Expanded by Jozef Goetz

DDA algorithm

line(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.