OpenGL Graphics Primitives Guide
OpenGL Graphics Primitives Guide
(CoSc3121)
1
GRAPHICS PRIMITIVES
Chapter Two- Graphics Primitives
2
Point Drawing in
OpenGL
Line Drawing
Algorithms
Circle Drawing
Algorithms
Fill-Area Primitives
Circle Drawing
Algorithms
Fill-Area Primitives
yend y0 c y0 mx0
m
xend x0 δy = m.δx
δx = (1/m).δy
Computer Graphics- CoSc3121
5
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.
If |m| > 1:
DDA Algorithm
δx = 1/m
δy = 1
Bresenham’s Paint (x0,y0)
Algorithm Find successive pixel positions
xk+1 = (xk + δx)
Circle Drawing yk+1 = (yk + δy)
Algorithms For each position (xk,yk) plot a
line point at
The gap between subsequent
(round(xk),round(yk))
Fill-Area Primitives points in the line is greater than 1
The round function will round to pixel.
the nearest integer.
Computer Graphics- CoSc3121
6
Digital Differential Analyzer Algorithm
Example1
Point Drawing in
Given (x0,y0) = (10,10) yend y0 (13 10) 3
m 0.6
OpenGL
and (xend,yend) = (15,13)
xend x0 (15 10) 5
Compute m
Line Drawing If |m| ≤ 1:
Algorithms δx = 1
δx = 1
δy = m δy = 0.6
If |m| > 1:
DDA Algorithm
δx = 1/m
δy = 1
Bresenham’s Paint (x0,y0) (xend,yend) = (15,13)
Algorithm
Circle Drawing
Algorithms
Point Drawing in
Given (x0,y0) = (10,10) yend y0 (13 10) 3
m 0.6
OpenGL
and (xend,yend) = (15,13)
xend x0 (15 10) 5
Compute m
Line Drawing If |m| ≤ 1:
Algorithms δx = 1
δx = 1
δy = m δy = 0.6
If |m| > 1:
DDA Algorithm
δx = 1/m
δy = 1
Bresenham’s Paint (x0,y0) (xend,yend) = (15,13)
Algorithm
Circle Drawing
Algorithms
Circle Drawing
Algorithms
Circle Drawing
Algorithms
Circle Drawing
Algorithms
Circle Drawing
Algorithms
Circle Drawing
Algorithms
Figure - The Operation of the DDA Line-
Draiwng Algorithm
Point Drawing in
Given (x0,y0) = (10,10)
m
yend y0 (15 10) 5 1.25
OpenGL
and (xend,yend) = (14,15) xend x0 (14 10) 4
Compute m
Line Drawing If |m| ≤ 1:
δx = 0.8
Algorithms δx = 1
δy = 1
δy = m
δy = 1
Bresenham’s Paint (x0,y0)
Algorithm
Circle Drawing
Algorithms
Point Drawing in
Given (x0,y0) = (10,10)
m
yend y0 (15 10) 5
OpenGL
and (xend,yend) = (14,15) xend x0 (14 10) 4
Compute m
Line Drawing If |m| ≤ 1:
δx = 0.8
Algorithms δx = 1
δy = 1
δy = m
δy = 1
Bresenham’s Paint (x0,y0)
Algorithm
Circle Drawing
Algorithms
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Fill-Area Primitives
(x0,y0) = (10,10)
Computer Graphics- CoSc3121
20
Digital Differential Analyzer Algorithm
Example2
Paint (x0,y0) δx = 0.8
Point Drawing in Find successive pixel δy = 1
OpenGL positions
xk+1 = (xk + δx)
Line Drawing yk+1 = (yk + δy)
(x5,y5) = (13.2+0.8,14+1)
Algorithms = (14,15)
For each position (xk,yk)
plot a line point at
(round(xk),round(yk))
colour pixel (14,15)
DDA Algorithm
(xend,yend) = (14,15)
Bresenham’s
Algorithm
Circle Drawing
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
DDA Algorithm
Circle Drawing
Algorithms
Circle Drawing
Algorithms
Fill-Area Primitives
Line Drawing •For example, the following code will draw two
Algorithms separate line segments:
•One line from (100,200) to (150,200)
DDA Algorithm •Another line from (150,250) to (200,250)
glLineWidth(3.0);
Bresenham’s glBegin(GL_LINES);
Algorithm glVertex2f(100.0, 200.0);
glVertex2f(150.0, 200.0);
Circle Drawing glVertex2f(150.0, 250.0);
Algorithms glVertex2f(200.0, 250.0);
glEnd();
Fill-Area Primitives
Circle Drawing
Algorithms
Line Drawing
Algorithms
Circle Drawing
Algorithms
Basic Algorithms
Midpoint
Algorithms
We will examine a number of approaches to
plotting points on a circle, culminating in the most
Fill-Area Primitives
efficient algorithm: the midpoint algorithm.
Computer Graphics- CoSc3121
39
Circle Drawing Algorithms
1. Plotting Points Using Cartesian Coordinates
Point Drawing in Incrimenting x and computing y
OpenGL
y yc r 2 x xc
2
Line Drawing
Algorithms 2. Plotting Points Using Polar Coordinates
The radius r will be constant and only changing θ
Circle Drawing
Algorithms Compute x and y
x xc r cos y yc r sin
Basic Algorithms
More efficient than
Midpoint Cartesian one.
Algorithms Even more efficient
with a little cost in
Fill-Area Primitives quality.
Line Drawing
Algorithms
Circle Drawing
Algorithms
Basic Algorithms
Circle Drawing p0 = -9
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
43
Midpoint Algorithm Example
p0 < 0,
Point Drawing in Plot (x1,y1) = (x0+1,y0) = (1,10)
OpenGL
p1 p0 2 x1 1 9 3 6
Line Drawing
Algorithms (10,10)
Circle Drawing p0 = -9
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
44
Midpoint Algorithm Example
p1 < 0,
Plot (x2,y2) = (x1+1,y1) = (2,10)
Point Drawing in
OpenGL p2 p1 2x2 1 6 5 1
Line Drawing
Algorithms (10,10)
Circle Drawing p1 = -6
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
45
Midpoint Algorithm Example
p2 < 0,
Plot (x3,y3) = (x2+1,y2) = (3,10)
Point Drawing in
OpenGL p3 p2 2 x3 1 1 7 6
Line Drawing
Algorithms (10,10)
Circle Drawing p2 = -1
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
46
Midpoint Algorithm Example
p3 ≥ 0
Plot (x4,y4) = (x3+1,y3-1) = (4,9)
Point Drawing in
OpenGL p4 p3 2 x4 1 2 y4 6 9 3
Line Drawing
Algorithms (10,10)
Circle Drawing p3 = 6
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
47
Midpoint Algorithm Example
p4 < 0,
Plot (x5,y5) = (x4+1,y4) = (5,9)
Point Drawing in
OpenGL p5 p4 2 x5 1 3 11 8
Line Drawing
Algorithms (10,10)
Circle Drawing p4 = -3
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
48
Midpoint Algorithm Example
p5 ≥ 0,
Plot (x6,y6) = (x5+1,y5-1) = (6,8)
Point Drawing in
OpenGL p 6 p5 2 x6 1 2 y 6 8 3 5
Line Drawing
Algorithms (10,10)
Circle Drawing p5 = 8
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
49
Midpoint Algorithm Example
p6 ≥ 0,
Plot (x7,y7) = (x6+1,y6-1) = (7,7)
Point Drawing in
OpenGL p7 p6 2 x7 1 2 y7 19 13 6
Line Drawing
Algorithms (10,10)
Circle Drawing p6 = 5
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
50
Midpoint Algorithm Example
p7 ≥ 0,
Plot (x8,y8) = (x7+1,y7-1) = (8,6)
Point Drawing in
OpenGL p8 p7 2 x8 1 2 y8 22 11 11
Line Drawing
Algorithms (10,10)
Circle Drawing p7 = 6
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
51
Midpoint Algorithm Example
p8 ≥ 0,
Plot (x9,y9) = (x8+1,y8-1) = (9,5)
Point Drawing in
OpenGL p9 p8 2 x9 1 2 y9 29 9 20
Line Drawing
Algorithms (10,10)
Circle Drawing p8 = 11
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
52
Midpoint Algorithm Example
p9 ≥ 0,
Plot (x10,y10) = (x9+1,y9-1) = (10,4)
Point Drawing in
OpenGL
Line Drawing
Algorithms (10,10)
Circle Drawing p9 = 20
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area Primitives
(0,0)
Computer Graphics- CoSc3121
53
Bresenham’s Line-Drawing Algorithm
Exercise Given (x0,y0) = (10,10) and (xend,yend) = (14,15)
Compute m
|m| ≥ 1 m = 1.25 ≥ 1
Point Drawing in
OpenGL Plot (x0,y0)
Compute the first
Line Drawing decision variable:
Algorithms p0 2x y
DDA Algorithm
(xend,yend) = (14,15)
Exercise4
Bresenham’s
Consider the above
Algorithm
examples and plotting the
line using Bresenham’s
Circle Drawing
algorithm:
Algorithms
Given (x0,y0) = (10,10) and
(xend,yend) = (14,15)
Fill-Area Primitives
(x0,y0) = (10,10)
Computer Graphics- CoSc3121
54
Midpoint Algorithm Example
Given the center (0,0) and radius r = 5
Plot the start-point of the circle (x0,y0) = (xc,r) = (0,5)
Point Drawing in
Compute the first decision variable:
OpenGL
p0 1 r
Line Drawing
Algorithms (5,5)
Circle Drawing P = -4
Algorithms Exercise5
Consider the above
Basic Algorithms example and express the
midpoint circle-drawing
algorithm for a circle
Midpoint centred at the origin.
Algorithms
(0,0) Given the center (0,0)
and radius r = 5
Fill-Area Primitives
Basic Algorithms
Exercise6
Midpoint Consider the above
Algorithms example and create a
OpenGL Circle solid sphere / circle with a
Drawing radius of 1 units.
Fill-Area Primitives
Computer Graphics- CoSc3121
Any Question?