1/*
2 * Copyright (c) 1993-1997, 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(R) is a registered trademark of Silicon Graphics, Inc.
36 */
37
38/*  accpersp.c
39 *  Use the accumulation buffer to do full-scene antialiasing
40 *  on a scene with perspective projection, using the special
41 *  routines accFrustum() and accPerspective().
42 */
43#include <stdlib.h>
44#include <math.h>
45#include "glut_wrap.h"
46#include "jitter.h"
47
48#define PI_ 3.14159265358979323846
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 */
65static void accFrustum(GLdouble left, GLdouble right, GLdouble bottom,
66   GLdouble top, GLdouble nnear, GLdouble ffar, GLdouble pixdx,
67   GLdouble pixdy, 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*nnear/focus);
79   dy = -(pixdy*ywsize/(GLdouble) viewport[3] + eyedy*nnear/focus);
80
81   glMatrixMode(GL_PROJECTION);
82   glLoadIdentity();
83   glFrustum (left + dx, right + dx, bottom + dy, top + dy, nnear, ffar);
84   glMatrixMode(GL_MODELVIEW);
85   glLoadIdentity();
86   glTranslatef (-eyedx, -eyedy, 0.0);
87}
88
89/* accPerspective()
90 *
91 * The first 4 arguments are identical to the gluPerspective() call.
92 * pixdx and pixdy are anti-alias jitter in pixels.
93 * Set both equal to 0.0 for no anti-alias jitter.
94 * eyedx and eyedy are depth-of field jitter in pixels.
95 * Set both equal to 0.0 for no depth of field effects.
96 *
97 * focus is distance from eye to plane in focus.
98 * focus must be greater than, but not equal to 0.0.
99 *
100 * Note that accPerspective() calls accFrustum().
101 */
102static void accPerspective(GLdouble fovy, GLdouble aspect,
103   GLdouble nnear, GLdouble ffar, GLdouble pixdx, GLdouble pixdy,
104   GLdouble eyedx, GLdouble eyedy, GLdouble focus)
105{
106   GLdouble fov2,left,right,bottom,top;
107
108   fov2 = ((fovy*PI_) / 180.0) / 2.0;
109
110   top = nnear / (cos(fov2) / sin(fov2));
111   bottom = -top;
112
113   right = top * aspect;
114   left = -right;
115
116   accFrustum (left, right, bottom, top, nnear, ffar,
117               pixdx, pixdy, eyedx, eyedy, focus);
118}
119
120/*  Initialize lighting and other values.
121 */
122static void init(void)
123{
124   GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
125   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
126   GLfloat light_position[] = { 0.0, 0.0, 10.0, 1.0 };
127   GLfloat lm_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
128
129   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
130   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
131   glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
132   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
133   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lm_ambient);
134
135   glEnable(GL_LIGHTING);
136   glEnable(GL_LIGHT0);
137   glEnable(GL_DEPTH_TEST);
138   glShadeModel (GL_FLAT);
139
140   glClearColor(0.0, 0.0, 0.0, 0.0);
141   glClearAccum(0.0, 0.0, 0.0, 0.0);
142}
143
144static void displayObjects(void)
145{
146   GLfloat torus_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
147   GLfloat cube_diffuse[] = { 0.0, 0.7, 0.7, 1.0 };
148   GLfloat sphere_diffuse[] = { 0.7, 0.0, 0.7, 1.0 };
149   GLfloat octa_diffuse[] = { 0.7, 0.4, 0.4, 1.0 };
150
151   glPushMatrix ();
152   glTranslatef (0.0, 0.0, -5.0);
153   glRotatef (30.0, 1.0, 0.0, 0.0);
154
155   glPushMatrix ();
156   glTranslatef (-0.80, 0.35, 0.0);
157   glRotatef (100.0, 1.0, 0.0, 0.0);
158   glMaterialfv(GL_FRONT, GL_DIFFUSE, torus_diffuse);
159   glutSolidTorus (0.275, 0.85, 16, 16);
160   glPopMatrix ();
161
162   glPushMatrix ();
163   glTranslatef (-0.75, -0.50, 0.0);
164   glRotatef (45.0, 0.0, 0.0, 1.0);
165   glRotatef (45.0, 1.0, 0.0, 0.0);
166   glMaterialfv(GL_FRONT, GL_DIFFUSE, cube_diffuse);
167   glutSolidCube (1.5);
168   glPopMatrix ();
169
170   glPushMatrix ();
171   glTranslatef (0.75, 0.60, 0.0);
172   glRotatef (30.0, 1.0, 0.0, 0.0);
173   glMaterialfv(GL_FRONT, GL_DIFFUSE, sphere_diffuse);
174   glutSolidSphere (1.0, 16, 16);
175   glPopMatrix ();
176
177   glPushMatrix ();
178   glTranslatef (0.70, -0.90, 0.25);
179   glMaterialfv(GL_FRONT, GL_DIFFUSE, octa_diffuse);
180   glutSolidOctahedron ();
181   glPopMatrix ();
182
183   glPopMatrix ();
184}
185
186#define ACSIZE	8
187
188static void display(void)
189{
190   GLint viewport[4];
191   int jitter;
192
193   glGetIntegerv (GL_VIEWPORT, viewport);
194
195   glClear(GL_ACCUM_BUFFER_BIT);
196   for (jitter = 0; jitter < ACSIZE; jitter++) {
197      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
198      accPerspective (50.0,
199         (GLdouble) viewport[2]/(GLdouble) viewport[3],
200         1.0, 15.0, j8[jitter].x, j8[jitter].y, 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
208static void reshape(int w, int h)
209{
210   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
211}
212
213/* ARGSUSED1 */
214static void keyboard(unsigned char key, int x, int y)
215{
216   switch (key) {
217      case 27:
218         exit(0);
219         break;
220   }
221}
222
223/*  Main Loop
224 *  Be certain you request an accumulation buffer.
225 */
226int main(int argc, char** argv)
227{
228   glutInit(&argc, argv);
229   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
230                        | GLUT_ACCUM | GLUT_DEPTH);
231   glutInitWindowSize (250, 250);
232   glutInitWindowPosition (100, 100);
233   glutCreateWindow (argv[0]);
234   init();
235   glutReshapeFunc(reshape);
236   glutDisplayFunc(display);
237   glutKeyboardFunc(keyboard);
238   glutMainLoop();
239   return 0;
240}
241