Chapter 3 &4 CG
Chapter 3 &4 CG
viewing
DEP ’ T: CSE I V
C O UR SE: C O MPUTER G R A PHI CS
I NSTR UC TO R : W I DA SSI E (MSC )
MEKEL L E I NSTI TUTE O F TEC HNO LO GY- MEKEL L E UNI V ER SI TY
Two Dimensional Viewing
The viewing Pipeline
Construct the scene in world coordinate (WC) using the output primitives such
as line and circle and their attributes.
Obtain a particular orientation for the window, set up a two-dimensional viewing
coordinate (VC) system in the world coordinate plane, and define a window in the
viewing coordinate system.
The Viewing Pipelined
The viewing – coordinate reference frame is used to provide a method for
setting up arbitrary orientations for rectangular windows. Once the viewing
reference frame is established, we can transform descriptions in world
coordinates (WC) to viewing coordinates (VC).
We then define a viewport in normalized coordinates (NVC) (in the range
from 0 to 1) and map the viewing – coordinate description of the scene to
normalized– coordinate.
The Viewing Pipelined
At the final step, all parts of the picture that lie outside the viewport are
clipped, and the contents of the viewport are transferred to device coordinates
(DC).
Mwc, vc = R.T
Where T is the translation matrix and R is the rotation matrix
The Viewing Pipelined
1 0 –x0
T(–x0, –y0) = 0 1 –y0
0 0 1
cos sin 0
R(–) = – sin cos 0
0 0 1
Where is the orientation angle between the xworld and xview axes.
The Viewing Pipelined
Window to Viewport Coordinate Transformation
The Viewing Pipelined
Window to Viewport Coordinate Transformation
Solving these expressions for the viewport position (xv, yv), we have
xv = xvmin + (xw – xwmin)sx
yv = yvmin + (yw – ywmin)sy ……………………………………………..
Equation 1
24
Cohen-Sutherland Line
Clipping
1001 1000 1010
Window
0001 0000 0010
26
Cohen-Sutherland Line
Clipping
Any lines that has a region code of 0000 for both endpoint are completely inside, we save
these lines.
Any lines that have a 1 in the same bit position in the region-codes for each endpoint are
completely outside and we reject these lines.
A method that can be used to test lines for total clipping is to perform the logical AND
operation with both region codes. If the result is not 0000, the line is completely outside the
clipping region.
27
Cohen-Sutherland Line
Clipping
p1 0100
p2 1001
p3 0001
p4 0100
p1 AND p2 =0000
p3 AND p4 =0000
28
Cohen-Sutherland Line
Clipping
For a line with endpoint (x1, y1) and (x2, y2)
29
Cohen-Sutherland Line
Clipping (Example)
30
Liang-Barsky Line Clipping
Algorithm
For a line segment with endpoints (x1, y1) and (x2, y2), we can describe the line with parametric
form:
x = x1 + u x
y = y1 + u y, 0 u 1
where x= x2 – x1 and y= y2– y1
The point-clipping condition in the parametric form:
xwmin x1 + u x xwmax
ywmin y1 + u y ywmax
31
Liang-Barsky Line Clipping
Algorithm
xwmin x1 + u x xwmax
ywmin y1 + u y ywmax
Each of these four inequalities can be expressed as:
upk qk, k=1, 2, 3, 4
where the parameters p and q are defined as:
p1 = – x, q1 = x1 – xwmin
p2 = x, q2 = xwmax – x1
p3 = – y, q3 = y1 – ywmin
p4 = y, q4 = ywmax – y1
32
Liang-Barsky Line Clipping
Algorithm
Any lines that is parallel to one of the clipping boundaries has pk=0 for the value of k
corresponding to that boundary where k = 1, 2, 3, 4 correspond to left, right, bottom and top
boundaries respectively.
If, for that value of k, we also find qk < 0, then the line is completely outside the boundary and
can be eliminated from further consideration.
If qk 0, then the line is inside the parallel clipping boundary.
33
Liang-Barsky Line Clipping
Algorithm
If pk < 0, the infinite extension of the line proceeds from the outside to the inside of the infinite
extension of this clipping boundary.
If pk > 0, the line proceeds from the inside to the outside.
For a nonzero value of pk (pk 0), we can calculate the value of u that corresponds to the point
where the infinitely extended line intersects the extension of boundary k as:
u = q k / pk
34
Liang-Barsky Line Clipping
Algorithm
For each line, we can calculate values for parameters u1 and u2 that define that part of the line
that lies within the clip rectangle.
The value of u1 is determined by looking at the rectangle edges for which the line proceeds
from outside to the inside (pk < 0).
For these edges, we calculate rk = qk / pk.
The value of u1 is taken as the largest of the set consisting of 0 and the various values of r.
35
Liang-Barsky Line Clipping
Algorithm
The value of u2 is determined by examining the boundaries for which the line proceeds from
inside to the outside (pk > 0).
A value of rk is calculated for each of these boundaries, and the value of u2 is the minimum of
the set consisting of 1 and the calculated r values.
36
Liang-Barsky Line Clipping
Algorithm
If u1 > u2, the line is completely outside the clip window and it can be discarded.
Otherwise, the endpoints of the clipped line are calculated from the two values of parameter u.
37
Liang-Barsky Line Clipping
Algorithm
The Liang-Barsky Line Clipping Algorithm can be summarized as follows:
1.For each line, calculate the parameters p, q and r:
p1 = – x, q1 = x1 – xwmin, r1 = q1/ p1
p2 = x, q2 = xwmax – x1 , r2 = q2/ p2
p3 = – y, q3 = y1 – ywmin, r3 = q3/ p3
p4 = y, q4 = ywmax – y1, r1 = q4/ p4
38
Liang-Barsky Line Clipping
Algorithm
The Liang-Barsky Line Clipping Algorithm can be summarized as follows:
2.Determine the value of u1 is by looking at the rectangle edges for which the line proceeds from
outside to the inside (pk < 0). The value of u1 is taken as the largest of the set consisting of 0 and
the various values of r.
3. Determine the value of u2 by examining the boundaries for which the line proceeds from inside
to the outside (pk > 0). A value of rk is calculated for each of these boundaries, and the value of
u2 is the minimum of the set consisting of 1 and the calculated r values.
39
Liang-Barsky Line Clipping
Algorithm
The Liang-Barsky Line Clipping Algorithm can be summarized as follows:
4.If u1 > u2, the line is completely outside the clip window and it can be discarded.
5. Otherwise, the endpoints of the clipped line are calculated from the two values of parameter u.
If u2 <1 x= x1+u2 x, y = y1 +u2 y
Otherwise x = x2, y = y2
If u1 >0 x= x1+u1 x, y = y1 +u1 dy
Otherwise x = x1, y = y1
40
Liang-Barsky Line Clipping Algorithm
(Example)
41
Area Clipping (polygons)
To clip a polygon, we cannot directly apply a line-clipping method to the individual polygon
edges because this approach would produce a series of unconnected line segments as shown in
figure
For polygon clipping, we require an algorithm that will generate one or more closed areas that
are then scan converted for the appreciate area fill
The output of a polygon clipper should be a sequence of vertices that defines the clipped
polygon boundaries
42
Sutherland-Hodgman Polygon Clipping
43
Sutherland-Hodgman Polygon Clipping
Lift Right
Clipper Clipper
Bottom
At each step, a new sequence of output Clipper
vertices is generated and passed to the
next window boundary clipper.
Top
Clipper
44
Sutherland-Hodgman Polygon Clipping
There are four possible cases when processing vertices in sequence around the
perimeter of a polygon.
As each pair of adjacent polygon vertices is passed to a next window boundary
clipper, we make the following tests
45
Sutherland-Hodgman Polygon Clipping
1.If the first vertex is outside the window boundary and the second vertex is
inside
Then , both the intersection point of the polygon edge with the window
boundary and the second vertex are added to the output vertex list.
46
Sutherland-Hodgman Polygon Clipping
47
Sutherland-Hodgman Polygon Clipping
3.If the first vertex is inside the window boundary and the second vertex is
outside.
Then, only the edge intersection with the window boundary is added to the
output vertex list.
48
Sutherland-Hodgman Polygon Clipping
49
Sutherland-Hodgman Polygon
Clipping (Example)
We illustrate this algorithm by processing the area in figure against the left window
boundary.
Vertices 1 and 2 are outside of the boundary.
Vertex 3, which is inside, 1' and vertex 3 are saved.
Vertex 4 and 5 are inside, and they also saved.
Vertex 6 is outside, 5' is saved.
Using the five saved points,
we would repeat the process for the next window boundary.
50
Sutherland-Hodgman Polygon
Clipping
51
Weiler-Atherton Polygon Clipping
This algorithm was developed for identifying visible surfaces, and can be used to
clip a fill area that is either a convex polygon or a concave polygon.
The basic idea of this algorithm is that instead of proceeding around the polygon
edges as vertices are processed, we will follow the window boundaries.
The path we follow depends on:
polygon-processing direction (clockwise or counterclockwise)
The pair of polygon vertices: outside-to-inside or an inside-to-outside.
52
Weiler-Atherton Polygon Clipping
53
Weiler-Atherton Polygon Clipping
(Example)
54
Text Clipping
There are several techniques that can be used to provide text clipping in a
graphics packages.
The choice of clipping method depends on how characters are
generated and what requirements we have for displaying character strings.
55
Text Clipping
All-or-none string-clipping
If all of the string is inside a clip window, we keep it.
Otherwise the string is discarded.
56
Text Clipping
All-or-none character-clipping
Here we discard only those characters that are not completely inside the
window
57
Text Clipping
58
3D Viewing Pipeline
59
Primitives
Object space
Modeling Transformation
World space
Viewing Transformation
Camera space
3D-Clipping
Projection
Normalized view space
Scan conversion, Hiding
Image space,
Device coordinates
Image
60 Contents
1. 3D Coordinate System
2. Vector Fundamentals
3. 3D Homogeneous Coordinate
4. 3D Geometric Transformations
5. 3D Inverse Transformations
6. 3D Coordinate Transformations
7. Composite transformations
61 3D Coordinate System
3D coordinate system consists of a reference point called
origin and three mutually perpendicular passing through
origin. y axis
z
x
is
At a distance of x units from YZ-plane y
At a distance of y units from XZ-plane
At a distance of z units from XY-plane
P
z
x
z axis x axis
63 Contents
1. 3D Coordinate System
2. Vector Fundamentals
3. 3D Homogeneous Coordinate
4. 3D Geometric Transformations
5. 3D Inverse Transformations
6. 3D Coordinate Transformations
7. Composite transformations
64 Vector Fundamentals
In 3D there are 3 natural coordinate vectors I, J and K having
unit along X, Y and Z axis respectively.
y axis
z axis x axis
65 Vector Fundamentals
If P(x0,y0,z0) and Q(x1,y1,z1) are two points in space then
PQ = (x1 – xo)I+ (y1 – yo)J+ (z1 – zo)K
y axis
u u -v
v
u-v
Vector Fundamentals
68
u 2u
-3u
70 Vector Fundamentals
IxJ=K
JxK=I
I x K = -J
or K x I = J
K I
Right-Hand
Reference System
74 Contents
1. 3D Coordinate System
2. Vector Fundamentals
3. 3D Homogeneous Coordinate
4. 3D Geometric Transformations
5. 3D Inverse Transformations
6. 3D Coordinate Transformations
7. Composite transformations
75 3D Homogeneous Coordinates
Similar to the 2-D situation we can use homogeneous
coordinates for 3-D transformations - 4 coordinate
y axis
column vector
y
All transformations can
then be represented
as matrices x P
y
P(x, y, z) =
z
1 z
x
z axis x axis
76 3D Homogeneous Coordinates
Transform a point:
px
M py d
pz
vx
M vy
vz
1. 3D Coordinate System
2. Vector Fundamentals
3. 3D Homogeneous Coordinate
4. 3D Geometric Transformations
5. 3D Inverse Transformations
6. 3D Coordinate Transformations
7. Composite transformations
3D Geometric Transformations
82
Translation
Scaling
Rotation
Reflection
Shearing
83 3D Geometric Transformations
Once we have an object described, transformations are used
to move that object, scale it and rotate it
Geometric Transformations
–Translation
–Scaling
–Rotation
–Reflection
–Shearing
85 3D Geometric Translation
(x, y, z)
(x’, y’, z’)
Translated Position
87 3D Geometric Translation
The translation of a point P(x,y,z) by (tx, ty, tz) can be
written in matrix form as:
1 0 0 tx x x
0 y
1 0 ty y
T v P ' P
0 0 1 tz z z
0 0 0 1 1 1
Geometric Transformations
–Translation
–Scaling
–Rotation
–Reflection
–Shearing
89 3D Geometric Scaling
Scaling is the process of expanding or compressing the
dimensions of an object determined by the scaling factor.
90 3D Geometric Scaling
To scale a point in three dimensions by sx, sy and sz simply
calculate the new points as follows:
x’ = sx*x y’ = sy*y z’ = sz*z
Scaled Position
91 3D Geometric Scaling
The scaling of a point P(x,y,z) by scaling factors Sx, Sy and
Sz about origin can be written in matrix form as:
P Ssx, sy, sz( P ) where
sx 0 0 0 x x
0 sy 0 y
0 y
Ssx, sy, sz P ' P
0 0 sz 0 z z
0 0 0 1 1 1
x sx 0 0 0 x sx x
y
0 sy 0 0 y sy y
such that '
z 0 0 sz 0 z sz z
1 0 0 0 1 1 1
Geometric Transformations
–Translation
–Scaling
–Rotation
–Reflection
–Shearing
93 3D Geometric Rotations
When we performed rotations in two dimensions we only
had the choice of rotating about the z axis
In the case of three dimensions we have more options
Rotate about x – pitch
Rotate about y – yaw
Rotate about z - roll
3D Geometric Rotations
94
Roll is known as the rising or dipping of the airplane's wing. The movement is done
about Longitudinal axis. The Ailerons controlling the roll are located on the trailing
edge of both wings.
Pitch refers to the movement of the airplane's nose either up or down. It movement is
done about Lateral axis. The elevator controls the pitch is also located on the rear of
the aircraft on the tail, along with the rudder.
Yaw allows the airplane to move towards the left or right while in flight. The
movement is done about a ventricle axis. The yaw is controlled by the rudder located
in the rear of the aircraft on the tail.
x
x
z
104 Contents
1. 3D Coordinate System
2. Vector Fundamentals
3. 3D Homogeneous Coordinate
4. 3D Geometric Transformations
5. 3D Inverse Transformations
6. 3D Coordinate Transformations
7. Composite transformations
Inverse Transformations
105
1
Inverse Translation: Tv T v
1
Inverse Scaling: S sx,sy,sz S1/ sx,1/ sy,1/ sz
1
Inverse Rotations: R , p R , p
1
M plane M plane
Inverse Reflections:
106 Contents
1. 3D Coordinate System
2. Vector Fundamentals
3. 3D Homogeneous Coordinate
4. 3D Geometric Transformations
5. 3D Inverse Transformations
6. 3D Coordinate Transformations
7. Combining transformations
3D Coordinate Transformations
107
Coordinate Translation: 1
T v Tv T v
1
Coordinate Scaling: S sx,sy,sz S sx,sy,sz S1/ sx,1/ sy,1/ sz
1
Coordinate Rotations: R , p R , p R , p
1
M plane M M plane
Coordinate Reflections: plane
111 Contents
1. 3D Coordinate System
2. Vector Fundamentals
3. 3D Homogeneous Coordinate
4. 3D Geometric Transformations
5. 3D Inverse Transformations
6. 3D Coordinate Transformations
7. Composite transformations
113 Composite Transformation
1 0 0 a sx 0 0 0 1 0 0 a
0 1 0 b 0 sy 0 0 0 1 0 b
S sx, sy, sz ( a ,b ,c )
0 0 1 c 0 0 sz 0 0 0 1 c
0 0 0 1 0 0 0 1 0 0 0 1
sx 0 0 a.sx a
0 sy 0 b.sy b
0 0 sz c.sz c
0 0 0 1
118 Exercise 2
Where
1 0 0 0
0 cos sin 1 0
R1 , I 1
0 sin 1 cos 1 0
0 0 0 1
b
sin 1
b2 c2
c
cos 1
b2 c2
R(0,0,a2+b2+c2)
Step2: Rotate V’ about Y-axis by an angle of
Q’(0,0,b2+c2)
– 2 , so that V’ aligns with +ve Z axis. V”=
V’
R ,I(V) a P’(0,b,c)
1
Q(a,0,b2+c2) C P(a,b,c)
Let Q’ be projection of Q(a,0,b2+c2) on Z 2 V
axis, Q’↔(0,0, b2+c2 ). If 2 be the angle
that OQ’ makes with +ve Z-axis, then from
O B
OQQ’ a Y axis
sin 2 A
a2 b2 c2
b2 c2
cos 2 X axis
a2 b2 c2
124 Exercise 3
0 0 0 1 1
b2 c2 a
2 0 0 a
a b2 c2 a2 b2 c2
0
0 1 0 0 .
b c
2 2
a b2 c2
0 0
a2 b2 c2 a2 b2 c2 1
0 0 0 1
a b2 c2 a b2 c2
2 2 2
0
a b c
0
0
2
a b c 2 2
a b c
2 2 2
2 2 2
a b c 1
1
Exercise 3
126
ab ac
a V 0
V 0 0 1 0 0 0 V V
V c b
0 0 0 c b
0 b2 c2
a0 1 0
0 .
b c where
0
0 0 0 a b c V a 2 b2 c2
V V 0
0
0 0 1 0 0 0 1 V V V
0 0 0 1
Exercise 3
127
Corollary I: If both b = c =0
then 0, V a and 2 900
a
0 0 0
a
AV , K a0 1 0 0 .
0 0 0
a
0 0 0 1
R 2 , J
Exercise 3
128
A 1V , K ( R 2 , J . R1 , I ) 1
1 1
R 1 , I .R 2 ,J
T T
R 1 , I .R 2 ,J
T
( R 2 , J . R1 , I )
T
( AV , K )
T
A V ,K
Exercise 4
129
Find transformation AV,N that aligns vector V = 2I – J – K with
another N = I+J+K.
2 2 2 1 1
0 0 0
6 6 3 2 3 2 3
2 1 1 1 1
0 AK ,N 0 0
AV ,K 2 6 2 6 2 2
2 1 1 1
1 1 0
0
2 6 2 6 3 3 3
0 0 0 1 0 0 0 1
4 1 1
0
18 18 18
1 2 1 2 1
0
AV ,N 18 18 2 18 2
1 2 1 2 1
0
18 18 2 18 2
0 0 0 1
130 Exercise 5
Thus P5 = T-1-P0(P4)
= T-1-P0.A-1V,K(P3)
= T-1-P0.A-1V,K. R,K(P2)
2 1 1 1 1 1 2
0 1
2 2 2 2 2 2
1 2 2 2 2 2 2
2 2 2 4 2
1
2 2
R450 , L
1
4 4 4 A' B' C ' D' 4 4 2
2 2 2 2 2 2 2 2
2 4 2
2 4 4 4 0
0 0 0 1 4 4 2
1 1 1 1
134
Thank you!