You are on page 1of 6

void myKeyboardFunc( unsigned char key, int x, int y )

switch ( key ) {
case 'r':
RunMode = 1-RunMode;
// Toggle to opposite value
if ( RunMode==1 ) {
case 's':
RunMode = 1;
RunMode = 0;
case 27:
// Escape key

This function will enable the escape button to close the window and enable R for
toggle the shape from stop to start by checking the RunMode value if its 1 it will
reshape the shape and S is for one step of the rotating the shape by toggling the
RunMode value first time by 1 to run the one step only and draw the shape then
turn the toggle to 0 to stop the movment

void mySpecialKeyFunc( int key, int x, int y )

switch ( key ) {
if ( AnimateStep < 1.0e3) {
AnimateStep *= sqrt(2.0);
if (AnimateStep>1.0e-6) {
AnimateStep /= sqrt(2.0);

// Avoid overflow problems

// Increase the angle increment

// Avoid underflow problems.

// Decrease the angle increment

This function is to enable up and down arrows keys :

UP for increase the speed by constant value by multiply the previous value with
square root of 2
Down for decrease the speed by constant value by divided the previous value
with square root of 2
void resizeWindow(int w, int h)
double scale, center;
double windowXmin, windowXmax, windowYmin, windowYmax;
// Define the portion of the window used for OpenGL rendering.
glViewport( 0, 0, w, h ); // View port uses whole window
// Set up the projection view matrix: orthographic projection
// Determine the min and max values for x and y that should appear in the window.
// The complication is that the aspect ratio of the window may not match the
aspect ratio of the scene we want to view.
w = (w==0) ? 1 : w;
h = (h==0) ? 1 : h;
if ( (Xmax-Xmin)/w < (Ymax-Ymin)/h ) {
scale = ((Ymax-Ymin)/h)/((Xmax-Xmin)/w);
center = (Xmax+Xmin)/2;
windowXmin = center - (center-Xmin)*scale;
windowXmax = center + (Xmax-center)*scale;
windowYmin = Ymin;
windowYmax = Ymax;
else {
scale = ((Xmax-Xmin)/w)/((Ymax-Ymin)/h);
center = (Ymax+Ymin)/2;
windowYmin = center - (center-Ymin)*scale;
windowYmax = center + (Ymax-center)*scale;
windowXmin = Xmin;
windowXmax = Xmax;
// Now that we know the max & min values for x & y
that should be visible in the window,
we set up the orthographic projection.
glMatrixMode( GL_PROJECTION );
glOrtho( windowXmin, windowXmax, windowYmin, windowYmax, -1, 1 );

This function is for resizing the shape for every time when the scroll the window
and change the window size by checking if the width is smaller than height then
the scale will be flipped vertically then the percentage of change the width will be
equal to the change of the height
And adjust the shape position by the old center with the value of the scale and set
the window min and max with the current min and max for the window
And if the width was bigger than height it will change the width equal to the
height by also adjust the position of the shape based on the position of the center
of the windows and set the window min and max with the current min and max
for the window

void drawScene(void)
// Clear the rendering window
if (RunMode==1) {
// Calculate animation parameters
CurrentAngle += AnimateStep;
if ( CurrentAngle > 360.0 ) {
CurrentAngle -= 360.0*floor(CurrentAngle/360.0); // Don't allow
// Rotate the image
glMatrixMode( GL_MODELVIEW );
glTranslatef( 1.5, 1.5, 0.0 );
rotation center from origin
glRotatef( CurrentAngle, 0.0, 0.0, 1.0 );
glTranslatef( -1.5, -1.5, 0.0 );
center to origin

// Current matrix affects objects

// Initialize to the
// Translate
// Rotate through animation angle
// Translate rotation

// Draw three overlapping triangles of different colors

glBegin( GL_TRIANGLES );
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.3, 1.0, 0.5 );

glVertex3f( 2.7, 0.85, 0.0 );

glVertex3f( 2.7, 1.15, 0.0 );
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f(2.53, 0.71, 0.5 );
glVertex3f(1.46, 2.86, 0.0 );
glVertex3f(1.2, 2.71, 0.0 );
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f(1.667, 2.79, 0.5);
glVertex3f(0.337, 0.786, 0.0);
glVertex3f(0.597, 0.636, 0.0);
// Flush the pipeline, swap the buffers
if ( RunMode==1 ) {
glutPostRedisplay(); // Trigger an automatic redraw for animation

This function will first initiate and clear the window variables which are
Then check the RunMode to define which state the shape is in if 1 means should
be run otherwise will toggle and wait to change the value again and do the
rotation by use the current angle with adding the step which we reached
If the current angle is greater than 360 which is a full cycle will decrease the angle
size to prevent the angle flow
Then define the display mode which we used GL_MODELVIEW
And then rotate the shape by the angle of rotate
Then start drawing the shape by define the glBegin that it will draw triangles then
define the triangles by 3 points using glVertex3f and the color for each by
The close the drawing
And then check the toggle RunMode and redraw the shape again

int main( int argc, char** argv )

// We're going to animate it, so double buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
// Window position (from top corner), and size (width% and hieght)
glutInitWindowPosition( 10, 60 );
glutInitWindowSize( 360, 360 );
glutCreateWindow( "SimpleAnim" );
// Initialize OpenGL as we like it..
// Set up callback functions for key presses
glutKeyboardFunc( myKeyboardFunc );
glutSpecialFunc( mySpecialKeyFunc );

// Handles "normal" ascii

// Handles "special" keyboard keys

// Set up the callback function for resizing windows

glutReshapeFunc( resizeWindow );
// Call this for background processing
// glutIdleFunc( myIdleFunction );
// call this whenever window needs redrawing
glutDisplayFunc( drawScene );
fprintf(stdout, "Arrow keys control speed.
// Start the main loop.
glutMainLoop( );

Press \"r\" to run,

\"s\" to single

glutMainLoop never returns.

// This line is never reached.

Now the main

First it will initiate the display mode and chose the color method which we are
going to use and the depth
Then initiate the position of the shape

And then initiate winow size by 360 *360 pixel

Then create the window titled SimpleAnim

Then call key function which will listen for ESC R and S for doing their functions
Then call another key function to listen for arrow up and down to do their

Then call reshape function to call resize window and listen whenever the user will
adjust the window size it will adjust the shape along with the window size
Then call the drawing function which will draw the shape
And then print the following message "Arrow keys control speed. Press \"r\" to
run, \"s\" to single step on the back window

Then call the loop to be sure to keep the shape on display.