2d transformation_Module 2
2d transformation_Module 2
MODULE 2
2D and 3D graphics with OpenGL: 2D Geometric Transformations: Basic 2D Geometric
Transformations, matrix representations and homogeneous coordinates, 2D Composite
transformations, other 2D transformations, raster methods for geometric transformations,
OpenGL raster transformations, OpenGL geometric transformations function,
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.
The translation distance pair (tx, ty) is called a translation vector or shift vector.
Column vector representation is given as
1
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
This allows us to write the two-dimensional translation equations in the matrix Form
Pseudocode:
class wcPt2D {
public: GLfloat x, y;
};
void translatePolygon (wcPt2D * verts, GLint nVerts, GLfloat tx, GLfloat ty)
GLint k;
glBegin (GL_POLYGON);
glEnd ( );
}
Example:
2
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
3
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
Two-Dimensional Rotation
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
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 are
shown in the 2nd figure shown above. 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
4
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
P’ = R· P
The transformation equations for rotation of a point about any specified rotation position
(xr , yr ):
Code:
class wcPt2D {
public: GLfloat x, y;
};
5
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
void rotatePolygon (wcPt2D * verts, GLint nVerts, wcPt2D pivPt, GLdouble theta)
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);
glEnd ( );
Formula:
To rotate a point (x, y) around the origin by an angle θ, the new coordinates (x', y') are given by:
x' = x cos(θ) - y sin(θ)
y' = x sin(θ) + y cos(θ)
Example:
Let's consider a point P with coordinates (3, 4) and rotate this point by θ = 30°.
Conversion to Radians:
First, we need to convert the angle from degrees to radians because the trigonometric functions in most
programming languages use radians. The conversion formula is:
θ_radians = θ_degrees * (π / 180)
For θ = 30°:
θ_radians = 30 * (π / 180) = π / 6
6
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
x' = x cos(π / 6) - y sin(π / 6)
y' = x sin(π / 6) + y cos(π / 6)
Answer:
The new coordinates of the point P' after rotating (3, 4) by 30° are approximately (0.598, 4.964).
Example 2
Let's consider a triangle with vertices A(1, 2), B(3, 4), and C(5, 6) and rotate this triangle by θ = 45°.
Conversion to Radians:
First, we need to convert the angle from degrees to radians because the trigonometric functions in most
programming languages use radians. The conversion formula is:
θ_radians = θ_degrees * (π / 180)
For θ = 45°:
θ_radians = 45 * (π / 180) = π / 4
Vertex A:
x1' = x1 cos(π / 4) - y1 sin(π / 4)
y1' = x1 sin(π / 4) + y1 cos(π / 4)
Substituting the values:
x1' = 1 * 0.707 - 2 * 0.707 = 0.707 - 1.414 = -0.707
y1' = 1 * 0.707 + 2 * 0.707 = 0.707 + 1.414 = 2.121
A'(-0.707, 2.121)
Vertex B:
x2' = x2 cos(π / 4) - y2 sin(π / 4)
y2' = x2 sin(π / 4) + y2 cos(π / 4)
Substituting the values:
x2' = 3 * 0.707 - 4 * 0.707 = 2.121 - 2.828 = -0.707
y2' = 3 * 0.707 + 4 * 0.707 = 2.121 + 2.828 = 4.949
B'(-0.707, 4.949)
Vertex C:
x3' = x3 cos(π / 4) - y3 sin(π / 4)
y3' = x3 sin(π / 4) + y3 cos(π / 4)
Substituting the values:
x3' = 5 * 0.707 - 6 * 0.707 = 3.535 - 4.242 = -0.707
y3' = 5 * 0.707 + 6 * 0.707 = 3.535 + 4.242 = 7.778
C'(-0.707, 7.778)
7
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
Answer:
The new coordinates of the vertices after rotating the triangle A(1, 2), B(3, 4), and C(5, 6) by 45° are
approximately:
A'(-0.707, 2.121)
B'(-0.707, 4.949)
C'(-0.707, 7.778)
Two-Dimensional Scaling
(x, y) by scaling factors sx and sy to produce the transformed coordinates (x’, y’):
The basic two-dimensional scaling equations can also be written in the following matrix form
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 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
8
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
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, (xf , 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 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;
GLint k;
glBegin (GL_POLYGON);
glEnd ( );
Matrix Representations
9
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
Each of the three basic two-dimensional transformations (translation, rotation, and scaling)
can be expressed in the general matrix form
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
A standard technique for accomplishing this is to expand each two dimensional coordinate-
position representation (x, y) to a three-element representation (xh, yh, h), called homogeneous
coordinates, where the homogeneous parameter h is a nonzero value such that
10
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
Two-Dimensional Rotation Matrix
The rotation transformation operator R(θ ) is the 3 × 3 matrix with rotation parameter θ.
A scaling transformation relative to the coordinate origin can now be expressed as the matrix
multiplication
A two-dimensional rotation through an angle θ about the coordinate origin has the inverse
transformation matrix
11
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
We form the inverse matrix for any scaling transformation by replacing the scaling parameters
with their reciprocals. the inverse transformation matrix is
The coordinate position is transformed using the composite matrix M, rather than applying the
individual transformations M1 and thenM2.
If two successive translation vectors (t1x, t1y) and (t2x, t2y) are applied to a two dimensional
coordinate position P, the final transformed location P’ is calculated as
12
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
By multiplying the two rotation matrices, we can verify that two successive rotations are
additive:
So that the final rotated coordinates of a point can be calculated with the composite rotation
matrix as
P’ = R(θ1 + θ2) · P
Concatenating transformation matrices for two successive scaling operations in two dimensions
produces the following composite scaling matrix
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.
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
13
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
Example
Rotate a triangle with vertices p[3][2] = {{-50, -50}, {50, -50}, {0, 50}}. Pivot points and rotation angle is given as
xp=50, yp=50 and t=30.
Given:
Vertices of the triangle: P1(-50, -50), P2(50, -50), P3(0, 50)
Pivot point: (xp, yp) = (50, 50)
Rotation angle: θ = 30°
Required:
New coordinates of the vertices P1', P2', and P3' after the rotation
14
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
Formula:
To rotate a point (x, y) around a pivot point (xp, yp) by an angle θ, the new coordinates (x', y') are given
by:
x' = xp + (x - xp) cos θ - (y - yp) sin θ
y' = yp + (x - xp) sin θ + (y - yp) cos θ
Conversion to Radians:
First, we need to convert the angle from degrees to radians because the trigonometric functions in most
programming languages use radians. The conversion formula is:
θ_radians = θ_degrees * (π / 180)
For θ = 30°:
θ_radians = 30 * (π / 180) = π / 6 ≈ 0.5236 radians
Vertex P1:
Original coordinates: P1(-50, -50)
x'1 = 50 + (-50 - 50) cos(30°) - (-50 - 50) sin(30°)
y'1 = 50 + (-50 - 50) sin(30°) + (-50 - 50) cos(30°)
Substituting the values:
x'1 = 50 + (-100 * 0.866) - (-100 * 0.5)
y'1 = 50 + (-100 * 0.5) + (-100 * 0.866)
x'1 = 50 - 86.6 + 50 = 13.4
y'1 = 50 - 50 - 86.6 = -86.6
Vertex P2:
Original coordinates: P2(50, -50)
x'2 = 50 + (50 - 50) cos(30°) - (-50 - 50) sin(30°)
y'2 = 50 + (50 - 50) sin(30°) + (-50 - 50) cos(30°)
Substituting the values:
x'2 = 50 + (0) - (-100 * 0.5)
y'2 = 50 + (0) + (-100 * 0.866)
x'2 = 50 + 50 = 100
y'2 = 50 - 86.6 = -36.6
Vertex P3:
Original coordinates: P3(0, 50)
x'3 = 50 + (0 - 50) cos(30°) - (50 - 50) sin(30°)
y'3 = 50 + (0 - 50) sin(30°) + (50 - 50) cos(30°)
Substituting the values:
x'3 = 50 + (-50 * 0.866) - (0)
y'3 = 50 + (-50 * 0.5) + (0)
x'3 = 50 - 43.3 = 6.7
y'3 = 50 - 25 = 25
15
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
Answer:
The new coordinates of the vertices after rotating the triangle P1(-50, -50), P2(50, -50), and P3(0, 50) by
30° are approximately:
P1' ≈ (13.4, -86.6)
P2' ≈ (100, -36.6)
P3' ≈ (6.7, 25)
Deciding the pivot point for rotation depends on the specific requirements of your application or
the effect you want to achieve with the rotation. Here are some considerations:
Choosing the pivot point ultimately depends on your specific application's requirements. For
natural-looking rotations, especially when dealing with geometric shapes, rotating around the
object's center or centroid is often preferred.
16
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
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:
We can scale an object in other directions by rotating the object to align the desired scaling
directions with the coordinate axes before applying the scaling transformation.
Suppose we want to apply scaling factors with values specified by parameters s1 and s2 in the
directions shown in Figure
17
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
The composite matrix resulting from the product of these three transformations is
We can construct a composite matrix either by multiplying from left to right (pre
multiplying) or by multiplying from right to left (post multiplying)
Property 2:
Transformation products, on the other hand, may not be commutative. The matrix
productM2 · M1 is not equal toM1 · M2, in general.
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
18
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
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.
The four elements 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
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
19
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
If a transformation matrix includes only translation and rotation parameters, it is a rigid- body
transformation matrix.
The general form for a two-dimensional rigid-body transformation matrix is
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
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
θ, cos θ).
20
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
The orthogonal property of rotation matrices is useful for constructing the matrix when we
know the final orientation of an object, rather than the amount of angular rotation necessary to
put the object into that position.
We might want to rotate an object to align its axis of symmetry with the viewing (camera)
direction, or we might want to rotate one object so that it is above another object.
Figure shows an object that is to be aligned with the unit direction vectors u_ and v
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.
Prove Additive Nature of Successive Translation and Rotation, and Multiplicative Nature
of Successive Scaling in 2D Transformations
In 2D transformations, successive translations and rotations are additive, while successive scaling are
multiplicative. Below are the proofs for these properties.
21
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
If we translate a point P(x, y) first by (T1_x, T1_y) and then by (T2_x, T2_y), the new position P' can be
computed as follows:
ii) Rotation:
Successive rotations are also additive. Let's consider two rotations:
1. Rotation by θ1
2. Rotation by θ2
If we rotate a point P(x, y) first by θ1 and then by θ2, the new position P' can be computed as follows:
Therefore:
22
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
If we scale a point P(x, y) first by S1 and then by S2, the new position P' can be computed as follows:
Summary:
In 2D transformations: Successive Translations: Additive.
Successive Rotations: Additive.
Successive Scalings: Multiplicative.
These properties can be confirmed by applying the transformations to points or using matrix
multiplication for composite transformations.
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
23
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
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
A reflection about the line x = 0 (the y axis) flips x coordinates while keeping y
Figure below 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
24
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
If we choose the reflection axis as the diagonal line y = x (Figure below), the reflection matrix is
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◦,
25
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
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.
A unit square (a) is converted to a parallelogram (b) using the x -direction shear with shx = 2.
26
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
A y-direction shear relative to the line x = xref is generated with the transformation
Matrix
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 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.
27
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
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 below demonstrates the array manipulations that can be used to rotate a pixel
For array rotations that are not multiples of 90◦, we need to do some extra processing.
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
28
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
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);
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);
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
Example: glScalef (2.0, -3.0, 1.0);
29
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
• In OpenGL, matrix stacks are used to manage and apply transformations to objects and scenes in
a hierarchical manner.
• There are several types of matrices and corresponding matrix stacks in OpenGL, each serving a
specific purpose in the rendering pipeline.
• These matrices include the modelview matrix, projection matrix, and texture matrix.
In OpenGL, there are several matrix stacks used for different purposes. The glMatrixMode() function sets
the current matrix mode, meaning it defines which matrix stack will be affected by subsequent matrix
operations such as glLoadIdentity(), glTranslatef(), glRotatef(), and glScalef().
Matrix Modes
OpenGL provides different matrix modes, each associated with a specific type of transformation:
1. GL_MODELVIEW:
o This mode is used for modeling and viewing transformations. It is used to define the
camera's position and orientation, as well as to position and orient objects within the scene.
o Commonly used for transformations that affect the position, rotation, and scale of objects.
2. GL_PROJECTION:
o This mode is used to define the projection transformation, which determines how objects
are projected onto the screen (e.g., perspective or orthographic projection).
o It affects how the 3D scene is mapped to the 2D screen.
3. GL_TEXTURE:
o This mode is used for texture transformations, which can modify how textures are applied
to objects.
4. GL_COLOR:
o This mode is used for color matrix transformations (less commonly used in basic OpenGL).
OpenGL uses several matrix modes to handle different types of transformations and projections in 3D
graphics rendering. These modes define how transformations are applied to objects in the scene. Here are
the primary matrix modes in OpenGL:
GL_MODELVIEW
The GL_MODELVIEW matrix mode is used for modeling and viewing transformations. This mode combines
the model transformation (which positions and orients objects) and the view transformation (which
positions and orients the camera).
Purpose:
o To transform object coordinates to eye coordinates.
o To apply transformations like translation, rotation, and scaling to objects.
Typical Usage:
o Setting up the camera's position and orientation.
o Positioning, orienting, and scaling objects in the scene.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, // Camera position
30
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
0.0, 0.0, 0.0, // Look-at point
0.0, 1.0, 0.0); // Up vector
glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
GL_PROJECTION
The GL_PROJECTION matrix mode is used for projection transformations. This mode defines how the 3D
world is projected onto the 2D screen.
Purpose:
o To define the viewing volume.
o To apply perspective or orthographic projections.
Typical Usage:
o Setting up perspective projection (mimicking the human eye).
o Setting up orthographic projection (parallel projection).
Example:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, // Field of view
1.33, // Aspect ratio
1.0, // Near clipping plane
100.0);// Far clipping plane
GL_TEXTURE
The GL_TEXTURE matrix mode is used for texture transformations. This mode allows you to apply
transformations to texture coordinates.
Purpose:
o To manipulate how textures are applied to surfaces.
o To apply texture translations, rotations, and scaling.
Typical Usage:
o Animating textures.
o Aligning textures on surfaces.
Example:
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
31
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
GL_COLOR
The GL_COLOR matrix mode is used for color transformations. This mode is less commonly used and is
primarily for color matrix operations in advanced applications.
Purpose:
o To transform color values.
o To apply effects like color scaling and biasing.
Typical Usage:
o Advanced color manipulation.
o Special effects involving color transformations.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xmin, xmax, ymin, ymax);
glMatrixMode(GL_MODELVIEW);
Projection Transformation:
32
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
o The GL_PROJECTION matrix mode affects how the scene is projected onto the screen. By
setting up an orthographic projection with gluOrtho2D, you ensure that objects are drawn
with parallel projection, suitable for 2D rendering.
o The orthographic projection defined by gluOrtho2D creates a 2D coordinate system where
xmin to xmax spans the horizontal axis and ymin to ymax spans the vertical axis.
o For example, if xmin = 0, xmax = 800, ymin = 0, and ymax = 600, the coordinate system
would correspond to a window or screen size of 800x600 units.
Identity Matrix:
o The call to glLoadIdentity ensures that any previous transformations in the projection
matrix are cleared, providing a clean slate for the new orthographic projection defined by
gluOrtho2D.
Switching Back to Modelview:
o After setting up the projection matrix, the matrix mode is switched back to
GL_MODELVIEW. This is crucial because most subsequent transformations (like
positioning and rotating objects) should affect the modelview matrix, not the projection
matrix.
o This separation ensures that projection transformations and object transformations are
handled independently.
Practical Impact
Rendering Context:
o By setting up an orthographic projection, the rendering context is now ready for 2D
drawing operations. Any objects drawn will use the 2D coordinate system defined by xmin,
xmax, ymin, and ymax.
2D Graphics:
o This setup is commonly used for 2D graphics, UI elements, and HUDs (Heads-Up Displays)
in games, where you want to draw without perspective distortion.
o For example, you could draw a rectangle from (0, 0) to (800, 600), and it would map directly
to the screen coordinates.
3D Transformations
Three-Dimensional Geometric Transformations
• Methods for geometric transformations in three dimensions are extended from two dimensional
methods by including considerations for the z coordinate.
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:
33
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
or
CODE:
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);
}
34
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
void translate3D (GLfloat tx, GLfloat ty, GLfloat tz)
{
Matrix4x4 matTransl3D;
/* Initialize translation matrix to identity. */ matrix4x4SetIdentity
(matTransl3D); matTransl3D [0][3] = tx; matTransl3D [1][3] = ty;
matTransl3D [2][3] = tz;
}
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.
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
35
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
Along x axis
Along y axis
An inverse three-dimensional rotation matrix is obtained in the same by replacing θ with −θ.
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.
Three-Dimensional Scaling
The matrix expression for the three-dimensional scaling transformation of a position P =
(x, y, z) is given by
36
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
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.
This sequence of transformations is demonstrated
CODE:
class wcPt3D
37
Dr. Sudhamani M J, Associate Professor, Dept of CSE
CG&IP - Module 2
{ 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;
}
GLfloat getx ( ) const { return x;
}
GLfloat gety ( ) const { return y;
}
GLfloat getz ( ) const { return z;
}
};
typedef float Matrix4x4 [4][4];
void scale3D (GLfloat sx, GLfloat sy, GLfloat sz, wcPt3D fixedPt)
{
Matrix4x4 matScale3D;
/* Initialize scaling matrix to identity. */ matrix4x4SetIdentity
(matScale3D); matScale3D [0][0] = sx;
matScale3D [0][3] = (1 - sx) * fixedPt.getx ( ); matScale3D [1][1] = sy;
matScale3D [1][3] = (1 - sy) * fixedPt.gety ( ); matScale3D [2][2] = sz;
matScale3D [2][3] = (1 - sz) * fixedPt.getz ( );
38
Dr. Sudhamani M J, Associate Professor, Dept of CSE