CG Module2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

Computer Graphics and Fundamentals of Image Processing(21CS63)

Module 2

2D Geometric Transformations

Operations that are applied to the geometric description of an object to change its position,
orientation, or size are called geometric transformations. Sometimes geometric transformation
operations are also referred to as modeling transformations.

Basic Two-Dimensional Geometric Transformations


The geometric-transformation functions that are available in all graphics packages are those for
translation, rotation, and scaling. Other useful transformation routines that are sometimes
included in a package are reflection and shearing operations.

Two-Dimensional Translation
Translation on single coordinate point is performed by adding offsets to its coordinates to
generate a new coordinate position. The original point position is moved along a straight line
path to its new location.

To translate a two-dimensional position, we add translation distances tx and ty to the original


coordinates (x,y) to obtain the new coordinate position (x,y) as shown in Figure 2.1.

The translation distance pair (tx, ty) is called a translation vector or shift vector.

Department of CSE Page 1


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.1: Translating a point from position P to position P using a translation vector T.

We can express Equations 1 as a single matrix equation by using the following column vectors to
represent coordinate positions and the translation vector:

The two-dimensional translation equations can be written in matrix forms as:

Translation is a rigid-body transformation that moves objects without deformation. That is, every
point on the object is translated by the same amount. Figure 2.2 illustrates the application of a
specified translation vector to move an object from one position to another.

Department of CSE Page 2


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.2: Moving a polygon from position (a) to position (b) with the
translation vector (-5.50, 3.75)

Two-Dimensional Rotation
Rotation transformation of an object is generated by specifying a rotation axis and a rotation
angle. All points of the object are then transformed to new positions by rotating the points
through the specified angle about the rotation axis.

A two-dimensional rotation of an object is obtained by repositioning the object along a circular


path in the xy plane. Parameters for the two-dimensional rotation are the rotation angle θ and a
position(xr,yr), called the rotation point (or pivot point), about which the object is to be rotated.

Department of CSE Page 3


Computer Graphics and Fundamentals of Image Processing(21CS63)

A positive value for the angle θ defines a counterclockwise rotation about the pivot point. A
negative value for the angle θ rotates objects in the clockwise direction.

Figure 2.3: Rotation of an object through angle θ about the pivot point (xr ,yr )

1) Determination of the transformation equations for rotation of a point position P


when the pivot point is at the coordinate origin.

The angular and coordinate relationships of the original and transformed point positions are
shown in Figure 2.4.

Figure 2.4: Rotation of a point from position (x,y) to position (x, y) through an angle θ
relative to the coordinate origin. The original angular displacement of the point from the x
axis is 

Department of CSE Page 4


Computer Graphics and Fundamentals of Image Processing(21CS63)

r is the constant distance of the point from the origin, angle  is the original angular position of
the point from the horizontal, and θ is the rotation angle.

Using standard trigonometric identities, transformed coordinates can be expressed in terms of


angles of  and 

x=rcos(+θ)=rcoscosθ-rsinsinθ (4)

y=rsin(+θ)=rcossinθ+rsincosθ

The original coordinates of the point in the polar coordinates are

x=rcos y=rsin (5)

Substituting expressions 5 into 4 we obtain the transformation equations for


rotating a point at position (x, y) through an angle θ about the origin.

x’=xcos-ysin (6)

y’=xsin+ycos

The rotation equation in matrix form is written as

P’=RP (7)

where the rotation matrix is

(8)

2) Determination of the transformation equations for rotation of a point position P


when the pivot point is at (xr,yr).

Rotation of a point about an arbitrary pivot position is illustrated in Figure 2.5

Department of CSE Page 5


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.5: Rotating a point from position(x,y) to position (x,y) through an angle  about
rotation point (xr,yr)

Using the trigonometric relationships indicated by the two right triangles in this figure, we can
generalize Equations 6 to obtain the transformation equations for rotation of a point about any
specified rotation position (xr, yr):

x=xr+(x-xr)cos - (y-yr)sin (9)

y=yr+(x-xr) sin + (y-yr)cos

Two-Dimensional Scaling

To alter the size of an object, scaling transformation is used. A two dimensional scaling
operation is performed by multiplying object positions (x,y) by scaling factors sx and sy to
produce the transformed coordinates(x’,y’).

Scaling factor sx scales an object in the x direction, while sy scales in the y direction. The basic
two-dimensional scaling equations 10 can also be written in the following matrix form:

Department of CSE Page 6


Computer Graphics and Fundamentals of Image Processing(21CS63)

or

P’=S.P (12)

where S is the 2 × 2 scaling matrix in Equation 11

Any positive values can be assigned to the scaling factors sx and sy. Values less than 1 reduce the
size of objects. Values greater than 1 produce enlargements. Specifying a value of 1 for both sx
and sy leaves the size of objects unchanged. When sx and sy are assigned the same value, a
uniform scaling is produced, which maintains relative object proportions. Unequal values for sx
and sy result in a differential scaling.

Figure 2.6: Turning a square (a) into a rectangle (b) with scaling factors s x = 2 and
sy = 1.

Figure 2.7 illustrates scaling of a line by assigning the value 0.5 to both sx and sy.
Both the line length and the distance from the origin are reduced by a factor of ½.

Department of CSE Page 7


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.7: A line scaled with Equation 12 using sx = sy = 0.5 is reduced in size and moved
closer to the coordinate origin

The location of a scaled object is controlled by choosing a position, called the fixed point, that is
to remain unchanged after the scaling transformation. Coordinates for the fixed point, (xf,yf), are
often chosen at some object position, such as its centroid but any other spatial position can be
selected.

Objects are now resized by scaling the distances between object points and the point (Figure
2.8). For a coordinate position (x, y), the scaled coordinates (x, y) are then calculated from the
following relationships:

x-xf=(x-xf)sx (13)

y-yf=(y-yf)sy

The above equation can be rewritten to separate the multiplicative and additive terms as

x=x.sx+xf (1-sx) (14)

y=y.sy+yf(1-sy)

The additive terms xf(1-sx) and yf(1-sy) are constants for all points in the object.

Department of CSE Page 8


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.8: Scaling relative to a chosen fixed point(xf , yf ). The distance from each polygon
vertex to the fixed point is scaled by Equations 13

Matrix Representations and Homogeneous Coordinates

Each of the three basic two-dimensional transformations (translation, rotation, and scaling) can
be expressed in the general matrix form.

P’=M1.P+M2 (15)

With coordinate positions P and P’ represented as column vectors. Matrix M1 is a 2 × 2 array


containing multiplicative factors, and M2 is a two-element column matrix containing
translational terms.

For translation, M1 is the identity matrix. For rotation or scaling, M2 contains the translational
terms associated with the pivot point or scaling fixed point.

Homogeneous Coordinates

Multiplicative and translational terms for a two-dimensional geometric transformation can be


combined into a single matrix, if representations are expanded to 3 × 3 matrices. The third
column of a transformation matrix can be used for the translation terms, and all transformation
equations can be expressed as matrix multiplications.

Department of CSE Page 9


Computer Graphics and Fundamentals of Image Processing(21CS63)

The matrix representation for a two-dimensional coordinate position is expanded to a three-


element column matrix. Each two dimensional coordinate-position representation(x,y) is
expanded to a three-element representation (xh,yh,h) called homogeneous coordinates.

The homogeneous parameter h is a nonzero value such that

x=xh/h (16)

y=yh/h

A general two-dimensional homogeneous coordinate representation could also be written as


(h· x, h· y, h).
A convenient choice is simply to set h = 1. Each two-dimensional position is then represented
with homogeneous coordinates (x,y,1). The term homogeneous coordinates is used in
mathematics to refer to the effect of this representation on Cartesian equations.

Expressing positions in homogeneous coordinates allows us to represent all


geometric transformation equations as matrix multiplications, which is the standard method used
in graphics systems.

Two-Dimensional Translation Matrix

The homogeneous-coordinate for translation is given by

(17)

This translation operation can be written in the abbreviated form

P=T(tx,ty).P (18)

with T(tx, ty) as the 3 × 3 translation matrix in Equation 17

Department of CSE Page 10


Computer Graphics and Fundamentals of Image Processing(21CS63)

Two-Dimensional Rotation Matrix

Two-dimensional rotation transformation equations about the coordinate origin can be expressed
in the matrix form as

(19)

or as

P’=R().P (20)

The rotation transformation operator R(θ) is the 3 × 3 matrix with rotation parameter θ.

Two-Dimensional Scaling Matrix

A scaling transformation relative to the coordinate origin can be expressed as the matrix
multiplication.

(21)

P=S(sx,sy).P (22)

The scaling operator S(sx,sy) is the 3x3 matrix with parameters sx and sy.

Inverse Transformations

For translation, inverse matrix is obtained by negating the translation distances. If the two
dimensional translation distances are t x and ty , then inverse translation matrix is

Department of CSE Page 11


Computer Graphics and Fundamentals of Image Processing(21CS63)

(23)

An inverse rotation is accomplished by replacing the rotation angle by its negative. A two-
dimensional rotation through an angle θ about the coordinate origin has the inverse
transformation matrix

(24)

Negative values for rotation angles generate rotations in a clockwise direction.

The inverse matrix for any scaling transformation is obtained by replacing the scaling parameters
with their reciprocals. For two-dimensional scaling with parameters sx and sy applied relative to
the coordinate origin, the inverse transformation matrix is

(25)

The inverse matrix generates an opposite scaling transformation.

Two-Dimensional Composite Transformations

Forming products of transformation matrices is referred to as a concatenation, or composition,


of matrices. If two transformations are applied to point position P, the transformed location
would be calculated as:

Department of CSE Page 12


Computer Graphics and Fundamentals of Image Processing(21CS63)

P=M2.M1.P

P’=M.P (26)

The coordinate position is transformed using the composite matrix M, rather than applying the
individual transformations M1 and then M2.

Composite Two-Dimensional Translations

If two successive translation vectors (t 1x, t1y) and (t2x, t2y) are applied to a two dimensional
coordinate position P. The final transformed location P is calculated as

P=T(t2x,t2y).{T(t1x,t1y).P}

P={T(t2x,t2y).T(t1x,t1y)}.P (27)

where P and P are represented as three-element, homogeneous-coordinate column vectors.

The composite transformation matrix for this sequence of translations is

(28)

or

T(t2x,t2y).T(t1x,t1y)=T(t1x+t2x,t1y+t2y) (29)

Composite Two-Dimensional Rotations

Two successive rotations applied to a point P produce the transformed position

P=R(2).{R(1).P}

= {R(2).{R(1).P} (30)

By multiplying the two rotation matrices, it can be verified that two successive rotations are
additive.

Department of CSE Page 13


Computer Graphics and Fundamentals of Image Processing(21CS63)

R(2).R(1)=R(1+2) (31)

The final rotated coordinates of a point can be calculated with the composite rotation matrix as

P=R(1+2).P (32)

Composite Two-Dimensional Scalings

Concatenating transformation matrices for two successive scaling operations in two dimensions
produces the following composite scaling matrix:

(33)

or

S(s2x,s2y). S(s1x,s1y)=S(s1x.s2x,s1y.s2y) (34)

General Two-Dimensional Pivot-Point Rotation

Two-dimensional rotation about any other pivot point (xr,yr) can be generated by performing the
following sequence of translate-rotate-translate operations:

1. Translate the object so that the pivot-point position is moved to the coordinate origin.

2. Rotate the object about the coordinate origin.

3. Translate the object so that the pivot point is returned to its original position.

The composite transformation matrix for this sequence is obtained with the concatenation

Department of CSE Page 14


Computer Graphics and Fundamentals of Image Processing(21CS63)

(35)

which can be expressed in form

T(xr,yr).R().T(-xr,-yr)=R(xr,yr,) (36)

where T(-xr ,-yr ) = T-1(xr ,yr)

Figure 2.9: A transformation sequence for rotating an object about a specified pivot point
using the rotation matrix R(θ)

General Two-Dimensional Fixed-Point Scaling

To produce a two-dimensional scaling with respect to a selected fixed position (x f,yf), following
sequence is followed.

Department of CSE Page 15


Computer Graphics and Fundamentals of Image Processing(21CS63)

1. Translate the object so that the fixed point coincides with the coordinate origin.

2. Scale the object with respect to the coordinate origin.

3. Use the inverse of the translation in step (1) to return the object to its original position.

Concatenating the matrices for these three operations produces the required scaling matrix:

(37)

or

T(xf,yf).S(sx,sy).T(-xf,-yf)=S(xf,yf,sx,sy) (38)

Figure 2.10: A transformation sequence for scaling an object with respect to a specified
fixed position using the scaling matrix S(sx, sy )

Other Two-Dimensional Transformations

Basic transformations such as translation, rotation, and scaling are standard components of
graphics libraries. Some packages provide a few additional transformations that are useful in
certain applications.

Department of CSE Page 16


Computer Graphics and Fundamentals of Image Processing(21CS63)

Two such transformations are reflection and shear.

1. Reflection and
2. Shear.

Reflection

A transformation that produces a mirror image of an object is called a reflection.

For a two-dimensional reflection, this image is generated relative to an axis of reflection


by rotating the object 180◦ about the reflection axis.

Reflection about the line y = 0 (the x axis) is accomplished with the transformation matrix

(39)

This transformation retains x values, but “flips” the y values of coordinate positions.

The resulting orientation of an object after it has been reflected about the x axis is shown
in Figure 2.11

Figure 2.11: Reflection of an object about the x axis

Department of CSE Page 17


Computer Graphics and Fundamentals of Image Processing(21CS63)

A reflection about the line x = 0 (the y axis) flips x coordinates while keeping y coordinates the
same. The matrix for this transformation is

(40)

Figure 2.12 illustrates the change in position of an object that has been reflected about
the line x = 0.

Figure 2.12: Reflection of an object about the y axis.

We flip both the x and y coordinates of a point by reflecting relative to an axis that is
perpendicular to the xy plane and that passes through the coordinate origin. The matrix
representation for this reflection is

(41)

Department of CSE Page 18


Computer Graphics and Fundamentals of Image Processing(21CS63)

An example of reflection about the origin is shown in Figure 2.13.

Figure 2.13: Reflection of an object relative to the coordinate origin. This transformation
can be accomplished with a rotation in the x y plane about the coordinate origin.

If we choose the reflection axis as the diagonal line y = x , the reflection matrix is

(42)

Department of CSE Page 19


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.14: Reflection of an object with respect to the line y = x .

To obtain a transformation matrix for reflection about the diagonal y = x, concatenate matrices
for the transformation sequence:

1. Clockwise rotation by 45◦,

2. Reflection about the x axis

3. Counterclockwise rotation by 45◦

Department of CSE Page 20


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.15: Sequence of transformations to produce a reflection about the line y = x : A


clockwise rotation of 45◦ (a), a reflection about the x axis (b), and a counterclockwise
rotation by 45◦ (c)

To obtain a transformation matrix for reflection about the diagonal y = -x, we could concatenate
matrices for the transformation sequence:
1. Clockwise rotation by 45◦,
2. Reflection about the y axis
3. Counterclockwise rotation by 45◦.
The resulting transformation matrix is

(43)

Department of CSE Page 21


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.16: Reflection with respect to the line y = -x .


Shear

A transformation that distorts the shape of an object such that the transformed shape appears as if
the object were composed of internal layers that had been caused to slide over each other is
called a shear.

Two common shearing transformations are those that shift coordinate x values and those that
shift y values.

An x-direction shear relative to the x axis is produced with the transformation Matrix

(44)

which transforms coordinate positions as

x=x+shx.y (45)

y=y

Department of CSE Page 22


Computer Graphics and Fundamentals of Image Processing(21CS63)

Any real number can be assigned to the shear parameter shx. A coordinate position (x, y) is then
shifted horizontally by an amount proportional to its perpendicular distance (y value) from the x
axis. Setting parameter shx to the value 2, for example, changes the square in Figure 2.17 into a
parallelogram. Negative values for shx shift coordinate positions to the left.

Figure 2.17: A unit square (a) is converted to a parallelogram (b) using the x -direction
shear matrix with shx= 2.

We can generate x-direction shears relative to other reference lines with

(46)

Coordinate positions are transformed as

x=x+shx(y-yref) (47)

y=y

A y-direction shear relative to the line x = xref is generated with the transformation matrix

(48)

Department of CSE Page 23


Computer Graphics and Fundamentals of Image Processing(21CS63)

which generates transformed coordinates as

x=x (49)

y=y+shy(x-xref)

Figure 2.18: A unit square (a) is transformed to a shifted parallelogram (b) with shx = 0.5
and yref = -1 in the shear matrix 46.

Figure 2.19: A unit square (a) is turned into a shifted parallelogram (b) with parameter
values shy = 0.5 and xref = -1 in the y -direction shearing transformation 48.

Department of CSE Page 24


Computer Graphics and Fundamentals of Image Processing(21CS63)

Raster Methods for Geometric Transformations


Raster systems store picture information as color patterns in the frame buffer. Therefore, some
simple object transformations can be carried out rapidly by manipulating an array of pixel
values. Few arithmetic operations are needed, so the pixel transformations are particularly
efficient.
Functions that manipulate rectangular pixel arrays are called raster operations and
moving a block of pixel values from one position to another is termed a block transfer, a
bitblt, or a pixblt.
Figure 2.20 illustrates a two-dimensional translation implemented as a block transfer of
a refresh-buffer area

Figure 2.20: Translating an object from screen position (a) to the destination position
shown in (b) by moving a rectangular block of pixel values. Coordinate positions Pmin and
Pmax specify the limits of the rectangular block to be moved, and P0 is the destination
reference position

Department of CSE Page 25


Computer Graphics and Fundamentals of Image Processing(21CS63)

Rotations in 90-degree increments are accomplished easily by rearranging the elements


of a pixel array. We can rotate a two-dimensional object or pattern 90◦ counterclockwise by
reversing the pixel values in each row of the array, then interchanging rows and columns
A 180◦ rotation is obtained by reversing the order of the elements in each row of the
array, then reversing the order of the rows.
Figure 2.21 demonstrates the array manipulations that can be used to rotate a pixel
block by 90◦ and by 180◦.

Figure 2.21: Rotating an array of pixel values. The original array is shown in (a), the
positions of the array elements after a 90◦ counterclockwise rotation are shown in (b), and
the positions of the array elements after a 180◦ rotation are shown in (c).

For array rotations that are not multiples of 90◦, we need to do some extra processing.
The general procedure is illustrated in Figure 2.22.

Figure 2.22: A raster rotation for a rectangular block of pixels can be accomplished by
mapping the destination pixel areas onto the rotated block.

Department of CSE Page 26


Computer Graphics and Fundamentals of Image Processing(21CS63)

Each destination pixel area is mapped onto the rotated array and the amount of overlap
with the rotated pixel areas is calculated. A color for a destination pixel can then be computed by
averaging the colors of the overlapped source pixels, weighted by their percentage of area
overlap.
Similar methods can be used to scale a block of pixels. Pixel areas in the original block are
scaled, using specified values for sx and sy, and then mapped onto a set of destination pixels. The
color of each destination pixel is then assigned according to its area of overlap with the scaled
pixel areas. (Figure 2.23)

Figure 2.23: Mapping destination pixel areas onto a scaled array of pixel values. Scaling
factors sx = sy = 0.5 are applied relative to fixed point (xf ,yf ).

OpenGL Raster Transformations

A translation of a rectangular array of pixel-color values from one buffer area to another
can be accomplished in OpenGL as the following copy operation:
glCopyPixels (xmin, ymin, width, height, GL_COLOR);
The first four parameters in this function give the location and dimensions of the pixel
block; and the OpenGL symbolic constant GL_COLOR specifies that it is color values
are to be copied

Department of CSE Page 27


Computer Graphics and Fundamentals of Image Processing(21CS63)

A block of RGB color values in a buffer can be saved in an array with the function

glReadPixels (xmin, ymin, width, height, GL_RGB, GL_UNSIGNED_BYTE, colorArray);


If color-table indices are stored at the pixel positions, we replace the constant GL RGB
with GL_COLOR_INDEX.
To rotate the color values, we rearrange the rows and columns of the color array, as
described in the previous section. Then we put the rotated array back in the buffer with
glDrawPixels (width, height, GL_RGB, GL_UNSIGNED_BYTE, colorArray);

A two-dimensional scaling transformation can be performed as a raster operation in


OpenGL by specifying scaling factors and then invoking either glCopyPixels or
glDrawPixels.
For the raster operations, we set the scaling factors with
glPixelZoom (sx, sy);
We can also combine raster transformations with logical operations to produce various
effects with the exclusive or operator

OpenGL Functions for Two-Dimensional Geometric Transformations

In the core library of OpenGL, a separate function is available for each of the basic geometric
transformations. OpenGL is designed as a three-dimensional graphics application programming
interface (API), all transformations are specified in three dimensions. Internally, all coordinate
positions are represented as four-element column vectors, and all transformations are represented
using 4 × 4 matrices.

To perform a translation, we invoke the translation routine and set the components for the
three-dimensional translation vector.

In the rotation function, we specify the angle and the orientation for a rotation axis that
intersects the coordinate origin. A scaling function is used to set the three coordinate scaling
factors relative to the coordinate origin. In each case, the transformation routine sets up a 4 × 4
matrix that is applied to the coordinates of objects that are referenced after the transformation
call

Department of CSE Page 28


Computer Graphics and Fundamentals of Image Processing(21CS63)

Basic OpenGL Geometric Transformations


A 4× 4 translation matrix is constructed with the following routine:
glTranslate* (tx, ty, tz);

Translation parameters tx, ty, and tz can be assigned any real-number values, and the single
suffix code to be affixed to this function is either f (float) or d (double).

For two-dimensional applications, we set tz = 0.0; and a two-dimensional position is represented


as a four-element column matrix with the z component equal to 0.0.
Example: glTranslatef (25.0, -10.0, 0.0);
Using above statement defined coordinate positions is translated 25 units in the x direction and
-10 units in the y direction.

A 4 × 4 rotation matrix is generated with

glRotate* (theta, vx, vy, vz);


where the vector v = (vx, vy, vz) can have any floating-point values for its components.
This vector defines the orientation for a rotation axis that passes through the coordinate origin.

If v is not specified as a unit vector, then it is normalized automatically before the elements of
the rotation matrix are computed.
The suffix code can be either f or d, and parameter theta is to be assigned a rotation angle in
degree.
For example, the statement: glRotatef (90.0, 0.0, 0.0, 1.0);
sets up the matrix for a 90◦ rotation about the z axis.
We obtain a 4 × 4 scaling matrix with respect to the coordinate origin with the following
routine:
glScale* (sx, sy, sz);

The suffix code is again either f or d, and the scaling parameters can be assigned
any real-number values.
Scaling in a two-dimensional system involves changes in the x and y dimensions,
so a typical two-dimensional scaling operation has a z scaling factor of 1.0

Department of CSE Page 29


Computer Graphics and Fundamentals of Image Processing(21CS63)

Example: glScalef (2.0, -3.0, 1.0);

The above statement produces a matrix that scales by a factor of 2 in the x direction, scales by a
factor of 3 in the y direction, and reflects with respect to the x axis:
OpenGL Matrix Operations
The glMatrixMode routine is used to set the projection mode which designates the matrix
that is to be used for the projection transformation.
modelview mode is specified with the following statement
glMatrixMode (GL_MODELVIEW);
which designates the 4×4 modelview matrix as the current matrix
Two other modes that can be set with the glMatrixMode function are the texture
mode and the color mode.
The texture matrix is used for mapping texture patterns to surfaces, and the color
matrix is used to convert from one color model to another. The default argument for the
glMatrixMode function is GL_MODELVIEW.
Identity matrix is assigned to the current matrix using following function:
glLoadIdentity( );
Other values can be assigned to the elements of the current matrix using
glLoadMatrix* (elements16);

A single-subscripted, 16-element array of floating-point values is specified with


parameter elements16, and a suffix code of either f or d is used to designate the data type. The
elements in this array must be specified in column-major order
To illustrate this ordering, we initialize the modelview matrix with the following code:
glMatrixMode (GL_MODELVIEW);
GLfloat elems [16];
GLint k;
for (k = 0; k < 16; k++)
elems [k] = float (k);
glLoadMatrixf (elems);

which produces the matrix

Department of CSE Page 30


Computer Graphics and Fundamentals of Image Processing(21CS63)

A specified matrix can be concatenated with the current matrix as follows:


glMultMatrix* (otherElements16);
The suffix code is either f or d, and parameter otherElements16 is a 16-element,
single-subscripted array that lists the elements of some other matrix in column-major
order.
Assuming that the current matrix is the modelview matrix, which we designate as
M, then the updated modelview matrix is computed as
M = M· M’

M represents the matrix whose elements are specified by parameter otherElements16 in the
preceding glMultMatrix statement.

The glMultMatrix function can also be used to set up any transformation sequence with
individually defined matrices.
For example,
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ( ); // Set current matrix to the identity.
glMultMatrixf (elemsM2); // Postmultiply identity with matrix M2.
glMultMatrixf (elemsM1); // Postmultiply M2 with matrix M1.
produces the following current modelview matrix:
M = M2 · M1

Department of CSE Page 31


Computer Graphics and Fundamentals of Image Processing(21CS63)

Three-Dimensional Geometric Transformations

Methods for geometric transformations in three dimensions are extended from two
dimensional methods by including considerations for the z coordinate.
A three-dimensional position, expressed in homogeneous coordinates, is represented as a
four-element column vector.

Three-Dimensional Translation

A position P = (x, y, z) in three-dimensional space is translated to a location P=(x,y,z) by


adding translation distances t x, ty, and tz to the Cartesian coordinates of P.
x'=x+tx
y=y+ty
z=z+tz
Three-dimensional translation operations can be represented in matrix form.
The coordinate positions, P and P , are represented in homogeneous coordinates with four-
element column matrices, and the translation operator T is a 4 × 4 matrix:

or
P’=T.P

Department of CSE Page 32


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.24: Moving a coordinate position with translation vector T = (tx, ty, tz )

An inverse of a three-dimensional translation matrix is obtained by negating the translation


distances tx, ty, and tz

Figure 2.25: Shifting the position of a three-dimensional object using translation vector T

Department of CSE Page 33


Computer Graphics and Fundamentals of Image Processing(21CS63)

Three-Dimensional Rotation
By convention, positive rotation angles produce counterclockwise rotations about a coordinate
axis. Positive rotations about a coordinate axis are counterclockwise, when looking along the
positive half of the axis toward the origin.

Figure 2.26:Positive rotations about a coordinate axis are counterclockwise, when looking
along the positive half of the axis toward the origin.
Three-Dimensional Coordinate-Axis Rotations
Three dimensional z-axis rotation equation is as follows:
x=xcos-ysin
y=xsin+ycos (50)
z=z

Department of CSE Page 34


Computer Graphics and Fundamentals of Image Processing(21CS63)

Parameter θ specifies the rotation angle about the z axis, and z-coordinate values are unchanged
by this transformation. In homogeneous-coordinate form, the three-dimensional z-axis rotation
equations are:

P=Rz().P

Figure 2.27: Rotation of an object about the z axis

Figure 2.27 illustrates rotation of an object about z-axis.

Transformation equations for rotations about the other two coordinate axes can be obtained with
a cyclic permutation of the coordinate parameters x, y, and z in equation 50.

x→y→z→x (51)

To obtain the x-axis and y-axis rotation transformations, cyclically replace x with y, y with z,
and z with x.

By substituting permutations 51 in equation 50, equations for x-axis rotation is obtained.

Department of CSE Page 35


Computer Graphics and Fundamentals of Image Processing(21CS63)

The equation for x axis rotation is:

y=ycos - zsin (52)

z=ysin + zcos

x=x

A cyclic permutation of coordinates in Equations 52 gives us the transformation equations for a


y-axis rotation:

The equation for y- axis rotation is:

z=zcos - xsin (53)

x=zsin + xcos

y=y

Figure 2.28: Cyclic permutation of the Cartesian-coordinate axes to produce the three sets
of coordinate-axis rotation equations

Department of CSE Page 36


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.29: Rotation of an object about the x axis

Figure 2.30: Rotation of an object about the y axis

Negative values for rotation angles generate rotations in a clockwise direction, and the identity
matrix is produced when we multiply any rotation matrix by its inverse.

i.e RR-1=I

Department of CSE Page 37


Computer Graphics and Fundamentals of Image Processing(21CS63)

General Three-Dimensional Rotations

A rotation matrix for any axis that does not coincide with a coordinate axis can be set up as a
composite transformation involving combinations of translations and the coordinate axis
rotations.

The following transformation sequence is used:

1. Translate the object so that the rotation axis coincides with the parallel coordinate axis

2. Perform the specified rotation about that axis.

3. Translate the object so that the rotation axis is moved back to its original position.

Figure 2.31: Sequence of transformations for rotating an object about an axis that is
parallel to the x axis.

Department of CSE Page 38


Computer Graphics and Fundamentals of Image Processing(21CS63)

A coordinate position P is transformed with the sequence

where the composite rotation matrix for the transformation is

When an object is to be rotated about an axis that is not parallel to one of the coordinate axes,
some additional transformations has to be performed.

The required rotation can be accomplished in five steps:

1. Translate the object so that the rotation axis passes through the coordinate origin.

2. Rotate the object so that the axis of rotation coincides with one of the coordinate axes.

3. Perform the specified rotation about the selected coordinate axis.

4. Apply inverse rotations to bring the rotation axis back to its original orientation.

5. Apply the inverse translation to bring the rotation axis back to its original spatial position.

Department of CSE Page 39


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.32 : Five transformation steps for obtaining a composite matrix for rotation about
an arbitrary axis, with the rotation axis projected onto the z axis.

Three-Dimensional Scaling

The matrix expression for the three-dimensional scaling transformation of a position P =(x, y, z)
is given by

(54)

Department of CSE Page 40


Computer Graphics and Fundamentals of Image Processing(21CS63)

The three-dimensional scaling transformation for a point position can be represented as

P=S.P

where scaling parameters sx, sy, and sz are assigned any positive values

Explicit expressions for the scaling transformation relative to the origin are

x = x · sx , y= y·sy , z = z ·sz

Scaling an object with transformation (54) changes the position of object relative to the
coordinate origin. A parameter value greater than 1 move a point farther from the origin. A
parameter value less than 1 move a point closer to the origin.

Uniform scaling is performed when s x=sy=sz. If the scaling parameters are not all equal, relative
dimensions of a transformed object are changed.

Figure 2.33: Doubling the size of an object with transformation 54 also moves the object
farther from the origin. Scaling parameter is set to 2.

Scaling transformation with respect to any selected fixed position (x f,yf,zf) can be constructed
using the following transformation sequence:

1. Translate the fixed point to the origin

Department of CSE Page 41


Computer Graphics and Fundamentals of Image Processing(21CS63)

2. Apply the scaling transformation relative to the coordinate origin

3. Translate the fixed point back to its original position.

Figure 2.34: A sequence of transformations for scaling an object relative to a selected fixed
point

The matrix representation for an arbitrary fixed-point scaling can be expressed as the
concatenation of translate-scale-translate transformations:

Department of CSE Page 42


Computer Graphics and Fundamentals of Image Processing(21CS63)

Composite Three-Dimensional Transformations

Composite three dimensional transformation can be formed by multiplying the matrix


representations for the individual operations in the transformation sequence. Transformation
sequence can be implemented by concatenating the individual matrices from right to left or from
left to right, depending on the order in which the matrix representations are specified. Rightmost
term in a matrix product is always the first transformation to be applied to an object. Leftmost
term is always the last transformation. Coordinate positions are represented as four-element
column vectors which are premultiplied by the composite 4 X 4 transformation matrix.

Other Three-Dimensional Transformations

Three-Dimensional Reflections

A reflection in a three-dimensional space can be performed relative to a selected reflection axis


or with respect to a reflection plane. In general, three dimensional-reflection matrices are set up
similarly to those for two dimensions. Reflection relative to a given axis is equivalent to 1800
rotations about that axis.

When the reflection plane is a coordinate plane (xy, xz, or yz), transformation can be thought as
a 180◦ rotation in four dimensional space with a conversion between a left-handed frame and a
right-handed frame.

An example of a reflection that converts coordinate specifications from a right handed system to
a left-handed system is shown in Figure 2.35.

Department of CSE Page 43


Computer Graphics and Fundamentals of Image Processing(21CS63)

Figure 2.35: Conversion of coordinate specifications between a right-handed and a left-


handed system can be carried out with the reflection transformation 55

The matrix representation for this reflection relative to the xy plane is

(55)

In this transformation, sign of z coordinates changes, but the values of x and y coordinates
remains unchanged.

Three-Dimensional Shears

These transformations can be used to modify object shapes. For three-dimensional, shears can be
generated relative to the z axis. A general z-axis shearing transformation relative to a selected
reference position is produced with the following matrix:

(56)

Department of CSE Page 44


Computer Graphics and Fundamentals of Image Processing(21CS63)

Shearing parameter shzx and shzy can be assigned any real value. Transformation matrix alters the
values for the x and y coordinates by an amount that is proportional to distance from zref . The z
coordinate value remain unchanged. Plane areas that are perpendicular to the z axis are shifted by
an amount equal to z-zref .

Figure 2.36: A unit cube (a) is sheared relative to the origin (b) by Matrix 56, with shzx =
shzy = 1 . Reference position zref =0

OpenGL Geometric-Transformation Functions


OpenGL Matrix Stacks

glMatrixMode specify which matrix is the current matrix.

There are four modes:

1. Modelview

2. Projection

3. Texture

4. Color

For each mode, OpenGL maintains a matrix stack. Initially, each stack contains only the identity
matrix. At any time during the processing of a scene, the top matrix on each stack is called the
“current matrix” for that mode. After we specify the viewing and geometric transformations, the

Department of CSE Page 45


Computer Graphics and Fundamentals of Image Processing(21CS63)

top of the modelview matrix stack is the 4 × 4 composite matrix that combines the viewing
transformations and the various geometric transformations that we want to apply to a scene.

OpenGL supports a modelview stack depth of at least 32.

glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, stackSize);

The above function determine the number of positions available in the modelview stack for a
particular implementation of OpenGL. It returns a single integer value to array stackSize

We can also find out how many matrices are currently in the stack with

glGetIntegerv (GL_MODELVIEW_STACK_DEPTH, numMats);

Other OpenGL symbolic constants are

1. GL_MAX_PROJECTION_STACK_DEPTH

2. GL_MAX_TEXTURE_STACK_DEPTH

3. GL_MAX_COLOR_STACK_DEPTH

There are two functions available in OpenGL for processing the matrices in a stack

glPushMatrix( );

Copy the current matrix at the top of the active stack and store that copy in the second stack
position.

glPopMatrix( );

Destroys the matrix at the top of the stack, and the second matrix in the stack becomes the
current matrix.

Department of CSE Page 46


Computer Graphics and Fundamentals of Image Processing(21CS63)

VTU Previous Year Questions

1. Explain general two dimensional pivot point rotation and derive the composite matrix.
2. Explain translation, rotation, scaling in 2D homogeneous coordinate system with matrix
representations.
3. What are the entities required to perform a rotation? Show that two successive rotations
are additive.
4. Explain with illustrations the basic 2-dimension geometric transformations used in
computer graphics.
5. What is the need of homogeneous coordinates ? Give 2-dimensional homogeneous
coordinate matrix for translation, rotation and scaling.
6. Obtain a matrix representation for rotation of a object about a specified pivot point in 2-
dimension?
7. Obtain the matrix representation for rotation of a object about an arbitrary axis ?
8. Prove that 2 successive 2D rotation are additive.
9. Prove that successive scaling are multiplicative.
10. Develop composite homogeneous transformation matrix to rotate an object with respect
to pivot point. For the triangle A(3,2) B(6,2) C(6,6) rotate it in anticlockwise direction by
90 degree keeping A(3,2) fixed. Draw the new polygon.
11. With the help of the diagram explain shearing and reflection transformation technique.
12. Give the reason to convert transformation matrix to homogeneous co-ordinate
representation and show the process of conversion. Shear the polygon A(1,1), B(3,1),
C(3,3), D(2,4), E(1,3) along x-axis with a shearing factor of 0.2.
13. With the help of suitable diagram explain basic 3D Geometric transformation techniques
and give the transformation matrix.
14. Design transformation matrix to rotate an 3D object about an axis that is parallel to one of
the co-ordinate axes ?
15. Describe 3D translation and scaling.
16. Describe any two of dimensional composite transformation

i. 2D translation ii) 2D fixed point scaling

Department of CSE Page 47


Computer Graphics and Fundamentals of Image Processing(21CS63)

17. Explain translation, rotation and scaling of 2D transformation with suitable diagrams,
code and matrix.
18. Explain OpenGL raster transformations and OpenGL geometric transformation functions.
19. Explain any two of the 3D geometrical transformation.
20. Scale the given triangle A(3,2), B(6,2), C(6,6) using the scaling factors sx=1/3 and sy=1/2
about the point A(3,2). Draw the original and scaled object.
21. Explain shear and reflection transformation technique.
22. What is concatenation of transformation? Explain the following consider 2D
i.Rotation about a fixed point
ii.Scaling about a fixed point
23. Define the following two dimensional transformations. Translation, rotation, scaling,
reflection and shearing. Give example for each.

Department of CSE Page 48

You might also like