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 / examples / dlists / dlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.4 KB  |  250 lines

  1. /* Copyright 1996, Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16.  
  17. /* dlist.c - uses the code in mountain.c to draw fractal-based 
  18.  *     mountains MAX_FRAME times, first using display lists and then
  19.  *     in immediate mode.  It prints the average frames/second for
  20.  *     both display list and immediate modes.
  21.  */
  22. #include <GL/gl.h>
  23. #include <GL/glu.h>
  24. #include <GL/glut.h>
  25.  
  26. #include <math.h>
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30.  
  31. /* Function Prototypes */
  32.  
  33. GLvoid  initgfx( GLvoid );
  34. GLvoid  drawScene( GLvoid );
  35. GLvoid  animate( GLvoid );
  36. GLvoid  visibility( int );
  37. GLvoid  reshape( GLsizei, GLsizei );
  38. GLvoid  keyboard( GLubyte, GLint, GLint );
  39.  
  40. GLvoid  makeMountainLists( GLvoid );
  41. double  getTime( void );
  42.  
  43. void  printHelp( char * );
  44.  
  45. /* Functions in mountain.c */
  46.  
  47. extern GLvoid setView( GLvoid );
  48. extern GLvoid computeMountain( GLvoid );
  49. extern GLvoid drawMountain( GLvoid );
  50.  
  51. /* Global Definitions */
  52.  
  53. #define KEY_ESC    27    /* ascii value for the escape key */
  54.  
  55. #define MAX_LIST 10
  56. #define MAX_FRAME 120
  57.  
  58. /* Global Variables */
  59.  
  60. static GLint        mountainBase;
  61. static GLboolean     displayListMode = GL_FALSE;
  62.  
  63. void
  64. main( int argc, char *argv[] )
  65. {
  66.     GLsizei width, height;
  67.  
  68.     glutInit( &argc, argv );
  69.  
  70.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  71.     height = glutGet( GLUT_SCREEN_HEIGHT );
  72.     glutInitWindowPosition( width / 4, height / 4 );
  73.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  74.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  75.     glutCreateWindow( argv[0] );
  76.  
  77.     initgfx();
  78.  
  79.     glutKeyboardFunc( keyboard );
  80.     glutReshapeFunc( reshape );
  81.     glutIdleFunc( animate );
  82.     glutVisibilityFunc( visibility );
  83.     glutDisplayFunc( drawScene ); 
  84.  
  85.     printHelp( argv[0] );
  86.  
  87.     glutMainLoop();
  88. }
  89.  
  90. void
  91. printHelp( char *progname )
  92. {
  93.     printf( "\n%s contains a simple benchmarking loop "
  94.         "that generates\nfractal-based mountains.  "
  95.         "It creates %d frames and displays them,\n"
  96.         "first in immediate mode and then in display list mode,\n"
  97.         "printing the average frames/second for each mode.\n\n",
  98.         progname, MAX_FRAME );
  99. }
  100.  
  101. GLvoid
  102. initgfx( void )
  103. {
  104.     static GLfloat darkGreen[] = { 0.0, 0.5, 0.0, 1.0 };
  105.  
  106.     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, darkGreen );
  107.  
  108.     glEnable( GL_LIGHTING );
  109.     glEnable( GL_LIGHT0 );
  110.  
  111.     glClearColor( 0, 0, 0, 1 );
  112.     glClear( GL_COLOR_BUFFER_BIT );
  113.  
  114.     setView();
  115.  
  116.     makeMountainLists();
  117.  
  118.     printf( "Starting Immediate Mode Benchmark\n");
  119.     printf( "Immediate Mode results:\t\t");
  120. }
  121.  
  122. /* makeMountainLists() creates MAX_LIST mountains and stores
  123.  * each in a display list.
  124.  */
  125. GLvoid
  126. makeMountainLists( GLvoid )
  127. {
  128.     int mountain;
  129.  
  130.     /* set the seed so that we generate the same sequence of 
  131.      * mountains in both display list and immediate mode.  */
  132.     srand( 1 );
  133.  
  134.     printf( "Initializing %d Display Lists: ", MAX_LIST );
  135.  
  136.     mountainBase = glGenLists( MAX_LIST );
  137.     for ( mountain = 0; mountain < MAX_LIST; mountain++ )
  138.     {
  139.         printf( "%d ", mountain );
  140.         fflush( stdout );
  141.  
  142.         glNewList( mountainBase + mountain, GL_COMPILE );
  143.             computeMountain();
  144.             drawMountain();
  145.         glEndList();
  146.     }
  147.  
  148.     printf( "Done.\n\n" );
  149. }
  150.  
  151. double
  152. getTime( void )
  153. {
  154.     struct timeval tp;
  155.     struct timezone tzp;
  156.  
  157.     gettimeofday(&tp,&tzp);
  158.     return ((double)tp.tv_sec + (double)tp.tv_usec/1000000);
  159. }
  160.  
  161. GLvoid
  162. keyboard( char key, int x, int y ) 
  163. {
  164.     switch (key) {
  165.     case KEY_ESC:
  166.         exit(0);
  167.     }
  168. }
  169.  
  170. GLvoid
  171. reshape( GLsizei width, GLsizei height )
  172. {
  173.     GLdouble     aspect;
  174.     glViewport( 0, 0, width, height );
  175.  
  176.     aspect = (GLdouble) width / (GLdouble) height;
  177.  
  178.     glMatrixMode( GL_PROJECTION );
  179.     glLoadIdentity();
  180.     gluPerspective( 45.0, aspect, 1.0, 350.0 );
  181.     glMatrixMode( GL_MODELVIEW );
  182.     glLoadIdentity();
  183. }
  184.  
  185. GLvoid 
  186. animate( GLvoid )
  187. {
  188.     /* Tell GLUT to redraw the scene */
  189.     glutPostRedisplay();
  190. }
  191.  
  192. GLvoid
  193. visibility( int state ) 
  194. {
  195.     if (state == GLUT_VISIBLE) {
  196.         glutIdleFunc( animate );
  197.     } else {
  198.         glutIdleFunc( NULL );
  199.     }
  200. }
  201.  
  202. GLvoid
  203. drawScene( GLvoid )
  204. {
  205.     static int mountain = 0;
  206.     static int frameCount = 0;
  207.  
  208.     static double t0;
  209.     double elapsed;
  210.  
  211.     if ( frameCount == 0 )
  212.         t0 = getTime();
  213.  
  214.     glClear( GL_COLOR_BUFFER_BIT );
  215.  
  216.     if ( displayListMode )
  217.         glCallList( mountainBase + mountain );
  218.     else {
  219.         computeMountain();
  220.         drawMountain();
  221.     }
  222.  
  223.     glutSwapBuffers();
  224.  
  225.     /* cycle through MAX_LIST different mountains */
  226.     mountain = ++frameCount % MAX_LIST;
  227.     if (mountain == 0) {
  228.         /* reset the seed so that we generate the same sequence    
  229.           * of MAX_LIST mountains as those in the display lists */
  230.         srand( 1 );
  231.     }
  232.  
  233.     if (frameCount == MAX_FRAME ) {
  234.         glFinish();    /* make sure everything is done */
  235.  
  236.         elapsed = getTime() - t0;
  237.         printf( "Average %3.1lf frames/second\n\n", 
  238.             MAX_FRAME/elapsed );
  239.  
  240.         if ( displayListMode ) {
  241.             exit(0);
  242.         } else {    /* switch to display list mode */
  243.             displayListMode = GL_TRUE;
  244.             frameCount = 0;
  245.             printf( "Starting Display List Mode Benchmark\n" );
  246.             printf( "Display List Mode results:\t" );
  247.         }
  248.     }
  249. }
  250.