3.c) Explain OpenGL raster transformations and OpenGL geometric transformation functions.
Answer:
OpenGL raster transformations: –
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
2. 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
3. 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
4. glPixelZoom (sx, sy);
We can also combine raster transformations with logical operations to produce various effects with the exclusive or operator.
OpenGL geometric transformation functions: –
- glTranslate* (tx, ty, tz);
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);
Using above statement defined coordinate positions is translated 25 units in the x direction and -10 units in the y direction.
2. glRotate* (theta, vx, vy, vz);
where the vector v = (vx, vy, vz) can have any floating-point values for its components.
This vector defines the orientation for a rotation axis that passes through the coordinate origin.
If v is not specified as a unit vector, then it is normalized automatically before the elements of the rotation matrix are computed.
The suffix code can be either f or d, and parameter theta is to be assigned a rotation angle in degree. Example: glRotatef (90.0, 0.0, 0.0, 1.0); sets up the matrix for a 90◦ rotation about the z axis.
3. 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);
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 Stacks
glMatrixMode specify which matrix is the current matrix.
There are four modes:
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.
OpenGL supports a modelview stack depth of at least 32. 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 glGetIntegerv (GL_MODELVIEW_STACK_DEPTH, numMats);
Other OpenGL symbolic constants are
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.