Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

accpersp.c 7.4KB

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