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 / koosh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.1 KB  |  326 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. /* koosh.c 
  18.  * This program draws a koosh ball. The lighting resources are kept in 
  19.  * display lists which are called by the koosh ball display list.
  20.  *
  21.  *    <m> key        - change material propertes 
  22.  *    <l> key         - toggle infinite/local light model
  23.  *    <i> key        - change light intensity
  24.  *    Escape key    - exit the program
  25.  */
  26. #include <GL/gl.h>
  27. #include <GL/glu.h>
  28. #include <GL/glut.h>
  29.  
  30. #include <math.h>
  31. #include <stdio.h>
  32.  
  33. /*  Function Prototypes  */
  34.  
  35. GLvoid  initgfx( GLvoid );
  36. GLvoid  drawScene( GLvoid );
  37. GLvoid  animate( GLvoid );
  38. GLvoid  visibility( int );
  39. GLvoid  reshape( GLsizei, GLsizei );
  40. GLvoid  keyboard( GLubyte, GLint, GLint );
  41.  
  42. GLvoid  makeKooshList( GLvoid );
  43. GLvoid  drawKoosh( GLvoid );
  44.  
  45. GLvoid  modifyMaterial( void );
  46. GLvoid  modifyLightIntensity( void );
  47. GLvoid  modifyLightModel( void );
  48.  
  49. void    printHelp( char *progname );
  50.  
  51. /* Global Definitions */
  52.  
  53. #define KEY_ESC    27    /* ascii value for the escape key */
  54.  
  55. #define MATERIAL    0
  56. #define LIGHT        1
  57. #define LIGHTMODEL    2
  58.  
  59. /* Global Variables */
  60.  
  61. static  GLfloat spin = 0.0;
  62.  
  63. static  GLuint kooshIndex, properties[3];
  64.  
  65. /* light properties */
  66.  
  67. static  GLfloat mat_ambient[] = { 0.25, 0.05, 0.4, 1.0 };
  68. static  GLfloat mat_diffuse[] = { 0.5, 0.1, 0.8, 1.0 };
  69. static  GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  70. static  GLfloat mat_shininess[] = { 10.0 };
  71.  
  72. static  GLfloat light_ambient[] = { 0.75, 0.95, 0.6, 1.0 };
  73. static  GLfloat light_diffuse[] = { 0.5, 0.9, 0.2, 1.0 };
  74. static  GLfloat light_specular[] = { 0.0, 0.0, 0.0, 1.0 };
  75. static  GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  76.  
  77. static  GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  78. static  GLfloat lmodel_viewer[] = { 0.0 };
  79.  
  80. void
  81. main( int argc, char *argv[])
  82. {
  83.     GLsizei width, height;
  84.  
  85.     glutInit( &argc, argv );
  86.  
  87.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  88.     height = glutGet( GLUT_SCREEN_HEIGHT );
  89.     glutInitWindowPosition( width / 4, height / 4 );
  90.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  91.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  92.     glutCreateWindow( argv[0] );
  93.  
  94.     initgfx();
  95.  
  96.     glutKeyboardFunc( keyboard );
  97.     glutReshapeFunc( reshape );
  98.     glutIdleFunc( animate );
  99.     glutVisibilityFunc( visibility );
  100.     glutDisplayFunc( drawScene ); 
  101.  
  102.     printHelp( argv[0] );
  103.     glutMainLoop();
  104. }
  105.  
  106. void
  107. printHelp( char *progname )
  108. {
  109.     fprintf( stdout, 
  110.         "\n%s draws a koosh ball. The lighting resources are kept in\n"
  111.         "display lists which are called by the main koosh ball "
  112.         "display list.\n\n"
  113.         "<m> key        - change material properties\n"
  114.         "<l> key         - change light model\n"
  115.         "<i> key        - change light intensity\n"
  116.         "Escape key        - exit the program\n\n",
  117.         progname );
  118. }
  119.  
  120. GLvoid
  121. initgfx( void )
  122. {
  123.     /* initialize lighting property display lists */
  124.     properties[MATERIAL] = glGenLists( 3 );
  125.     glNewList( properties[MATERIAL], GL_COMPILE );
  126.         glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  127.         glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  128.         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  129.         glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  130.     glEndList();
  131.  
  132.     properties[LIGHT] = properties[MATERIAL] + 1;
  133.     glNewList( properties[LIGHT], GL_COMPILE );
  134.         glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  135.         glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  136.         glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  137.     glEndList();
  138.  
  139.     properties[LIGHTMODEL] = properties[MATERIAL] + 2;
  140.     glNewList( properties[LIGHTMODEL], GL_COMPILE );
  141.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  142.         glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_viewer);
  143.     glEndList();
  144.  
  145.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  146.  
  147.     glEnable(GL_LIGHTING);
  148.     glEnable(GL_LIGHT0);
  149.     glClearColor( 0, 0, 0, 1 );
  150.  
  151.     glEnable(GL_DEPTH_TEST);
  152.  
  153.     makeKooshList();
  154. }
  155.  
  156. GLvoid
  157. modifyMaterial( void )
  158. {
  159.     int i;
  160.  
  161.     for (i = 0; i < 3; i++) {
  162.         mat_ambient[i] = drand48();
  163.         mat_diffuse[i] = drand48();
  164.         mat_specular[i] = drand48();
  165.     }
  166.     mat_shininess[0] = drand48() * 128.0;
  167.     printf("\nChanging MATERIAL properties to:\n"
  168.         "\tambient = %g %g %g\n\tdiffuse = %g %g %g\n"
  169.         "\tspecular = %g %g %g\n\tshininess = %g\n",
  170.         mat_ambient[0], mat_ambient[1], mat_ambient[1],
  171.         mat_diffuse[0], mat_diffuse[1], mat_diffuse[1],
  172.         mat_specular[0], mat_specular[1], mat_specular[1],
  173.         mat_shininess[0]);
  174.  
  175.     glNewList( properties[MATERIAL], GL_COMPILE );
  176.         glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  177.         glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  178.         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  179.         glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  180.     glEndList();
  181. }
  182.  
  183. GLvoid
  184. modifyLightIntensity( void )
  185. {
  186.     int i;
  187.  
  188.     for (i = 0; i < 3; i++) {
  189.         light_ambient[i] = drand48();
  190.         light_diffuse[i] = drand48();
  191.         light_specular[i] = drand48();
  192.     }
  193.     printf("\nChanging LIGHT properties to:\n"
  194.         "\tambient = %g %g %g\n\tdiffuse = %g %g %g\n"
  195.         "\tspecular = %g %g %g\n",
  196.         light_ambient[0], light_ambient[1], light_ambient[1],
  197.         light_diffuse[0], light_diffuse[1], light_diffuse[1],
  198.         light_specular[0], light_specular[1], light_specular[1]);
  199.  
  200.     glNewList( properties[LIGHT], GL_COMPILE );
  201.         glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  202.         glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  203.         glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  204.     glEndList();
  205. }
  206.  
  207. GLvoid
  208. modifyLightModel( void )
  209. {
  210.     int i;
  211.  
  212.     for (i = 0; i < 3; i++) {
  213.         lmodel_ambient[i] = drand48();
  214.     }
  215.     lmodel_viewer[0] = !lmodel_viewer[0];
  216.         printf("\nChanging LIGHT MODEL properties to:\n"
  217.                 "\tambient = %g %g %g\n\tmodel is %s\n",
  218.                 lmodel_ambient[0], lmodel_ambient[1], lmodel_ambient[1],
  219.                 (lmodel_viewer[0] == 0 ? "local" : "infinite"));
  220.  
  221.     glNewList( properties[LIGHTMODEL], GL_COMPILE );
  222.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  223.         glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_viewer);
  224.     glEndList();
  225. }
  226.  
  227. GLvoid
  228. makeKooshList( GLvoid )
  229. {
  230.     kooshIndex = glGenLists(1);
  231.     glNewList( kooshIndex, GL_COMPILE );
  232.         glCallLists( 3, GL_UNSIGNED_INT, properties );
  233.         drawKoosh();
  234.     glEndList();
  235. }
  236.  
  237. GLvoid
  238. drawKoosh( GLvoid )
  239. {
  240. #define MAX_STRANDS 60
  241. #define RADIAN_INCREMENT 2*M_PI/MAX_STRANDS
  242. #define DEGREE_INCREMENT 360.0/MAX_STRANDS
  243.  
  244.     register int i, j;
  245.     static GLfloat origin[] = { 0.0, 0.0 };
  246.     static GLfloat v[] = { 0.0, 0.0, 0.0 };
  247.     float radians;
  248.  
  249.     for( i = 0; i < MAX_STRANDS; i++ ) {
  250.         glRotatef( i * DEGREE_INCREMENT, 0, 1, 0 );
  251.         glBegin( GL_LINES );
  252.         for( j = 0; j < MAX_STRANDS; j++ ) {
  253.             radians = j * RADIAN_INCREMENT;
  254.             v[0] = cosf( radians ); v[1] = sinf( radians );
  255.             glNormal3fv( v );
  256.             glVertex2fv( origin );
  257.             glVertex2fv( v );
  258.         }
  259.         glEnd();
  260.     }
  261. }
  262.  
  263. GLvoid
  264. keyboard( char key, int x, int y ) 
  265. {
  266.     switch (key) {
  267.     case 'm':
  268.         modifyMaterial();
  269.         break;
  270.     case 'l':
  271.         modifyLightModel();
  272.         break;
  273.     case 'i':
  274.         modifyLightIntensity();
  275.         break;
  276.     case KEY_ESC:
  277.         exit(0);
  278.     }
  279.     glutPostRedisplay();
  280. }
  281.  
  282. GLvoid
  283. reshape( GLsizei width, GLsizei height )
  284. {
  285.     GLdouble     aspect;
  286.  
  287.     glViewport( 0, 0, width, height );
  288.  
  289.     aspect = (GLdouble) width / (GLdouble) height;
  290.  
  291.     glMatrixMode( GL_PROJECTION );
  292.     glLoadIdentity();
  293.     gluPerspective( 45.0, aspect, 1.0, 9.0 );
  294.     glMatrixMode( GL_MODELVIEW );
  295.     glLoadIdentity();
  296.     glTranslatef( 0.0, 0.0, -5.0 );
  297. }
  298.  
  299. GLvoid 
  300. animate( GLvoid )
  301. {
  302.     spin = fmodf( spin + 1.0, 360.0 );
  303.     glutPostRedisplay();    /* Tell GLUT to redraw the scene */
  304. }
  305.  
  306. GLvoid
  307. visibility( int state ) 
  308. {
  309.     if (state == GLUT_VISIBLE) {
  310.         glutIdleFunc( animate );
  311.     } else {
  312.         glutIdleFunc( NULL );
  313.     }
  314. }
  315.  
  316. GLvoid
  317. drawScene( GLvoid )
  318. {
  319.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  320.     glPushMatrix();
  321.         glRotatef( spin, 1, 1, 1 );
  322.         glCallList( kooshIndex );
  323.     glPopMatrix();
  324.     glutSwapBuffers();
  325. }
  326.