home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / accum / dof.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.0 KB  |  200 lines

  1. /*
  2.  * Copyright 1993, 1995, 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. /* dof.c
  19.  *    This program demonstrates using the accumlation buffer to 
  20.  *    simulate depth-of-field.  Objects at the focal point are
  21.  *    crisp, the rest are somewhat fuzzy.
  22.  *
  23.  *        Escape key        - exit the program
  24.  *        <d> key            - toggle depth-of-field
  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. #include "jitter.h"
  34.  
  35. /*  Function Prototypes  */
  36.  
  37. GLvoid  initgfx( GLvoid );
  38. GLvoid  drawScene( GLvoid );
  39. GLvoid  reshape( GLsizei, GLsizei );
  40. GLvoid  animate( GLvoid );
  41. GLvoid  visibility( GLint );
  42. GLvoid  keyboard( GLubyte, GLint, GLint );
  43.  
  44. GLvoid    toggleJitter(GLvoid);
  45.  
  46. GLvoid accFrustum(GLdouble, GLdouble, GLdouble, GLdouble,
  47.         GLdouble, GLdouble, GLdouble, GLdouble, 
  48.         GLdouble, GLdouble, GLdouble);
  49. GLvoid accPerspective(GLdouble, GLdouble, GLdouble, GLdouble,
  50.         GLdouble, GLdouble, GLdouble, GLdouble, GLdouble);
  51.  
  52. void printHelp( char * );
  53.  
  54. /* Global Definitions */
  55.  
  56. #define KEY_ESC    27    /* ascii value for the escape key */
  57.  
  58. /* Global Variables */
  59.  
  60. static GLint        maxJitters = 1;
  61. static GLdouble        aspect = 1.0;
  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/4, height/4 );
  74.     glutInitDisplayMode( GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE|GLUT_ACCUM );
  75.     glutCreateWindow( argv[0] );
  76.     
  77.     initgfx();
  78.  
  79.     glutKeyboardFunc( keyboard );
  80.     glutReshapeFunc( reshape );
  81.     glutDisplayFunc( drawScene ); 
  82.  
  83.     printHelp( argv[0] );
  84.  
  85.     glutMainLoop();
  86. }
  87.  
  88. void
  89. printHelp( char *progname )
  90. {
  91.     fprintf(stdout, "\n%s - use the accumulation buffer "
  92.         "to simulate depth of field.\n\n"
  93.         "Expect it to run very slowly on systems without \n"
  94.         "a hardware accumulation buffer.\n\n"
  95.         "Escape key        - exit the program \n"
  96.         "<d> key        - toggle depth-of-field\n\n",
  97.         progname );
  98.  
  99.     printf("depth-of-field is OFF\n");
  100. }
  101.  
  102. GLvoid
  103. initgfx( void )
  104. {
  105.     GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
  106.     GLfloat mat_shininess[] = { 128.0 };
  107.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0    };
  108.  
  109.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  110.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  111.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  112.     
  113.     glEnable(GL_LIGHTING);
  114.     glEnable(GL_LIGHT0);
  115.  
  116.     glColorMaterial(GL_FRONT, GL_DIFFUSE);
  117.     glEnable(GL_COLOR_MATERIAL);
  118.  
  119.     glClearColor( 0, 0, 0, 1 );
  120.     glClearAccum( 0.0, 0.0, 0.0, 1.0 );
  121.  
  122.     glEnable( GL_DEPTH_TEST );
  123. }
  124.  
  125. GLvoid  
  126. toggleJitter(GLvoid)
  127. {
  128.     maxJitters = ( maxJitters == 1 ? 8 : 1 );
  129.  
  130.     printf("depth-of-field is %s\n", (maxJitters==1?"OFF":"ON"));
  131. }
  132.  
  133. GLvoid 
  134. keyboard( GLubyte key, GLint x, GLint y )
  135. {
  136.     switch (key) {
  137.     case 'd':    /* toggle depth-of-field */
  138.         toggleJitter();
  139.         glutPostRedisplay();
  140.         break;
  141.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  142.         exit(0);
  143.     }
  144. }
  145.  
  146. GLvoid
  147. reshape( GLsizei width, GLsizei height )
  148. {
  149.     glViewport( 0, 0, width, height );
  150.  
  151.     aspect = (GLdouble) width/(GLdouble) height ;
  152.  
  153.     glMatrixMode( GL_PROJECTION );
  154.     glLoadIdentity();
  155.     glMatrixMode( GL_MODELVIEW );
  156. }
  157.  
  158. /* When maxJitters is greater than 1, drawScene() draws 5 torii
  159.  * into the accumulation buffer 8 times; each time with a 
  160.  * jittered perspective.
  161.  *
  162.  * The torii are drawn at +4,+2,0,2,-4 in z from the focal plane, 
  163.  * so the third torii will stay in focus.  
  164.  * The amount of jitter is adjusted by aperture times the 
  165.  * magnitude of the accPerspective() jitter (see jitter.h)
  166.  */
  167. GLvoid
  168. drawScene( GLvoid )
  169. {
  170.     int i, jitter, torii = 5;
  171.     static GLfloat focalPlane = 10.0, aperture = 0.5;
  172.  
  173.     if (maxJitters > 1) glClear(GL_ACCUM_BUFFER_BIT);
  174.  
  175.     for (jitter = 0; jitter < maxJitters; jitter++) {
  176.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  177.  
  178.         glPushMatrix();
  179.         accPerspective (30.0, aspect, 1.0, 15.0, 0.0, 0.0,
  180.             aperture*j8[jitter].x, aperture*j8[jitter].y, 
  181.             focalPlane);
  182.         for ( i = 0; i < torii; i++ )
  183.         {
  184.             glColor3f(i/5.0, i/5.0, 1.0 - i/5.0);
  185.             glPushMatrix();
  186.                 /* draw 5 torii at +4,+2,0,-2,-4
  187.                  * from focalPlane in z. */
  188.                 glTranslatef( -1.0 + i, 0.0,
  189.                     -focalPlane + (4.0 - i*2.0) );
  190.                 glutSolidTorus( 0.25, 0.75, 15, 15 );
  191.             glPopMatrix();
  192.         }
  193.         glPopMatrix();
  194.  
  195.         if (maxJitters > 1) glAccum (GL_ACCUM, 1/(GLfloat) maxJitters);
  196.     }
  197.     if (maxJitters > 1) glAccum (GL_RETURN, 1.0);
  198.     glutSwapBuffers();
  199. }
  200.