Ch-4 Geometry and Line Generation
Ch-4 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.
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)
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.
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)
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
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
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
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
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
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
Circle Drawing
Algorithms
Fill-AreaComputer
Primitives
Graphics- CoSc3121 (x0,y0) = (10,10) 22
Digital Differential Analyzer Algorithm Example1
• Paint (x0,y0)
• (round(xk),round(yk))
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
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
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)
• (round(xk),round(yk))
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Fill-AreaComputer
Primitives
Graphics- CoSc3121
30
(x0,y0) = (10,10)
Digital Differential Analyzer Algorithm Example2
• Paint (x0,y0)
• (round(xk),round(yk))
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:
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
The Bresenham
algorithm is another
incremental scan
conversion algorithm
The big advantage of
this algorithm is that it
uses only integer
calculations
Bresenham’s
• Plot (xk+1,yk)
Algorithm
pk1 pk 2y
Circle Drawing
Algorithms • Otherwise:
• Plot (xk+1,yk+1)
pk 1 pk 2y 2x
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 2y x p0 2x y
• For each k, starting with
For each k, starting with
k=0:
DDA Algorithm k=0:
• If pk < 0: If pk < 0:
DDA 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 2y x 2Δy - 2Δx = -4
0
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 2y 2x
DDA Algorithm
1 4 3
pk1 pk 2y
Bresenham’s • Otherwise: (xend,yend) = (15,13)
Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1 pk 2y 2x
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
pk1 pk 2y 3 6 3
Bresenham’s • Otherwise: (xend,yend) = (15,13)
Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1 pk 2y 2x
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 2y 2x
DDA Algorithm
pk1 pk 2y 3 4 1
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)
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
pk1 pk 2y
Bresenham’s • Otherwise: (xend,yend) = (15,13)
Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1 pk 2y 2x
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
pk1 pk 2y
Bresenham’s • Otherwise: (xend,yend) = (15,13)
Algorithm
• Plot (xk+1,yk+1)
Circle Drawing pk 1 pk 2y 2x
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
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
• 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
- 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