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 / twoSide.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.6 KB  |  216 lines

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