Beruflich Dokumente
Kultur Dokumente
Computer Graphics
Computers Graphics
P4 E3
P3
S1
E4 E2
P1 E1 P2
Preparing Picture for presentation
– CG images are made by setting the intensity and color of the pixel
which compose the screen
– It collects and stores the pixel values for use by the display device
00 0 0 0 0
00 0 1 0 0
Line 00 0 0 1 0
00 0 0 0 0
01 0 0 01
Computer Graphics
The Problem Of Scan Conversion
(7, 5)
(2, 2)
x
The Problem (cont…)
• But what happens when we try to draw this on a pixel
based display?
yend y m x b
• where:
y0 yend y0
m
xend x0
x
x0 xend b y0 m x0
Lines & Slopes
• The slope of a line (m) is defined by its start and end
coordinates
• The diagram below shows some examples of lines
and their slopes
m=∞
m = -4 m=4
m = -2 m=2
m = -1 m=1
m = - 1/2 m = 1/2
m = -1/3 m = 1/3
m=0 m=0
A Very Simple Solution
• We could simply work out the corresponding y
coordinate for each unit x coordinate
• Let’s consider the following example:
y
(7, 5)
5
2
(2, 2)
x
2 7
y
(7, 5) • First work out m and b:
5
52 3
m
72 5
2 3 4
(2, 2)
b 2 2
5 5
x
2 3 4 5 6 7
3 4 4 3 4 2
y (5) 5 3 y ( 6) 6 4
5 5 5 5 5 5
• Now just round off the results and turn on these pixels to
draw our line
3
7
y (3) 2 3
5
6
1
5 y ( 4) 3 3
5
4
3 4
y (5) 3 4
2 5
1
2
0 y (6) 4 4
5
0 1 2 3 4 5 6 7 8
• However, this approach is just way too slow
• In particular look out for:
– The equation y = mx + b requires the multiplication of
m by x
– Rounding off the resulting y coordinates
• We need a faster solution
The DDA Algorithm
1
xk 1 xk
m
m<1
m >1
Digital Differential Algorithm
y = mx + b
d2
y = m(xi+1) + b
yi d1
xi
X+1
DDA versus Bresenham’s Algorithm
• Translations
• Rotations
• Scaling
• Mirroring or Refection
2D Translation
– In this case ‘w’ is a dummy and on normalization gives [x/w y/w z/w 1].
Translation in homogenous coordinates
x' = ax + by + c
y' = dx + ey + f
Homogeneous formulation
x' a b c x
= f y
y‘ d e
1 0 0 1 1
p' = Mp
Homogeneous Coordinates
• Most of the time w = 1, and we can ignore it
x' a b c d x
y' e f g h y
=
z' i j k l z
1 0 0 0 1 1
3DTranslation
y
( x0 , y0 , z0 )
(0 , 0 , 0)
x
and let ( x' , y ' , z ' ) be the coordinates after the transformation.
x ' x x0
y ' y y
0
z ' z z 0
3DScaling
Scaling makes an object larger or smaller by multiplying the coordinates
by constants.
Scaling does not preserve distances. However, uniform scaling preserves
the shape of an object by preserving the ratios of the distances.
2
1
2 4
2
4
An object before and after uniform scaling. The numbers shown on the
edges are their lengths.
3D Scaling
Uniform scaling
3D Scaling
• The general equation of the scaling is
x' s1 0 0 x
y ' 0 s2 0 y
z ' 0 0 s3 z
General scaling
P’ = S . P
x ' s 0 0 x
Where P’ = y ' 0 s 0 y
z ' 0 0 s z
x ' s 0 0 x
y ' 0 s 0 y
S
z ' 0 0 s z
x ' s 0 0 x
y ' 0 s P 0= y
z ' 0 0 s z
y p'
Scale(s,s,s) Scale (sx, sy, sz)
p
q'
• Isotropic (uniform) q
scaling: sx = sy = sz
x
x' sx 0 0 0 x
y' 0 sy 0 0 y
z' = 0 0 sz 0 z
1 0 0 0 1 1
3D Rotation
A rotation rotates an object along an axis
Rotation
Rotation About Z-axis
y
p'
θ p
x
z
Rotation About X-axis
Rotation About Y-axis
Line Clipping Algorithm
– If the line is inside the window if the both end points are equal to
“0000”.
– If the line is outside the window if the both end points are not equal to
“0000”.
– For those lines which are partially inside the window, they are split at
the window edge and discord the line segment outside the window
Hidden Surface Removal
• Specific needs of realism
– Most real objects are opaque and we only see the lines and
surfaces at front, those portions that are back had therefore
to be eliminated from the mathematical model before
displaying
The procedure that distinguishes between visible surfaces from invisible/hidden surfaces is called visible-surface determination, which is often called hidden-
surface removal.
Z-buffer
It’s called z-buffer algorithm, where we need z-buffer in addition to
the frame buffer.
should be “blue”
350
?
350
Z-buffer Algorithm
The frame buffer will have a color-value for each pixel.
The z-buffer will have a z-value for each pixel.
Similarly, the frame buffer is initialized to the background color (in our case,
white).
① initial states
frame buffer z-buffer
…………….
: …………….
: w w w w ….. :
: w w w w ….. : -1 -1 -1 -1 ..
: -1 -1 -1 -1 ..
1) Project each triangle onto the z=0 plane, and draw it using the scan-line
algorithm.
2) When drawing each polygon, if the polygon point at (x,y) has a bigger
z-value than the current value of the z-buffer, the point’s color & z-
value replace the old ones in both buffers.
x=241 x=241
updated! updated!
……………. …………….
: :
: B B w w ….. : 0.8 0.7 -1
: B B R w ….. -1 ..
y=200 R R R R : 0.8 0.7 0.5 y=200
-1 ..
: 0.7 0.6 0.5 0.4 ..
x=241 x=241
Note that we will have the same result if we process blue triangle first and
then red triangle (unless they are partially transparent).
A point (242,200,0.5) on
② When scan line 200 is completed the red triangle
frame buffer z-buffer
…………….
: …………….
: w w w w ….. :
: w R R w ….. : -1 -1 -1 -1 ..
y=200 : -1 0.6 0.5 -1 .. y=200
x=241 x=241
pixel (241,200)
updated! updated!
……………. …………….
: :
: B B w w ….. : 0.8 0.7 -1
: B B R w ….. -1 ..
y=200 R R R R : 0.8 0.7 0.5 y=200
-1 ..
: 0.7 0.6 0.5 0.4 ..
x=241 x=241
Note that we will have the same result if we process blue triangle first and
then red triangle (unless they are partially transparent).