home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1995, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* dof.c
- * This program demonstrates using the accumlation buffer to
- * simulate depth-of-field. Objects at the focal point are
- * crisp, the rest are somewhat fuzzy.
- *
- * Escape key - exit the program
- * <d> key - toggle depth-of-field
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- #include "jitter.h"
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid toggleJitter(GLvoid);
-
- GLvoid accFrustum(GLdouble, GLdouble, GLdouble, GLdouble,
- GLdouble, GLdouble, GLdouble, GLdouble,
- GLdouble, GLdouble, GLdouble);
- GLvoid accPerspective(GLdouble, GLdouble, GLdouble, GLdouble,
- GLdouble, GLdouble, GLdouble, GLdouble, GLdouble);
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLint maxJitters = 1;
- static GLdouble aspect = 1.0;
-
- void
- main(int argc, char *argv[])
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width/4, height/4 );
- glutInitWindowSize( width/4, height/4 );
- glutInitDisplayMode( GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE|GLUT_ACCUM );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - use the accumulation buffer "
- "to simulate depth of field.\n\n"
- "Expect it to run very slowly on systems without \n"
- "a hardware accumulation buffer.\n\n"
- "Escape key - exit the program \n"
- "<d> key - toggle depth-of-field\n\n",
- progname );
-
- printf("depth-of-field is OFF\n");
- }
-
- GLvoid
- initgfx( void )
- {
- GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
- GLfloat mat_shininess[] = { 128.0 };
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
-
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
-
- glColorMaterial(GL_FRONT, GL_DIFFUSE);
- glEnable(GL_COLOR_MATERIAL);
-
- glClearColor( 0, 0, 0, 1 );
- glClearAccum( 0.0, 0.0, 0.0, 1.0 );
-
- glEnable( GL_DEPTH_TEST );
- }
-
- GLvoid
- toggleJitter(GLvoid)
- {
- maxJitters = ( maxJitters == 1 ? 8 : 1 );
-
- printf("depth-of-field is %s\n", (maxJitters==1?"OFF":"ON"));
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'd': /* toggle depth-of-field */
- toggleJitter();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width/(GLdouble) height ;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glMatrixMode( GL_MODELVIEW );
- }
-
- /* When maxJitters is greater than 1, drawScene() draws 5 torii
- * into the accumulation buffer 8 times; each time with a
- * jittered perspective.
- *
- * The torii are drawn at +4,+2,0,2,-4 in z from the focal plane,
- * so the third torii will stay in focus.
- * The amount of jitter is adjusted by aperture times the
- * magnitude of the accPerspective() jitter (see jitter.h)
- */
- GLvoid
- drawScene( GLvoid )
- {
- int i, jitter, torii = 5;
- static GLfloat focalPlane = 10.0, aperture = 0.5;
-
- if (maxJitters > 1) glClear(GL_ACCUM_BUFFER_BIT);
-
- for (jitter = 0; jitter < maxJitters; jitter++) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- glPushMatrix();
- accPerspective (30.0, aspect, 1.0, 15.0, 0.0, 0.0,
- aperture*j8[jitter].x, aperture*j8[jitter].y,
- focalPlane);
- for ( i = 0; i < torii; i++ )
- {
- glColor3f(i/5.0, i/5.0, 1.0 - i/5.0);
- glPushMatrix();
- /* draw 5 torii at +4,+2,0,-2,-4
- * from focalPlane in z. */
- glTranslatef( -1.0 + i, 0.0,
- -focalPlane + (4.0 - i*2.0) );
- glutSolidTorus( 0.25, 0.75, 15, 15 );
- glPopMatrix();
- }
- glPopMatrix();
-
- if (maxJitters > 1) glAccum (GL_ACCUM, 1/(GLfloat) maxJitters);
- }
- if (maxJitters > 1) glAccum (GL_RETURN, 1.0);
- glutSwapBuffers();
- }
-