Graphics Output Primitives - PowerPoint PPT Presentation

1 / 85
About This Presentation
Title:

Graphics Output Primitives

Description:

... which could be either 2D or 3D. ... For a 2D figure, ... The scan-conversion algorithm stores info about the scene, ... – PowerPoint PPT presentation

Number of Views:149
Avg rating:3.0/5.0
Slides: 86
Provided by: lase9
Category:

less

Transcript and Presenter's Notes

Title: Graphics Output Primitives


1
  • Chapter 3
  • Graphics Output Primitives
  • Some slides adapted from
    Benjamin Lok, Ilmi Yoon and Falko
    Kuester

2
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

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

4
(No Transcript)
5
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

6
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

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

8
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

9
Basic Elements
  • We will need
  • Three basic elements
  • Scalars
  • Vectors
  • Points
  • Develop mathematical operations among them
  • Define basic primitives
  • Line segments
  • Polygons

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

11
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 coordinate extents are
    sometimes called an object's bounding rectangle.

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

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

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

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

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

17
(No Transcript)
18
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.

19
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 ( )

20
3-3 QpenGL POINT FUNCTIONS
  • glVertex ( )
  • 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

21
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 ( )

22
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 ( )

23
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 ( )

24
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

p3
pl
p2
p4
25
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.

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

27
3-5 LINE-DRAWING ALGORITHM
  • A straight-line segment in a scene is defined by
    the coordinate positions for the endpoints of the
    segment.
  • 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.
  • Then the line color is loaded into the frame
    buffer at the corresponding pixel coordinates.
  • 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.

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

29
Towards the Ideal Line
  • We can only do a discrete approximation
  • Illuminate pixels as close to the true path as
    possible, consider bi-level display only
  • Pixels are either lit or not lit

30
ltMain Conceptgt
  • In the raster line alg.,
  • we sample at unit intervals and
  • determine the closest pixel position to the
    specified line path at each step

31
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!!!

32
Simple Line
  • The 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

33
Does it Work?
  • It seems to work okay for lines with a slope m lt
    1
  • but doesnt work well for lines with slope m gt 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.

34
Modify algorithm per octant
OR 2. use increment along x-axis if dyltdx
(e.g. mlt1) else
increment along y-axis We will use
approach 2
35
Digital Differential Analyzer DDA alg.
  • include ltstdlib.hgt
  • include ltmath.hgt
  • 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) gt fabs (dy))
    // for m lt1 //
    for m gt1
  • 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),

36
-s of DDA algorithm
  • Still need a lot of floating point arithmetic
    which is still time consuming.
  • 2 rounds and 2 adds per pixel.
  • Is there a simpler way ?
  • Can we use only integer arithmetic ?
  • Easier to implement in hardware.

37
Bresenhams 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.

38
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

39
  • To illustrate Bresenham's approach, we first
    consider the scan-conversion process for lines
    with positive m lt 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
    (xk, yk) is to be displayed,
  • we next need to decide which pixel to plot in
    column xk1?
  • Our choices are the pixels at positions (xk1,
    yk) and
  • (xk1, yk1).

40
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.
41
Bresenhams Line Algorithm
  • include ltstdlib.hgt
  • include ltmath.hgt
  • / Bresenham line-drawing procedure for m lt
    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 gt xEnd)
  • x xEnd
  • y yEnd
  • xEnd x0
  • else
  • x x0
  • An implementation for slopes in the range 0 lt m lt
    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.

42
Bresenhams Line Algorithm
  • To 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 8
  • The initial decision parameter has the value
  • p 20dy dx 6
  • and 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)
  • Pixel positions along the line path between
    endpoints
  • (20, 10) and (30, 18), plotted with Bresenham's
    line algorithm.

43
Bresenhams 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 gt 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.

44
Displaying Polylines
  • Implementation of a polyline procedure is
    accomplished by invoking a linedrawing 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.

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

46
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 lt m lt 1.0 and left
    endpoint coordinate position (x0, y0) , we
    partition the line along the positive x
    direction.

47
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

48
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

49
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 gt 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.

50
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) lt 1.0, each
    processor simply solves the line equation for y,
    given an x column value.
  • For a line with slope magnitude m gt 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.

51
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
    threedimensional 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.)

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

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

54
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
  • Easy to prove Flt0 for points above the line,
    Fgt0 for points below.

55
Testing for the side of a line.
  • Need to find coefficients a,b,c.
  • Recall explicit, slope-intercept form
  • So

56
Decision variable.
Evaluate F at point M Referred to as decision
variable
NE
M
E
Previous Pixel (xp,yp)
Choices for Current pixel
Choices for Next pixel
57
Decision variable.
Evaluate d for next pixel, Depends on whether E
or NE Is chosen If E chosen
NE
M
E
Previous Pixel (xp,yp)
Choices for Next pixel
Choices for Current pixel
58
Decision variable.
If NE was chosen
M
NE
E
Previous Pixel (xp,yp)
Choices for Next pixel
Choices for Current pixel
59
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

60
Initial value of d.
Start point is (x1,y1)
But (x1,y1) is a point on the line, so F(x1,y1) 0
Conventional to multiply by 2 to remove fraction
? doesnt effect sign.
61
Bresenham algorithm
  • void MidpointLine(int x1,y1,x2,y2)
  • int dxx2-x1
  • int dyy2-y1
  • int d2dy-dx
  • int increE2dy
  • int incrNE2(dy-dx)
  • xx1
  • yy1
  • WritePixel(x,y)

while (x lt x2) if (dlt 0) dincrE x
else dincrNE x y WritePixel
(x,y)
62
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.
63
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
64
Circle drawing.
  • Can also use Bresenham to draw circles.
  • Use 8-fold symmetry

E
M
SE
Previous Pixel
Choices for Next pixel
Choices for Current pixel
65
Circle drawing.
  • Implicit form for a circle is
  • Functions are linear equations in terms of
    (xp,yp)
  • Termed point of evaluation

66
Problems with Bresenham algorithm
  • Pixels are drawn as a single line ? unequal line
    intensity with change in angle.

Pixel density ?2.n pixels/mm
Can draw lines in darker colours according to
line direction. - Better solution antialiasing !
Pixel density n pixels/mm
67
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.

68
(No Transcript)
69
PieChart p.164
  • include ltGL/glut.hgt
  • include ltstdlib.hgt
  • include ltmath.hgt
  • 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)

70
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 dataValues12 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 lt nSlices k)
  • dataSum dataValuesk

71
  • 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. /

72
DisplstReshape
  • // 3DisplstReshape.cpp Defines the entry point
    for the console application.
  • //
  • include "stdafx.h"
  • include ltGL/glut.hgt
  • include ltmath.hgt
  • include ltstdlib.hgt
  • const double TWO_PI 6.2831853
  • / Initial display-window size. /
  • GLsizei winWidth 400, winHeight 400
  • GLuint regHex
  • class screenPt
  • public
  • GLint x, y

73
  • / 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 lt 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)

74
  • 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 ( )

75
(No Transcript)
76
(No Transcript)
77
(No Transcript)
78
(No Transcript)
79
(No Transcript)
80
(No Transcript)
81
(No Transcript)
82
(No Transcript)
83
Digital Differential Analyzer -DDA algorithm
  • DDA Digital Differential Analyser
  • finite differences
  • Treat line as parametric equation in t

Start point - End point -
84
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

85
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.
Write a Comment
User Comments (0)
About PowerShow.com