Virtual University Computer Graphics
OpenGL Programming - II
#include "GL/glut.h"
#include <stdlib.h>
static void reshape( int w, int h )
{
glViewport( 0, 0, w, h );
}
static void keyboard( unsigned char key, int x, int y )
{
switch( key )
{
case 27: //Escape
exit(0);
break;
}
}
static void display()
{
}
static void idle()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
///////////////////////////////////////////////////////
// glRotatef(0.5f,0.0f,1.0f,0.0);
glBegin(GL_TRIANGLES);
glVertex3f(0.5f,0.2f,0.0f);
glVertex3f(0.5f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glEnd();
glutSwapBuffers();
}
static void initGL()
{
float ratio= (float)640 /480 ;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0,(float) 640 /480 , 1.0, 100.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0.0, 0.0, -5.0 );
}
int main( int argc, char* argv[] )
{
//Initialize the GLUT library.
glutInit( &argc, argv );
//Set up the OpenGL rendering context.
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
//Create a window and set its width and height.
glutCreateWindow( "Deform" );
glutReshapeWindow( 640, 480 );
//Initialize our data structures.
initGL();
//The keyboard function gets called whenever we hit a key.
glutKeyboardFunc( keyboard );
//The display function gets called whenever the window
//requires an update or when we explicitly call
//glutPostRedisplay()
glutDisplayFunc( display );
//The reshape function gets called whenever the window changes
//shape.
glutReshapeFunc( reshape );
//The idle function gets called when there are no window
//events to process.
glutIdleFunc( idle );
//Get the ball rolling!
glutMainLoop();
return 0;
}
//eof
In the above program we have first set perspective projection matrix and then rendered a triangle in idle function.
glMatrixMode
The glMatrixMode function specifies which matrix is the current matrix.
void glMatrixMode(
GLenum mode
);
Parameters
mode
The matrix stack that is the target for subsequent matrix operations. The mode parameter can assume one of three values:
Value / MeaningGL_MODELVIEW / Applies subsequent matrix operations to the modelview matrix stack.
GL_PROJECTION / Applies subsequent matrix operations to the projection matrix stack.
GL_TEXTURE / Applies subsequent matrix operations to the texture matrix stack.
Remarks
The glMatrixMode function sets the current matrix mode.
The following function retrieves information related to glMatrixMode:
Error Codes
The following are the error codes generated and their conditions.
Error code / ConditionGL_INVALID_ENUM / mode was not an accepted value.
GL_INVALID_OPERATION / glMatrixMode was called between a call to glBegin and the corresponding call to glEnd.
glLoadIdentity
The glLoadIdentity function replaces the current matrix with the identity matrix.
void glLoadIdentity(
void
);
Remarks
The glLoadIdentity function replaces the current matrix with the identity matrix. It is semantically equivalent to calling glLoadMatrix with the identity matrix
but in some cases it is more efficient.
The following functions retrieve information related to glLoadIdentity:
Error Codes
The following is the error code and its condition.
Error code / ConditionGL_INVALID_OPERATION / glLoadIdentity was called between a call to glBegin and the corresponding call to glEnd.
glTranslated, glTranslatef
The glTranslated and glTranslatef functions multiply the current matrix by a translation matrix.
void glTranslated(
GLdouble x,
GLdouble y,
GLdouble z
);
void glTranslatef(
GLfloat x,
GLfloat y,
GLfloat z
);
Parameters
x, y, z
The x, y, and z coordinates of a translation vector.
Remarks
The glTranslate function produces the translation specified by (x, y, z). The translation vector is used to compute a 4x4 translation matrix:
The current matrix (see glMatrixMode) is multiplied by this translation matrix, with the product replacing the current matrix. That is, if M is the current matrix and T is the translation matrix, then M is replaced with M•T.
If the matrix mode is either GL_MODELVIEW or GL_PROJECTION, all objects drawn after glTranslate is called are translated. Use glPushMatrix and glPopMatrix to save and restore the untranslated coordinate system.
The following functions retrieve information related to glTranslated and glTranslatef:
Error Codes
The following is the error code and its condition.
Error code / ConditionGL_INVALID_OPERATION / glTranslate was called between a call to glBegin and the corresponding call to glEnd.
gluPerspective
The gluPerspective is the function of gl utility library. This function sets up a perspective projection matrix.
void gluPerspective(
GLdouble fovy,
GLdouble aspect,
GLdouble zNear,
GLdouble zFar
);
Parameters
fovy
The field of view angle, in degrees, in the y-direction.
aspect
The aspect ratio that determines the field of view in the x-direction. The aspect ratio is the ratio of x(width) to y(height).
zNear
The distance from the viewer to the near clipping plane (always positive).
zFar
The distance from the viewer to the far clipping plane (always positive).
Remarks
The gluPerspective function specifies a viewing frustum into the world coordinate system. In general, the aspect ratio in gluPerspective should match the aspect ratio of the associated viewport. For example, aspect=2.0 means the viewer's angle of view is twice as wide in x as it is in y. If the viewport is twice as wide as it is tall, it displays the image without distortion.
The matrix generated by gluPerspective is multiplied by the current matrix, just as if glMultMatrix were called with the generated matrix. To load the perspective matrix onto the current matrix stack instead, precede the call to gluPerspective with a call to glLoadIdentity.
glRotated, glRotatef
The glRotated and glRotatef functions multiply the current matrix by a rotation matrix.
void glRotated(
GLdouble angle,
GLdouble x,
GLdouble y,
GLdouble z );
void glRotatef(
GLfloat angle,
GLfloat x,
GLfloat y,
GLfloat z );
Parameters
angle
The angle of rotation, in degrees.
x, y, z
The x, y, and z coordinates of a vector, respectively.
Remarks
The glRotate function computes a matrix that performs a counterclockwise rotation of angle degrees about the vector from the origin through the point (x, y, z).
The current matrix (see glMatrixMode) is multiplied by this rotation matrix, with the product replacing the current matrix. That is, if M is the current matrix and R is the translation matrix, then M is replaced with M•R.
If the matrix mode is either GL_MODELVIEW or GL_PROJECTION, all objects drawn after glRotate is called are rotated. Use glPushMatrix and glPopMatrix to save and restore the unrotated coordinate system.
Error Codes
The following is the error code and its condition.
Error code / ConditionGL_INVALID_OPERATION / glRotate was called between a call to glBegin and the corresponding call to glEnd.
glClearColor
The glClearColor function specifies clear values for the color buffers.
void glClearColor(
GLclampf red,
GLclampf green,
GLclampf blue,
GLclampf alpha
);
Parameters
red, green, blue, alpha
The red, green, blue, and alpha values that glClear uses to clear the color buffers. The default values are all zero.
Remarks
The glClearColor function specifies the red, green, blue, and alpha values used by glClear to clear the color buffers. Values specified by glClearColor are clamped to the range [0,1].
Error Codes
The following is the error code generated and its condition.
Error code / ConditionGL_INVALID_OPERATION / glClearColor was called between a call to glBegin and the corresponding call to glEnd.
glColor
These functions set the current color.
glColor3b, glColor3d, glColor3f, glColor3i, glColor3s,
glColor3ub, glColor3ui, glColor3us, glColor4b, glColor4d,
glColor4f, glColor4i, glColor4s, glColor4ub, glColor4ui,
glColor4us, glColor3bv, glColor3dv, glColor3fv, glColor3iv,
glColor3sv, glColor3ubv, glColor3uiv, glColor3usv,
glColor4bv, glColor4dv, glColor4fv, glColor4iv, glColor4sv,
glColor4ubv, glColor4uiv, glColor4usv.
void glColor3b(
GLbyte red,
GLbyte green,
GLbyte blue );
void glColor3f(
GLfloat red,
GLfloat green,
GLfloat blue );
Consult yourself for the documentation of rest of the functions of this type.
Parameters
red, green, blue
New red, green, and blue values for the current color.
alpha
A new alpha value for the current color. Included only in the four-argument glColor4 function.
Remarks
OpenGL stores both a current single-valued color index and a current four-valued RGBA color. The glColor function sets a new four-valued RGBA color.
There are two major variants to glColor:
· The glColor3 variants specify new red, green, and blue values explicitly, and set the current alpha value to 1.0 implicitly.
· The glColor4 variants specify all four color components explicitly.
The glColor3b, glColor4b, glColor3s, glColor4s, glColor3i, and glColor4i functions take three or four signed byte, short, or long integers as arguments. When you append v to the name, the color functions can take a pointer to an array of such values.
Current color values are stored in floating-point format, with unspecified mantissa and exponent sizes. Unsigned integer color components, when specified, are linearly mapped to floating-point values such that the largest representable value maps to 1.0 (full intensity), and zero maps to 0.0 (zero intensity). Signed integer color components, when specified, are linearly mapped to floating-point values such that the most positive representable value maps to 1.0, and the most negative representable value maps to 1.0. Floating-point values are mapped directly.
Neither floating-point nor signed integer values are clamped to the range [0,1] before updating the current color. However, color components are clamped to this range before they are interpolated or written into a color buffer.
You can update the current color at any time. In particular, you can call glColor between a call to glBegin and the corresponding call to glEnd.
glClear
The glClear function clears buffers to preset values.
void glClear(
GLbitfield mask
);
Parameters
mask
Bitwise OR operators of masks that indicate the buffers to be cleared. The four masks are as follows.
Mask / Buffer to be ClearedGL_COLOR_BUFFER_BIT / The buffers currently enabled for color writing.
GL_DEPTH_BUFFER_BIT / The depth buffer.
GL_ACCUM_BUFFER_BIT / The accumulation buffer.
GL_STENCIL_BUFFER_BIT / The stencil buffer.
Remarks
The glClear function sets the bitplane area of the window to values previously selected by glClearColor, glClearIndex, glClearDepth, glClearStencil, and glClearAccum. You can clear multiple color buffers simultaneously by selecting more than one buffer at a time using glDrawBuffer.
The pixel-ownership test, the scissor test, dithering, and the buffer writemasks affect the operation of glClear. The scissor box bounds the cleared region. The glClear function ignores the alpha function, blend function, logical operation, stenciling, texture mapping, and z-buffering.
The glClear function takes a single argument (mask) that is the bitwise OR of several values indicating which buffer is to be cleared.
The value to which each buffer is cleared depends on the setting of the clear value for that buffer.
If a buffer is not present, a glClear call directed at that buffer has no effect.
Error Codes
The following are the error codes generated and their conditions.
Error code / ConditionGL_INVALID_VALUE / Any bit other than the four defined bits was set in mask.
GL_INVALID_OPERATION / glClear was called between a call to glBegin and the corresponding call to glEnd.
glBegin, glEnd
The glBegin and glEnd functions delimit the vertices of a primitive or a group of like primitives.
void glBegin(
GLenum mode
);
void glEnd(
void
);
Parameters
mode
The primitive or primitives that will be created from vertices presented between glBegin and the subsequent glEnd. The following are accepted symbolic constants and their meanings:
GL_POINTS
Treats each vertex as a single point. Vertex n defines point n. N points are drawn.
GL_LINES
Treats each pair of vertices as an independent line segment. Vertices 2n 1 and 2n define line n. N/2 lines are drawn.
GL_LINE_STRIP
Draws a connected group of line segments from the first vertex to the last. Vertices n and n+1 define line n. N 1 lines are drawn.
GL_LINE_LOOP
Draws a connected group of line segments from the first vertex to the last, then back to the first. Vertices n and n+1 define line n. The last line, however, is defined by vertices N and 1. N lines are drawn.
GL_TRIANGLES
Treats each triplet of vertices as an independent triangle. Vertices 3n 2, 3n 1, and 3n define triangle n. N/3 triangles are drawn.
GL_TRIANGLE_STRIP
Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. For odd n, vertices n, n + 1, and n + 2 define triangle n. For even n, vertices n + 1, n, and n + 2 define triangle n. N 2 triangles are drawn.
GL_TRIANGLE_FAN
Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. Vertices 1, n + 1, and n + 2 define triangle n. N 2 triangles are drawn.
GL_QUADS
Treats each group of four vertices as an independent quadrilateral. Vertices 4n 3, 4n 2, 4n 1, and 4n define quadrilateral n. N/4 quadrilaterals are drawn.
GL_QUAD_STRIP
Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair. Vertices 2n 1, 2n, 2n + 2, and 2n + 1 define quadrilateral n. N quadrilaterals are drawn. Note that the order in which vertices are used to construct a quadrilateral from strip data is different from that used with independent data.
GL_POLYGON
Draws a single, convex polygon. Vertices 1 through N define this polygon.
Remarks
The glBegin and glEnd functions delimit the vertices that define a primitive or a group of like primitives. The glBegin function accepts a single argument that specifies which of ten primitives the vertices compose. Taking n as an integer count starting at one, and N as the total number of vertices specified, the interpretations are as follows:
· You can use only a subset of OpenGL functions between glBegin and glEnd. The functions you can use are:
glVertex
glColor
glIndex
glNormal
glMaterial
You can also use glCallList or glCallLists to execute display lists that include only the preceding functions. If any other OpenGL function is called between glBegin and glEnd, the error flag is set and the function is ignored.
· Regardless of the value chosen for mode in glBegin, there is no limit to the number of vertices you can define between glBegin and glEnd. Lines, triangles, quadrilaterals, and polygons that are incompletely specified are not drawn. Incomplete specification results when either too few vertices are provided to specify even a single primitive or when an incorrect multiple of vertices is specified. The incomplete primitive is ignored; the complete primitives are drawn.
· The minimum specification of vertices for each primitive is: