132001f49Smrg/*
232001f49Smrg * Copyright (c) 1993-1997, Silicon Graphics, Inc.
332001f49Smrg * ALL RIGHTS RESERVED
432001f49Smrg * Permission to use, copy, modify, and distribute this software for
532001f49Smrg * any purpose and without fee is hereby granted, provided that the above
632001f49Smrg * copyright notice appear in all copies and that both the copyright notice
732001f49Smrg * and this permission notice appear in supporting documentation, and that
832001f49Smrg * the name of Silicon Graphics, Inc. not be used in advertising
932001f49Smrg * or publicity pertaining to distribution of the software without specific,
1032001f49Smrg * written prior permission.
1132001f49Smrg *
1232001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
1332001f49Smrg * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
1432001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
1532001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
1632001f49Smrg * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
1732001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
1832001f49Smrg * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
1932001f49Smrg * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
2032001f49Smrg * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
2132001f49Smrg * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
2232001f49Smrg * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
2332001f49Smrg * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
2432001f49Smrg *
2532001f49Smrg * US Government Users Restricted Rights
2632001f49Smrg * Use, duplication, or disclosure by the Government is subject to
2732001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
2832001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software
2932001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor
3032001f49Smrg * clauses in the FAR or the DOD or NASA FAR Supplement.
3132001f49Smrg * Unpublished-- rights reserved under the copyright laws of the
3232001f49Smrg * United States.  Contractor/manufacturer is Silicon Graphics,
3332001f49Smrg * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
3432001f49Smrg *
3532001f49Smrg * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
3632001f49Smrg */
3732001f49Smrg
3832001f49Smrg/*  accpersp.c
3932001f49Smrg *  Use the accumulation buffer to do full-scene antialiasing
4032001f49Smrg *  on a scene with perspective projection, using the special
4132001f49Smrg *  routines accFrustum() and accPerspective().
4232001f49Smrg */
4332001f49Smrg#include <stdlib.h>
4432001f49Smrg#include <math.h>
4532001f49Smrg#include "glut_wrap.h"
4632001f49Smrg#include "jitter.h"
4732001f49Smrg
4832001f49Smrg#define PI_ 3.14159265358979323846
4932001f49Smrg
5032001f49Smrg/* accFrustum()
5132001f49Smrg * The first 6 arguments are identical to the glFrustum() call.
5232001f49Smrg *
5332001f49Smrg * pixdx and pixdy are anti-alias jitter in pixels.
5432001f49Smrg * Set both equal to 0.0 for no anti-alias jitter.
5532001f49Smrg * eyedx and eyedy are depth-of field jitter in pixels.
5632001f49Smrg * Set both equal to 0.0 for no depth of field effects.
5732001f49Smrg *
5832001f49Smrg * focus is distance from eye to plane in focus.
5932001f49Smrg * focus must be greater than, but not equal to 0.0.
6032001f49Smrg *
6132001f49Smrg * Note that accFrustum() calls glTranslatef().  You will
6232001f49Smrg * probably want to insure that your ModelView matrix has been
6332001f49Smrg * initialized to identity before calling accFrustum().
6432001f49Smrg */
6532001f49Smrgstatic void accFrustum(GLdouble left, GLdouble right, GLdouble bottom,
6632001f49Smrg   GLdouble top, GLdouble nnear, GLdouble ffar, GLdouble pixdx,
6732001f49Smrg   GLdouble pixdy, GLdouble eyedx, GLdouble eyedy, GLdouble focus)
6832001f49Smrg{
6932001f49Smrg   GLdouble xwsize, ywsize;
7032001f49Smrg   GLdouble dx, dy;
7132001f49Smrg   GLint viewport[4];
7232001f49Smrg
7332001f49Smrg   glGetIntegerv (GL_VIEWPORT, viewport);
7432001f49Smrg
7532001f49Smrg   xwsize = right - left;
7632001f49Smrg   ywsize = top - bottom;
7732001f49Smrg
7832001f49Smrg   dx = -(pixdx*xwsize/(GLdouble) viewport[2] + eyedx*nnear/focus);
7932001f49Smrg   dy = -(pixdy*ywsize/(GLdouble) viewport[3] + eyedy*nnear/focus);
8032001f49Smrg
8132001f49Smrg   glMatrixMode(GL_PROJECTION);
8232001f49Smrg   glLoadIdentity();
8332001f49Smrg   glFrustum (left + dx, right + dx, bottom + dy, top + dy, nnear, ffar);
8432001f49Smrg   glMatrixMode(GL_MODELVIEW);
8532001f49Smrg   glLoadIdentity();
8632001f49Smrg   glTranslatef (-eyedx, -eyedy, 0.0);
8732001f49Smrg}
8832001f49Smrg
8932001f49Smrg/* accPerspective()
9032001f49Smrg *
9132001f49Smrg * The first 4 arguments are identical to the gluPerspective() call.
9232001f49Smrg * pixdx and pixdy are anti-alias jitter in pixels.
9332001f49Smrg * Set both equal to 0.0 for no anti-alias jitter.
9432001f49Smrg * eyedx and eyedy are depth-of field jitter in pixels.
9532001f49Smrg * Set both equal to 0.0 for no depth of field effects.
9632001f49Smrg *
9732001f49Smrg * focus is distance from eye to plane in focus.
9832001f49Smrg * focus must be greater than, but not equal to 0.0.
9932001f49Smrg *
10032001f49Smrg * Note that accPerspective() calls accFrustum().
10132001f49Smrg */
10232001f49Smrgstatic void accPerspective(GLdouble fovy, GLdouble aspect,
10332001f49Smrg   GLdouble nnear, GLdouble ffar, GLdouble pixdx, GLdouble pixdy,
10432001f49Smrg   GLdouble eyedx, GLdouble eyedy, GLdouble focus)
10532001f49Smrg{
10632001f49Smrg   GLdouble fov2,left,right,bottom,top;
10732001f49Smrg
10832001f49Smrg   fov2 = ((fovy*PI_) / 180.0) / 2.0;
10932001f49Smrg
11032001f49Smrg   top = nnear / (cos(fov2) / sin(fov2));
11132001f49Smrg   bottom = -top;
11232001f49Smrg
11332001f49Smrg   right = top * aspect;
11432001f49Smrg   left = -right;
11532001f49Smrg
11632001f49Smrg   accFrustum (left, right, bottom, top, nnear, ffar,
11732001f49Smrg               pixdx, pixdy, eyedx, eyedy, focus);
11832001f49Smrg}
11932001f49Smrg
12032001f49Smrg/*  Initialize lighting and other values.
12132001f49Smrg */
12232001f49Smrgstatic void init(void)
12332001f49Smrg{
12432001f49Smrg   GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
12532001f49Smrg   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
12632001f49Smrg   GLfloat light_position[] = { 0.0, 0.0, 10.0, 1.0 };
12732001f49Smrg   GLfloat lm_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
12832001f49Smrg
12932001f49Smrg   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
13032001f49Smrg   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
13132001f49Smrg   glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
13232001f49Smrg   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
13332001f49Smrg   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lm_ambient);
13432001f49Smrg
13532001f49Smrg   glEnable(GL_LIGHTING);
13632001f49Smrg   glEnable(GL_LIGHT0);
13732001f49Smrg   glEnable(GL_DEPTH_TEST);
13832001f49Smrg   glShadeModel (GL_FLAT);
13932001f49Smrg
14032001f49Smrg   glClearColor(0.0, 0.0, 0.0, 0.0);
14132001f49Smrg   glClearAccum(0.0, 0.0, 0.0, 0.0);
14232001f49Smrg}
14332001f49Smrg
14432001f49Smrgstatic void displayObjects(void)
14532001f49Smrg{
14632001f49Smrg   GLfloat torus_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
14732001f49Smrg   GLfloat cube_diffuse[] = { 0.0, 0.7, 0.7, 1.0 };
14832001f49Smrg   GLfloat sphere_diffuse[] = { 0.7, 0.0, 0.7, 1.0 };
14932001f49Smrg   GLfloat octa_diffuse[] = { 0.7, 0.4, 0.4, 1.0 };
15032001f49Smrg
15132001f49Smrg   glPushMatrix ();
15232001f49Smrg   glTranslatef (0.0, 0.0, -5.0);
15332001f49Smrg   glRotatef (30.0, 1.0, 0.0, 0.0);
15432001f49Smrg
15532001f49Smrg   glPushMatrix ();
15632001f49Smrg   glTranslatef (-0.80, 0.35, 0.0);
15732001f49Smrg   glRotatef (100.0, 1.0, 0.0, 0.0);
15832001f49Smrg   glMaterialfv(GL_FRONT, GL_DIFFUSE, torus_diffuse);
15932001f49Smrg   glutSolidTorus (0.275, 0.85, 16, 16);
16032001f49Smrg   glPopMatrix ();
16132001f49Smrg
16232001f49Smrg   glPushMatrix ();
16332001f49Smrg   glTranslatef (-0.75, -0.50, 0.0);
16432001f49Smrg   glRotatef (45.0, 0.0, 0.0, 1.0);
16532001f49Smrg   glRotatef (45.0, 1.0, 0.0, 0.0);
16632001f49Smrg   glMaterialfv(GL_FRONT, GL_DIFFUSE, cube_diffuse);
16732001f49Smrg   glutSolidCube (1.5);
16832001f49Smrg   glPopMatrix ();
16932001f49Smrg
17032001f49Smrg   glPushMatrix ();
17132001f49Smrg   glTranslatef (0.75, 0.60, 0.0);
17232001f49Smrg   glRotatef (30.0, 1.0, 0.0, 0.0);
17332001f49Smrg   glMaterialfv(GL_FRONT, GL_DIFFUSE, sphere_diffuse);
17432001f49Smrg   glutSolidSphere (1.0, 16, 16);
17532001f49Smrg   glPopMatrix ();
17632001f49Smrg
17732001f49Smrg   glPushMatrix ();
17832001f49Smrg   glTranslatef (0.70, -0.90, 0.25);
17932001f49Smrg   glMaterialfv(GL_FRONT, GL_DIFFUSE, octa_diffuse);
18032001f49Smrg   glutSolidOctahedron ();
18132001f49Smrg   glPopMatrix ();
18232001f49Smrg
18332001f49Smrg   glPopMatrix ();
18432001f49Smrg}
18532001f49Smrg
18632001f49Smrg#define ACSIZE	8
18732001f49Smrg
18832001f49Smrgstatic void display(void)
18932001f49Smrg{
19032001f49Smrg   GLint viewport[4];
19132001f49Smrg   int jitter;
19232001f49Smrg
19332001f49Smrg   glGetIntegerv (GL_VIEWPORT, viewport);
19432001f49Smrg
19532001f49Smrg   glClear(GL_ACCUM_BUFFER_BIT);
19632001f49Smrg   for (jitter = 0; jitter < ACSIZE; jitter++) {
19732001f49Smrg      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
19832001f49Smrg      accPerspective (50.0,
19932001f49Smrg         (GLdouble) viewport[2]/(GLdouble) viewport[3],
20032001f49Smrg         1.0, 15.0, j8[jitter].x, j8[jitter].y, 0.0, 0.0, 1.0);
20132001f49Smrg      displayObjects ();
20232001f49Smrg      glAccum(GL_ACCUM, 1.0/ACSIZE);
20332001f49Smrg   }
20432001f49Smrg   glAccum (GL_RETURN, 1.0);
20532001f49Smrg   glFlush();
20632001f49Smrg}
20732001f49Smrg
20832001f49Smrgstatic void reshape(int w, int h)
20932001f49Smrg{
21032001f49Smrg   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
21132001f49Smrg}
21232001f49Smrg
21332001f49Smrg/* ARGSUSED1 */
21432001f49Smrgstatic void keyboard(unsigned char key, int x, int y)
21532001f49Smrg{
21632001f49Smrg   switch (key) {
21732001f49Smrg      case 27:
21832001f49Smrg         exit(0);
21932001f49Smrg         break;
22032001f49Smrg   }
22132001f49Smrg}
22232001f49Smrg
22332001f49Smrg/*  Main Loop
22432001f49Smrg *  Be certain you request an accumulation buffer.
22532001f49Smrg */
22632001f49Smrgint main(int argc, char** argv)
22732001f49Smrg{
22832001f49Smrg   glutInit(&argc, argv);
22932001f49Smrg   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
23032001f49Smrg                        | GLUT_ACCUM | GLUT_DEPTH);
23132001f49Smrg   glutInitWindowSize (250, 250);
23232001f49Smrg   glutInitWindowPosition (100, 100);
23332001f49Smrg   glutCreateWindow (argv[0]);
23432001f49Smrg   init();
23532001f49Smrg   glutReshapeFunc(reshape);
23632001f49Smrg   glutDisplayFunc(display);
23732001f49Smrg   glutKeyboardFunc(keyboard);
23832001f49Smrg   glutMainLoop();
23932001f49Smrg   return 0;
24032001f49Smrg}
241