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