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 / alpha_blending / spotLightAlpha.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.3 KB  |  296 lines

  1. /*
  2.  * Copyright 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. /* spotLightAlpha.c - spotlight shining on a stack of flat 
  19.  *        alpha blended grids
  20.  *
  21.  *    Escape key            - exit the program
  22.  *    Left Mouse Button        - change incidence and azimuth angles
  23.  *    <d> key                - toggle writing to the depth buffer
  24.  *    <s> key                - toggle flat/smooth grid
  25.  *     <R> Key                - reset viewpoint
  26.  */
  27. #include <GL/gl.h>
  28. #include <GL/glu.h>
  29. #include <GL/glut.h>
  30.  
  31. #include <math.h>
  32. #include <stdio.h>
  33.  
  34. /* Function Prototypes */
  35.  
  36. GLvoid  initgfx( GLvoid );
  37. GLvoid  visibility( GLint );
  38. GLvoid  animate( GLvoid );
  39. GLvoid  drawScene( GLvoid );
  40. GLvoid  reshape( GLsizei, GLsizei );
  41. GLvoid  keyboard( GLubyte, GLint, GLint );
  42. GLvoid  mouse( GLint, GLint, GLint, GLint );
  43. GLvoid  motion( GLint, GLint );
  44.  
  45. GLvoid  toggleSmooth( GLvoid );
  46.  
  47. void resetView( GLvoid );
  48. void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
  49. void printHelp( char * );
  50.  
  51. /* Global Definitions */
  52.  
  53. #define KEY_ESC    27    /* ascii value for the escape key */
  54.  
  55. /* Global Variables */
  56.  
  57. static GLboolean    depthMaskFlag = GL_FALSE;
  58.  
  59. static GLfloat        spin = 0.0;
  60.  
  61. static GLboolean    moving = GL_FALSE;
  62.  
  63. static GLdouble        xStart = 0.0, yStart = 0.0;
  64.  
  65. static GLdouble        fovy, near, far; 
  66. static GLfloat        distance, twistAngle, incAngle, azimAngle;
  67.  
  68. void
  69. main( int argc, char *argv[] )
  70. {
  71.     GLsizei width, height;
  72.  
  73.     glutInit( &argc, argv );
  74.  
  75.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  76.     height = glutGet( GLUT_SCREEN_HEIGHT );
  77.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  78.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  79.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  80.     glutCreateWindow( argv[0] );
  81.  
  82.     initgfx();
  83.  
  84.     glutMouseFunc( mouse );
  85.     glutMotionFunc( motion );
  86.     glutKeyboardFunc( keyboard );
  87.     glutIdleFunc( animate );
  88.     glutVisibilityFunc( visibility );
  89.     glutReshapeFunc( reshape );
  90.     glutDisplayFunc( drawScene );
  91.  
  92.     printHelp( argv[0] );
  93.  
  94.     glutMainLoop();
  95. }
  96.  
  97. void
  98. printHelp( char *progname )
  99. {
  100.     fprintf(stdout, "\n%s - demonstrate alpha and lighting\n\n"
  101.         "Escape key        - exit the program\n"
  102.         "Left Mousebutton    - move eye position\n"
  103.         "<d> key        - toggle writing to depth buffer\n"
  104.         "<s> key        - toggle flat/smooth grid\n" 
  105.         "<R> key        - reset viewpoint \n\n", 
  106.         progname);
  107. }
  108.  
  109. GLvoid
  110. initgfx( GLvoid )
  111. {
  112.     GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 0.5 };
  113.  
  114.     glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse );
  115.  
  116.     glBlendFunc( GL_SRC_ALPHA, GL_ONE );
  117.     glEnable( GL_BLEND );
  118.  
  119.     glEnable( GL_LIGHTING );
  120.     glEnable( GL_LIGHT0 );
  121.  
  122.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  123.  
  124.     glEnable( GL_DEPTH_TEST );
  125.  
  126.     fovy = 60.0;    /* field of view in Y */
  127.     near = 3.0;    /* Near clipping plane location */
  128.     far  = 12.0;    /* Far clipping plane location */
  129.  
  130.     resetView();
  131. }
  132.  
  133. GLvoid
  134. reshape( GLsizei width, GLsizei height )
  135. {
  136.     GLdouble    aspect;
  137.  
  138.     glViewport( 0, 0, width, height );
  139.  
  140.     glMatrixMode( GL_PROJECTION );
  141.     glLoadIdentity();
  142.     aspect = (GLdouble) width / (GLdouble) height;
  143.     gluPerspective( fovy, aspect, near, far );
  144.     glMatrixMode( GL_MODELVIEW );
  145. }
  146.  
  147. GLvoid 
  148. animate( GLvoid )
  149. {
  150.     /* update the rotation of the spotlight */
  151.     spin = fmodf( (spin + 3.0), 360.0 );
  152.  
  153.     /* Tell GLUT to redraw the scene */
  154.     glutPostRedisplay();
  155. }
  156.  
  157. GLvoid
  158. visibility( int state ) 
  159. {
  160.     if (state == GLUT_VISIBLE) {
  161.         glutIdleFunc( animate );
  162.     } else {
  163.         glutIdleFunc( NULL );
  164.     }
  165. }
  166.  
  167. GLvoid 
  168. keyboard( GLubyte key, GLint x, GLint y )
  169. {
  170.     switch (key) {
  171.     case 'd':    /* toggle writing to the depth buffer */
  172.         depthMaskFlag = !depthMaskFlag;
  173.         printf("writing into the depth buffer is %s\n",
  174.             (depthMaskFlag ? "disabled" : "enabled"));
  175.         glutPostRedisplay();
  176.         break;
  177.     case 's':
  178.         toggleSmooth();
  179.         glutPostRedisplay();
  180.         break;
  181.     case 'R':
  182.         resetView();
  183.         glutPostRedisplay();
  184.         break;
  185.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  186.         exit(0);
  187.     }
  188. }
  189.  
  190. GLvoid 
  191. mouse( GLint button, GLint state, GLint x, GLint y )
  192. {
  193.     if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON) {
  194.         /* Update the saved mouse position */
  195.         xStart = x;
  196.         yStart = y;
  197.         moving = GL_TRUE;
  198.     } else {
  199.         moving = GL_FALSE;
  200.     }
  201.  
  202. }
  203.  
  204. GLvoid
  205. motion( GLint x, GLint y )
  206. {
  207.     if (moving) {
  208.         /* Adjust the eye position based on the mouse position */
  209.         azimAngle += (GLdouble) (x - xStart);
  210.         incAngle -= (GLdouble) (y - yStart);
  211.     
  212.         /* Update the stored mouse position for later use */
  213.         xStart = x;
  214.         yStart = y;
  215.  
  216.         glutPostRedisplay();
  217.     }
  218. }
  219.  
  220. void
  221. resetView()
  222. {
  223.     distance = 4.5;
  224.     incAngle = -25.0;
  225.     azimAngle = 0.0;
  226. }
  227.  
  228. void
  229. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence, 
  230.             GLfloat twist)
  231. {
  232.     glTranslatef( 0.0, 0.0, -distance);
  233.     glRotatef( -twist, 0.0, 0.0, 1.0);
  234.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  235.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  236. }
  237.  
  238. GLvoid
  239. toggleSmooth( GLvoid )
  240. {
  241.     static GLboolean drawSmooth = GL_TRUE;
  242.  
  243.     drawSmooth = !drawSmooth;
  244.  
  245.     if (drawSmooth) {
  246.         glShadeModel( GL_SMOOTH );
  247.     } else {
  248.         glShadeModel( GL_FLAT );
  249.     }
  250. }
  251.  
  252. GLvoid
  253. drawScene( GLvoid )
  254. {
  255.     int i;
  256.  
  257.     GLfloat spot_dir[] = { 0.2, -1.0, 0.0 };
  258.     GLfloat spot_cutoff = 10.0;
  259.     GLfloat spot_exp = 128.0;
  260.  
  261.     /* Note that a spotlight is a local light */
  262.     GLfloat light_position[] = { 0.0, 2.0, 0.0, 1.0 };
  263.  
  264.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  265.  
  266.     glPushMatrix();
  267.         polarView( distance, azimAngle, incAngle, 0 );
  268.  
  269.         glPushMatrix();
  270.             /* make the spotlight rotate */
  271.             glRotatef( spin, 0.0, 1.0, 0.0 );
  272.             glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spot_dir );
  273.             glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, spot_cutoff );
  274.             glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, spot_exp );
  275.             glLightfv( GL_LIGHT0, GL_POSITION, light_position );
  276.         glPopMatrix();
  277.  
  278.         if ( depthMaskFlag )
  279.             glDepthMask( GL_FALSE );
  280.  
  281.         for( i = -2; i < 2; i++) {
  282.             glPushMatrix();
  283.                 glTranslatef( 0.0, i * 0.5, 0.0 );
  284.                 glRotatef( -90.0, 1.0, 0.0, 0.0 );
  285.                 SolidGrid();
  286.             glPopMatrix();
  287.         }
  288.  
  289.         if ( depthMaskFlag )
  290.             glDepthMask( GL_TRUE );
  291.     glPopMatrix();
  292.  
  293.     glutSwapBuffers();
  294. }
  295.  
  296.