/* * 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. */ /* robot_arm.c - draw a robot arm by composing modeling * transformations * * Escape Key - exit program */ #include /* includes gl.h, glu.h */ #include #include "axes.h" #include "shapes.h" /* Function Prototypes */ GLvoid initgfx( GLvoid ); GLvoid keyboard( GLubyte, GLint, GLint ); GLvoid drawScene( GLvoid ); void printHelp( char * ); /* 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 ); glutCreateWindow( argv[0] ); initgfx(); glutKeyboardFunc( keyboard ); glutDisplayFunc( drawScene ); printHelp( argv[0] ); glMatrixMode( GL_PROJECTION ); gluPerspective( 45.0, (GLdouble) width/ (GLdouble)height, 1.0, 20.0 ); glMatrixMode( GL_MODELVIEW ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - renders a robot arm\n\n" "Escape Key - exit the program\n\n", progname); } GLvoid initgfx( GLvoid ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glShadeModel( GL_FLAT ); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case KEY_ESC: /* Exit whenever the Escape key is pressed */ exit(0); } } 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 ); glPushMatrix(); /* translate into the viewing volume */ glTranslatef( 0.0f, 0.0f, -8.0f ); XYaxes(); glPushAttrib( GL_LINE_BIT ); glLineWidth( 2.0 ); /* Draw the upper arm with the shoulder * on the Z axis */ /* Step 1: move to the center of the upper arm */ glTranslatef( 1.0f, 0.0f, 0.0f ); /* Step 2: draw the upper arm */ glColor3fv( upperArmColor ); WireBox( 2.0, 0.4, 1.0 ); /* Draw the lower arm at the end of the upper arm and * rotate it 45 degrees */ /* Step 3: move to the elbow */ glTranslatef( 1.0f, 0.0f, 0.0f ); /* Step 4: rotate the lower arm 45 degrees */ glRotatef( 45.0f, 0.0f, 0.0f, 1.0f ); /* Step 5: move to the center of the lower arm */ glTranslatef( 1.0f, 0.0f, 0.0f ); /* Step 6: draw the lower arm */ glColor3fv( lowerArmColor ); WireBox( 2.0, 0.4, 1.0 ); glPopAttrib(); glPopMatrix(); glFlush(); }