/* * Copyright (c) 1996-1999 Silicon Graphics, Inc. All rights reserved. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* flicker_arm.c - animates a robot arm in single buffer mode to * demonstrate the flicker problem * * Key - toggle arm rotation on / off * Escape key - exit the program */ #include /* includes gl.h, glu.h */ #include /* for fmod */ #include /* for NULL */ #include /* for printf */ #include "axes.h" #include "shapes.h" /* Function Prototypes */ GLvoid initgfx( GLvoid ); GLvoid animate( GLvoid ); GLvoid visibility( GLint ); GLvoid drawScene( GLvoid ); GLvoid reshape( GLsizei, GLsizei ); GLvoid keyboard( GLubyte, GLint, GLint ); void printHelp( char * ); /* Global Variables */ static GLfloat shoulderAngle = 0.0f; /* controls shoulder rotation */ static GLboolean rotateFlag = GL_TRUE; /* Global Definitions */ #define KEY_ESC 27 /* ascii value for the escape key */ void main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); width = glutGet( GLUT_SCREEN_WIDTH ); height = glutGet( GLUT_SCREEN_HEIGHT ); glutInitWindowPosition( 0, height / 4 ); glutInitWindowSize( (width / 2) - 4, height / 2 ); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH ); glutCreateWindow( argv[0] ); initgfx(); glutIdleFunc( animate ); glutVisibilityFunc( visibility ); glutKeyboardFunc( keyboard ); glutReshapeFunc( reshape ); glutDisplayFunc( drawScene ); printHelp( argv[0] ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - animate a robot arm\n\n" "Axes: X - red, Y - green, Z - blue\n\n" " Key - toggle arm rotation on / off\n" "Escape Key - exit the program\n", progname); } GLvoid initgfx( GLvoid ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glShadeModel( GL_FLAT ); glEnable( GL_DEPTH_TEST ); } GLvoid reshape( GLsizei width, GLsizei height ) { GLdouble aspect; glViewport( 0, 0, width, height ); aspect = (GLdouble) width / (GLdouble) height; glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 45.0, aspect, 1.0, 20.0 ); glMatrixMode( GL_MODELVIEW ); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case 'r': rotateFlag = !rotateFlag; if (rotateFlag) { glutIdleFunc( animate ); } else { glutIdleFunc( NULL ); } break; case KEY_ESC: /* Exit when the Escape key is pressed */ exit(0); } } GLvoid animate( GLvoid ) { /* update the rotation of the shoulder for each scene */ shoulderAngle = fmod( (shoulderAngle + 1.0), 360.0 ); glutPostRedisplay(); /* Tell GLUT to redraw the scene */ } GLvoid visibility( int state ) { /* restart the animation function if we were * animated when the window was hidden */ if (state == GLUT_VISIBLE && rotateFlag) { glutIdleFunc( animate ); } else { glutIdleFunc( NULL ); } } GLvoid drawScene( GLvoid ) { static GLfloat upperArmColor[] = { 0.5f, 0.5f, 0.8f }; static GLfloat lowerArmColor[] = { 0.5f, 0.8f, 0.5f }; glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); gluLookAt( 0.0, 0.0, 8.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); XYaxes(); glPushAttrib( GL_LINE_BIT ); glLineWidth( 2.0 ); /* rotate the shoulder by shoulderAngle degrees */ glRotatef( shoulderAngle, 0.0f, 0.0f, 1.0f ); /* Draw the upper arm */ glTranslatef( 1.0f, 0.0f, 0.0f ); glColor3fv( upperArmColor ); WireBox( 2.0, 0.4, 1.0 ); /* Draw the lower arm */ glTranslatef( 1.0f, 0.0f, 0.0f ); glRotatef( 45.0f, 0.0f, 0.0f, 1.0f ); glTranslatef( 1.0f, 0.0f, 0.0f ); glColor3fv( lowerArmColor ); WireBox( 2.0, 0.4, 1.0 ); glPopAttrib(); glPopMatrix(); glFlush(); }