Void Unsigned Char Int Int Switch Case If: // Toggle To Opposite Value
Void Unsigned Char Int Int Switch Case If: // Toggle To Opposite Value
Void Unsigned Char Int Int Switch Case If: // Toggle To Opposite Value
{
switch ( key ) {
case 'r':
RunMode = 1-RunMode;
// Toggle to opposite value
if ( RunMode==1 ) {
glutPostRedisplay();
}
break;
case 's':
RunMode = 1;
drawScene();
RunMode = 0;
break;
case 27:
// Escape key
exit(1);
}
}
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
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
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (RunMode==1) {
// Calculate animation parameters
CurrentAngle += AnimateStep;
if ( CurrentAngle > 360.0 ) {
CurrentAngle -= 360.0*floor(CurrentAngle/360.0); // Don't allow
overflow
}
}
// Rotate the image
glMatrixMode( GL_MODELVIEW );
positions
glLoadIdentity();
identity
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
This function will first initiate and clear the window variables which are
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT
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
glColor3f
The close the drawing
And then check the toggle RunMode and redraw the shape again
\"s\" to single
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