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