home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / demos / enterprise.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.9 KB  |  179 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* enterprise.c - creates a model of the Enterprise
  19.  *
  20.  * Author: Benedikt Kesseler    
  21.  *       benedikt@munich.sgi.com
  22.  */
  23. #include <math.h>    /* for fmodf */
  24. #include <stdlib.h>    /* for NULL */
  25.  
  26. #include <GL/gl.h>
  27. #include <GL/glu.h>
  28. #include <GL/glut.h>
  29.  
  30. /*  Function Prototypes  */
  31.  
  32. GLvoid  initgfx();
  33. GLvoid  animate( GLvoid );
  34. GLvoid  visibility( GLint );
  35. GLvoid  keyboard( GLubyte, GLint, GLint );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  drawScene( GLvoid );
  38.  
  39. /* Global Definitions */
  40.  
  41. #define KEY_ESC    27    /* ascii value for the escape key */
  42.  
  43. /*  Global Variables */
  44.  
  45. static GLfloat    angle1 = 0.0, angle2 = 0.0;    /* controls rotation */
  46.  
  47. void
  48. main( int argc, char *argv[] )
  49. {
  50.     GLsizei        width, height;
  51.  
  52.     glutInit( &argc, argv );
  53.  
  54.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  55.     height = glutGet( GLUT_SCREEN_HEIGHT );
  56.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  57.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  58.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  59.     glutCreateWindow( argv[0] );
  60.  
  61.     initgfx();
  62.  
  63.     glutIdleFunc( animate );
  64.     glutVisibilityFunc( visibility );
  65.     glutKeyboardFunc( keyboard );
  66.     glutReshapeFunc( reshape );
  67.     glutDisplayFunc( drawScene ); 
  68.  
  69.     glutMainLoop();
  70. }
  71.  
  72. GLvoid
  73. initgfx()
  74. {
  75.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  76.     glEnable( GL_DEPTH_TEST );
  77. }
  78.  
  79. GLvoid
  80. reshape( GLsizei width, GLsizei height )
  81. {
  82.     GLdouble    aspect;
  83.  
  84.     glViewport( 0, 0, width, height );
  85.  
  86.     aspect = (GLdouble) width / (GLdouble) height;
  87.  
  88.     /* Set the matrix mode to affect the Projection matrix */
  89.     glMatrixMode( GL_PROJECTION );
  90.  
  91.     /* Clear the current Projection matrix */
  92.     glLoadIdentity();
  93.  
  94.     /* Multiply the new perspective projection on the matrix */
  95.     gluPerspective( 90.0, aspect, 3.0, 37.0 );
  96.  
  97.     /* Return to the ModelView mode */
  98.     glMatrixMode( GL_MODELVIEW );
  99. }
  100.  
  101. GLvoid 
  102. keyboard( GLubyte key, GLint x, GLint y )
  103. {
  104.     switch (key) {
  105.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  106.         exit(0);
  107.     }
  108. }
  109.  
  110. GLvoid 
  111. animate( GLvoid )
  112. {
  113.     /* update the rotation for each scene */
  114.     angle1 = fmodf( (angle1 + 1.0), 360.0 );
  115.     angle2 = fmodf( (angle2 + 2.0), 360.0 );
  116.  
  117.     glutPostRedisplay();    /* Tell GLUT to redraw the scene */
  118. }
  119.  
  120. GLvoid
  121. visibility( int state ) 
  122. {
  123.     if (state == GLUT_VISIBLE) {
  124.         glutIdleFunc( animate );
  125.     } else {
  126.         glutIdleFunc( NULL );
  127.     }
  128. }
  129.  
  130. GLvoid
  131. drawScene( GLvoid )
  132. {
  133.     static GLfloat    redColor[] = { 1.0, 0.0, 0.0 };
  134.     static GLfloat    darkgreenColor[] = { 0.0, 0.5, 0.0 };
  135.     static GLfloat    whiteColor[] = { 1.0, 1.0, 1.0 };
  136.     
  137.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  138.  
  139.     /* Save the Identity matrix that's on the ModelView
  140.      * matrix stack 
  141.      */
  142.  
  143.     glPushMatrix();
  144.  
  145.         gluLookAt(0.0, .0, 25.0,  0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  146.  
  147.         glRotatef(angle1, 0.0, 1.0, 0.0);
  148.         glRotatef(angle2, 1.0, 0.0, 1.0);
  149.  
  150.         glPushMatrix();
  151.  
  152.             glScalef(5.0, 0.6, 5.0);
  153.             glColor3fv( whiteColor );
  154.             glutSolidSphere( 2.0, 15, 15 );
  155.  
  156.         glPopMatrix();
  157.  
  158.         glColor3fv( darkgreenColor );
  159.         glutWireSphere( 3.0, 15, 15 );
  160.  
  161.         glPushMatrix();
  162.  
  163.             glColor3fv( redColor );
  164.             glRotatef(90.0, 0.0, 0.0, 1.0);
  165.             glTranslatef(4.0, 20.0, 5.0);
  166.             WireCylinder(1., 10.0);
  167.             glTranslatef(0.0, 0.0, -10.0);
  168.             WireCylinder(1., 10.0);
  169.  
  170.         glPopMatrix();
  171.  
  172.         glTranslatef(0.0, -5.0, 0.0);
  173.         glutWireTeapot(4.0);
  174.             
  175.     glPopMatrix();                
  176.  
  177.     glutSwapBuffers();
  178. }
  179.