For updated version, please click on
http://ocw.ump.edu.my
Computer Graphics
Introduction to Computer
Graphics
Edited by
Dr. Md. Manjur Ahmed
Dr. Suryanti Awang
Faculty of Computer Systems and Software Engineering
[email protected]
Chapter Description
• Aims
– Basic of Computer Graphics.
• Expected Outcomes
– Understand the basic concept of computer graphics. (CO1: Knowledge)
– Ability to use the computer graphics technology. (CO1: Knowledge)
• References
– Computer Graphics by Zhigang Xiang, Schaum’s Outlines.
– Donald Hearn & M. Pauline Baker, Computer Graphics with OpenGL, 4th Edition, Boston :
Addison Wesley, 2011.
COORDINATES SYSTEM
Most windowing systems:
(0,0) x
OpenGL framebuffer:
y
(0,0) x
COORDINATES SYSTEM
Does it matter? No, we just need to be aware of the difference:
Where a pixel in the framebuffer will show up on screen?
How do we get the pixel address under the mouse pointer?
Could some other display library have its framebuffer lay-out
match your windowing system? Absolutely. Many do.
What if all we never directly displayed our framebuffer, but
wrote it out as an image for later display?
Virtually all image formats use screen-space coordinates.
RASTER DISPLAY
Represented by a 2D array of positions called pixels
Reference: Donald Hearn & M. Pauline Baker, Computer Graphics with OpenGL, 4th Edition, Boston : Addison Wesley, 2011.
pixel at location (0,0), lower left corner
Color frequently requires 1 byte per channel (three color
channels per pixel namely R=red, G=green, B=blue).
RASTER DISPLAY
Frame Buffer: ~ is stored the color data, often called
an image map or bitmap.
Scan conversion: convert to basic and low level
objects into corresponding pixel map depictions.
OUTPUT PRIMITIVES
A picture consists of a complex objects
Come from basic geometric structures called
Object Primitives
Points and Lines
• A line can be completed by calculating the line
path between 2 endpoints. Points and Lines
Simple Line
• A point on the screen with position (x, y): plot
a pixel with corresponding position
• Sample code:
SetPixel ( x, y ) a function in windows.h
(x, y) (x, y)
Simple Line
Raster-scan devices address
discrete pixels
The endpoints and intermediate
points must be set individually
The points are calculated from
the slope-intercept equation
Line drawing is a fundamental
operation on which many
other routines are built
Equation of a line
(x1,y1)
y m.x c
y 2 y1
m
dy
dx
x2 x1
(x2,y2) c y1 m.x1
Based on eq. and positions of 2 endpoints (x1, y1), (x2, y2):
y2 y1 y
m
x2 x1 x
Therefore,
y m x
and y
x
m
Example
Digital Differential Analyzer (DDA)
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
Consider endpoints:
P1(0,0), P2(7,4)
DDA Example
Let P1(2,2), P2(7, 5)
Calculate the points that made up the line P1P2
First work out m and c:
52 3 3 4
m c 2 *2
72 5 5 5
Now work for each x value work out the y value:
3 4 3
y (3) *3 2 3
5 5 5
3 4 1
y (4) * 4 3 3
5 5 5
3 4 4
y (5) * 5 3 4
5 5 5
3 4 2
y ( 6) * 6 4 4
5 5 5
DDA in C
DDA Exercise
1. Consider endpoints:
P1(0,0), P2(6, 4)
Calculate the points that made up the line P1P2
2. Now, consider endpoints:
P1(0,0), P2(4, 6)
Calculate the points that made up the line P1P2
Limitation of DDA
Not identify the positions if x1 < x0.
Answer: Shift the order of the points if x1 < x0.
Bresenham’s line drawing
algorithm
Consider the first condition:
m < 1, m has a positive value
Bresenham’s increments x by 1 and y by 0 or 1
This makes sense for our lines, we want them to be continuous
If the magnitude of the slope were more than 1, we’d swap x & y
Bresenham’s line drawing
algorithm
Algorithm for |m| < 1:
1. Take 2 endpoints as input. Assign (x1, y1) = first end point.
2. Load to frame buffer and plot the first point in display.
3. Compute constant values of x, y, 2y, 2y – 2x . Use initial
value of decision parameter:
p1 2y x
4. Start form t=1, for each xt along the line, test:
if pt < 0, plot (xt+1, yt) and pt 1 pt 2y
else plot (xt+1, yt+1) and pt 1 pt 2y 2x
5. Continue step 4 x times.
Example
Digitize the line with endpoints (20,10) and (25,13).
Plot the line by determining the pixel positions.
y mx c
t pt (xt+1, yt+1)
1
5
Exercise
Calculate pixel positions that made up the line
connecting endpoints: (12, 10) and (17, 14).
1. (x1, y1) = ?
2. x = ?, y =?, 2y = ?, 2y – 2x =?
3. p1 = 2y – x =?
t pt (xt+1, yt+1)
Exercise
Calculate pixel positions that made up the line
connecting endpoints: (12, 10) and (17, 14).
1. (x1, y1) = (12, 10)
2. x = 5, y = 4, 2y = 8, 2y – 2x = -2
3. p1 = 2y – x = 3
k pk (xk+1, yk+1)
1 3
3
Circle Drawing
Simple way to start
Equation of a circle:
If we solve for y:
Simple way to start
“uncertainty”
In certain cases, the slope
of the line tangent is
greater than 1. Therefore,
tangent > 1 shows the
uncertainty of this
algorithm.
Looping (or stepping) using
x won’t work here.
Reference: Computer Graphics: Principles
and Practice by James D. Foley and et.al.
Circles symmetrical nature
To solve the issue “slope
of the tangent”
Let’s take the advantage
of the circle’s symmetric
nature.
Consider both positive
and negative values for y
(or x).
Midpoint Circle Algorithm
• Consider current point is at (xk,
yk) •Use the discriminator
• Next point: (xk+1, yk), or (xk+1, function to decide:
yk-1)? f x, y x 2 y 2 r 2
• Take the midpoint: (xk+1, yk-0.5)
yk
yk-1
yk
Circle path
yk-1
xk xk+1 xk+2
xk
Midpoint
Using the circle discriminator
Based on the value return:
<0 inside the circle
f(x, y) = 0; on the circle path
>0; outside the circle
By using the midpoint between 2 pixel candidates, we can introduce a
decision parameter, pk , to decide which to plot next:
1) p k f x k 1, y k 0.5
x k 1 y k 0.5 r 2
2 2
2 and 3)
-ve: midpoint is inside the circle; plot (xk+1, yk)
pk
+ve: midpoint is outside the circle; plot (xk+1, yk-1)
If the current point is inside the
circle …
If pk < 0
We want to know f(x+1, y) so we can update p:
f(x+1, y) = (x + 1)2 + y2 – r2
f(x+1, y) = (x2 + 2x + 1) + y2 – r2
f(x+1, y) = f(x, y) + 2x + 1
Pk+1 Pk
So we increment:
p += 2x + 1
If the current point is outside the
circle …
If pk > 0
Let’s drop the subscript for a while…
We want to know f(x+1, y-1) so we can update p:
f(x+1, y–1) = (x + 1)2 + (y – 1)2 – r2
f(x+1, y–1) = (x2 + 2x + 1) + (y2 – 2y + 2) – r2
f(x+1, y–1) = f(x, y) + 2x – 2y + 2
Pk+1 Pk
And we increment:
p += 2x – 2y + 2
Where to begin?
We can determine where to go next, how do we start?
We have a variety of choices for our first point on the circle,
but we’ve designed the increments to work from (0, r).
Calculate initial value of p0 by evaluating:
p0 = f(1, r–0.5) = 12 + (r – 0.5)2 – r2
p0 = f(1, r–0.5) = 1 + (r2 – r + 0.25) – r2
p0 = 1.25 – r
**We want to use integer calculation; you can round p0
Midpoint circle algorithm
Homework
Ellipse Drawing
Equation
F2
General equation of an ellipse: d2
F1
x x1 2 y y1 2 x x2 2 y y2 2 constant d1 (x, y)
d1 + d2 = constant
Or,
2
y yc
2
x xc
1
r
ry rx y
rx
Symmetry
An ellipse only has a (-a, b) (a, b)
2-way symmetry.
(-a, -b) (a, -b)
Equation of an ellipse revisited
Consider an ellipse centered at the origin:
2
y
2
x
1
r
rx y
What is the discriminator function?
f e x, y ry x 2 rx y 2 rx ry
2 2 2 2
…and its properties:
fe(x,y) < 0 for a point inside the ellipse
fe(x,y) > 0 for a point outside the ellipse
fe(x,y) 0 for a point on the ellipse
Midpoint Ellipse Algorithm
• Ellipse is different from
circle.
• Similar approach with
circle, different is sampling Slope =
direction. -1
• Region 1:
– Sampling is at x direction
– Choose between (xk+1, yk),
or (xk+1, yk-1)
Region 1
– Midpoint: (xk+1, yk-0.5)
• Region 2: Region 2
– Sampling is at y direction
– Choose between (xk, yk-1), or
(xk+1, yk-1)
– Midpoint: (xk+0.5, yk-1)
Conclusion of The Chapter
• Next Part more in these….