home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / ACCPERSP.C < prev    next >
Text File  |  1995-03-04  |  7KB  |  226 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. /*  accpersp.c
  38.  */
  39. #include <GL/gl.h>
  40. #include <GL/glu.h>
  41. #include <math.h>
  42. #include "aux.h"
  43. #include "jitter.h"
  44.  
  45. #define PI_ 3.14159265358979323846
  46.  
  47. /*    accFrustum()
  48.  *  The first 6 arguments are identical to the glFrustum() call.
  49.  *  
  50.  *  pixdx and pixdy are anti-alias jitter in pixels. 
  51.  *  Set both equal to 0.0 for no anti-alias jitter.
  52.  *  eyedx and eyedy are depth-of field jitter in pixels. 
  53.  *  Set both equal to 0.0 for no depth of field effects.
  54.  *
  55.  *  focus is distance from eye to plane in focus. 
  56.  *  focus must be greater than, but not equal to 0.0.
  57.  *
  58.  *  Note that accFrustum() calls glTranslatef().  You will 
  59.  *  probably want to insure that your ModelView matrix has been 
  60.  *  initialized to identity before calling accFrustum().
  61.  */
  62. void accFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,
  63.     GLdouble nearZ, GLdouble farZ, GLdouble pixdx, GLdouble pixdy, 
  64.     GLdouble eyedx, GLdouble eyedy, GLdouble focus)
  65. {
  66.     GLdouble xwsize, ywsize; 
  67.     GLdouble dx, dy;
  68.     GLint viewport[4];
  69.  
  70.     glGetIntegerv (GL_VIEWPORT, viewport);
  71.     
  72.     xwsize = right - left;
  73.     ywsize = top - bottom;
  74.     
  75.     dx = -(pixdx*xwsize/(GLdouble) viewport[2] + eyedx*nearZ/focus);
  76.     dy = -(pixdy*ywsize/(GLdouble) viewport[3] + eyedy*nearZ/focus);
  77.     
  78.     glMatrixMode(GL_PROJECTION);
  79.     glLoadIdentity();
  80.     glFrustum (left + dx, right + dx, bottom + dy, top + dy, nearZ, farZ);
  81.     glMatrixMode(GL_MODELVIEW);
  82.     glLoadIdentity();
  83.     glTranslatef (-eyedx, -eyedy, 0.0);
  84. }
  85.  
  86. /*  accPerspective()
  87.  * 
  88.  *  The first 4 arguments are identical to the gluPerspective() call.
  89.  *  pixdx and pixdy are anti-alias jitter in pixels. 
  90.  *  Set both equal to 0.0 for no anti-alias jitter.
  91.  *  eyedx and eyedy are depth-of field jitter in pixels. 
  92.  *  Set both equal to 0.0 for no depth of field effects.
  93.  *
  94.  *  focus is distance from eye to plane in focus. 
  95.  *  focus must be greater than, but not equal to 0.0.
  96.  *
  97.  *  Note that accPerspective() calls accFrustum().
  98.  */
  99. void accPerspective(GLdouble fovy, GLdouble aspect, 
  100.     GLdouble nearZ, GLdouble farZ, GLdouble pixdx, GLdouble pixdy, 
  101.     GLdouble eyedx, GLdouble eyedy, GLdouble focus)
  102. {
  103.     GLdouble fov2,left,right,bottom,top;
  104.  
  105.     fov2 = ((fovy*PI_) / 180.0) / 2.0;
  106.  
  107.     top = nearZ / (cos(fov2) / sin(fov2));
  108.     bottom = -top;
  109.  
  110.     right = top * aspect;
  111.     left = -right;
  112.  
  113.     accFrustum (left, right, bottom, top, nearZ, farZ,
  114.     pixdx, pixdy, eyedx, eyedy, focus);
  115. }
  116.  
  117. /*  Initialize lighting and other values.
  118.  */
  119. void myinit(void)
  120. {
  121.     GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
  122.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  123.     GLfloat light_position[] = { 0.0, 0.0, 10.0, 1.0 };
  124.     GLfloat lm_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  125.  
  126.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  127.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  128.     glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
  129.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  130.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lm_ambient);
  131.     
  132.     glEnable(GL_LIGHTING);
  133.     glEnable(GL_LIGHT0);
  134.     glDepthFunc(GL_LESS);
  135.     glEnable(GL_DEPTH_TEST);
  136.     glShadeModel (GL_FLAT);
  137.  
  138.     glClearColor(0.0, 0.0, 0.0, 0.0);
  139.     glClearAccum(0.0, 0.0, 0.0, 0.0);
  140. }
  141.  
  142. void displayObjects(void) 
  143. {
  144.     GLfloat torus_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
  145.     GLfloat cube_diffuse[] = { 0.0, 0.7, 0.7, 1.0 };
  146.     GLfloat sphere_diffuse[] = { 0.7, 0.0, 0.7, 1.0 };
  147.     GLfloat octa_diffuse[] = { 0.7, 0.4, 0.4, 1.0 };
  148.     
  149.     glPushMatrix ();
  150.     glTranslatef (0.0, 0.0, -5.0); 
  151.     glRotatef (30.0, 1.0, 0.0, 0.0);
  152.  
  153.     glPushMatrix ();
  154.     glTranslatef (-0.80, 0.35, 0.0); 
  155.     glRotatef (100.0, 1.0, 0.0, 0.0);
  156.     glMaterialfv(GL_FRONT, GL_DIFFUSE, torus_diffuse);
  157.     auxSolidTorus (0.275, 0.85);
  158.     glPopMatrix ();
  159.  
  160.     glPushMatrix ();
  161.     glTranslatef (-0.75, -0.50, 0.0); 
  162.     glRotatef (45.0, 0.0, 0.0, 1.0);
  163.     glRotatef (45.0, 1.0, 0.0, 0.0);
  164.     glMaterialfv(GL_FRONT, GL_DIFFUSE, cube_diffuse);
  165.     auxSolidCube (1.5);
  166.     glPopMatrix ();
  167.  
  168.     glPushMatrix ();
  169.     glTranslatef (0.75, 0.60, 0.0); 
  170.     glRotatef (30.0, 1.0, 0.0, 0.0);
  171.     glMaterialfv(GL_FRONT, GL_DIFFUSE, sphere_diffuse);
  172.     auxSolidSphere (1.0);
  173.     glPopMatrix ();
  174.  
  175.     glPushMatrix ();
  176.     glTranslatef (0.70, -0.90, 0.25); 
  177.     glMaterialfv(GL_FRONT, GL_DIFFUSE, octa_diffuse);
  178.     auxSolidOctahedron (1.0);
  179.     glPopMatrix ();
  180.  
  181.     glPopMatrix ();
  182. }
  183.  
  184. #define ACSIZE    8
  185.  
  186. void display(void)
  187. {
  188.     GLint viewport[4];
  189.     int jitter;
  190.  
  191.     glGetIntegerv (GL_VIEWPORT, viewport);
  192.  
  193.     glClear(GL_ACCUM_BUFFER_BIT);
  194.     for (jitter = 0; jitter < ACSIZE; jitter++) {
  195.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  196.     accPerspective (50.0, 
  197.         (GLdouble) viewport[2]/(GLdouble) viewport[3], 
  198.         1.0, 15.0, j8[jitter].x, j8[jitter].y,
  199.         0.0, 0.0, 1.0);
  200.     displayObjects ();
  201.     glAccum(GL_ACCUM, 1.0/ACSIZE);
  202.     }
  203.     glAccum (GL_RETURN, 1.0);
  204.     glFlush();
  205. }
  206.  
  207. void myReshape(GLint w, GLint h)
  208. {
  209.     glViewport(0, 0, w, h);
  210. }
  211.  
  212. /*  Main Loop
  213.  *  Open window with initial window size, title bar, 
  214.  *  RGBA display mode, and handle input events.
  215.  */
  216. int main(int argc, char** argv)
  217. {
  218.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB
  219.             | AUX_ACCUM | AUX_DEPTH);
  220.     auxInitPosition (0, 0, 250, 250);
  221.     auxInitWindow (argv[0]);
  222.     myinit();
  223.     auxReshapeFunc (myReshape);
  224.     auxMainLoop(display);
  225. }
  226.