CG Module2
CG Module2
CG Module2
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.
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.
The translation distance pair (tx, ty) is called a translation vector or shift vector.
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:
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.
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 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 )
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
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.
x=rcos(+θ)=rcoscosθ-rsinsinθ (4)
y=rsin(+θ)=rcossinθ+rsincosθ
x’=xcos-ysin (6)
y’=xsin+ycos
P’=RP (7)
(8)
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):
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:
or
P’=S.P (12)
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 ½.
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
y=y.sy+yf(1-sy)
The additive terms xf(1-sx) and yf(1-sy) are constants for all points in the object.
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
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)
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
x=xh/h (16)
y=yh/h
(17)
P=T(tx,ty).P (18)
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 θ.
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
(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)
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)
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.
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)
(28)
or
T(t2x,t2y).T(t1x,t1y)=T(t1x+t2x,t1y+t2y) (29)
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.
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)
Concatenating transformation matrices for two successive scaling operations in two dimensions
produces the following composite scaling matrix:
(33)
or
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.
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
(35)
T(xr,yr).R().T(-xr,-yr)=R(xr,yr,) (36)
Figure 2.9: A transformation sequence for rotating an object about a specified pivot point
using the rotation matrix R(θ)
To produce a two-dimensional scaling with respect to a selected fixed position (x f,yf), following
sequence is followed.
1. Translate the object so that the fixed point coincides with 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 )
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.
1. Reflection and
2. Shear.
Reflection
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
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.
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)
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)
To obtain a transformation matrix for reflection about the diagonal y = x, concatenate matrices
for the transformation sequence:
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)
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)
x=x+shx.y (45)
y=y
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.
(46)
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)
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.
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
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.
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 ).
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
A block of RGB color values in a buffer can be saved in an array with the function
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
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).
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
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);
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
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
or
P’=T.P
Figure 2.24: Moving a coordinate position with translation vector T = (tx, ty, tz )
Figure 2.25: Shifting the position of a three-dimensional object using translation vector T
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
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
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.
z=ysin + zcos
x=x
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
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
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.
1. Translate the object so that the rotation axis coincides with the parallel coordinate 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.
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.
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.
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.
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)
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
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:
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:
Three-Dimensional Reflections
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.
(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)
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
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
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.
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
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.
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
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.