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 / adv_lighting / indexLighting.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.6 KB  |  221 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. /* indexLighting.c - demonstrates lighting in color index mode.
  18.  *
  19.  *    <l> key            - toggle LIGHT0 on / off
  20.  *    Escape key        - exit the program
  21.  */
  22. #include <math.h>
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26. #include <stdio.h>
  27.  
  28. /*  Function Prototypes  */
  29.  
  30. GLvoid  initgfx( GLvoid );
  31. GLvoid  visibility( GLint );
  32. GLvoid  animate( GLvoid );
  33. GLvoid  drawScene( GLvoid );
  34. GLvoid  reshape( GLsizei, GLsizei );
  35. GLvoid  keyboard( GLubyte, GLint, GLint );
  36.  
  37. GLvoid  toggleLight( GLvoid );
  38.  
  39. void resetView( GLvoid );
  40. void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
  41. void printHelp( char * );
  42.  
  43. /* Global Definitions */
  44.  
  45. #define KEY_ESC    27    /* ascii value for the escape key */
  46.  
  47. /* Global Variables */
  48.  
  49. static GLfloat        spin = 0.0;
  50.  
  51. static GLdouble        fovy, near, far; 
  52. static GLfloat        distance, twistAngle, incAngle, azimAngle;
  53.  
  54. GLvoid
  55. main( int argc, char *argv[] )
  56. {
  57.     GLsizei        width, height;
  58.  
  59.     glutInit( &argc, argv );
  60.  
  61.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  62.     height = glutGet( GLUT_SCREEN_HEIGHT );
  63.     glutInitWindowSize( width / 2, height / 2 );
  64.     glutInitWindowPosition( width / 4, height / 4 );
  65.     glutInitDisplayMode( GLUT_INDEX | GLUT_DEPTH | GLUT_DOUBLE );
  66.     glutCreateWindow( argv[0] );
  67.  
  68.     initgfx();
  69.  
  70.     glutKeyboardFunc( keyboard );
  71.     glutVisibilityFunc( visibility );
  72.     glutIdleFunc( animate );
  73.     glutReshapeFunc( reshape );
  74.     glutDisplayFunc( drawScene ); 
  75.  
  76.     printHelp( argv[0] );
  77.  
  78.     glutMainLoop();
  79. }
  80.  
  81. void
  82. printHelp( char *progname )
  83. {
  84.     fprintf(stdout, "\n%s - demonstrate two sided lighting\n\n"
  85.         "Escape key        - exit the program\n"
  86.         "<l> key        - toggle LIGHT0 on / off\n\n", 
  87.         progname);
  88. }
  89.  
  90. GLvoid
  91. initgfx( GLvoid )
  92. {
  93.     GLint i;
  94.  
  95.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  96.     GLfloat mat_colormap[] = { 16.0, 48.0, 79.0 };
  97.     GLfloat mat_shininess[] = { 10.0 };
  98.  
  99.     GLfloat lmodel_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
  100.     GLfloat two_side = 1.0;
  101.  
  102.     glMaterialfv(GL_FRONT_AND_BACK, GL_COLOR_INDEXES, mat_colormap);
  103.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  104.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  105.  
  106.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  107.     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, two_side);
  108.  
  109.     glEnable(GL_LIGHTING);
  110.     glEnable(GL_LIGHT0);
  111.  
  112.     glEnable(GL_DEPTH_TEST);
  113.  
  114.     for (i = 0; i < 32; i++) {
  115.         glutSetColor(mat_colormap[0] + i, 1.0 * (i/32.0), 0.0, 1.0 * (i/32.0));
  116.         glutSetColor(mat_colormap[1] + i, 1.0, 1.0 * (i/32.0), 1.0);
  117.     }
  118.     glClearIndex(0);
  119.  
  120.     fovy = 60.0;    /* field of view in Y */
  121.     near = 3.0;    /* Near clipping plane location */
  122.     far  = 12.0;    /* Far clipping plane location */
  123.  
  124.     resetView();
  125. }
  126.  
  127. void
  128. animate()
  129. {
  130.     spin = fmodf(spin + 1.0, 360.0);
  131.     glutPostRedisplay();
  132. }
  133.  
  134. GLvoid
  135. visibility( int state ) 
  136. {
  137.     if (state == GLUT_VISIBLE) {
  138.         glutIdleFunc( animate );
  139.     } else {
  140.         glutIdleFunc( NULL );
  141.     }
  142. }
  143.  
  144. GLvoid
  145. keyboard( char key, int x, int y ) 
  146. {
  147.  
  148.     switch (key) {
  149.     case 'l':
  150.         toggleLight();
  151.         break;
  152.     case KEY_ESC:
  153.         exit(0);
  154.     }
  155. }
  156.  
  157.  
  158. GLvoid
  159. reshape( GLsizei width, GLsizei height )
  160. {
  161.     GLdouble    aspect;
  162.  
  163.     glViewport( 0, 0, width, height );
  164.  
  165.     glMatrixMode( GL_PROJECTION );
  166.     glLoadIdentity();
  167.     aspect = (GLdouble) width / (GLdouble) height;
  168.     gluPerspective( fovy, aspect, near, far );
  169.     glMatrixMode( GL_MODELVIEW );
  170. }
  171.  
  172. GLvoid
  173. toggleLight( GLvoid )
  174. {
  175.     static GLboolean lightOn = GL_TRUE;
  176.     
  177.     lightOn = !lightOn;
  178.     if ( lightOn )
  179.         glEnable( GL_LIGHT0 );
  180.     else
  181.         glDisable( GL_LIGHT0 );
  182. }
  183.  
  184. void
  185. resetView()
  186. {
  187.     distance = 8.0;
  188.     incAngle = 30.0;
  189.     azimAngle = 0.0;
  190. }
  191.  
  192. void
  193. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  194.             GLfloat twist)
  195. {
  196.     glTranslatef( 0.0, 0.0, -distance);
  197.     glRotatef( -twist, 0.0, 0.0, 1.0);
  198.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  199.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  200. }
  201.  
  202. GLvoid
  203. drawScene( GLvoid )
  204. {
  205.  
  206.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  207.  
  208.     glPushMatrix();
  209.  
  210.         polarView( distance, azimAngle, incAngle, 0 );
  211.  
  212.         glPushMatrix();
  213.             glRotatef( spin, 1.0, 1.0, 0.0 );
  214.             SolidCylinder( 1.0, 2.0 );
  215.         glPopMatrix();
  216.  
  217.     glPopMatrix();
  218.  
  219.     glutSwapBuffers();
  220. }
  221.