0% found this document useful (0 votes)
7 views29 pages

CH 3

Uploaded by

Elroi Teshome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views29 pages

CH 3

Uploaded by

Elroi Teshome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 3:- Graphics primitives

computer graphice 12/19/2024 1


Outline
 Introduction

 OpenGL Point Drawing Primitives

 OpenGL Line drawing primitives

 OpenGL Fill-Area Routines

computer graphice 12/19/2024 2


Introduction
 All graphics packages construct pictures from basic building

blocks known as graphics primitives.

 They can be anything from 2-D primitives such as points, lines and

polygons to more complex 3-D primitives such as spheres and

polyhedron (a 3-D surface made from a mesh of 2-D polygons).

 In the following sections, we will examine some algorithms for

drawing different primitives, and where appropriate we will

introduce the routines for displaying these primitives in OpenGL

computer graphice 12/19/2024 3


Basic routines to drown any primitives'
 glClear: This function is used to clear the screen before drawing. The
background color set earlier will be used for clearing.
 Actually the glBegin … glEnd pair of commands is used to draw many
different types of primitive in OpenGL, with the symbolic constant
argument to glBegin defining how the vertices in between should be
interpreted.
 glFlush: When we draw primitives in OpenGL it sometimes queues the
routines in a buffer.
 This function tells OpenGL to ‘flush’ this buffer, i.e. to draw any
primitives now to the frame buffer .

computer graphice 12/19/2024 4


OpenGL Point Drawing Primitives
 The most basic type of primitive is the point.

 Many graphics packages, including OpenGL, provide routines for


displaying points.
 We use the pair of functions glBegin … glEnd, using the symbolic
constant GL_POINTS to specify how the vertices in between should
be interpreted.
 In addition, the function glPointSize can be used to set the size of
the points in pixels.
 The default point size is one pixel.

computer graphice 12/19/2024 5


Cont.…
 For example, the following code draws three 2-D points with a size
of 2 pixels.
 glPointSize(2.0);

 glBegin(GL_POINTS);

 glVertex2f(100.0, 200.0);

 glVertex2f(150.0, 200.0);

 glVertex2f(150.0, 250.0);

 glEnd();

 Note that when we specify 2-D points, OpenGL will actually create
3-D points with the third coordinate (the z-coordinate) equal to
zero.

computer graphice 12/19/2024 6


OpenGL Line drawing
 Lines are a very common primitive and will be supported by almost
all graphics packages.
 lines form the base of more complex primitives such as polylines (a
connected sequence of straight-line segments) or polygons (2-D objects
formed by straight-line edges).
 Lines are normally represented by the two end-points of the line.
 points (x,y) along the line must satisfy the following slope-intercept
equation:
 y = mx + c where
 m- slope or gradient of the line
 c is the coordinate at which the line intercepts the y-axis.

computer graphice 12/19/2024 7


Cont…
 Given two end-points (x0,y0) and (xend,yend), we can calculate values for m
and c as follows:

 Furthermore, for any given x-interval δx, we can calculate the


corresponding y-interval δy :-

 These equations form the basis of the two line-drawing algorithms


described below: the DDA algorithm and Bresenham’s algorithm.

computer graphice 12/19/2024 8


Cont..
DDA Line-Drawing Algorithm

 The Digital Differential Analyser (DDA) algorithm operates by

starting at one initial-point of the line, and then generate successive

pixels until the end-point is reached by calculating x-interval and y

intervals.

 Therefore, first, we need to assign values for δx and δy.

computer graphice 12/19/2024 9


Cont..
 To ensure this, we must first check the size of the line gradient. The
conditions are:
 If |m| ≤ 1:
 δx = 1
 δy = m
 If |m| > 1:
 δx = 1/m
 δy = 1

computer graphice 12/19/2024 10


Cont..
 Once we have computed values for δx and δy, the basic DDA algorithm is:

 Start with (x0,y0)

 Find successive pixel positions by adding on (δx, δy) and rounding to the
nearest integer, i.e.

 xk+1 = xk + δx

 yk+1 = yk + δy

 For each position (xk,yk) computed, plot a line point at (round(xk),round(yk)),

where the round function will round to the nearest integer.


 Note that the actual pixel value used will be calculated by rounding to the
nearest integer, but we keep the real-valued location for calculating the
next pixel position.
 E.g:- (x0,y0)=(10,10) to (Xend,Yend)=(15,13)

computer graphice 12/19/2024 11


DDA Line-Drawing Algorithm

computer graphice 12/19/2024 12


Cont..
DDA Line-Drawing Algorithm
 The DDA algorithm is simple and easy to implement it involve floating
point operations to calculate each new point.
 Floating point operations are time-consuming when compared to
integer operations.
 Since line-drawing is a very common operation in computer graphics,
it would be nice if we could devise a faster algorithm which uses
integer operations only that is called Bresenham’s Line-Drawing
Algorithm.

computer graphice 12/19/2024 13


Bresenham’s Line-Drawing Algorithm
 Bresenham’s line-drawing algorithm provides significant

improvements in efficiency over the DDA algorithm.

 These improvements arise from the observation that for any given line,

if we know the previous pixel location, we only have a choice of 2

locations for the next pixel.

 This concept is illustrated in figure 2: given that we know (xk,yk) is a

point on the line, we know the next line point must be either pixel A or

pixel B.

computer graphice 12/19/2024 14


Cont…

 Therefore, we do not need to compute the actual floating-point location of


the ‘true’ line point; we need only make a decision between pixels A and B.

computer graphice 12/19/2024 15


Cont…
 Bresenham’s algorithm works as follows.

 First, we denote by dupper and dlower to the distances between the centres

of pixels A and B and the ‘true’ line y (above figure ).

 Using slope-intercept equations, the ‘true’ y-coordinate at xk+1 can be

calculated as:

 Now, we can decide which of pixels A and B to choose based on


comparing the values of dupper and dlower:
 If dlower > dupper, choose pixel A
 Otherwise choose pixel B
computer graphice 12/19/2024 16
Cont…
 We make this decision by first subtracting dupper from dlower:

 If the value of this expression is positive we choose pixel A; otherwise


we choose pixel B.
 The question now is how we can compute this value efficiently.

 To do this, we define a decision variable pk for the kth step in the

algorithm and try to formulate pk so that it can be computed using only


integer operations.
 To achieve this, we substitute

 and define pk as

computer graphice 12/19/2024 17


Cont…
 Where d is a constant that has the value .
 Note that the sign of pk will be the same as the sign of (dlower – dupper), so if
pk is positive we choose pixel A and if it is negative we choose pixel B.
 An efficient incremental calculation makes Bresenham’s algorithm even
faster. (An incremental calculation means we calculate the next value of
pk from the last one.)
 Given that we know a value for pk, we can calculate pk+1, by observing
that:
 Always xk+1 = xk+1
 If pk < 0, then yk+1 = yk, otherwise yk+1 = yk+1

 Therefore, we can define the incremental calculation as:

computer graphice 12/19/2024 18


Cont ..
 The initial value for the decision variable, p0, is calculated by

 Finally

computer graphice 12/19/2024 19


Cont….
• Example:-
 Draw a line with starting point (1,1) to (8,5) by using bersenham’s
algorithm?
 Draw a line with starting point (9,18) to (14,22) by using bersenham’s
algorithm?

computer graphice 12/19/2024 20


OpenGL Line Functions

 We can draw straight-lines in OpenGL using the same glBegin … glEnd


functions that we saw for point-drawing.
 This time we specify that vertices should be interpreted as line end-
points by using the symbolic constant GL_LINES.
 For example, the following code
 glLineWidth(3.0);
 glBegin(GL_LINES);
 glVertex2f(100.0, 200.0);
 glVertex2f(150.0, 200.0);
 glVertex2f(150.0, 250.0);
 glVertex2f(200.0, 250.0);
 glEnd()

computer graphice 12/19/2024 21


Cont…
 It will draw two separate line segments: one from (100,200) to
(150,200) and one from (150,250) to (200,250).
 The line will be drawn in the current drawing colour and with a width
defined by the argument of the function glLineWidth.
 Two other symbolic constants allow us to draw slightly different types
of straight-line primitive like GL_LINE_STRIP and GL_LINE_LOOP.

computer graphice 12/19/2024 22


Cont…
 We can see that GL_LINES treats the vertices as pairs of end-points

 Lines are drawn separately and any extra vertices (i.e. a start-point
with no end-point) are ignored.
 GL_LINE_STRIP will create a connected polyline, in which each vertex
is joined to the one before it and after it.
 The first and last vertices are only joined to one other vertex

 Finally, GL_LINE_LOOP is the same as GL_LINE_STRIP except that the


last point is joined to the first one to create a loop.

computer graphice 12/19/2024 23


Filed area primitives
 The most common type of primitive in 3-D computer graphics is the
fill-area primitive.
 The term fill-area primitive refers to any enclosed boundary that can
be filled with a solid color or pattern.
 However, fill-area primitives are normally polygons, as they can be
filled more efficiently by graphics packages.
 Polygons are 2-D shapes whose boundary is formed by any number of
connected straight-line segments.
 They can be defined by three or more vertices.

 Each pair of adjacent vertices is connected in sequence by edges.


Normally polygons should have no edge crossings: in this case they are
known as simple polygons or standard polygons
computer graphice 12/19/2024 24
Cont…
 Polygons are the most common form of graphics primitive because
they form the basis of polygonal meshes, which is the most common
representation for 3-D graphics objects.
 Polygonal meshes approximate curved surfaces by forming a mesh of
simple polygons.

computer graphice 12/19/2024 25


OpenGL Fill-Area Routines
 OpenGL provides a variety of routines to draw fill-area polygons.

 In all, there are may different ways to draw polygons in OpenGL:


 glRect*
 Five different symbolic constants for use with glBegin … glEnd
 We will now consider each of these techniques in turn.
glRect
 Used to draw 2D rectangle

 The basic format of the routine is

 glRecti (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 following order: (x1,y1), (x2,y1),
(x2,y2), (x1,y2) eg:- glRecti(200,100,50,250);
computer graphice 12/19/2024 26
Cont…
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.

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.

GL_TRIANGLE_STRIP
 To form polygonal meshes it is often convenient to define a number of
triangles using a single glBegin … glEnd pair.

computer graphice 12/19/2024 27


Cont…
 The GL_TRIANGLE_STRIP primitive enables us to define a strip of
connected triangles.

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.

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.
computer graphice 12/19/2024 28
Next class

 Attributes of Graphics Primitives

computer graphice 12/19/2024 29

You might also like