home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / DOF.C < prev    next >
Text File  |  1995-03-04  |  9KB  |  236 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  *  dof.c
  39.  *  This program demonstrates use of the accumulation buffer to
  40.  *  create an out-of-focus depth-of-field effect.  The teapots
  41.  *  are drawn several times into the accumulation buffer.  The
  42.  *  viewing volume is jittered, except at the focal point, where
  43.  *  the viewing volume is at the same position, each time.  In
  44.  *  this case, the gold teapot remains in focus.
  45.  */
  46. #include <GL/gl.h>
  47. #include <GL/glu.h>
  48. #include <math.h>
  49. #include "aux.h"
  50. #include "jitter.h"
  51.  
  52. #define PI_ 3.14159265358979323846
  53.  
  54. /*    accFrustum()
  55.  *  The first 6 arguments are identical to the glFrustum() call.
  56.  *  
  57.  *  pixdx and pixdy are anti-alias jitter in pixels. 
  58.  *  Set both equal to 0.0 for no anti-alias jitter.
  59.  *  eyedx and eyedy are depth-of field jitter in pixels. 
  60.  *  Set both equal to 0.0 for no depth of field effects.
  61.  *
  62.  *  focus is distance from eye to plane in focus. 
  63.  *  focus must be greater than, but not equal to 0.0.
  64.  *
  65.  *  Note that accFrustum() calls glTranslatef().  You will 
  66.  *  probably want to insure that your ModelView matrix has been 
  67.  *  initialized to identity before calling accFrustum().
  68.  */
  69. void accFrustum(GLdouble left, GLdouble right, GLdouble bottom, 
  70.     GLdouble top, GLdouble nearZ, GLdouble farZ, GLdouble pixdx, 
  71.     GLdouble pixdy, GLdouble eyedx, GLdouble eyedy, GLdouble focus)
  72. {
  73.     GLdouble xwsize, ywsize; 
  74.     GLdouble dx, dy;
  75.     GLint viewport[4];
  76.  
  77.     glGetIntegerv (GL_VIEWPORT, viewport);
  78.     
  79.     xwsize = right - left;
  80.     ywsize = top - bottom;
  81.     
  82.     dx = -(pixdx*xwsize/(GLdouble) viewport[2] + eyedx*nearZ/focus);
  83.     dy = -(pixdy*ywsize/(GLdouble) viewport[3] + eyedy*nearZ/focus);
  84.     
  85.     glMatrixMode(GL_PROJECTION);
  86.     glLoadIdentity();
  87.     glFrustum (left + dx, right + dx, bottom + dy, top + dy, nearZ, farZ);
  88.     glMatrixMode(GL_MODELVIEW);
  89.     glLoadIdentity();
  90.     glTranslatef (-eyedx, -eyedy, 0.0);
  91. }
  92.  
  93. /*  accPerspective()
  94.  * 
  95.  *  The first 4 arguments are identical to the gluPerspective() call.
  96.  *  pixdx and pixdy are anti-alias jitter in pixels. 
  97.  *  Set both equal to 0.0 for no anti-alias jitter.
  98.  *  eyedx and eyedy are depth-of field jitter in pixels. 
  99.  *  Set both equal to 0.0 for no depth of field effects.
  100.  *
  101.  *  focus is distance from eye to plane in focus. 
  102.  *  focus must be greater than, but not equal to 0.0.
  103.  *
  104.  *  Note that accPerspective() calls accFrustum().
  105.  */
  106. void accPerspective(GLdouble fovy, GLdouble aspect, 
  107.     GLdouble nearZ, GLdouble farZ, GLdouble pixdx, GLdouble pixdy, 
  108.     GLdouble eyedx, GLdouble eyedy, GLdouble focus)
  109. {
  110.     GLdouble fov2,left,right,bottom,top;
  111.  
  112.     fov2 = ((fovy*PI_) / 180.0) / 2.0;
  113.  
  114.     top = nearZ / (cos(fov2) / sin(fov2));
  115.     bottom = -top;
  116.  
  117.     right = top * aspect;
  118.     left = -right;
  119.  
  120.     accFrustum (left, right, bottom, top, nearZ, farZ,
  121.     pixdx, pixdy, eyedx, eyedy, focus);
  122. }
  123.  
  124. void myinit(void)
  125. {
  126.     GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  127.     GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  128.     GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
  129.     GLfloat position[] = { 0.0, 3.0, 3.0, 0.0 };
  130.     
  131.     GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  132.     GLfloat local_view[] = { 0.0 };
  133.  
  134.     glEnable(GL_DEPTH_TEST);
  135.     glDepthFunc(GL_LESS);
  136.  
  137.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  138.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  139.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  140.     
  141.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  142.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  143.  
  144.     glFrontFace (GL_CW);
  145.     glEnable(GL_LIGHTING);
  146.     glEnable(GL_LIGHT0);
  147.     glEnable(GL_AUTO_NORMAL);
  148.     glEnable(GL_NORMALIZE);
  149.  
  150.     glMatrixMode (GL_MODELVIEW);
  151.     glLoadIdentity ();
  152.  
  153.     glClearColor(0.0, 0.0, 0.0, 0.0);
  154.     glClearAccum(0.0, 0.0, 0.0, 0.0);
  155. }
  156.  
  157. void renderTeapot (GLfloat x, GLfloat y, GLfloat z, 
  158.     GLfloat ambr, GLfloat ambg, GLfloat ambb, 
  159.     GLfloat difr, GLfloat difg, GLfloat difb, 
  160.     GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine)
  161. {
  162.     float mat[4];
  163.  
  164.     glPushMatrix();
  165.     glTranslatef (x, y, z);
  166.     mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0;    
  167.     glMaterialfv (GL_FRONT, GL_AMBIENT, mat);
  168.     mat[0] = difr; mat[1] = difg; mat[2] = difb;    
  169.     glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
  170.     mat[0] = specr; mat[1] = specg; mat[2] = specb;
  171.     glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
  172.     glMaterialf (GL_FRONT, GL_SHININESS, shine*128.0);
  173.     auxSolidTeapot(0.5);
  174.     glPopMatrix();
  175. }
  176.  
  177. /*  display() draws 5 teapots into the accumulation buffer 
  178.  *  several times; each time with a jittered perspective.
  179.  *  The focal point is at z = 5.0, so the gold teapot will 
  180.  *  stay in focus.  The amount of jitter is adjusted by the
  181.  *  magnitude of the accPerspective() jitter; in this example, 0.33.
  182.  *  In this example, the teapots are drawn 8 times.  See jitter.h
  183.  */
  184. void display(void)
  185. {
  186.     int jitter;
  187.     GLint viewport[4];
  188.  
  189.     glGetIntegerv (GL_VIEWPORT, viewport);
  190.     glClear(GL_ACCUM_BUFFER_BIT);
  191.  
  192.     for (jitter = 0; jitter < 8; jitter++) {
  193.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  194.     accPerspective (45.0, 
  195.         (GLdouble) viewport[2]/(GLdouble) viewport[3], 
  196.         1.0, 15.0, 0.0, 0.0,
  197.         0.33*j8[jitter].x, 0.33*j8[jitter].y, 5.0);
  198. /*    ruby, gold, silver, emerald, and cyan teapots    */
  199.     renderTeapot (-1.1, -0.5, -4.5, 0.1745, 0.01175, 0.01175,
  200.         0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 0.6);
  201.     renderTeapot (-0.5, -0.5, -5.0, 0.24725, 0.1995, 0.0745,
  202.         0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
  203.     renderTeapot (0.2, -0.5, -5.5, 0.19225, 0.19225, 0.19225,
  204.         0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 0.4);
  205.     renderTeapot (1.0, -0.5, -6.0, 0.0215, 0.1745, 0.0215, 
  206.         0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
  207.     renderTeapot (1.8, -0.5, -6.5, 0.0, 0.1, 0.06, 0.0, 0.50980392, 
  208.         0.50980392, 0.50196078, 0.50196078, 0.50196078, .25);
  209.     glAccum (GL_ACCUM, 0.125);
  210.     }
  211.  
  212.     glAccum (GL_RETURN, 1.0);
  213.     glFlush();
  214. }
  215.  
  216. void myReshape(GLint w, GLint h)
  217. {
  218.     glViewport(0, 0, w, h);
  219. }
  220.  
  221. /*  Main Loop
  222.  *  Open window with initial window size, title bar, 
  223.  *  RGBA display mode, depth buffer, and handle input events.
  224.  */
  225. int main(int argc, char** argv)
  226. {
  227.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB
  228.             | AUX_ACCUM | AUX_DEPTH);
  229.     auxInitPosition (0, 0, 400, 400);
  230.     auxInitWindow (argv[0]);
  231.     myinit();
  232.     auxReshapeFunc (myReshape);
  233.     auxMainLoop(display);
  234. }
  235.  
  236.