0% found this document useful (0 votes)
136 views55 pages

Ch-4 Geometry and Line Generation

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

Ch-4 Geometry and Line Generation

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

Chapter Four

Geometry and Line Generation

1
Contents
 Point And Lines, DDA & Bresenham’s Algorithm
 Line Thickness
 Line Style
 Generating Circles
 Polygons
 Filling
 Text And Characters

2
Point Drawing in OpenGL
 Point - is a basic type of primitive.
 Use the pair of functions glBegin … glEnd, using the symbolic constant
GL_POINTS.
Point attributes:
 Point size: glPointSize(size) - to control the size of a rendered point.
 Points are drawn as squares with a side length equal to the point size,

default point size is 1 pixel and supply the desired size in pixels as the
argument. void glPointSize(GLfloat size);
 Point colour: glColor*
 glVertex* - * specifies number of arguments, and type of arguments.
 glVertex3f: 3 Glfloat arguments
3

 If we specify 2-D point, OpenGL will actually create 3-D point with z=0
Point Drawing in OpenGL
Points are the most basic type of primitive.
• glPointSize(2.0); //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 .
• glBegin(GL_POINTS); // draw 3 points
glVertex2f(100.0, 200.0);
glVertex2f(150.0, 200.0);
glVertex2f(150.0, 250.0);
glEnd();
• GL_POINTS : specify how the vertices in between should be interpreted.
• There is only 3D in OpenGL.
4
• 2D + constant Z coordinate (equal to zero)
Point Drawing in OpenGL
The following code fragment draws three 2-D points with a size of 1, 2, and 3
pixels respectively.
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(50, 100);
glPointSize(2.0);
glColor3f(0.0, 1.0, 0.0);
glVertex2i(75, 150);
glPointSize(3.0);
glColor3f(0.0, 0.0, 1.0);
glVertex2i(100,200);
glEnd();

5
Lines Drawing in OpenGL
• Lines are a very common primitive and will be supported by almost all graphics packages.
• Lines are normally represented by the two end-points.

 y end  y0  c  y 0  mx 0
m
x end  x0  δy = m.δx
6
δx = (1/m).δy
Lines Drawing in OpenGL
• Description:
 Given the specification for a straight line, find the collection of addressable pixels
which most closely approximates this line.
• Goals:
 Straight lines should appear straight.

 Lines should start and end accurately, matching endpoints with connecting line.

 Lines should have constant brightness.

 Lines should be drawn as rapidly as possible

For any given x-interval δx, we can calculate the corresponding y-interval δy
7
Lines Drawing in OpenGL

• Problems:
 How do we determine which pixels to illuminate to satisfy the above goals.
 Vertical, horizontal, and lines with slope = + / - 1, are easy to draw.
 Other create problems: stair-case/jaggies/aliasing.

8
Lines Drawing in OpenGL

9
Lines Drawing in OpenGL
Choice of pixels in the raster, as an integer values: next highest or round about
x = 1, y = 2 = round(8/5) /(1.6)
x = 2, y = 3 = round(11/5) /(2.2)
x = 3, y = 3 = round(14/5) /(2.8)
x = 4, y = 4 = round(17/5) / (3.4)
x = 5, y = 4 = round(20/5) /(4.0)

From the above example:


Next highest or round about algorithms is needed to select the appropriate pixel to draw the
line using raster display. 10

11
Digital Differential Analyzer Algorithm
•The Digital Differential Analyser (DDA) algorithm operates by starting at one end-point of the line,
and to generate successive pixels until the second end-point is reached.

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


• Before we consider the actual DDA algorithm, let us
consider a simple first approach to this problem.
• Suppose we simply increment the value of x at each
iteration (i.e. δx = 1), and then compute the
corresponding value for y.
• This would compute correct line points but, as
illustrated by Figure, it would leave gaps in the line.
The reason for this is that the value of δy is greater than
one,
so the gap between subsequent points in the line is greater The gap between subsequent points in the
than 1 pixel. line is greater than 1 pixel.
12
Digital Differential Analyzer Algorithm

• Given (x0,y0) and (xend,yend)


• Compute m
• If |m| ≤ 1:
• δx = 1
•The solution to this problem is • δy = m
to make sure that both δx and • If |m| > 1:
δy have values less than or equal
• δx = 1/m
to one. To ensure this, we must
• δy = 1
first check the size of the line
• Paint (x0,y0)
gradient.
• Find successive pixel positions
• xk+1 = (xk + δx)
• yk+1 = (yk + δy)
• For each position (xk,yk) plot a line point at
• (round(xk),round(yk))
13
• The round function will round to the nearest integer.
Digital Differential Analyzer Algorithm

14
Digital Differential Analyzer Algorithm

Step 4 – find the next point by following three cases. current point (Xp, Yp), next
points (Xp+1, Yp+1)

Case 1: - if m < 1, Case 2: - if m = 1, Case 3: - if m > 1,


Xp+1 = Xp + 1 Xp+1 = Xp + 1/m
Xp+1 = Xp + 1
Yp+1 = Yp + 1 Yp+1 = Yp + 1
Yp+1 = Yp + m

Step 5 – repeat Step 4 until end point is reached.


Example –
Draw a line from A(5,6) and B(8,12), using DDA Algorithm

15
Digital Differential Analyzer Algorithm
Example: plotting the line using DDA algorithm: Given (x0, y0) = (10,10)
and (xend, yend) = (15,13)

16
Digital Differential Analyzer Algorithm Example1
• Given (x0,y0) = (10,10)
Point Drawing in • and (xend,yend) = (15,13) m
 yend  y0   (13 10)  3  0.6
OpenGL • Compute m xend  x0  (15 10) 5
• If |m| ≤ 1:
Line Drawing • δx = 1
Algorithms • δy = m δx = 1
• If |m| > 1:
δy = 0.6
DDA Algorithm • δx = 1/m
• δy = 1

Bresenham’s • Paint (x0,y0) (xend,yend) = (15,13)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 17
Digital Differential Analyzer Algorithm Example1
• Given (x0,y0) = (10,10)
Point Drawing in •  yend  y0  (13  10) 3
and (xend,yend) = (15,13) m    0.6
OpenGL • Compute m xend  x0  (15  10) 5
• If |m| ≤ 1:
Line Drawing • δx = 1
Algorithms • δy = m δx = 1
• If |m| > 1:
δy = 0.6
DDA Algorithm • δx = 1/m
• δy = 1

Bresenham’s • Paint (x0,y0) (xend,yend) = (15,13)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 18
Digital Differential Analyzer Algorithm Example1
• Paint (x0,y0)
δx = 1
Point Drawing in • Find successive pixel positions δy = 0.6
OpenGL • xk+1 = (xk + δx)
• yk+1 = (yk + δy) (x1,y1) = (10+1,10+0.6)
Line Drawing
Algorithms • For each position (xk,yk) plot a = (11,10.6)
line point at
• (round(xk),round(yk)) colour pixel (11,11)
DDA Algorithm

Bresenham’s (xend,yend) = (15,13)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 19
Digital Differential Analyzer Algorithm Example1
• Paint (x0,y0)
δx = 1
Point Drawing in • Find successive pixel positions δy = 0.6
OpenGL • xk+1 = (xk + δx)
• yk+1 = (yk + δy) (x2,y2) = (11+1,10.6+0.6)
Line Drawing
Algorithms • For each position (xk,yk) plot a = (12,11.2)
line point at
• (round(xk),round(yk)) colour pixel (12,11)
DDA Algorithm

Bresenham’s (xend,yend) = (15,13)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 20
Digital Differential Analyzer Algorithm Example1
• Paint (x0,y0)
δx = 1
Point Drawing in • Find successive pixel positions δy = 0.6
OpenGL • xk+1 = (xk + δx)
• yk+1 = (yk + δy) (x3,y3) = (12+1,11.2+0.6)
Line Drawing
Algorithms • For each position (xk,yk) plot a = (13,11.8)
line point at
• (round(xk),round(yk)) colour pixel (13,12)
DDA Algorithm

Bresenham’s (xend,yend) = (15,13)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 21
Digital Differential Analyzer Algorithm Example1
• Paint (x0,y0)
δx = 1
Point Drawing in • Find successive pixel positions δy = 0.6
OpenGL • xk+1 = (xk + δx)
• yk+1 = (yk + δy) (x4,y4) = (13+1,11.8+0.6)
Line Drawing
Algorithms • For each position (xk,yk) plot a = (14,12.4)
line point at
• (round(xk),round(yk)) colour pixel (14,12)
DDA Algorithm

Bresenham’s (xend,yend) = (15,13)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 22
Digital Differential Analyzer Algorithm Example1
• Paint (x0,y0)

• Find successive pixel positions δx = 1



Point Drawing in δy = 0.6
xk+1 = (xk + δx)

• yk+1 = (yk + δy)

OpenGL • For each position (xk,yk) plot a line point at

• (round(xk),round(yk))

Line Drawing (x5,y5) = (14+1,12.4+0.6)


Algorithms = (15,13)

colour pixel (15,13)


DDA Algorithm Figure - The Operation of the DDA Line-Draiwng Algorithm

Bresenham’s (xend,yend) = (15,13)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 23
Digital Differential Analyzer Algorithm Example2
• Given (x0,y0) = (10,10)
Point Drawing in • and (xend,yend) = (14,15) m
 yend  y0   (15 10)  5  1.25
OpenGL • Compute m xend  x0  (14 10) 4
• If |m| ≤ 1:
Line Drawing • δx = 1 δx = 0.8
Algorithms • δy = m δy = 1
• If |m| > 1:
DDA Algorithm (xend,yend) = (14,15)
• δx = 1/m
• δy = 1

Bresenham’s • Paint (x0,y0)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 24
Digital Differential Analyzer Algorithm Example2
• Given (x0,y0) = (10,10)
Point Drawing in • and (xend,yend) = (14,15) m
 yend  y0   (15 10)  5
OpenGL • Compute m xend  x0  (14 10) 4
• If |m| ≤ 1:
Line Drawing • δx = 1 δx = 0.8
Algorithms • δy = m δy = 1
• If |m| > 1:
DDA Algorithm (xend,yend) = (14,15)
• δx = 1/m
• δy = 1

Bresenham’s • Paint (x0,y0)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 25
Digital Differential Analyzer Algorithm Example2
• Paint (x0,y0)
δx = 0.8
Point Drawing in • Find successive pixel positions δy = 1
OpenGL • xk+1 = (xk + δx)
• yk+1 = (yk + δy) (x1,y1) = (10+0.8,10+1)
Line Drawing
Algorithms • For each position (xk,yk) plot a = (10.8,11)
line point at
• (round(xk),round(yk)) colour pixel (11,11)
DDA Algorithm
(xend,yend) = (14,15)

Bresenham’s
Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 26
Digital Differential Analyzer Algorithm Example2
• Paint (x0,y0)
δx = 0.8
Point Drawing in • Find successive pixel positions δy = 1
OpenGL • xk+1 = (xk + δx)
• yk+1 = (yk + δy) (x2,y2) = (10.8+0.8,11+1)
Line Drawing
Algorithms • For each position (xk,yk) plot a = (11.6,12)
line point at
• (round(xk),round(yk)) colour pixel (12,12)
DDA Algorithm
(xend,yend) = (14,15)

Bresenham’s
Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 27
Digital Differential Analyzer Algorithm Example2
• Paint (x0,y0)
δx = 0.8
Point Drawing in • Find successive pixel positions δy = 1
OpenGL • xk+1 = (xk + δx)
• yk+1 = (yk + δy) (x3,y3) = (11.6+0.8,12+1)
Line Drawing
Algorithms • For each position (xk,yk) plot a = (12.4,13)
line point at
• (round(xk),round(yk)) colour pixel (12,13)
DDA Algorithm
(xend,yend) = (14,15)

Bresenham’s
Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 28
Digital Differential Analyzer Algorithm Example2
• Paint (x0,y0)
δx = 0.8
Point Drawing in • Find successive pixel positions δy = 1
OpenGL • xk+1 = (xk + δx)
• yk+1 = (yk + δy) (x4,y4) = (12.4+0.8,13+1)
Line Drawing
Algorithms • For each position (xk,yk) plot a = (13.2,14)
line point at
• (round(xk),round(yk)) colour pixel (13,14)
DDA Algorithm
(xend,yend) = (14,15)

Bresenham’s
Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 29
Digital Differential Analyzer Algorithm Example2


Paint (x0,y0)

Find successive pixel positions


δx = 0.8
Point Drawing in • xk+1 = (xk + δx)
δy = 1
• yk+1 = (yk + δy)
OpenGL • For each position (xk,yk) plot a line point at

• (round(xk),round(yk))

Line Drawing (x5,y5) = (13.2+0.8,14+1)


Algorithms = (14,15)

colour pixel (14,15)


DDA Algorithm
(xend,yend) = (14,15)

Bresenham’s
Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121
30
(x0,y0) = (10,10)
Digital Differential Analyzer Algorithm Example2
• Paint (x0,y0)

• Find successive pixel positions δx = 0.8


Point Drawing in •

xk+1 = (xk + δx)

yk+1 = (yk + δy)


δy = 1
OpenGL • For each position (xk,yk) plot a line point at

• (round(xk),round(yk))

Line Drawing (x5,y5) = (13.2+0.8,14+1)


Algorithms = (14,15)

colour pixel (14,15)


DDA Algorithm
(xend,yend) = (14,15)

Bresenham’s
Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 31
Digital Differential Analyzer Algorithm
Exercise1
Point Drawing in • Consider the above examples and plotting
OpenGL the line using DDA algorithm:

Line Drawing • Given (x0,y0) = (5,5) and (xend,yend) = (10,9)


Algorithms

Exercise2
DDA Algorithm
 Consider the above examples and plotting
the line using DDA algorithm:
Bresenham’s  Given (x0,y0) = (2,2) and (xend,yend) = (6,12)
Algorithm

Circle Drawing Exercise3


Algorithms  Write a fragment of OpenGL code to
draw three points in GLUT window?
Fill-AreaComputer
Primitives  Given P1(50.0,100.0), P2(100.0,200.0) and
Graphics- CoSc3121
32
P3(200.0,100.0)
The Bresenham Line
Algorithm /Midpoint Line Algorithm

The Bresenham
algorithm is another
incremental scan
conversion algorithm
The big advantage of
this algorithm is that it
uses only integer
calculations

Computer Graphics- CoSc3121 33


Bresenham’s Line-Drawing Algorithm
•Given (x ,y ) and (x ,y )
• •Compute0 0
|m| < 1 m
end end

Point Drawing in • Plot (x0,y0)


OpenGL • Compute the first decision
variable:
Line Drawing
Algorithms p0  2y  x
• For each k, starting with k=0:
DDA Algorithm
• If pk < 0:

Bresenham’s
• Plot (xk+1,yk)
Algorithm
pk1  pk  2y
Circle Drawing
Algorithms • Otherwise:
• Plot (xk+1,yk+1)
pk 1  pk  2y  2x
Fill-AreaComputer
Primitives
Graphics- CoSc3121
34
Bresenham’s Line-Drawing Algorithm
•Given (x0,y0) and (xend,yend)
• |m| < 1m
•Compute
• Plot (x0,y0)  |m| ≥ 1
Point Drawing in
 Plot (x0,y0)
OpenGL • Compute the first decision
variable:  Compute the first decision
Line Drawing variable:
Algorithms p0  2y  x p0  2x  y
• For each k, starting with
 For each k, starting with
k=0:
DDA Algorithm k=0:
• If pk < 0:  If pk < 0:

Bresenham’s • Plot (xk+1,yk)  Plot (xk,yk+1)


Algorithm
pk1  pk  2y pk1  pk  2x
Circle Drawing
• Otherwise:  Otherwise:
Algorithms
 Plot (xk+1,yk+1)
• Plot (xk+1,yk+1)
pk 1  pk  2y  2x pk 1  pk  2x  2y
Fill-AreaComputer
Primitives
Graphics- CoSc3121
35
Bresenham’s Line-Drawing Algorithm
•Example Given (x0,y0) = (10,10) and (xend,yend) = (15,13)
Compute m
m = 0.6 < 1
Point Drawing in • |m| < 1
OpenGL • Plot (x0,y0)

Line Drawing • Compute the first decision


Algorithms variable:
p  2y  x
0

DDA Algorithm

Bresenham’s (xend,yend) = (15,13)


Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 36
Bresenham’s Line-Drawing Algorithm
•Given (x0,y0) = (10,10) and (xend,yend) = (15,13)
•Compute m
m = 0.6 < 1
Point Drawing in • |m| < 1
Δx = 5
OpenGL • Plot (x0,y0)
Δy = 3
Line Drawing • Compute the first decision 2Δy = 6
Algorithms variable:
p  2y  x 2Δy - 2Δx = -4
0

DDA Algorithm p0  2y  x  2  3  5  1


Bresenham’s (xend,yend) = (15,13)
Algorithm

Circle Drawing
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 37
Bresenham’s Line-Drawing Algorithm
• |m| < 1
m = 0.6 < 1
• Plot (x0,y0)
Δx = 5 Δy = 3 2Δy = 6
Point Drawing in
OpenGL
• For each k, starting with 2Δy - 2Δx = -4
k=0: p0 =1
p0 ≥ 0, so
Line Drawing • If pk < 0: Plot (x1,y1) = (x0+1,y0+1) =
Algorithms
• Plot (xk+1,yk) (11,11)
p1  p0  2y  2x
DDA Algorithm
 1 4  3
pk1  pk  2y
Bresenham’s • Otherwise: (xend,yend) = (15,13)
Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1  pk  2y  2x
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 38
Bresenham’s Line-Drawing Algorithm
• |m| < 1
m = 0.6 < 1
• Plot (x0,y0)
Δx = 5 Δy = 3 2Δy = 6
Point Drawing in
OpenGL
• For each k, starting with 2Δy - 2Δx = -4
k=0: p1 = -3
Line Drawing • If pk < 0: p1 < 0, so
Algorithms Plot (x2,y2) = (x1+1,y1) =
• Plot (xk+1,yk) (12,11)
p 2  p1  2  y
DDA Algorithm
pk1  pk  2y  3  6  3
Bresenham’s • Otherwise: (xend,yend) = (15,13)
Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1  pk  2y  2x
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 39
Bresenham’s Line-Drawing Algorithm
• |m| < 1
m = 0.6 < 1
• Plot (x0,y0)
Δx = 5 Δy = 3 2Δy = 6
Point Drawing in
OpenGL
• For each k, starting with 2Δy - 2Δx = -4
k=0: p2 =3
p2 ≥ 0, so
Line Drawing • If pk < 0:
Plot (x3,y3) = (x2+1,y2+1)
Algorithms
• Plot (xk+1,yk) = (13,12)
p3  p2  2y  2x
DDA Algorithm
pk1  pk  2y  3  4  1

Bresenham’s • Otherwise: (xend,yend) = (15,13)


Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1  pk  2y  2x
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 40
Bresenham’s Line-Drawing Algorithm
• |m| < 1
m = 0.6 < 1
• Plot (x0,y0)
Δx = 5 Δy = 3 2Δy = 6
Point Drawing in
OpenGL
• For each k, starting with 2Δy - 2Δx = -4
k=0: p3 = -1
p3 < 0, so
Line Drawing • If pk < 0:
Plot (x4,y4) = (x3+1,y3) =
Algorithms
• Plot (xk+1,yk) (14,12)

DDA Algorithm p4  p3  2y


pk1  pk  2y  1  6  5
Bresenham’s • Otherwise: (xend,yend) = (15,13)
Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1  pk  2y  2x
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 41
Bresenham’s Line-Drawing Algorithm
• |m| < 1
m = 0.6 < 1
• Plot (x0,y0)
Δx = 5 Δy = 3 2Δy = 6
Point Drawing in
OpenGL
• For each k, starting with 2Δy - 2Δx = -4
k=0: p4 =5,
Line Drawing • If pk < 0: p4 ≥ 0, so
Algorithms Plot (x5,y5) = (x4+1,y4+1)
• Plot (xk+1,yk) = (15,13)
DDA Algorithm
pk1  pk  2y
Bresenham’s • Otherwise: (xend,yend) = (15,13)
Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1  pk  2y  2x
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 42
Bresenham’s Line-Drawing Algorithm
• |m| < 1
m = 0.6 < 1
• Plot (x0,y0)
Δx = 5 Δy = 3 2Δy = 6
Point Drawing in
OpenGL
• For each k, starting with 2Δy - 2Δx = -4
k=0: p4 =5,
Line Drawing • If pk < 0: p4 ≥ 0, so
Algorithms Plot (x5,y5) = (x4+1,y4+1)
• Plot (xk+1,yk) = (15,13)
DDA Algorithm
pk1  pk  2y
Bresenham’s • Otherwise: (xend,yend) = (15,13)
Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1  pk  2y  2x
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 43
Bresenham’s vs. DDA Line Drawing
Algorithms
Point Drawing in
OpenGL
• They plot the same pixels.
Line Drawing
Algorithms • Bresenham’s algorithm use only integer
operations.
DDA Algorithm
• Bresenham’s algorithm is more efficient
Bresenham’s than DDA.
Algorithm
• Bresenham’s algorithm preferable for line
Circle Drawing drawing than DDA in computer Graphics.
Algorithms

Fill-AreaComputer
Primitives
Graphics- CoSc3121
44
• Draw using glBegin … glEnd functions
• We specify
OpenGL Linethat verticesFunctions
Drawing should be interpreted as line
end-points by using the symbolic constant GL_LINES
• GL_LINES : specify that vertices should be
Point Drawing in interpreted as line end-points.
OpenGL
• glLineWidth(3.0); //set the width of the line
Line Drawing For example, the following code will draw two separate
Algorithms line segments:
Line Drawing • One line from (100,200) to (150,200)
Functions • Another line from (150,250) to (200,250)
glLineWidth(3.0);
GL_LINES glBegin(GL_LINES);
glVertex2f(100.0, 200.0);
Circle Drawing glVertex2f(150.0, 200.0);
Algorithms glVertex2f(150.0, 250.0);
glVertex2f(200.0, 250.0);
glEnd()
Fill-AreaComputer
Primitives
Graphics- CoSc3121
45
OpenGL Data Types
46

Computer Graphics- CoSc3121


Points, Lines and Polygons
47

Computer Graphics- CoSc3121


Points, Lines and Polygons

Computer Graphics- CoSc3121

48
OpenGL Line Drawing
Example
•GL_LINES : specify that vertices should be interpreted as line end-points.
•glLineWidth(3.0); //set the width of the line
GLint p1[] = {200,100}; GLint p2[] = {50,0};
GLint p3[] = {100,200}; GLint p4[] = {150,0};
GLint p5[] = {0,100};

glBegin(GL_LINES);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glEnd(); (GL_LINES)

(GL_LINE_STRIP) (GL_LINE_LOOP)
49
OpenGL Line Drawing
GL_LINE_STRIP and GL_LINE_LOOP
Glint p1[] = {200,100};
Glint p2[] = {50,0};
Glint p3[] = {100,200};
Glint p4[] = {150,0};
Glint p5[] = {0,100};
glBegin(GL_LINE_STRIP); glBegin(GL_LINE_LOOP);
glVertex2i(p1); glVertex2i(p1);
glVertex2i(p2); glVertex2i(p2);
glVertex2i(p3); glVertex2i(p3);
glVertex2i(p4); glVertex2i(p4);
glVertex2i(p5); glVertex2i(p5);
glEnd(); glEnd();

50
OpenGL Line Drawing

• Line style – is specified using a pixel mask


 Stippled lines – to make stippled (dotted or dashed) lines

 Firstly, must enable line stipple using: glEnable(GL_LINE_STIPPLE);

• Next, use glLineStipple function to define line style, takes 2 arguments: glLineStipple(GLint
repeatFactor, GLushort pattern);
 repeatFactor, specifies how many times each bit in the pixel mask should be repeated,

and
 pattern, which is a 16-bit pixel mask, with the low-order bit used first – series of 0’s and

1’s
 E.g., pixel mask is the hexadecimal number 00FF, which is 8 zeros followed by 8 ones

repeat factor is 1 so each one or zero in the pixel mask corresponds to a single pixel in the line

51
OpenGL Line Drawing
• Line style – is specified using a pixel mask
 Stippled lines – to make stippled (dotted or
dashed) lines
 Firstly, must enable line stipple using:

glEnable(GL_LINE_STIPPLE);
• Next, use glLineStipple function to define line style,
takes 2 arguments: glLineStipple(GLint
repeatFactor, GLushort pattern);
glEnable(GL_LINE_STIPPLE);
glLineWidth(3.0);
glLineStipple(1, 0x00ff);
glBegin(GL_LINE_STRIP);
glVertex2i(100,100);
glVertex2i(150,100);
glVertex2i(150,200);
glVertex2i(250,250); 52

glEnd();
glDisable(GL_LINE_STIPPLE);
OpenGL Line Drawing

53
Triangle Drawing Primitives

Triangle - a primitive formed by 3 vertices


It is 2D shape with the smallest number of vertices
3 kinds of triangle primitives, based again on different interpretations of
the vertex stream:
- GL_TRIANGLES: vertices 0, 1, and 2 form a triangle. Vertices 3,
4, and 5 form a triangle. And so on.
- GL_TRIANGLE_STRIP: every group of 3 adjacent vertices
forms a triangle. A vertex stream of n length will generate n-2
triangles
- GL_TRIANGLE_FAN: the first vertex is always held fixed.
54
Triangle Drawing Primitives

- From there on, every group of 2 adjacent vertices form a triangle with
the first
- So with a vertex stream, you get a list of triangles like so: (0, 1, 2)
(0, 2, 3), (0, 3, 4), etc. A vertex stream of n length will generate n-2
triangles.

55

You might also like