CG Reference
CG Reference
2
What is Computer Graphics??
6
COMPUTER GRAPHICS ? Contd..
A field related to the generation of graphics using
computer
Includes the creation ,storage and manipulation of
images of objects
Objects Entertainment and Advertisement, Medicine,
Physical, Mathematical, Engineering, Architecture
Computer graphics- generating 2D images of a 3D
world represented in a computer
7
MAIN TASKS
Imaging : formation of an image
: representation of 2D images
8
BASIC CONCEPTS
RASTER : A rectangular array of points or dots.
PIXEL (picture element) :One dot or picture element of the raster
SCAN LINE : A row of pixels
BITMAP :ones and zeros representation of the rectangular array
points on screen
Black and white bitmap
pixmap color (colored raster image)
n–1
Scan Line
We don’t have any pixel
like (1.2, 5.8). In raster
device co-ordinates can
take integer values
Pixel only.
0
0 m–1
m X n Raster
DEVELOPMENT OF COMPUTER GRAPHICS
1951- whirlwind computer developed in MIT had computer driven
CRT displays for output
Mid 1950’s- SAGE air defense system uses commands and control
CRT on which operator targets with light pens. Light pens sense
light emitted by objects on the screen.
1962- sketchpad, by Ivan Sutherland
Keyboard and light pen were used for drawing and making
choices
Modern interactive graphics
1964- CAD and CAM-shows graphical interaction and
graphical activities
General Motor DAC, Itek, Digitek for Lens Design
1968- Evans and Sutherland
Commercial company- flight simulators
High cost of graphics hardware
10
DEVELOPMENT OF COMPUTER GRAPHICS contd..
11
APPLICATIONS OF COMPUTER GRAPHICS
Entertainment
Films-Jurasic Park
Video games, Formula 1 etc
Computer-aided design
Design components and system of mechanical,
Medical Image
electrical, electronics, buildings, automobile etc
aeroplane
Scientific Visualization and Business Visualization
Scientific visualization-generating computer graphics for
scientific, engineering ,medical data
E.g. airflow inside a thunderstorm
Visible human
Business visualization- non scientific data
obtained in economics
AutoCAD 2002
12
APPLICATIONS OF COMPUTER GRAPHICS contd…
Training
Driving Simulation
Flight Simulation
Desk Assembly
Education
Human skeleton
3D MAX Flight Simulator
E-commerce
Virtual phone store
Interactive kitchen planner
charts
13
CHAPTER 2- HARDWARE CONCEPTS
Basic Graphics System
VIDEO DISPLAY DEVICE
• capable of performing a high-speed switching process and preventing
of video flicker form occurring.
TERMINOLOGIES
1.Florescence/ phosphorescence
2.Persistence
3.Refresh Rate
4.Horizontal Scan Rate
5.Resolution
6.Aspect Ratio
FLUORESCENCE / PHOSPHORESCENCE
When the beam of electrons emitted by electron gun is strikes phosphor-coated screen of the
CRT and then phosphor emits a small spot of light at each position contacted by the electron
beam, such phenomenon is known as Fluorescence / Phosphorescence which last just a
fraction of microseconds.
PERSISTENCE
-defined as the time from the removal of excitation to the moment when phosphorescence
has decayed to 10 percent of the initial light output .
-The phosphor used for graphics display device usually have persistence of 10 to 60
microseconds.
RESOLUTION
Resolution is defined as the maximum member of points that can be displayed horizontally
and vertically without overlap on a display device.
ASPECT RATIO
Aspect Ratio gives that ratio of vertical point to horizontal points necessary to produce equal
length lines in both directions on the screen an aspect ratio of 3/4 means that a vertical line
plotted with 3 points has the same length as a horizontal line plotted with 4 points.
HORIZONTAL SCAN RATE
The horizontal Scan Rate is the number of scan lines per seconds.
The rate is approximately the product of the refresh rate and number of scan lines.
REFRESH RATE
The number of times the screen is redrawn each second.
Higher refresh rates mean less flicker on the screen, which translates into less
eyestrain.
As refresh rate decreases , flicker develops because eye can no longer
integrate the individual light impulses coming from pixel.
The refresh rate above which a picture stops flickering and fuses into a steady
image is called the critical fusion frequency.
6
Video display device types
8 8
Refresh Cathode-Ray Tube
o Focusing system
o Electrostatic focusing
o Positively charged metal cylinder
o Deflection of electron beam
o Persistence
o How long phosphors continue to emit light
o Low persistence is useful for animation
o Resolution
o Number of points per centimeter can be
plotted horizontally and vertically
Refresh Cathode-Ray Tube
o Beam-penetration method
o Used with random-scan monitors
o Two layers of phosphor: red and green
o The displayed color depends on how far the electron beam
penetrates into the phosphor layers.
o Only four colors are possible: red, green, orange, and yellow.
ADVANTAGE:
• Economical way to produce colors
LIMITATIONS:
• Generation of only four colors is possible
• Poor picture quality
Color CRT Monitors
o Shadow-mask method
o Three phosphor color dots at each pixel position
o Three electron guns
o Color variations: Varying the intensity levels of the three
electron beams
o Full-color (true-color) system: 24 bits of storage per pixel
Direct-View Storage Tubes
o Coordinate system
o Frame-buffer locations are referenced in
Cartesian coordinates.
o Coordinate origin
o Lower left screen corner
o Upper left screen corner
o Refresh operations of video controller
o Top-to-bottom, left-to right
o x register ( initial value = 0)
o y register (initial value = ymax)
Video Controller
Display System
CPU Memory
Processor
System Bus
I/O Devices
Raster-Scan Display Processor
System Bus
I/O Devices
Advanced 3D Graphics Architecture
Object Geometric
Geometric
Object Transformation
Database
Database Transformation
Engine
Engine
Clipping
Clipping
perspective
perspective
scaling
scaling
Video
Video
Controler
Controler
Lighting
Lighting
Model
Model
Calculator
Calculator
Shading
Shading Frame
Rasterizer
Rasterizer buffer
INPUT DEVICES
- piece of hardware used to enter and manipulate information on a computer
KEYBOARD MOUSE
TABLET
1.No keyboard, no mouse. Instead, you have an LCD screen and a stylus.
The key goals are to recognize one or more fingers touching a display, to
interpret the command that this represents, and to communicate the command to
the appropriate application.
When a user touches the surface, the system records the change in the electrical
current that flows through the display
2
Points and Lines
Points
Plotted by converting co-ordinate position to appropriate
operations for the output device (e.g.: in CRT monitor, the
electron beam is turned on to illuminate the screen phosphor at
the selected location.)
Line
Plotted by calculating intermediate positions along the line
path between two specified endpoint positions.
Screen locations are referenced with integer values, so
plotted positions may only approximate actual line
positions between two specified endpoints “the
jaggies”. E.g.: position (10.48,20.51) (10,21).
3
Pixel Position
4
LINE DRAWING ALGORITHM
DDA Algorithm- (Digital Differential Analyzer)
Bresenham’s Line Algorithm
Note:
Pixel- picture element smallest piece of information in an
image represented using dots, squares and rectangle
Components-
RGB
or
4 components
Y Scan Line
(cyan ,magenta,
yellow & black)
Pixel
0
0
Column number X
5
RASTER and RANDOM SCAN
Raster Scan Random or vector Scan
Electron beam Electron beam
swept across the directed only to
screen ,one row at a parts of the screen
time from top to where a picture is
bottom. e.g. drawn. e.g. pen
television plotter
6
Straight Line Equation
Slope-Intercept Equation
y m. x
b
Slope
y2 y1 rise
m
x2 x1 run
Intercept
b y1 m.x1
Interval Calculation
y
y m.x x
m
7
Digital System
8
DDA Algorithm
Digital Differential Analyzer
Scan-conversion line – algorithm based on calculating either
9
CASES
Cases 1 positive slope
LEFT END TO RIGHT END POINT
a. m ≤1 sampling in x-direction
10
RIGHT END TO LEFT END POINT
a. m ≤1 sampling in x-direction
11
Case
Case 2 Negative slope
LEFT END TO RIGHT END POINT
a. |m| ≤1 sampling in x-direction
12
Case 2
13
DDA ALGORITHM
1. Input points (x1 ,y1) and (x2,y2)
2. Compute dx = x2- x1 & dy= y2 - y1
3. If |dx|>|dy| then steps=|dx|
otherwise steps =|dy|
4. xinc = dx/ steps
yinc = dy/ steps
5. x= x1 , y= y1 , k=0
6. do:
15
DDA
DDA PROBLEMS & IMPROVEMENT
PROBLEMS
Accumulations of round off error in successive
additions of the floating point increment– this
causes the calculated pixel drift away from the
true line path for long line segments
ALIASING EFFECT
Rounding operations and floating-point
arithmetic operations are still time consuming
IMPROVEMENT
Can be removed by making increments m and
1/m into integer and fractional parts so that all
calculations are reduced to integer operations
16
Bresenham’s Line Algorithm
Another incremental scan conversion algorithm
Uses only incremental integer calculations
Accurate and efficient line generating
algorithm
IDEA : move across the x-axis in unit
intervals and at each step choose
between two y coordinates
E.g.
Which pixel to draw ?
(11,11) or (11,12) ?
(51,50) or (51,49) ?
Solution choice is that which is
closer to the original line
17
Bresenham’s Line Algorithm contd…
For |m|<1
i.e.
18
Bresenham’s Line Algorithm contd…
y- coordinate on the mathematical line at
is
y m ( xk 1) b
Then
d1 y yk
m( xk 1) b yk
And
d 2 ( yk 1) y
yk 1 m( xk 1) b
Difference between separations
19
Bresenham’s Line Algorithm contd…
Defining decision parameter Constant=2Δy + Δx(2b-1) Which
is independent of pixel position
p k x(d 1 d 2 )
2y. xk 2x. yk c m = Δy/Δx
Sign of pk is same as that of d1-d2 for Δx>0 (left to right sampling)
k pk (xk+1,yk+1) k pk (xk+1,yk+1)
0 6 (21,11) 5 6 (26,15)
1 2 (22,12) 6 2 (27,16)
2 -2 (23,12) 7 -2 (28,16)
3 14 (24,13) 8 14 (29,17)
4 10 (25,14) 9 10 (30,18) 23
Circle
Circle Equation:
( x xc ) ( y yc ) r
2 2 2
y yc r 2 ( xc x) 2
24
Example
x=19 then y ≈6
x=20 then y =0
25
Adjustments (To fix problems)
1. Spacing problem (2 ways):
Interchange the role of x and y whenever the absolute value of the
slope of the circle tangent > 1
Use polar co-ordinate: x xc r cos
y yc r sin
Equally spaced points are plotted along the circumference with fixed
angular step size.
step size chosen for θdepends on the application and display device.
For more continuous boundary on a raster display, set step
size at 1/r; i.e. pixel positions are approximately 1 unit apart.
2. Computation Problem:
Use symmetry of circle; i.e. calculate for one octant and use
symmetry for others.
26
Eight way Symmetry of circle
27
Mid--Point Circle Algorithm
Mid
1 ---- 2
pk 1 f circle ( xk 1 1, y k 1 )
2
1
[( xk 1) 1]2 ( y k 1 )2 r 2
2
pk 1 pk 2 ( xk 1) ( y k 1 y k ) ( y k 1 y k ) 1
2 2
34
Midpoint Circle Algorithm contd….
if pk < 0
yk+1 = y k
Pk+1 = Pk + 2xk+1+1
if pk > 0
yk+1 = yk-1
Pk+1 = Pk + 2xk+1+1-2yk+1
Note:
Incremental evaluation of 2xk+1 and 2yk+1
2xk+1 = 2xk + 2
2yk+1 = 2yk – 2 if pk >0
At start position (x0,y0) = (0,r)
2x0 = 0 and 2y0 = 2r
35
Midpoint Circle Algorithm contd….
36
Algorithm
1. Input radius r and circle center (xc , yc ) and obtain the first point on the
circumference of a circle centered on the origin as
(x0,y 0) = (0,r)
2. Calculate the initial value of the decision parameter as
P0 = 5/4 – r
3. At each xk position, starting at k = 0, perform the following test:
If pk < 0 /* next point (x k+1,yK)*/
xk+1 = xk + 1
yk+1 = yk
Pk+1 = pk + 2x k+1 + 1
37
Numerical
Numerical
Complete Plot
40
Example
Digitize the circle with r=10 and center located at origin
Initial point (x0,y0)=(0,10) 2x0 =0 2y0 =20
P0 = 1- r = 1-10 = -9
0 -9 (1,10) 2 20
1 -6 (2,10) 4 20
2 -1 (3,10) 6 20
3 6 (4,9) 8 18
4 -3 (5,9) 10 18
5 8 (6,8) 12 16
6 5 (7,7) 14 14 41
Ellipse
Elongated circle
Equation of ellipse:
d 1 d 2 constant
F1(x1 ,y1), F2(x2 ,y2)
( x x1 ) 2 ( y y1 )2 ( x x 2 ) 2 ( y y 2 ) 2 constant
General Equation
Ax 2 By 2 Cxy Dx Ey F 0
In terms of ellipse center coordinates
2
x xc y yc
2
r r 1
x y
In polar co-ordinate
x x c rx cos
y y c r y sin
42
Mid Point Ellipse Algorithm
Ellipse function
f ellipse ( x , y ) r y x r x y r x r y
2 2 2 2 2 2
0,
if ( x, y ) is inside the ellipse boundary
0,
fellipse ( x, y ) if ( x, y) is on the ellipse boundary
0,
if ( x, y) is outside the ellipse boundary
43
Mid Point Ellipse Algorithm contd…
2ry 2 x 2rx 2 y
Start from (0,ry), take x samples to
boundary between 1 and 2
Switch to sample y from boundary
between 1 and 2
(i.e whenever 2 r y x 2 r x y )
2 2
44
Mid Point Ellipse Algorithm contd…
•For increment
In the region 1 calculation; Initially:
1
p1k f ellipse ( xk 1, y k )
2 2r x 0
2
y
1
ry ( xk 1)2 rx ( yk ) 2 rx ry
2r y 2r r
2 2 2 2
2 2
2 x x y
For next sample •Incrementally:
1
p1k 1 f ellipse ( xk 1 1, yk 1 ) Update x by adding
2 2ry2 to first equation
ry ( xk 1) 1rx2 ( yk 1 ) 2 rx ry
2 2 1 2 2
and update y by
2
2 1 1
2 2 subtracting 2rx 2 to
p1k 1 p1k 2ry ( xk 1) ry rx
2 2
yk 1 yk second equation
2 2
Thus increment
2 ry xk 1 ry2 ,
2
if p1k 0
increment 2
2
ry xk
1 ry
2
2 rx
2
yk 1 , if p1k 0
45
Mid Point Ellipse Algorithm contd…
Initial value
1
p10 f ellipse 1, ry
2
2
1
ry2 rx2 ry rx2 ry2
2
1 2
p10 ry rx ry rx
2 2
46
Mid Point Ellipse Algorithm contd…
In the region 2
1
p2 k f ellipse ( xk , yk 1)
2
1
ry ( xk ) 2 rx ( yk 1) 2 rx ry
2 2 2 2
54
TRANSFORMATION
55
HOMOGENOUS FORM
56
TRANSLATION
Every point on the object is
translated by the same amount Translation
x' x tx, y' y ty vector
x1
x1'
tx
P , p' ' , T
1
y
y1
ty
P' P T
In homogeneous representation if
position P = (x, y) is translated to new
position P’= (x’, y’) then:
x'
1 0 tx x
y'
0 1 t .y P' T (tx , ty ).P
y
1
0 0 1
1
57
ROTATION
Clockwise rotation
(Negative)
Anticlockwise rotation
( positive )
58
ROTATION contd…
Coordinates of point ( x,y ) in polar form
x r cos , y r sin
x' r cos() r cos. cosr sin.sin
y' r sin() r cos.sinr sin.cos
x' x cos y sin If Co-ordinates represented as row vector, Then:
y' x sin y cos
P 'T
( R .P ) T
P 'R.P P T .R T
cos sin
R
sin cos In homogeneous co-ordinate
Composite Transformations
1 0 x r cos sin 0 1 0 x r
0 1 yr .
sin cos 0 .0 1 y
r
0 0 1 0 0 10 0 1
cos sin x r (1 cos ) y r sin
sin cos y r (1 cos ) x r sin
0 0 1
x ' s x 0 x
.
y ' 0 s y y
P'S.P
In homogeneous co-ordinate
x ' x f ( x x f ).s x ,
j
y ' y f ( y y f ).s y
x ' x. s x x f (1 s x )
y ' y .s y y f (1 s y )
x'
sx 0 x x f (1 s x )
.
y ' 0 y f (1 s y )
s y y
P ' S . P C 63
Fixed Point Scaling contd...
GENERAL SCALING DIRECTIONS
65
REFLECTION
x '
1 0 0 x
y'0 1 0 .y
1
0 0 1
1
66
REFLECTION contd…
Reflection about y-axis (x=0)
x' 1 0 0x
y '0 1 0 . y
1
0 0 1
1
67
REFLECTION contd…
Reflection about y = x
x' 0 1 0x
y '
1 0 0 . y
1
0 0 1
1
Equivalent to:
Reflection about x-axis
Rotate anticlockwise 90˚
OR
Clockwise rotation 45 ˚
Reflection with x-axis
anticlockwise rotation 45 ˚ 68
REFLECTION contd…
Reflection about y = - x
x' 0 1 0 x
y '1 0 0 . y
1
0 0 1 1
Equivalent to:
Clockwise rotation 45 ˚
Reflection with y-axis
anticlockwise rotation 45 ˚
69
REFLECTION ABOUT y=m*x+c
ASSIGNMENT 70
REFLECTION ABOUT y=mx+c
y= mx+c
Combination of translate-rotate-reflect transformation
1. First translate the line so that it passes through the
origin
1 0 0
0 1 c
0 0 1
translatio n
cos sin 0
sin cos 0
0 0 1
rotation
72
REFLECTION ABOUT y=m*x+c contd…
1 0 0
0 1 c
0 0 1
translatio n
73
REFLECTION ABOUT y=m*x+c contd…
Substituting value of tanθ, sinθ& cosθyou will get the
reflection matrix
1 m 2
2m 2 cm
2
1 m 2
1 m 2
1 m
2m m 2 1 2c
2
1 m 2
1 m 2
1 m
0 0 1
74
SHEARING
Transformation that distorts the shape of an object
Transformed object appears as if it were composed of
internal layers that had been caused to slide over each
other.
Shearing factor (Shx, Shy)
75
SHEARING contd…
76
SHEARING contd…
x- direction shear relative to other reference line y= yref
x’=x +Shx(y-yref)
y’=y x ' 1 Shx Shx . y ref x
y ' 0 1 0
. y
1 0 0 1
1
77
COMPOSITE TRANSFORMATIONS
78
COMPOSITE TRANSFORMATIONS
1 0 x f sx 0 0 1 0 x f sx 0 x f (1 sx )
S ( sx 2 , s y2 ).S ( sx1 , s y1 ) S ( s x1. sx 2 , s y1 .s y 2 )
0 1 yf . 0 sy 0 . 0 1 y f 0 sy y f (1 s y )
0 0 1
0 0 1
0 0 1
0 0 1
sx2 0 0 s x1 0 0 s x1 .sx 2 0 0
0 sy2 0.0 s y1 0 0 S y1 .s y 2 0
0 0 1
0 0 1
0 0 1
T ( x f , y f ).S (sx , s y ).T (x f ,y f ) S ( x f , y f , sx , sy )
Concatenation Properties
cos θ sin θ 0
R( θ)
sin θ cos θ 0
0 0 1
0 0 1
0 0 1 80
2-D VIEWING
81
2-D VIEWING
Window
- a world coordinate area selected for display
define what is to be viewed
View-port
An area on a display device to which a window is mapped
Define where it is to be displayed
Windows and view-port
Rectangle in standard positions, with rectangle edges
parallel to coordinate axes
Other geometric takes longer to proceed
Viewing transformation- the mapping of a world coordinate
scene to device coordinates
Transformation pipeline-
takes the object coordinates through several intermediate
coordinates systems before finishing with device
coordinates
82
VIEWING TRANSFORMATION
Transformation from world to viewport
84
VIEWING REFERENCE FRAME
87
CLIPPING
Any procedure that identifies those portions of a picture that are either
inside or outside of a specified region of a space
A clip window can be polygon or curved boundaries
World- coordinate clipping removes the primitives outside the window
from further consideration; thus eliminating the processing necessary
to transform these primitives to device space.
Clipping type- point ,line, area, curve and text clipping
APPLICATIONS
Extracting part of a defined scene for viewing
Identifying visible surfaces in 3-dimensional views
Antialiasing line segments or object boundaries
Creating objects using solid modeling procedures
Displaying a multiwindow environment
Drawing and painting operations that allow parts of a picture to
be selected for copying, moving erasing or duplication.
88
POINT CLIPPING
91
COHEN--SUTHERLAND LINE CLIPPING
COHEN
advantage of the algorithm is that it vastly reduces the number of
line intersections that must be calculated
World space is divided into regions based on the window
boundaries
Each region has a unique four bit region code
Region codes indicate the position of the regions with respect to
the window 4 3 2 1
Top Below Right Left TBRL
Region Code Legend
bit 4 : y yw
1001 1000 1010
max
bit 3 : y yw 0000
min 0001 0010
Window
bit 2 : x xw
max
bit 1 : x xw 0101 0100 0110
min
Cohen--Sutherland line clipping
Cohen
Top
op--Left Top Top
op--Right
Bottom
ottom--Left Bottom Bottom
ottom-- Right
1001 B1 B1=1001,B2=1010
1000 1010
D1
both NOT (0000)
B2 AND Operation
C1 B1 1001
A2
0000 0010 B2 1010
0001 A1 Result 1000
C2
0110 Result = NOT (0000) reject trivially
0101 0100
D2
94
ALGORITHM
1. Assign a region code for each endpoints.
2. If both endpoints have a region code 0000 trivially
accept these line.
3. Else, perform the logical AND operation for both region
codes.
3.1 if the result is NOT 0000 trivially reject the line.
3.2 else (i.e. result = 0000, need clipping)
3.2.1. Choose an endpoint of the line that is outside the
window.
96
COHEN--SUTHERLAND LINE CLIPPING
COHEN contd…
Obtain the endpoints of line P1P 2 after cohen-sutherland
clipping
ASSIGNMENT
97
A COHEN--SUTHERLAND LINE CLIPPING Numerical
COHEN
1. P1=1001 P 2=0100
2. Both (0000)NO
3. And Operation
1001
0100
Result0000
3.1 not(0000)NO
3.2 0000 yes
3.2.1 choose P1
3.2.2 Intersection with LEFT boundary
m=(5-120)/(130-0)= -0.8846
y = y 1 + m(x-x1) where x=10
y=120-0.8846(10-0)=111.15≈111
P1’= (10, 111)
3.2.3 Update region code P1’= 1000 (TOP)
3.2.4 Repeat step 2.
98
B COHEN--SUTHERLAND LINE CLIPPING Numerical
COHEN
1. P1 ’=1000 P2 =0100
2. Both (0000)NO
And Operation
1000
0100
Result0000
3.1 not(0000)NO
3.2 0000 yes
3.2.1 choose P1’
3.2.2 Intersection with TOP boundary
m=(5-120)/(130-0)= -0.8846
x = x1 + (y-y 1)/m where y= 100
x=10+ (100-111)/(-0.8846)=22.44 ≈22
P1’’= (22, 100)
3.2.3 Update region code P1’’= 0000
3.2.4 Repeat step 2.
99
c COHEN--SUTHERLAND LINE CLIPPING Numerical
COHEN
1. P1”=0000 P2=0100
2. Both (0000)NO
And Operation
0000
0100
Result0000
3.1 not(0000)NO
3.2 0000 yes
3.2.1 choose P2
3.2.2 Intersection with BOTTOM boundary
m=(5-120)/(130-0)= -0.8846
x = x1 + (y-y1)/m where y= 10
x=130+ (10-5)/(-0.8846)=124.35 ≈124
P2’= (124, 10)
3.2.3 Update region code P2’= 0000
3.2.4 Repeat step 2.
100
D COHEN--SUTHERLAND LINE CLIPPING Numerical
COHEN
Both (0000)YES
ACCEPT & DRAW
101
Liang--Barsky Line Clipping
Liang
Based on parametric equation of a line:
x = x1 + u.
u.
x
y = y 1 + u.
u.y
Similarly,, the clipping window is represented by:
Similarly
xwmin x1 + u. x xwmax
u.
0 u 1
ywmin y1 + u. y ywmax
u.
… or, (x 1,y1)
u. p k qk k = 1, 2, 3, 4
where::
where
(x 2 ,y2 )
Pk<0 infinite extension of the line proceeds from outside to inside the infinitely
extended boundary
Pk>0 infinite extension of the line proceeds from inside to outside of the infinitely
102
extended boundary
Liang--Barsky Line Clipping
Liang contd…
Trivial rejection :
Reject line with pk = 0 for some k and one qk < 0 for these k.
x2’ = x1 + u2 .x u2 ≤1
y2’ = y1 + u2.y
Calculate
qk
uk
pk
u1 --(For intersection with the boundaries to which line enters the boundary)
maximum value between 0 and u (for p k < 0), where starting value of u 1=0
u2 --(For intersection with the boundaries to which line leaves the boundary)
minimum value between u and 1 (for pk > 0), where starting value of u2 =1
103
COMPUTER GRAPHICS
EG678EX
1
CHAPTER 6- THREE DIMENSIONAL TRANSFORMATIONS
2
“The art of 3D graphics is the art of fooling the
y x '
1 0 0 tx
x
y ' 0 1 0 ty
y
z '
0 0 1 tz
z
1 0 0 0 1 1
x
z
P ' T . P
3D Rotation
y' y cos z sin z' z cos x sin
x ' x cosy sin
z ' y sin z cos x ' z sin x cos
y' x sin y cos
x' x y ' y
z ' z
x' cos sin 0 0x x'
1 0 0 0x x' cos 0 sin 0 x
y' 0 0y
y' sin cos 0 0 y
y'
0 cos sin 0 y
1 0
. . .
z' 0 0 1 0z z'
0 sin cos 0 z z '
sin 0 cos 0 z
1 0 0 0 11 1
0 0 0 11 1 0 0 0 1 1
x yz x
5
Rotation about axis parallel to co-ordinate axis
E.g. x-axis
STEPS
1. Translate the
object so as to
Step 1 coincide rotation
axis to parallel co-
ordinate axis
2. Perform the
rotation about x-
axis
Step 2 Step 3
3. Translate back
the object so as to
move rotation axis
to original position
6
General 3D Rotations
Rotation about an Arbitrary Axis
Basic
BasicIdea
Idea
1.
1. Translate
Translatethe
theobject
objectso
sothat
thatthe
therotation
rotationaxis
axispasses
passesthrough
throughthe
thecoordinate
coordinate
origin
origin
2.
2. Rotate
Rotatethe
theobject
objectso
sothat
thatthe
theaxis
axisof
ofrotation
rotation coincides
coincideswith
withone
oneof
ofthe
the
coordinate
coordinateaxes
axes
3.
3. Perform
Performthe
thespecified
specifiedrotation
rotationabout
aboutthe
thecoordinate
coordinateaxis
axis
4.
4. Apply
Applyinverse
inverserotations
rotationsto
tobring
bringthe
therotation
rotationaxis
axisback
backto
toits
itsoriginal
original
orientation
orientation
5.
5. Apply
Applythe
theinverse
inversetranslation
translationto
tobring
bringthe
therotation
rotationaxis
axisback
backto
toits
itsoriginal
original
position.
position.
Arbitrary Axis Rotation
Arbitrary Axis Rotation
Arbitrary Axis Rotation
Arbitrary Axis Rotation
Arbitrary Axis Rotation
Arbitrary Axis Rotation
Example
Find
Findthe
thenew
newcoordinates
coordinatesof
ofaaunit
unitcube
cube90º-rotated
90º-rotatedabout
about
an
anaxis
axisdefined
definedby
byits
itsendpoints
endpointsA(2,1,0)
A(2,1,0)and
andB(3,3,1).
B(3,3,1).
N T
M E
G N
SI
A Unit Cube AS
Example
Step1. Translate point A to the origin
y
1 0 0 2
B’(1,2,1) 0 1 0 1
T
0 0 1 0
A’(0,0,0)
x
0 0 0 1
z
Example
Step 2. Rotate axis A’B’ about the x axis by and angle ,
until it lies on the xz plane.
2 2 2 5
sin
22 12 5 5
1 5
cos
y 5 5
Projected point
(0,2,1) B’(1,2,1)
l 12 2 2 12 6
l
1 0 0 0
x 5 2 5
z 0
0
B”(1,0,5) 5
R x 5
2 5 5
0 0
5 5
0
0 0 1
Example
Step 3. Rotate axis A’B’’ about the y axis by and angle
until it coincides with the z axis.
1 6
sin
y 6 6
5 30
cos
6 6
l 30 6
x 0 0
(0,0,6)
6 6
B”(1,0, 5)
0
R y 1 0 0
z 6 0
30
0
6 6
0 0 0 1
Example
Step 4. Rotate the cube 90° about the z axis
0 1 0 0
1 0 0 0
Rz
90
0 0 1 0
0
0 0 1
R
T 1R
x
1
R y
1
R z
90R y
R x
T
Example
1 0 0 0 30 6
1 0 02
5 2 5 0
00 1 0 0
0 1 01
0 06 6
1 0 0 0
R
5 5 0 1 0 0
0 0 10
2 5 5 6 30 0 0 1 0
0 0 0 0
0 0 0
1 5 5 6 6 0 0 0 1
0 0 0 1
0 0 0 1
30 6 1 0 0 0
0 0 5 2 5
1 0 0 2
6 6 0
00 1 0 1
0 1 0 0 5 5
6 0 30 2 5 5 0 0 1 0
0 0 0
6 6 5 5 0 0 0 1
0 0 0 1 0 0 0 1
0 .166 0.075 0. 983 1. 742
0 .742 0 .667 0. 075 1.151
0 .650 0 .741 0.167 0.560
0 0 0 1
Example
Multiplying R(θ) by the point matrix of the original cube
P
R P
x ' s x 0 0 0 x s x 0 0 0
y ' 0 0 sy 0 0
sy 0 0 y S ( s , s , s )
.
0 0
x y z
z ' 0 0 sz 0 z 0 sz
1 0 0 0 1 1 0 0 0 1
Fixed Point Scaling
x ' sx 0 0 (1 s x ) x f x
y' 0 sy 0 ( 1 s y ) y f y
.
z ' 0 0 sz (1 sz ) z f z
1 0 0 0 1 1
Reflection in xy plane
x’ = x
y’ = y
z’ = -z Note :
yz plane
Matrix is as x -x
&
zx plane
y -y
1 0 0
0
0 1 0 0
RFz
0 0 1 0
0 0 0 1 22
3-D SHEAR
x '
1 0 a 0x
Z-axis shear y'
0 1 b 0
y
x’ = x + a.z .
z '
0 0 1 0z
y’ = y + b.z
z’ = z 1 0 0 0 11
23
3-D OBJECT REPRESENTATION
24
3-D OBJECT REPRESENTATION
Most commonly used representation for 3-d object
Speeds up the surface rendering
wired frame applications can be displayed quickly to
give a general indication of surface structure.
Greater the number of smaller surfaces, better the
approximations
Polygon Surface
Approximation of
Cylinder
3-D OBJECT REPRESENTATION
Data Requirements for Polygon Surface Representation
Vertex Table
Edge Table
Polygon Surface Table
27
Guidelines to Generate Error Free Table
Surface Normal N to the plane Ax+By+Cz+D=0 has the Cartesian components (A,B,C)
29
3-D Object Representation
Two sides of a surface (Inside or Outside the Plane Surface)
For right handed cartesian system (i.e. if vertices are taken in
Counter-Clockwise order to evaluate A,B,C and D)
z
30
Projections
Transformation that changes a point in n-dimensional coordinate system
into a point in a coordinate system that has dimension less than n.
TYPES OF PROJECTION
Parallel Projection
Perspective Projection
PARALLEL PROJECTION
Orthographic
Oblique
31
PARALLEL PROJECTION
1. Parallel Projection
- Coordinate positions are transformed to view plane along parallel
lines (projection lines)
- Preserves relative proportions of objects
- Accurate views of various sides of an object are obtained.
- Doesn’t give realistic representation of the appearance of the 3-D
object
- Types
- Orthographic- when the projection is perpendicular to the view
plane. Used to produce Front, Side and Top view of an object
- Oblique – when the projection is not perpendicular to the view plane
p2 View Plane
p1 P2 ’
Parallel Projection
PERSPECTIVE PROJECTION
- Coordinate positions are transformed to view plane along lines
(projection lines) that converges to a point called projection
reference point (center of projection)
- Produce realistic view
- Does not preserve relative proportions
- Equal sized object appears in different size according as distance from
view plane
P2 View Plane
P1
P 2’ View Plane
P1’
Projection Projection
Reference Point
Reference Point
ORTHOGRAPHIC PROJECTION OF A POINT ON TO A VIEW PLANE
x p=x
yp=y
Note: Z value is preserved for the depth information needed in depth culling and
visible surface determination procedure
yv
(x,y,z)
(x,y)
xv
zv
Oblique Projection
Oblique projection vectors are specified by two angles
(x,y) are also the orthographic co-ordinates on view plane
and Ø
Projection Matrix Calculation yv
(xp,y p)
xp = x + L.cosØ
yp = y + L.sinØ α L
Z Z (x,y,z)
tanα ,L , L ZL1 Ø xv
L tan
Coordinates
1 (x,y)
L1 (x,y,z) are
tan viewing
coordinates View
xp = x + z(L1cosØ) Plane
yp = y + z(L1sinØ)
Final projection matrix for producing parallel projection will be zv
1
0 L1 cos 0
0 1 L1 sin 0 Orthographic projection: When L 1 = 0
M parallel xp = x
0 0 0 0 yp = y
0
0 0 1
35
Perspective Projection
P = (x,y,z)
Projection vectors meet at
projection reference point zprp
along the zv axis (xp,yp,zvp)
x’ = x – xu
y’ = y – yu zv
z’ = z – (z-zprp)u
u takes value between 0 and 1
zvp zprp
P’(x’,y’,z’) represents any point View
along the projection line
Plane
On view plane
z’ = zvp ; therefore
u = (zvp - z)/(zprp – z)
thus projection transformation
equations are
z prp zvp d p xh
1 0 0 0
x p x
z z x
z z
x
prp prp y 0 1 0 0
y
z prp zvp d p h .
y p y
z z y
z z
zh
0 0 zvp / d p zvp ( z prp / d p ) z
prp prp
h 0
0 1 / d p z prp / d p 1
Where, h = (zprp –z)/dp
and xp= xh/h, yp = yh/h
36
Perspective projection
Vanishing Point: a set of parallel lines that are not parallel to view
plane are projected as converging lines that appear to converge at a
point called vanishing point y
•a set of parallel lines that are parallel to view plane are projected as
parallel lines
x
•More than one set of parallel lines form more than one vanishing
z
points in the scene
•We can control the number of principal vanishing point to one, two or Vanishing
Point
three with the orientation of projection plane and classify as one, two
or three point perspective projection
z-axis
X-axis
vanishing
vanishing
Point
Point
37
3-D Viewing Pipeline
38
VISIBLE SURFACE DETECTION
39
Visible Surface Detection Method
Also called hidden surface elimination method
Considerations
Memory
Time
Object Type
Two approaches
Depends upon object definition or projected image
Object-space
Deals with object definition directly
Compares objects and parts of them to each other to
determine the visible object or its parts.
E.g. Line Display algorithms in wire-frame display
40
Back Face Detection Method (Plane Equation Method)
Fast and simple object-space method
Based on “inside” or “outside” test
Test whether view point is inside or outside the polygon surface
Point (x’,y’,z’) is “inside” a polygon surface with plane parameters A,B,C,D if
Ax+By+Cz+D<0 i.e. the polygon surface must be a back face.
Alternatively
1. Make the center of object at origin
2. Calculate the value of D of the plane by taking three points in anticlockwise
direction.
3. If D<=0 the surface is visible
D>0 the surface in invisible. N
Consider Normal Vector N = (A,B,C) to
polygon surface and Vector V in viewing direction
The polygon is back face if: V.N>0
If V.N<0 Visible Surface
V
If object description is converted to viewing
co-ordinates then, V is along zv axis i.e V = (0,0,Vz),
then:
V.N = VzC
Fig: Vector V in the Viewing
i.e V.N > 0 VzC
Direction and a back face normal
So we need to consider only sign of C Vector N of a polyhedron
41
Back Face Detection Method (Plane Equation Method)
For each pixel position (x,y) on projection plane, object depths can be compared
by comparing z-values as each (x,y,z) position on a polygon surface corresponds to
the orthographic projection point (x,y) on projection plane
Usually applied to scene containing only polygon surfaces; but possible to apply for
non-planar surfaces
DRAWBACK: Deals only with opaque surface but not with transparent surface
i.e. it can only find one visible surface at each pixel position
43
Depth Buffer Method- Algorithm
1. Initialize the depth buffer and refresh buffer so that for
all buffer positions (x,y),
depth(x,y) = 0, refresh(x,y) = Ibackgrnd
z' z B
C x X’
45
A-Buffer Method
Extension of depth buffer method
represents Antialiased, area-average, accumulation-buffer
The depth buffer is expanded so that
each position in the buffer can reference
a linked list of surfaces foreground
background
thus enabling more than one opaque
transparent
surface
surface intensity consideration surface
DATA STRUCTURE
A. Edge table containing
Coordinate endpoints for each line in a scene
Inverse slope of each line
Pointers into polygon table to identify the surfaces bounded by each line
B. Surface table containing
Coefficients of the plane equation for each surface
Intensity information for each surface
Pointers to edge table
Note :
The edges are sorted in order of increasing x
Define flags for each surface to indicate whether a position is inside or outside the surface
48
SCAN LINE ALGORITHM
I. Initialize the necessary data structure
1. Edge table containing end point coordinates, inverse slope
and polygon pointer.
2. Surface table containing plane coefficients and surface
intensity
3. Active Edge List
4. Flag for each surface
50
Scan-Line Method (contd…)
yv
E
B F
Scan Line 1
A Subdividing line
Scan Line 2
S1 S2
Scan Line 3
Scan Line 4
H C
D
G
xv
51
Illumination Models & Surface Rendering Methods
52
Illumination and Rendering
55
Ambient Light
N
Diffuse Reflection
When cosƟis
+ve surface is illuminated
-ve light source is behind the surface
Diffuse Reflection
When cosƟis
+ve surface is illuminated
-ve light source is behind the surface
Ambient + Diffuse
ka I a kd Il ( N
L) if N
L 0
I
ka Ia if N
L 0
Near total reflector reflects most of light over a range of positions close to
the direction
63
Specular Reflection
cosns
Specular Reflection
R L (2N
L)N
R (2N.L)N L
Specular Reflection
Simplified Phong model using halfway vector
H is constant if both viewer and the light source are sufficiently far
from the surface
L V
H
| L V |
Combined Diffuse and Specular Reflection
I I diff I spec
k a I a k d I l ( N . L ) k s I l ( N . H ) n s
Multiple light sources (n light sources)
n
I k a I a I li k d ( N .Li ) k s ( N .H i ) ns
i 1
68
Intensity Attenuation
Intensity attenuation must be considered for producing realistic lighting
effects.
Intensity of radiant energy at a point d distance far from source is attenuated
by 1/d2
attenuation factor 1/d2 produces too much intensity variation
f (d ) 1
a 0 a 1 d a 2 d 2
a0 can be adjusted to prevent f(d) from becoming too large when d is
very small
Magnitude of attenuation function is limited to 1 as
1
f ( d ) min
1,
a a d a d 2
0 1 2
The Phong illumination model considering attenuation is:
n
I k a I a f ( d i ) I li k d ( N . L i ) k s ( N . H i ) n s
i 1
69
Color Consideration in Phong Illumination model
For RGB description, each color in a scene is expressed in terms of R,G and B
components
Various methods:
Described by considering the RGB components for e.g. (k dR,kdG ,kdB) of
diffuse reflection coefficient vector
e.g. For blue light (kdR=k dG=0)
n
I B k aB I aB f ( d i ) I lBi k dB ( N . Li ) k sB ( N . H i ) n s
i 1
n
I B k a S dB I aB f ( d i ) I lBi k d S dB ( N . Li ) k s S sB ( N . H i ) ns
i 1
Described by specifying wavelength for a color specification. This
specification is useful to specify color as more than three components
n
I B ka S dI a f (d i ) I li kd S d( N .Li ) ks S s( N .H i )ns
i
1
70
Transparency
Transparent surface produces both reflected and transmitted
light
I = (1-kt)Irefl + ktItrans
where (1-k t) is opacity factor
The shadow area for all light sources are determined and
these shadows could be treated as a surface pattern arrays
Light
direction
73
Assigning Intensity Level
The intensity calculated by illumination model must be converted to the
allowable intensity of a particular graphics system
The difference between intensities 0.20 and 0.22 should be perceived same as
that of 0.80 and 0.88
The intensity level in a monitor should be spaced so that the ratio of successive
intensities is constant
for n+1 successive intensity levels
I1/I0=I2/I1=……..In/In-1 = r
Ik = r kI0
also In = 1 r = (1/I0)1/n
so, Ik = I0(n-k)/n
Methods:
1. Constant Intensity Shading
2. Gouraud Shading
3. Phong Shading
75
Constant Intensity Shading or Flat Shading
-Fast and simple method for rendering an object
with polygon surface
-Each polygon shaded with single intensity
calculated for the polygon
PROCEDURE
1. Take a point on the object surface and
calculate the intensity
2. Render the surface with same intensity
throughout the surface
3. Repeat above procedure for each polygon
surface
ASSUMPTIONS
1. Object is a polyhedron
2. light sources should be sufficiently
(i.e. N.L and attenuation function are constant)
3. Viewing position is sufficiently far
(i.e. V.R is constant over the surface)
PROCEDURE
1. Determine the average unit normal vector at each polygon
vertex
2. Calculate each of the vertex intensities by applying an
illumination model
3. Linearly interpolate the vertex intensities over the polygon
surface
77
Gouraud Shading (contd…)
N2 N3
Average Unit Normal: Obtained by averaging the
surface normals of all polygons sharing the vertex
n N1
N k
N v k 1
n
N4 V
N
k 1
k y 3
Intensity interpolation: 1
P scan line
Along the polygon edges are obtained by 4 5
interpolating intensities at the edge ends
2
Recursive calculation along the edge
y 4 y 2 y y 4 I ' I 2
I I 1
I4 I1 1 I2 y 1 y 2 x
y 1 y 2 y 1 y 2
dy
1
dx
x a
x0 , y 0
a, a
|m| <1 for R2 so sample to x
Since curve is symmetric about the point
(x0,y0) we may take this point as starting point
and calculate the pixels in R2 and find
symmetric points in R1 by interchanging x and y
The conjugate curve can be drawn by taking
changing x to –x and y to -y
x.y = a
f ( x , y ) x . y a
0 ,
if ( x , y ) is below the curve
f ( x , y ) 0 , if ( x , y ) is on the curve
0 , if ( x , y ) is above the curve
x. y = a
1
pk f ( x k 1, y k )
2
( xk 1).( y k 1 ) a
2
1
pk 1 f ( xk 1 1, yk 1 )
2
1
[( xk 1) 1].( yk 1 ) a p 0 f ( x 0 1 , y 0 1 )
2 2
pk 1 pk ( xk 1)( y k 1 yk ) ( y k 1 1 ) ( x 0 1 ).( y 0 2 ) a
1
2 ( a 1 ).( a 1 ) a
pk ( y k 1 ) if pk 0 2
2 a 1
pk xk yk 5 otherwise
2 2
2
Straight line (y = m.x + b)
For |m|<1
f ( x, y) m.x y b
pk f ( xk 1, y k 1 )
2
1
m.( xk 1) ( y k ) b
2
pk 1 f ( xk 1 1, yk 1 1 )
2
1
m.[( xk 1) 1] ( yk 1 ) b
2
1
pk 1 pk m ( yk 1 yk ) p 0 f ( x 0 1 , y 0 )
2
pk m if pk 0
m .( x 0 1 ) ( y 0 1 ) b
pk m 1 otherwise 2
m 1
2
For the curve
x
y Ae
At boundary
dy
A . e . x
dx
dy
1
dx
ln( A . )
x
Two regions
Region 1 |m|>1
Region 2 |m|<1
f ( x, y) Ae x y
0, if ( x, y) is above the curve
f ( x, y) 0, if ( x, y ) is on the curve
0, if ( x, y ) is below the curve
Region 1
ln(A.)
x
1
p1k f ( x k , y k 1 )
1
2
( x k )
Ae ( y k 1 )
2
/ 2 . x k
Ae .e y k 1 1
1 p10 f ( x0 , y 0 1 )
p1k 1 f ( x k 1 , y k 1 1 ) 1
2
1
2 Ae
( 0
2
)
( A 1 )
( x
( y k 1 ) 1
k 1 ) / 2
Ae 2 A (e 1 ) 1
/ 2
Ae . e . x k 1 y k 2
2
. x k 1
Ae .e yk
2
1
p 2 k 1 f ( x k 1 1 , y k 1 1 ) p 2 0 f ( x 0 1, y 0 )
2 2
Ae ( x k 2 ) ( y k 1 1 ) 1
2 Ae .e . x 0 y 0
Ae 2 .e . x k y k 1 1 2
2
p 2k 1 p 2k A(e 1).e .( xk 1) ( yk 1 yk )
.( x k
p 2k A(e 1).e 1)
if p2 k 0
p 2k A(e 1).e .( xk 1) 1 otherwise