You are on page 1of 69

Computer Graphics and Visualization

– UNIT V
Geometric Objects & Transformations
By K. Satyanarayan Reddy, Professor
Dept. of CSE,
Cambridge Institute of Technology,
Bangalore - 36
TRANSLATION, ROTATION, AND SCALING
Introduce Standard Transformations
Rotation
Translation
Scaling
Shear
Derive homogeneous coordinate transformation matrices
Learn to build arbitrary transformation matrices from simple
transformations

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 2
Bangalore - 560036.
TRANSLATION, ROTATION, AND SCALING cont’d….
A transformation maps points to other points and / or vectors to
other vectors

v=T(u)

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 3
Bangalore - 560036.
TRANSLATION, ROTATION, AND SCALING cont’d….
Line preserving Characteristic of many physically
important transformations
Rigid Body Transformations:
ROTATION, TRANSLATION,
Non-Rigid Body Transformations:
SCALING and SHEAR
Importance in graphics is that only the endpoints of
line segments need to be transformed and then the
implementation draws line segment between the
transformed endpoints
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 4
Bangalore - 560036.
Pipeline Implementation

(from Application Program)


T
frame
T(u) buffer
u
Transformation Rasterizer
v
T(v) T(v)
T(v)

u T(u) T(u)
vertices vertices pixels
v
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011
Bangalore - 560036.
5
Notation
We will be working with both coordinate free representations of
Transformations and Representations within a particular
frame
P,Q, R: points in an affine space
u, v, w: vectors in an affine space
α, β, γ: scalars
p, q, r: representations of points
- Array of 4 scalars in homogeneous coordinates
u, v, w: representations of points
- Array of 4 scalars in homogeneous
coordinates

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 6
Bangalore - 560036.
Translation
Move (translate, displace) a point to a new location

P’
z

d
y
P
x
Displacement determined by a vector d
There are 3 degrees of freedom
P’= P + d

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 7
Bangalore - 560036.
How many ways can Translation be done?
Although a point can be moved to a new location in infinite
ways, when many points are moved, there is usually only one
way

object Translation: Every Point Displaced


by same vector
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 8
Bangalore - 560036.
Translation Using Representations
Using the homogeneous coordinate representation in some
frame
p = [ x y z 1]T
p’ = [x’ y’ z’ 1]T
d=[dx dy dz 0]T
Hence p’ = p + d or
x’ = x + dx
y’ = y + dy
z’ = z + dz
note that this expression is in
four dimensions and expresses
point = vector + point

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011
Bangalore - 560036. 9
Translation Matrix
We can also express translation using a 4 x 4 matrix T in
homogeneous coordinates
p’ = Tp where
1 0 0 dx 
0 1 0 dy 
T = T(dx, dy, dz) =  
0 0 1 dz 
 
0 0 0 1

This form is better for implementation because all affine


transformations can be expressed this way and multiple
transformations can be concatenated together

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011
Bangalore - 560036. 10
Rotation (2D)
Consider rotation about the origin by θ degrees
radius stays the same, angle increases by θ from
φ. COS(A + B) = COSA .COSB – SINA .SINB
SIN( A + B) = SINA .COSB + COSA .SINB

x’ = r cos (φ + θ) x’= x cos θ – y sin θ


y' = r sin (φ + θ) y’ = x sin θ + y cos θ

x = r cos φ
y = r sin φ

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011
Bangalore - 560036. 11
EXPAND THIS FORM TO 3-D
1. There is one point—the origin, in this case—that is
unchanged by the rotation.
This point is called as the fixed point of the
transformation.
Figure 36 shows a 2-D rotation about a fixed point in
the center of the object rather than about the origin
of the frame.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 12
Bangalore - 560036.
EXPAND THIS FORM TO 3-D cont’d….
2. Knowing that the 2-D plane is part of 3-D space,
This rotation in 3-D can be re-interpreted. In a Right-
Handed System, when the x- and y-axes are drawn in
the standard way, the positive z-axis comes out of
the page.
The definition of rotation in a positive direction means
that the rotation of the object is done in
counterclockwise direction when we look down the
positive z-axis toward the origin.
Same definition is used for defining positive rotations
about other axes.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 13
Bangalore - 560036.
EXPAND THIS FORM TO 3-D cont’d….
3. Rotation in the 2-D plane z = 0 (i.e. XY-plane) is
equivalent to a 3-D rotation about the z-axis.
Points in planes of constant z will all rotate in a similar
manner, leaving their z values unchanged.

Three entities must be


specified as shown in
the Figure 37:
A fixed point (Pf), a
Rotation Angle (θ), and
a line or vector ‘v’ about
which the object is to be
rotated.
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 14
Bangalore - 560036.
EXPAND THIS FORM TO 3-D cont’d….
For a given fixed point, there are 3 degrees of freedom: the two
angles necessary to specify the orientation of the vector and
the angle that specifies the amount of rotation about the
vector.
Rotation and Translation are known as Rigid Body
Transformations.
No combination of Rotations and Translations can alter the
shape or volume of an object; they can alter only the object's
location and orientation.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 15
Bangalore - 560036.
Scaling
Scaling is an Affine non rigid-body transformation by which an
object can be made bigger or smaller.

Non Uniform Scaling

Figure 39 illustrates both uniform


scaling in all directions and non
uniform scaling in a single
direction.
A non-uniform scaling is needed
to build up the full set of affine
transformations that are used in
Uniform Scaling
modeling and viewing by
combining a properly chosen
sequence of Scalings,
Translations, and Rotations.
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 16
Bangalore - 560036.
Scaling cont’d….
Scaling transformations have a fixed point, as seen in Figure 40.
Hence, to specify a scaling, a fixed point can be specified, a direction in which
the object is to be scaled, and a scale factor (α).
For α > 1, the object gets longer in the specified direction; for 0 < α < 1, the
object gets smaller in that direction.
Negative values of α give us Reflection (Figure 41) about the fixed point, in
the scaling direction.
Scaling has six degrees of freedom because we can specify an arbitrary fixed
point and three independent scaling factors. sx = -1 sy = 1 original

sx = -1 sy = -1 sx = 1 sy = -1
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 17
Bangalore - 560036.
TRANSFORMATIONS IN HOMOGENEOUS COORDINATES
All graphics APIs force us to work within some reference system.
Hence, we cannot work with high-level expressions such as
Instead, we work with representations in homogeneous
coordinates and with expressions such as
Within a frame, each affine transformation is represented by a
4 x 4 matrix of the form

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 18
Bangalore - 560036.
Translation
Translation displaces points to new positions defined by a
displacement vector.
If we move the point p to p' by displacing through a distance d,
then p' = p + d.
Looking at their Homogeneous Coordinate forms

we see that these equations can be written component by


component as = 1x + 0y + 0z + αx
= 0x + 1y + 0z + αy
= 0x + 0y + 1z + αz
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 19
Bangalore - 560036.
Translation cont’d….
This method of representing translation using the addition of
column matrices does not combine well with the
representations of other affine transformations.
However, we can also get this result using the matrix
multiplication of the form: p' = Tp, where

T is called the Translation Matrix.


Sometimes it is written as T(αx, αy, αz) to emphasize the three
independent parameters.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 20
Bangalore - 560036.
Translation cont’d….
The inverse of a translation matrix can be obtained by
either applying an inversion algorithm or by noting
that if a point is displace by the vector ‘d’, we can
return to the original position by a displacement of ‘-
d’.
By either method, we find that

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 21
Bangalore - 560036.
Scaling
For both scaling and rotation, there is a fixed point that is
unchanged by the transformation.
Let the fixed point be the origin, then it can be shown as to how
transformations can be concatenated to obtain the
transformation for an arbitrary fixed point.
A scaling matrix with a fixed point of the origin allows for
independent scaling along the coordinate axes. The three
equations are
= βx x + 0 y + 0z
= 0 x + βy y + 0z
= 0 x + 0y + βz z

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 22
Bangalore - 560036.
Scaling cont’d….
These three equations can be combined in
homogeneous form as p' = S p, where

We obtain the inverse of a scaling matrix by applying


the reciprocals of the scale factors:

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 23
Bangalore - 560036.
Rotation about the Z axis
Rotation about Z axis in three dimensions leaves all
points with the same z
Equivalent to rotation in two dimensions in planes of
constant z x’= x cos θ – y sin θ
y’ = x sin θ + y cos θ
z’ = z
or in homogeneous coordinates p’ = Rz(θ)
θ p

cos θ − sin θ 0 0
 sin θ cos θ 0 0
R = Rz(θ) = 
 0 0 1 0
 
 0 0 0 1
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011
Bangalore - 560036. 24
Rotation about X and Y axes
Same argument as for rotation about z axis
For rotation about X axis, x is unchanged
For rotation about Y axis, y is unchanged

1 0 0 0
0 cos θ - sin θ 0
R = Rx(θ) = 
0 sin θ cos θ 0
 
0 0 0 1

 cos θ 0 sin θ 0
 0 1 0 0
R = Ry(θ) =  
- sin θ 0 cos θ 0
 
 0 0 0 1
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011
Bangalore - 560036. 25
Rotation cont’d….
The signs of the sine terms are consistent with the
definition of a positive rotation in a right-handed
system.
Suppose that R denotes any of the three rotation
matrices. A rotation by θ can always be undone by a
subsequent rotation by —θ; hence, R-1(θ) = R(-θ).
In addition, noting that all the cosine terms are on the
diagonal and the sine terms are off-diagonal, the
following trigonometric identities can be used
cos(—θ) = cos θ; sin(—θ) = — sin θ to find
R -1(θ)=RT(θ).

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 26
Bangalore - 560036.
Rotation cont’d….
To construct any desired Rotation Matrix, with a fixed point at
the origin, as a product of individual rotations about the three
axes R = R R R . Rx Ry Rz
y x y z 1 0 cos θ 0 sin θ 0 cos θ − sin θ 0 0
0 0  
 0  0 0
θ v
0 cos θ - sin θ 0 1 0 0  sin θ cos θ
  
0 sin θ cos θ 0 - sin θ 0 cos θ 0  0 0 1 0
R=     
0 0 0 1  0 0 0 1  0 0 0 1
x

z Using the fact that the transpose of a product is the product of


the transposes in the reverse order, It can be seen that for any
rotation matrix, R-1 = RT.
A matrix whose inverse is equal to its transpose is called an
Orthogonal Matrix.
Normalized orthogonal matrices correspond to rotations about
the origin.
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 27
Bangalore - 560036.
Shear
 Helpful to add one more basic transformation
 Equivalent to pulling faces in opposite directions
Consider a cube centered at the origin, aligned with the axes and
viewed from the positive z-axis, as shown in Figure 42. If the
top is pulled to the right and the bottom to the left, the object
is sheared in the x direction.
Note: Here neither the y nor the z values are changed by the
shear, so this operation can be called x shear to distinguish it
from shears of the cube in other possible directions.
Using simple trigonometry on
Figure 43, It can be seen that each
shear is characterized by a single
angle θ;
Figure 42 Figure 43
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 28
Bangalore - 560036.
Shear cont’d….
the equations for this shear are

The inverse can be obtained by noting that it needs to


sheared in only the opposite direction; hence

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 29
Bangalore - 560036.
CONCATENATION OF TRANSFORMATIONS
Consider the Affine Transformations obtained by multiplying
together, or concatenating, sequences of the basic
transformations.
Suppose that three successive transformations are carried out
on a point p, creating a new point q. Because the matrix
product is associative, so it can be written as the sequence

Here the matrices A, B, and C (and thus M) are arbitrary 4 x 4


matrices, although in practice they most likely will be affine.
The order in which the transformations are carried out, affects
the efficiency of the calculation.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 30
Bangalore - 560036.
CONCATENATION OF TRANSFORMATIONS cont’d….
To transform a single point, this order is the most efficient
because each matrix multiplication involves multiplying a
column matrix by a square matrix.
If many points are to be transformed, then we can proceed in
two steps.
Step I: Calculate M = CBA.
Step II: Use this matrix on each point q = Mp.
This order corresponds to the pipeline shown in Figure 45, where
M is computed first; then it is loaded into a pipeline
transformation unit.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 31
Bangalore - 560036.
Rotation About a Fixed Point
Consider a cube with its center at pf and its sides aligned with
the axes.
This cube is to be rotated about the z-axis, about its center pf,
which becomes the fixed point of the transformation, as
shown in Figure 46 below.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 32
Bangalore - 560036.
Rotation About a Fixed Point cont’d….
If pf were the origin, then to solve the problem: the
transformation Rz(θ) is used.
This observation suggests the strategy
Step – I: move the cube to the origin.
Step – II: Apply Rz(θ) and
Step – III: move the object back such that its center is again at pf .
This sequence is shown in the following Figure 47.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 33
Bangalore - 560036.
Rotation About a Fixed Point cont’d….
In terms of our basic affine transformations, the first is T(—pf),
the second is Rz(θ), and the final is T(pf).
Concatenating them together, to obtain the single matrix

On multiplying the matrices, following resultant matrix is


obtained

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 34
Bangalore - 560036.
General Rotation of a Cube about Origin
Consider the cube, again centered at the origin with its sides
aligned with the axes, as shown in Figure 48(a).
Step – I: Rotate the cube about the z-axis by an angle α to orient
it, as shown in Figure 48(b).

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 35
Bangalore - 560036.
General Rotation of a Cube about Origin cont’d….
Step – II: Rotate the cube by an angle β about the Y-axis, as
shown in a top view as shown in Figure 49 below:

Step – III: Rotate the cube by an angle γ about the x-axis, as


shown in a side view in Figure 50.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 36
Bangalore - 560036.
The Instance Transformation
Consider a scene composed of many simple
objects, such as that shown in Figure 51.
One option is to define each of these
objects, through its vertices, in the desired
location with the desired orientation and size.
An alternative is to define each of the
object types once at a convenient size, place, and an orientation.

Each occurrence of an object in the scene is an instance of that


object's prototype, and the desired size, orientation, and
location can be obtained by applying an affine transformation
called as the Instance Transformation to the prototype.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 37
Bangalore - 560036.
The Instance Transformation cont’d….
The instance transformation is applied in the order shown in
Figure 52.
Objects are usually defined in their own frames, with the origin
at the center of mass and the sides aligned with the model
frame axes.
First, the object is scaled to the desired size. Then it is oriented
with a rotation matrix.
Finally, it is translated to the desired orientation. Hence, the
instance transformation is of the form
M = TRS

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 38
Bangalore - 560036.
Rotation About an Arbitrary Axis
Consider rotating a cube, as shown in Figure 53.
Three entities are needed to specify this rotation. There is a fixed
point p0 that assumed to be the center of the cube, a vector
‘u’ about which the object is to be rotated, and an angle of
rotation θ.
The vector about which the cube is to be
rotated can be specified in various ways.
One way is to use two points, p1 and p2,
defining the vector u = p2 – p1 .
Note: The order of the points determines
the positive direction of rotation for θ and
that even though u passes through p0,
only the orientation of u matters.
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 39
Bangalore - 560036.
Rotation About an Arbitrary Axis cont’d….
Replacing u with a unit length vector ‘v’
in the same direction as that of vector ‘u’.
Now moving the fixed point p0 to the origin.

The final rotation matrix will be of the form

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 40
Bangalore - 560036.
Rotation About an Arbitrary Axis cont’d….

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 41
Bangalore - 560036.
Rotation About an Arbitrary Axis cont’d….
Considering the components of v, as v is a unit-length vector.

Now draw a line segment from the origin to the point


(αx, αy, αz).
This line segment has unit length and the orientation of v. Next,
draw the perpendiculars from the point (αx, αy, αz) to the
coordinate axes, as shown in Figure 56.
The three direction angles (Øx, Øy, Øz) are
the angles between the line segment (or v) v
and the axes.
The direction cosines are given by
Satisfying the condition:

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 42
Bangalore - 560036.
Rotation About an Arbitrary Axis cont’d….
We can now compute θX and θY using these angles. N
Consider Figure 4.57.
It shows that the effect of the desired rotation on P’
the point (αx, αy, αz) is to rotate the line segment
into the plane Y = 0 (XZ-Plane). O

If we look at the projection of the line segment


(before the rotation) on the plane x = 0, we see a M

line segment of length d on this plane.

Another way to see this figure is to think of YZ-Plane: X = 0

the plane x = 0 as a wall and consider a distant αz


light source located far down the positive x-axis.
αy
The line that we see on the wall is the shadow of the θx

line segment from the origin to (αx, αy, αz) .


Here since αx = o (in YZ-Plane).
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 43
Bangalore - 560036.
Rotation About an Arbitrary Axis cont’d….
1 0 0 0  cos θ 0 sin θ 0
0 cos θ - sin θ  0
0 & R (θ ) =  0 1 0

Rx(θx) =  y y
0 sin θ cos θ 0  - sin θ 0 cos θ 0
   
0 0 0 1  0 0 0 1

the rotation matrix is determined by the sine and


αx
cosine of θx. θy
d
Thus, we never need to compute θx; rather,
αx
we need to compute only
We compute Ry(θy), in a similar manner. Figure 58 above shows the rotation.
This angle is clockwise about the y-axis; therefore, we have to be careful of
the sign of the sine terms in the matrix, which is

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 44
Bangalore - 560036.
Rotation About an Arbitrary Axis cont’d….
Finally, we concatenate all the matrices to find

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 45
Bangalore - 560036.
OPENGL TRANSFORMATION MATRICES cont’d….
The Current Transformation Matrix: The Current Transformation
Matrix (CTM) is a generalization common to most graphics
systems.
It is the matrix that is applied to any vertex that is defined
subsequent to its setting.
The CTM is part of the pipeline (Figure 59); thus, if p is a vertex
specified in the application, then the pipeline produces Cp.
The CTM is a 4 x 4 matrix; it can be altered by a set of
functions provided by the graphics package.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 46
Bangalore - 560036.
OPENGL TRANSFORMATION MATRICES cont’d….
The CTM is defined in the user program and loaded into a
transformation unit.
If the symbol <-- is used to denote replacement, then the
initialization operation can be written as
The functions which alter C are of two forms: those that load it
with some matrix and those that modify it by pre-
multiplication or post-multiplication by a matrix.
The operations in post-multiplication form is written as

and in load form as

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 47
Bangalore - 560036.
OPENGL TRANSFORMATION MATRICES
Most systems allow us to load the CTM with an arbitrary matrix
M, or to post-multiply by an arbitrary matrix M,
Rotation, Translation, and Scaling in OpenGL: OpenGL has a
model-view and a projection matrix in the pipeline which are
concatenated together to form the CTM as shown below in
the figure.
Each of theses Matrices can be manipulated by first setting the
correct matrix mode

A matrix can be loaded by using the function


glLoadMatrixf(pointer_to_matrix);
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 48
Bangalore - 560036.
OPENGL TRANSFORMATION MATRICES cont’d….
or set a matrix to the identity matrix with the function

Arbitrary 4 x 4 matrices can be specified by a pointer to a one-


dimensional array of 16 entries organized by the columns of
the desired matrix.
The selected matrix can be altered with

Rotation, translation, and scaling are provided through the


following three functions:
glRotatef(angle, vx, vy, vz);
glTranslatef(dx, dy, dz);
glScalef(sx, sy, sz);
For rotation, the angle is specified in degrees, and the variables vx, vy, and
vz are the components of a vector about which object is to be rotated.
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 49
Bangalore - 560036.
Rotation About a Fixed Point in OpenGL
The following sequence sets the matrix mode, then
forms the required matrix for a 45-degree rotation
about the line through the origin and the point (1, 2,
3) with a fixed point of (4, 5, 6):

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 50
Bangalore - 560036.
Order of Transformations
The rule in OpenGL for applying the transformation is this: The
transformation specified last is the one applied first.

In each step, we postmultiply at the end of the existing CTM,


forming the matrix

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 51
Bangalore - 560036.
Spinning of the Cube
Cube is defined with its vertices specified as follows:

The face (0, 3, 2, 1) of the cube is defined as

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 52
Bangalore - 560036.
Spinning of the Cube cont’d….
Colour of each face of the cube is defined as follows:

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 53
Bangalore - 560036.
Spinning of the Cube cont’d….
The Cube is rotated using the three buttons of the mouse. The
following three callback functions will be defined as :

Where the display() function is defined as

The function display first


sets a model-view
matrix using the values
of three angles
determined by the
mouse callback.
It then draws a cube,
using the colorcube()
function
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 54
Bangalore - 560036.
Spinning of the Cube cont’d….

The idle callback increments the angle associated with the


chosen axis by 2 degrees each time:

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 55
Bangalore - 560036.
Spinning of the Cube cont’d….
the program can be terminated by using the
keyboard with the simple keyboard callback as
follows:

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 56
Bangalore - 560036.
A Virtual Trackball
The use of the mouse position to control rotation about two axes
provides us with most of the functionality of a trackball.
Consider the trackball shown in Figure 4.61. We assume that the
ball has a radius of 1 unit.
A position on the surface of the Trackball can be mapped to the
plane y = 0 by doing an orthogonal projection to the plane, as
shown in Figure 62.
The position (x, y, z) on the surface of the ball is mapped to
(x, 0, z) on the plane
Y = 0.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 57
Bangalore - 560036.
A Virtual Trackball cont’d….
This projection is reversible because it is
known that the 3D point (x, y, z) which is
projected to the point (x, 0, z) on the
plane Y = 0 must satisfy the equation of
the sphere
Thus, given the point (x, 0, z) on the plane
Y = 0, the corresponding point on the
hemisphere must be (x, y, z), where
.
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 58
Bangalore - 560036.
A Virtual Trackball cont’d….
Suppose p1 and p2 be two positions on the
hemisphere; then the vectors from the origin
to these points determine the orientation of a
plane, as shown in Figure 63, whose normal
‘n’ is defined by their cross product

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 59
Bangalore - 560036.
A Virtual Trackball cont’d….
The motion of the trackball which moves from
pt to p2 can be achieved by a rotation about
n.
The angle of rotation θ is the angle between the
vectors p1 and p2, which can be computed by
using the magnitude of the cross product.
Because both p1 and p2 have unit length,

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 60
Bangalore - 560036.
A Virtual Trackball cont’d….
If the mouse is tracked at a high rate, then the changes detected
will be small; so, rather than using an inverse trigonometric
function to find θ, we can use the approximation
the virtual trackball can be implemented through the use of the
idle, motion, and mouse callbacks in GLUT.
We the process in terms of three logical variables can think of, or
flags, that control the tracking of the mouse and of the display
redrawing.
These are set initially as follow:
If redrawContinue is true, the idle function posts a redisplay. If
trackingMouse is true, the trackball position is updated as
part of the motion callback.
If trackballMove is true, the rotation matrix which is used in the
display routine is updated.
K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 61
Bangalore - 560036.
QUATERNIONS
Quaternions: The Quaternions are an extension of complex
numbers that provide an alternative method for describing
and manipulating the rotations.
Complex Numbers and Quaternions: suppose that we let i
denote the pure imaginary number such that i2 = — 1.
Recalling Euler's identity: .
The polar representation of a complex number c can be written
as where a = r.cosθ; b = r.sinθ with

If c is rotated about the origin by an angle Ø to c', then c' can be


found using a rotation matrix, or the polar representation can
be used to write

Thus, eiØ is a rotation operator in the complex plane.


K. Satyanarayan Reddy, Professor, CiTech,
April 29, 2011 62
Bangalore - 560036.
QUATERNIONS cont’d….
In 3-D the problem is more difficult; because to specify a rotation
about the origin both a direction (a vector) and the amount of
rotation need to be specified about the origin (a scalar).
Usually, this representation is written as the Quaternion.

where q = (q1, q2, q3).


The operations among Quaternions are based on the use of
three "complex" numbers i, j and k with the properties

These numbers are similar to the unit vectors in 3-D, and q can
be written as:

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 63
Bangalore - 560036.
QUATERNIONS cont’d….
Now using the relationships among i, j , and k the quaternion addition and
multiplication can be derived.
Consider the two quaternion a, b is given by equations:
a = (q0, q1, q2, q3) = (q0, q) and b = (p0, p1, p2, p3) = (p0, p)
Where q = q1i + q2j + q3k and p = p1i + p2j + p3k
then using the concept of dot and cross products for vectors we can derive
quaternion addition and multiplication as follows:

The magnitude for quaternions can also be defined in the normal manner as

Quaternions have a multiplicative identity, the quaternion (1,0), and it is easy


to verify that the inverse of a quaternion is given by

Where 0 = 0i + 0j + 0k

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 64
Bangalore - 560036.
Quaternions and Rotation
Suppose that we use the vector part of a quaternion to
represent a point in space .
Thus, the components of p = (x, y, z) give the location of the
point.
Consider the quaternion where v has unit length.
It can be shown that the quaternion r is a unit quaternion
(|r| = 1), and therefore
If we consider the Quaternion product of the quaternion p
(which represents a point) with r, we obtain the quaternion

This quaternion has the form (0, p'), where

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 65
Bangalore - 560036.
Quaternions and Rotation cont’d….
and thus p' is the representation of a point.
Where p' is the result of rotation of the point p by θ degrees
about the vector v.
Example: Suppose that we consider the rotation about the z-axis
by θ with a fixed point at the origin.
The desired unit vector v is (0,0,1) yielding the quaternion

The rotation of an arbitrary point p = (x, y, z) yields the


quaternion

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 66
Bangalore - 560036.
Quaternions and Rotation cont’d….
For rotation in 3-D, we can use the product of the corresponding
quaternions to form rxryrz ; representing a sequence of
rotations about the coordinate axes.
The elements of p' = rpr-1 can be used to find the elements of the
homogeneous coordinate rotation matrix embedded in M.
For the Rotation matrix is given by

where as v is a unit vector. Also

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 67
Bangalore - 560036.
Advantages of Quaternions
Quaternion products can be used to form r and then form the
rotation part of M by matching terms between R and r.
In addition to the efficiency of using quaternions over the
rotation matrices is that, the quaternions can be interpolated
to obtain smooth sequences of rotations for animation.

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 68
Bangalore - 560036.
END OF UNIT V

THANK YOU

K. Satyanarayan Reddy, Professor, CiTech,


April 29, 2011 69
Bangalore - 560036.

You might also like