Module 2 1
Module 2 1
VTUPulse.com
The geometric-transformation functions that are available in all graphics packages are
those for translation, rotation, and scaling.
Two-Dimensional Translation
We perform a translation on a single coordinate point by adding offsets to its
coordinates so as to generate a new coordinate position.
We are moving the original point position 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
1
Module 2 2D Viewing
The translation distance pair (tx, ty) is called a translation vector or shift vector Column
vector representation is given as
This allows us to write the two-dimensional translation equations in the matrix Form
VTUPulse.com
void translatePolygon (wcPt2D * verts, GLint nVerts, GLfloat tx, GLfloat ty)
{
GLint k;
for (k = 0; k < nVerts; k++) {
verts [k].x = verts [k].x + tx;
verts [k].y = verts [k].y + ty;
}
glBegin (GL_POLYGON);
for (k = 0; k < nVerts; k++)
glVertex2f (verts [k].x, verts [k].y);
glEnd ( );
}
Two-Dimensional Rotation
We generate a rotation transformation of an object by specifying a rotation axis and a
rotation angle.
2
Module 2 2D Viewing
A positive value for the angle θ defines a counterclockwise rotation about the pivot point,
as in above Figure , and a negative value rotates objects in the clockwise direction.
The angular and coordinate relationships of the original and transformed point positions
VTUPulse.com
are shown in Figure
In this figure, 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.
we can express the transformed coordinates in terms of angles θ and φ as
3
Module 2 2D Viewing
VTUPulse.com
The transformation equations for rotation of a point about any specified rotation position
(xr , yr ):
Code:
class wcPt2D {
public:
GLfloat x, y;
};
void rotatePolygon (wcPt2D * verts, GLint nVerts, wcPt2D pivPt, GLdouble theta)
{
wcPt2D * vertsRot;
GLint k;
for (k = 0; k < nVerts; k++) {
4
Module 2 2D Viewing
vertsRot [k].x = pivPt.x + (verts [k].x - pivPt.x) * cos (theta) - (verts [k].y -
pivPt.y) * sin (theta);
vertsRot [k].y = pivPt.y + (verts [k].x - pivPt.x) * sin (theta) + (verts [k].y -
pivPt.y) * cos (theta);
}
glBegin (GL_POLYGON);
for (k = 0; k < nVerts; k++)
glVertex2f (vertsRot [k].x, vertsRot [k].y);
glEnd ( );
}
Two-Dimensional Scaling
To alter the size of an object, we apply a scaling transformation.
A simple twodimensional scaling operation is performed by multiplying object positions
(x, y) by scaling factors sx and sy to produce the transformed coordinates (x’, y’):
VTUPulse.com
The basic two-dimensional scaling equations can also be written in the following matrix
form
5
Module 2 2D Viewing
Unequal values for sx and sy result in a differential scaling that is often used in design
applications.
In some systems, negative values can also be specified for the scaling parameters. This
not only resizes an object, it reflects it about one or more of the coordinate axes.
Figure below illustrates scaling of a line by assigning the value 0.5 to both sx and sy
We can control the location of a scaled object by choosing a position, called the fixed
point, that is to remain unchanged after the scaling transformation.
Coordinates for the fixed point, (x f , yf ), are often chosen at some object position, such
as its centroid but any other spatial position can be selected.
For a coordinate position (x, y), the scaled coordinates (x’, y’) are then calculated from
VTUPulse.com
the following relationships:
Where the additive terms x f (1 − sx) and yf (1 − sy) are constants for all points in the
object.
Code:
class wcPt2D {
public:
GLfloat x, y;
};
void scalePolygon (wcPt2D * verts, GLint nVerts, wcPt2D fixedPt, GLfloat sx, GLfloat sy)
{
wcPt2D vertsNew;
6
Module 2 2D Viewing
GLint k;
for (k = 0; k < nVerts; k++) {
vertsNew [k].x = verts [k].x * sx + fixedPt.x * (1 - sx);
vertsNew [k].y = verts [k].y * sy + fixedPt.y * (1 - sy);
}
glBegin (GL_POLYGON);
for (k = 0; k < nVerts; k++)
glVertex2f (vertsNew [k].x, vertsNew [k].y);
glEnd ( );
}
VTUPulse.com
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 we expand the representations to 3 × 3 matrices
We can use the third column of a transformation matrix for the translation terms, and all
transformation equations can be expressed as matrix multiplications.
We also need to expand the matrix representation for a two-dimensional coordinate
position to a three-element column matrix
7
Module 2 2D Viewing
VTUPulse.com
This translation operation can be written in the abbreviated form
The rotation transformation operator R(θ ) is the 3 × 3 matrix with rotation parameter θ.
8
Module 2 2D Viewing
VTUPulse.com
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
We form the inverse matrix for any scaling transformation by replacing the scaling
parameters with their reciprocals. the inverse transformation matrix is
9
Module 2 2D Viewing
The coordinate position is transformed using the composite matrixM, rather than
applying the individual transformations M1 and thenM2.
VTUPulse.com
column vectors
Also, the composite transformation matrix for this sequence of translations is
By multiplying the two rotation matrices, we can verify that two successive rotations are
additive:
R(θ2) · R(θ1) = R(θ1 + θ2)
10
Module 2 2D Viewing
So that the final rotated coordinates of a point can be calculated with the composite
rotation matrix as
P’ = R(θ1 + θ2) · P
VTUPulse.com
We can generate a two-dimensional rotation about any other pivot point (xr , yr ) 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
11
Module 2 2D Viewing
VTUPulse.com
To produce a two-dimensional scaling with respect to a selected fixed position (x f , yf ),
when we have a function that can scale relative to the coordinate origin only. This
sequence is
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:
12
Module 2 2D Viewing
The composite matrix resulting from the product of these three transformations is
VTUPulse.com
Matrix Concatenation Properties
Property 1:
Multiplication of matrices is associative.
For any three matrices,M1,M2, andM3, the matrix product M3 · M2 · M1 can be
performed by first multiplying M3 and M2 or by first multiplyingM2 and M1:
M3 ·M2 ·M1 = (M3 ·M2) ·M1 = M3 · (M2 ·M1)
We can construct a composite matrix either by multiplying from left to right
(premultiplying) or by multiplying from right to left (postmultiplying)
Property 2:
Transformation products, on the other hand, may not be commutative. The matrix
productM2 ·M1 is not equal toM1 ·M2, in general.
13
Module 2 2D Viewing
This means that if we want to translate and rotate an object, we must be careful about the
order in which the composite matrix is evaluated
Reversing the order in which a sequence of transformations is performed may affect the
transformed position of an object. In (a), an object is first translated in the x direction,
then rotated counterclockwise through an angle of 45◦. In (b), the object is first rotated
45◦ counterclockwise, then translated in the x direction.
VTUPulse.com
rotations, and scalings, can be expressed as
The four elements rsjk are the multiplicative rotation-scaling terms in the transformation,
which involve only rotation angles and scaling factors if an object is to be scaled and
rotated about its centroid coordinates (xc , yc ) and then translated, the values for the
elements of the composite transformation matrix are
Although the above matrix requires nine multiplications and six additions, the explicit
calculations for the transformed coordinates are
14
Module 2 2D Viewing
We need actually perform only four multiplications and four additions to transform
coordinate positions.
Because rotation calculations require trigonometric evaluations and several
multiplications for each transformed point, computational efficiency can become an
important consideration in rotation transformations
If we are rotating in small angular steps about the origin, for instance, we can set cos θ to
1.0 and reduce transformation calculations at each step to two multiplications and two
additions for each set of coordinates to be rotated.
These rotation calculations are
x’= x − y sin θ, y’ = x sin θ + y
VTUPulse.com
where the four elements r jk are the multiplicative rotation terms, and the elements trx
and try are the translational terms
A rigid-body change in coordinate position is also sometimes referred to as a rigid-
motion transformation.
In addition, the above matrix has the property that its upper-left 2 × 2 submatrix is an
orthogonal matrix.
If we consider each row (or each column) of the submatrix as a vector, then the two row
vectors (rxx, rxy) and (ryx, ryy) (or the two column vectors) form an orthogonal set of
unit vectors.
Such a set of vectors is also referred to as an orthonormal vector set. Each vector has unit
length as follows
15
Module 2 2D Viewing
Therefore, if these unit vectors are transformed by the rotation submatrix, then the vector
(rxx, rxy) is converted to a unit vector along the x axis and the vector (ryx, ryy) is
transformed into a unit vector along the y axis of the coordinate system
For example, the following rigid-body transformation first rotates an object through an
angle θ about a pivot point (xr , yr ) and then translates the object
Here, orthogonal unit vectors in the upper-left 2×2 submatrix are (cos θ, −sin θ) and (sin
VTUPulse.com
θ, cos θ).
16
Module 2 2D Viewing
The rotation matrix for revolving an object from position (a) to position (b) can be constructed
with the values of the unit orientation vectors u’ and v’ relative to the original orientation.
VTUPulse.com
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
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
17
Module 2 2D Viewing
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
Figure below illustrates the change in position of an object that has been reflected about
the line x = 0.
VTUPulse.com
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
18
Module 2 2D Viewing
If we choose the reflection axis as the diagonal line y = x (Figure below), the reflection
matrix is
VTUPulse.com
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, and
(3) counterclockwise rotation by 45◦.
The resulting transformation matrix is
19
Module 2 2D Viewing
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
Any real number can be assigned to the shear parameter shx Setting parameter shx to the
value 2, for example, changes the square into a parallelogram is shown below. Negative
values for shx shift coordinate positions to the left.
VTUPulse.com
A unit square (a) is converted to a parallelogram (b) using the x -direction shear with shx = 2.
20
Module 2 2D Viewing
A y-direction shear relative to the line x = xref is generated with the transformation
Matrix
VTUPulse.com
moving a block of pixel values from one position to another is termed a block transfer, a
bitblt, or a pixblt.
Figure below illustrates a two-dimensional translation implemented as a block transfer of
a refresh-buffer area
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.
21
Module 2 2D Viewing
For array rotations that are not multiples of 90◦, we need to do some extra processing.
The general procedure is illustrated in Figure below.
VTUPulse.com
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.
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
22
Module 2 2D Viewing
VTUPulse.com
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);
23
Module 2 2D Viewing
We can also combine raster transformations with logical operations to produce various
effects with the exclusive or operator
VTUPulse.com 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);
Similarly, 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.
24
Module 2 2D Viewing
VTUPulse.com
glMatrixMode (GL_MODELVIEW);
which designates the 4×4 modelview matrix as the current matrix
Two other modes that we can 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.
With the following function, we assign the identity matrix to the current matrix:
glLoadIdentity ( );
Alternatively, we can assign other values 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:
25
Module 2 2D Viewing
glMatrixMode (GL_MODELVIEW);
GLfloat elems [16];
GLint k;
for (k = 0; k < 16; k++)
elems [k] = float (k);
glLoadMatrixf (elems);
Which produces the matrix
We can also concatenate a specified matrix with the current matrix as follows:
glMultMatrix* (otherElements16);
Again, 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.
VTUPulse.com
Thus, 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’
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
26
Module 3 3D Geometric Transformations
VTUPulse.com
3.2.2 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 tx, ty, and tz to the Cartesian coordinates of P:
or
14
Module 3 3D Geometric Transformations
CODE:
VTUPulse.com
typedef GLfloat Matrix4x4 [4][4];
/* Construct the 4 x 4 identity matrix. */
void matrix4x4SetIdentity (Matrix4x4 matIdent4x4)
{
GLint row, col;
for (row = 0; row < 4; row++)
for (col = 0; col < 4 ; col++)
matIdent4x4 [row][col] = (row == col);
}
void translate3D (GLfloat tx, GLfloat ty, GLfloat tz)
{
Matrix4x4 matTransl3D;
/* Initialize translation matrix to identity. */
matrix4x4SetIdentity (matTransl3D);
15
Module 3 3D Geometric Transformations
VTUPulse.com
Three-Dimensional Coordinate-Axis Rotations
Along z axis:
16
Module 3 3D Geometric Transformations
✓ 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
x → y→ z→ x
Along x axis
Along y axis
VTUPulse.com
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.
17
Module 3 3D Geometric Transformations
✓ When an object is to be rotated about an axis that is not parallel to one of the coordinate
axes, we must perform some additional transformations we can accomplish the required
rotation 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.
VTUPulse.com
18
Module 3 3D Geometric Transformations
Where the components a, b, and c are the direction cosines for the rotation axis
• The first step in the rotation sequence is to set up the translation matrix that repositions
the rotation axis so that it passes through the coordinate origin.
• Translation matrix is given by
•
VTUPulse.com
Because rotation calculations involve sine and cosine functions, we can use standard
vector operations to obtain elements of the two rotation matrices.
A vector dot product can be used to determine the cosine term, and a vector cross product
can be used to calculate the sine term.
• Rotation of u around the x axis into the x z plane is accomplished by rotating u’ (the
projection of u in the y z plane) through angle α onto the z axis.
• If we represent the projection of u in the yz plane as the vector u’= (0, b, c), then the
cosine of the rotation angle α can be determined from the dot product of u’ and the unit
vector uz along the z axis:
19
Module 3 3D Geometric Transformations
or
• We have determined the values for cos α and sin α in terms of the components of vector
VTUPulse.com
u, the matrix elements for rotation of this vector about the x axis and into the xz plane
• Rotation of unit vector u” (vector u after rotation into the x z plane) about the y axis.
Positive rotation angle β aligns u” with vector uz .
• We can determine the cosine of rotation angle β from the dot product of unit vectors u’’
and uz. Thus,
20
Module 3 3D Geometric Transformations
• we find that
VTUPulse.com
• The specified rotation angle θ can now be applied as a rotation about the z axis as
follows:
• The transformation matrix for rotation about an arbitrary axis can then be expressed as
the composition of these seven individual transformations:
• The composite matrix for any sequence of three-dimensional rotations is of the form
21
Module 3 3D Geometric Transformations
• Assuming that the rotation axis is not parallel to any coordinate axis, we could form the
following set of local unit vectors
• If we express the elements of the unit local vectors for the rotation axis as
• Then the required composite matrix, which is equal to the product Ry(β) · Rx(α), is
VTUPulse.com
Quaternion Methods for Three-Dimensional Rotations
✓ A more efficient method for generating a rotation about an arbitrarily selected axis is to
use a quaternion representation for the rotation transformation.
✓ Quaternions, which are extensions of two-dimensional complex numbers, are useful in a
number of computer-graphics procedures, including the generation of fractal objects.
✓ One way to characterize a quaternion is as an ordered pair, consisting of a scalar part and
a vector part:
q = (s, v)
✓ A rotation about any axis passing through the coordinate origin is accomplished by first
setting up a unit quaternion with the scalar and vector parts as follows:
22
Module 3 3D Geometric Transformations
✓ Rotation of the point is then carried out with the quaternion operation
✓ The second term in this ordered pair is the rotated point position p’, which is evaluated
with vector dot and cross-products as
VTUPulse.com
✓ Using the following trigonometric identities to simplify the terms
23
Module 3 3D Geometric Transformations
where scaling parameters sx, sy, and sz are assigned any positive values.
✓ Explicit expressions for the scaling transformation relative to the origin are
✓ Because some graphics packages provide only a routine that scales relative to the
coordinate origin, we can always construct a scaling transformation with respect to any
selected fixed position (xf , yf , zf ) using the following transformation sequence:
1. Translate the fixed point to the origin.
2. Apply the scaling transformation relative to the coordinate origin
3. Translate the fixed point back to its original position.
VTUPulse.com
✓ This sequence of transformations is demonstrated
24
Module 3 3D Geometric Transformations
CODE:
class wcPt3D
{
private:
GLfloat x, y, z;
public:
/* Default Constructor:
* Initialize position as (0.0, 0.0, 0.0).
*/
wcPt3D ( ) {
x = y = z = 0.0;
}
setCoords (GLfloat xCoord, GLfloat yCoord, GLfloat zCoord)
{ x = xCoord;
y = yCoord;
z = zCoord;
VTUPulse.com
}
GLfloat getx ( ) const {
}
return x;
25
Module 3 3D Geometric Transformations
VTUPulse.com
3.2.6 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.
➔ Reflections with respect to a plane are similar; when the reflection plane is a coordinate
plane (xy, xz, or yz), we can think of the transformation 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 froma right handed
system to a left-handed system is shown below
26
Module 3 3D Geometric Transformations
Three-Dimensional Shears
➔ These transformations can be used to modify object shapes.
➔ For three-dimensional we can also generate shears relative to the z axis.
➔ A general z-axis shearing transformation relative to a selected reference position is
produced with the following matrix:
VTUPulse.com
A unit cube (a) is sheared relative to the origin (b) by Matrix 46, with shzx = shzy = 1.
27
Module 3 3D Geometric Transformations
❖ Affine transformations (in two dimensions, three dimensions, or higher dimensions) have
the general properties that parallel lines are transformed into parallel lines, and finite
points map to finite points.
❖ Translation, rotation, scaling, reflection,andshear are examples of affine transformations.
❖ Another example of an affine transformation is the conversion of coordinate descriptions
for a scene from one reference system to another because this transformation can be
described as a combination of translation and rotation
❖ for that mode. the modelview matrix stack is the 4 × 4 composite matrix that
VTUPulse.com
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,
28
Module 3 3D Geometric Transformations
We have 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 ( );
which destroys the matrix at the top of the stack, and the second matrix in the stack
becomes the current matrix
VTUPulse.com
29