0% found this document useful (0 votes)
12 views

Module 2 Notes

The document covers OpenGL polygon fill-area functions and 2D geometric transformations, detailing how to create polygons and triangles using various OpenGL functions. It explains geometric transformations including translation, rotation, and scaling, along with their matrix representations and homogeneous coordinates. Additionally, it introduces 3D geometric transformations and how they extend from 2D methods, emphasizing the inclusion of the z coordinate.

Uploaded by

ranjitha1065r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Module 2 Notes

The document covers OpenGL polygon fill-area functions and 2D geometric transformations, detailing how to create polygons and triangles using various OpenGL functions. It explains geometric transformations including translation, rotation, and scaling, along with their matrix representations and homogeneous coordinates. Additionally, it introduces 3D geometric transformations and how they extend from 2D methods, emphasizing the inclusion of the z coordinate.

Uploaded by

ranjitha1065r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Module 1 contd

OpenGL Polygon Fill-Area Functions :


 A glVertex function is used to input the coordinates for a single polygon vertex, and a
complete polygon is described with a list of vertices placed between a glBegin/glEnd
pair.
 By default, a polygon interior is displayed in a solid color, determined by the current
color settings we can fill a polygon with a pattern and we can display polygon edges as
line borders around the interior fill.
 There are six different symbolic constants that we can use as the argument in the glBegin
function to describe polygon fill areas
 In some implementations of OpenGL, the following routine can be more efficient than
generating a fill rectangle using glVertex specifications:
glRect* (x1, y1, x2, y2);
 One corner of this rectangle is at coordinate position (x1, y1), and the opposite corner of
the rectangle is at position (x2, y2).
 Suffix codes for glRect specify the coordinate data type and whether coordinates are to be
expressed as array elements.
 These codes are i (for integer), s (for short), f (for float), d (for double), and v (for
vector).
 Example
glRecti (200, 100, 50, 250);

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:

 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

 The translation values of x’ and y’ is calculated as

 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

 Substituting expressions of x and y in the equations of x’ and y’ we get

 We can write the rotation equations in the matrix form


P’ = R·P
Where the rotation matrix is,

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

 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

Where S is the 2 × 2 scaling matrix


 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

 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

Matrix Representations and Homogeneous Coordinates:


 Each of the three basic two-dimensional transformations (translation, rotation, andscaling)
can be expressed in the general matrix form

 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 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

 A general two-dimensional homogeneous coordinate representation could also be writtenas


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

Two-Dimensional Translation Matrix:


 The homogeneous-coordinate for translation is given by

 This translation operation can be written in the abbreviated formwith

T(tx,ty) as the 3 × 3 translation matrix

Two-Dimensional Rotation Matrix:

 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 θ.

Two-Dimensional Scaling Matrix:


 A scaling transformation relative to the coordinate origin can now be expressed as the
matrix multiplication

 The scaling operator S(sx, sy) is the 3 × 3 matrix with parameters sx and sy

OpenGL Raster Transformations:


 A translation of a rectangular array of pixel-color values from one buffer area to another rcan
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
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

OpenGL Functions for Two-Dimensional Geometric Transformations


 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.
 In addition, 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

Basic OpenGL Geometric Transformations


 A 4× 4 translation matrix is constructed with the following routine:
glTranslate* (tx, ty, tz);
o Translation parameters tx, ty, and tz can be assigned any real-number values, andthe
single suffix code to be affixed to this function is either f (float) or d (double).
o For two-dimensional applications, we set tz = 0.0; and a two-dimensional positionis
represented as a four-element column matrix with the z component equal to 0.0.
o example: glTranslatef (25.0, -10.0, 0.0);
 Similarly, a 4 × 4 rotation matrix is generated with
glRotate* (theta, vx, vy, vz);
o where the vector v = (vx, vy, vz) can have any floating-point values for its
components.
o This vector defines the orientation for a rotation axis that passes through the
coordinate origin.
o If v is not specified as a unit vector, then it is normalized automatically before the
elements of the rotation matrix are computed.
o The suffix code can be either f or d, and parameter theta is to be assigned a
rotation angle in degree.
o 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);
o The suffix code is again either f or d, and the scaling parameters can be assignedany
real-number values.
o 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
o Example: glScalef (2.0, -3.0, 1.0);
Module 2 Continued
Session 5

3D Geometric 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.
A three-dimensional position, expressed in homogeneous coordinates, is represented as a
four-element column vector

Three-Dimensional Translation
 A position P = (x, y, z) in three-dimensional space is translated to a location P’= (x’, y’,
z’) by adding translation distances tx, ty, and tz to the Cartesian coordinates of P:

 We can express these three-dimensional translation operations in matrix form

or

 Moving a coordinate position with translation vector T = (tx , ty , tz ) .

 Shifting the position of a three-dimensional object using translation vector T.


 An inverse of a three-dimensional translation matrix is obtained by negating the
translation distances tx, ty, and tz

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.

Three-Dimensional Coordinate-Axis Rotations


Along z axis:

 In homogeneous-coordinate form, the three-dimensional z-axis rotation equations are


 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

 An inverse three-dimensional rotation matrix is obtained in the same by replacing θ with


−θ.

General Three-Dimensional Rotations


 A rotation matrix for any axis that does not coincide with a coordinate axis can be set up
as a composite transformation involving combinations of translations and the coordinate-
axis rotations the following transformation sequence is used:

1. Translate the object so that the rotation axis coincides with the parallel coordinate axis.
2. Perform the specified rotation about that axis.
3. Translate the object so that the rotation axis is moved back to its original position.
 A coordinate position P is transformed with the sequence shown in this figure as

Where the composite rotation matrix for the transformation is

 When an object is to be rotated about an axis that is not parallel to one of the coordinate
axes, 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

Components of the rotation-axis vector are then computed as


V = P2 − P1
= (x2 − x1, y2 − y1, z2 − z1)
 The unit rotation-axis vector u is

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

 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:

where d is the magnitude of u’

 The coordinate-independent form of this cross-product is


 and the Cartesian form for the cross-product gives us

 Equating the above two equations

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,

 Comparing the coordinate-independent form of the cross-product


with the Cartesian form

we find that

 The transformation matrix for rotation of u” about the y axis is

 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

 The upper-left 3 × 3 submatrix of this matrix is orthogonal

 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

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

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

Basic OpenGL Geometric Transformations


 A 4× 4 translation matrix is constructed with the following routine:
glTranslate* (tx, ty, tz);
o 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).
o 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.
o example: glTranslatef (25.0, -10.0, 0.0);
 Similarly, a 4 × 4 rotation matrix is generated with
glRotate* (theta, vx, vy, vz);
o where the vector v = (vx, vy, vz) can have any floating-point values for its
components.
o This vector defines the orientation for a rotation axis that passes through the
coordinate origin.

o If v is not specified as a unit vector, then it is normalized automatically before the


elements of the rotation matrix are computed.
o The suffix code can be either f or d, and parameter theta is to be assigned a rotation
angle in degree.
o 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);
o The suffix code is again either f or d, and the scaling parameters can be assigned any
real-number values.

o 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
o Example: glScalef (2.0, -3.0, 1.0);

Session 8

What are OpenGL Matrix Operations?

OpenGL Matrix Stacks

 The default argument for the glMatrixMode function is GL_MODELVIEW.


 Once we are in the modelview mode (or any other mode), a call to a transformation routine
generates a matrix that is multiplied by the current matrix for that mode.
 In addition, we can assign values to the elements of the current matrix, and there are two
functions in the OpenGL library for this purpose.
 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. That is, we first list the
four elements in the first column, and then we list the four elements in the second column,
the third column, and finally the fourth column.
 To illustrate this ordering, we initialize the modelview matrix with the following code:

which produces the matrix


 We can also concatenate a specified matrix with the current matrix:

glMultMatrix*(otherElements16);

 The suffix code is either f or d, and parameter otherElements16 is a 16-element , single


subscripted array that lists the elements of some other matrix in column-major order.
 The current matrix is postmultiplied by the matrix specified in glMultMatrix, and this
product replaces the current matrix. Thus, assuming that the current matrix is the
modelview matrix, which we designate as M, then the updated modelview matrix is
compted as

M = M.M1

 OpenGL supports a modelview stack depth of at least 32,


glGetIntegerv (GL_MAX_MODELVIEW_STACK_DEPTH, stackSize);
 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

 other OpenGL symbolic constants:


GL_MAX_PROJECTION_STACK_DEPTH,
GL_MAX_TEXTURE_STACK_DEPTH, or GL_MAX_COLOR_STACK_DEPTH.
 We can also find out how many matrices are currently in the stack with
glGetIntegerv (GL_MODELVIEW_STACK_DEPTH, numMats);

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

You might also like