Professional Documents
Culture Documents
Points
We have already seen the OpenGL routines for point drawing in the
simple OpenGL program introduced in Chapter 3.
To recap, we use the pair of functions glBegin … glEnd, with
symbolic constant GL_POINTS to specify how the vertices in
between should be interpreted.
The function glPointSize can be used to set the size of the points in
pixels.
The default point size is 1 pixel. Points that have a size greater than
one pixel are drawn as squares with a side length equal to their size.
Geometry and line generation : Lines
Lines
Lines are normally represented by the two end-points of the line, and
points (x, y) along the line must satisfy the following slope-intercept
equation:
y=mx+c………………………………………………………(1)
Where m is the slope
c is y-intercept.
Given two end-points (x0,y0) and (xend, yend), we can calculate values for
m and c as follows.
…………………………………………………………………(2)
cy0mxo………………………………………………………………... (3)
Δy = m.Δx………………………………………………………………..(4)
Δx= (1/m).Δy………………………….…………………………………(5)
Lines: DDA Line-Drawing Algorithm
Iteration 3:
o P3< 0, so Plot (x4,y4) = (x3+1,y3) = (14,12)
o P4 = p3 + 2Δy = -1+6 = 5
Iteration 4:
o P2> 0, so Plot (x5,y5) = (x4+1,y4+1) = (15,13)
o We have reached the end-point, so the algorithm terminates
We can see that the algorithm plots exactly the same points
as the DDA algorithm but it computes those using only
integer operations. For this reason, Bresenham’s algorithm
is the most popular choice for line-drawing in computer
graphics
Circle-Drawing Algorithms
D) Midpoint Circle-Drawing
Algorithm
The midpoint algorithm takes
advantage of the symmetry
property of circles to produce a
more efficient algorithm for
drawing circles
The algorithm works in a similar
way to Bresenham’s line-
drawing algorithm, in that it
formulates a decision variable
that can be computed using
Figure 7 - The Midpoint Circle-Drawing
integer operations only. Algorithm
Circle-Drawing Algorithms . . .
Recall that we only need to draw one octant of the circle, as the
other seven octants can be generated by symmetry.
In Figure 7 we are drawing the right-hand upper octant the one
with coordinate (y,x) in Figure 6, but the midpoint algorithm
would work with slight modifications whichever octant we chose.
Notice that in this octant, when we move from one pixel to try to
draw the next pixel there is only a choice of two pixels: A and B,
or (xk+1,yk) and (xk+1,yk-1).
Therefore we don’t need to calculate a real-valued coordinate and
then round to the nearest pixel, we just need to make a decision as
to which of the two pixels to choose.
Circle-Drawing Algorithms . . .
Summary
we can express the midpoint circle-drawing algorithm for a circle
centered at the origin as follows:
Plot the start-point of the circle (x0,y0) = (0,r)
Compute the first decision variable:
o p0 = 1-r
For each k, starting with k=0:
o If pk < 0: Plot (xk+1,yk)
o pk+1 = pk+2xk+1+1
Otherwise:
o Plot (xk+1,yk-1)
o pk+1 = pk + 2xk+1+1-2yk+1
Circle-Drawing Algorithms . . .
Example: - given a circle of radius r=10, centered at the origin, the steps
are:
First, compute the initial decision variable:
o p0 = 1-r => 1-10 = -9
o Plot (x0,y0) = (0,r) = (0,10)
Iteration 0:
o p0<0,so plot (x1,y1) = (xo+1, y0) (0+1,10) =(1,10)
o p0+1 p1 = p0+2x0+1+1 -9+3 = -6
Iteration 1:
o P1<0 so plot (x2,y2) (x1+1,y1) (1+1,10) = (2,10)
o P1+1 =p1+2x1+1+1 p2 = p1+2x2+1 p2 = -6+2*2+1 = -1
Iteration 2:
o P2<0 so plot (x3,y3) (x2+1,y2) (2+1,10) = (3,10)
o P2+1 =p2+2x3+1+1 p3 = p2+2x3+1 p2 = -1+2*3+1 = 6
Circle-Drawing Algorithms . . .
Iteration 3:
o P3>0 so plot (x4,y4) (x3+1,y3-1) (3+1,10-1) = (4,9)
o P3+1 = p3+2x4+1 – 2y4 p4= p3+2x4+1-2y4 p4 = 6+2*4+1-
2*9 = 15-18 = -3
Iteration 4:
o P4< 0 so plot (x5,y5) (x4+1,y4) (4+1,9) = (5,9)
o P5 = p4 +2x5+1= -3+11 = 8
Iteration 5:
o P5>0 so plot (x6,y6) (x5+1,y5-1) (5+1, 9-1) = (6,8)
o P6 = p5+2x6+1-2y6 = 8+2*6+1-2*8= 8+12+1-16 = 5
Etc.
Polygons
Figure 10 - Polygon
Polygons . . .
Polygons are the most common form of
graphics primitive because they form the basis
of polygonal meshes,
polygonal meshes is the most common
representation for 3-D graphics objects.
Polygons . . .
Convex and Concave Polygons
We can differentiate between convex and concave polygons:
o Convex polygons have all interior angles ≤ 180o
o Concave polygons have at least one interior angle > 180o
glRect*
Six different symbolic constants for use with
glBegin … glEnd
glRect* (x1, y1, x2, y2) where (x1,y1) and
(x2,y2) define opposite corners of the rectangle.
Actually when we call the glRect* routine,
OpenGL will construct a polygon with vertices
defined in the order:(x1,y1), (x2,y1), (x2,y2),
(x1,y2)
OpenGL Fill-Area Routines . . .
GL_POLYGON
The GL_POLYGON symbolic constant defines a
single convex polygon. Like all of the following
techniques for drawing fill-area primitives it should
be used as the argument to the glBegin routine.
For example, the code shown below will draw the
shape shown in Figure 16 Notice that the vertices of
the polygon are specified in anti-clockwise order.
OpenGL Fill-Area Routines . . .
GL_TRIANGLES
The GL_TRIANGLES symbolic constant causes the glBegin
… glEnd pair to treat the vertex list as groups of three 3
vertices, each of which defines a triangle.
The vertices of each triangle must be specified in anti-
clockwise order.
GL_TRIANGLE_STRIP
GL_TRIANGLE_STRIP primitive
enables us to define a strip of
connected triangles.
The vertices of the first triangle only
must be specified in anti-clockwise
order. Figure 17 illustrates the use of
GL_TRIANGLE_ST RIP.
Figure:18 draw triangle using
TRIANGLE_STRIP
OpenGL Fill-Area Routines . . .
GL_TRIANGLE_FAN
It is similar to GL_TRIANGLE_STRIP, in that it
allows us to define a number of triangles using a
single glBegin … glEnd command.
The first point of the first triangle is the source of the
fan, and the vertices of this triangle must be specified
in an anti-clockwise direction. In addition, the fan of
triangles must also be defined in an anti-clockwise
direction. See Figure 23 for an example of the use of
GL_TRIANGLE_FAN.
OpenGL Fill-Area Routines . . .
GL_QUADS
Using the GL_QUADS primitive, the vertex list
is treated as groups of four vertices, each of
which forms a quadrilateral.
If the number of vertices specified is not a
multiple of four, then the extra vertices are
ignored. The vertices for each quadrilateral must
be defined in an anti-clockwise direction. See
Figure 20 for an example of GL_QUADS.
OpenGL Fill-Area Routines . . .
GL_QUAD_STRIP
GL_QUAD_STRIP allows us to define a strip of
quadrilaterals.
The first four vertices form the first quadrilateral, and
each subsequent pair of vertices is combined with the
two before them to form another quadrilateral.
The vertices of the first quadrilateral must be
specified in an anti-clockwise direction. Figure 21
illustrates the useof GL_QUAD_STRIP.
OpenGL Fill-Area Routines . . .