Module 2 Notes
Module 2 Notes
If we put the coordinate values for this rectangle into arrays, we can generate the same square
with the following code:
int vertex1 [ ] = {200, 100};
int vertex2 [ ] = {50, 250};
glRectiv (vertex1, vertex2);
Polygon
With the OpenGL primitive constant GL POLYGON, we can display a single polygon
fill area.
Each of the points is represented as an array of (x, y) coordinate values:
glBegin (GL_POLYGON);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glVertex2iv (p6);
glEnd ( );
A polygon vertex list must contain at least three vertices. Otherwise, nothing is displayed.
a) A single convex polygon fill area generated with the primitive constant GL POLYGON.
b) Two unconnected triangles generated with GL TRIANGLES.
c) Four connected triangles generated with GL TRIANGLE STRIP.
d) Four connected triangles generated with GL TRIANGLE FAN.
Triangles:
Displays the trianlges.
Three primitives in triangles, GL_TRIANGLES, GL_TRIANGLE_FAN,
GL_TRIANGLE_STRIP
glBegin (GL_TRIANGLES);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p6);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glEnd ( );
In this case, the first three coordinate points define the vertices for one triangle, the next
three points define the next triangle, and so forth.
For each triangle fill area, we specify the vertex positions in a counterclockwise order
triangle strip
glBegin (GL_TRIANGLE_STRIP);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p6);
glVertex2iv (p3);
glVertex2iv (p5);
glVertex2iv (p4);
glEnd ( );
Assuming that no coordinate positions are repeated in a list of N vertices, we obtain N – 2
triangles in the strip. Clearly, we must have N = 3 or nothing is displayed.
Each successive triangle shares an edge with the previously defined triangle, so the
ordering of the vertex list must be set up to ensure a consistent display.
Example, our first triangle (n = 1) would be listed as having vertices (p1, p2, p6). The
second triangle (n = 2) would have the vertex ordering (p6, p2, p3). Vertex ordering for
the third triangle (n = 3) would be (p6, p3, p5). And the fourth triangle (n = 4) would be
listed in the polygon tables with vertex ordering (p5, p3, p4).
Module 2
Session-1
2D Geometric Transformation
Operations that are applied to the geometric description of an object to change its position,
orientation, or size are called geometric transformations.
The geometric-transformation functions that are available in all graphics packages are those for
translation, rotation, and scaling.
Two-Dimensional Translation:
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
Translation is a rigid-body transformation that moves objects without deformation.
Two-Dimensional Rotation:
We generate a rotation transformation of an object by specifying a rotation axis and a rotation
angle.
A two-dimensional rotation of an object is obtained by repositioning the object along a
circular path in the xy plane.
In this case, we are rotating the object about a rotation axis that is perpendicular to the xy
plane (parallel to the coordinate z axis).
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 berotated.
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 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
The original coordinates of the point in polar coordinates are
The transformation equations for rotation of a point about any specified rotation position(xr ,
yr ):
Session 2
Two-Dimensional Scaling
To alter the size of an object, we apply a scaling transformation.
A simple 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’):
The basic two-dimensional scaling equations can also be written in the following matrixform
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, suchas
its centroid but any other spatial position can be selected.
For a coordinate position (x, y), the scaled coordinates (x’, y’) are then calculated fromthe
following relationships:
We can rewrite Equations to separate the multiplicative and additive terms as
Where the additive terms xf (1 − sx) and yf(1 − sy) are constants for all points in the
object.
Session 3
Homogeneous Coordinates:
Multiplicative and translational terms for a two-dimensional geometric transformationcan
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
Two dimensional rotation transformation equations about the coordinate origin can be
expressed in the matrix form
The rotation transformation operator R(θ ) is the 3 × 3 matrix with rotation parameter θ.
The scaling operator S(sx, sy) is the 3 × 3 matrix with parameters sx and sy
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
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
Session 4
3D Geometric Transformations:
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
Session 6
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.
Along y axis
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.
A coordinate position P is transformed with the sequence shown in this figure as
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
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.
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:
or
We have determined the values for cos α and sin α in terms of the components of vector
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,
we find that
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
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
Session 7
Three-Dimensional Scaling
The matrix expression for the three-dimensional scaling transformation of a position P =
(x, y, z) is given by
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
Session 8
glLoadIdentity ( );
Alternatively, we can assign other values to the elements of the current matrix using
glLoadMatrix* (elements16);
glMultMatrix*(otherElements16);
M = M.M1
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