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.

fog.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright (c) Mark J. Kilgard, 1994. */
  2. /**
  3. * (c) Copyright 1993, Silicon Graphics, Inc.
  4. * ALL RIGHTS RESERVED
  5. * Permission to use, copy, modify, and distribute this software for
  6. * any purpose and without fee is hereby granted, provided that the above
  7. * copyright notice appear in all copies and that both the copyright notice
  8. * and this permission notice appear in supporting documentation, and that
  9. * the name of Silicon Graphics, Inc. not be used in advertising
  10. * or publicity pertaining to distribution of the software without specific,
  11. * written prior permission.
  12. *
  13. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  17. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  22. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25. *
  26. * US Government Users Restricted Rights
  27. * Use, duplication, or disclosure by the Government is subject to
  28. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30. * clause at DFARS 252.227-7013 and/or in similar or successor
  31. * clauses in the FAR or the DOD or NASA FAR Supplement.
  32. * Unpublished-- rights reserved under the copyright laws of the
  33. * United States. Contractor/manufacturer is Silicon Graphics,
  34. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  35. *
  36. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37. */
  38. /*
  39. * fog.c
  40. * This program draws 5 red teapots, each at a different
  41. * z distance from the eye, in different types of fog.
  42. * Pressing the left mouse button chooses between 3 types of
  43. * fog: exponential, exponential squared, and linear.
  44. * In this program, there is a fixed density value, as well
  45. * as fixed start and end values for the linear fog.
  46. */
  47. #include <stdlib.h>
  48. #include <math.h>
  49. #include <GL/glut.h>
  50. GLint fogMode;
  51. void
  52. selectFog(int mode)
  53. {
  54. switch(mode) {
  55. case GL_LINEAR:
  56. glFogf(GL_FOG_START, 1.0);
  57. glFogf(GL_FOG_END, 5.0);
  58. /* falls through */
  59. case GL_EXP2:
  60. case GL_EXP:
  61. glFogiv(GL_FOG_MODE, (int *) &mode);
  62. glutPostRedisplay();
  63. break;
  64. case 0:
  65. exit(0);
  66. }
  67. }
  68. /* Initialize z-buffer, projection matrix, light source,
  69. * and lighting model. Do not specify a material property here.
  70. */
  71. void
  72. myinit(void)
  73. {
  74. GLfloat position[] =
  75. {0.0, 3.0, 3.0, 0.0};
  76. GLfloat local_view[] =
  77. {0.0};
  78. glEnable(GL_DEPTH_TEST);
  79. glDepthFunc(GL_LESS);
  80. glLightfv(GL_LIGHT0, GL_POSITION, position);
  81. glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  82. glFrontFace(GL_CW);
  83. glEnable(GL_LIGHTING);
  84. glEnable(GL_LIGHT0);
  85. glEnable(GL_AUTO_NORMAL);
  86. glEnable(GL_NORMALIZE);
  87. glEnable(GL_FOG);
  88. {
  89. GLfloat fogColor[4] =
  90. {0.5, 0.5, 0.5, 1.0};
  91. fogMode = GL_EXP;
  92. glFogi(GL_FOG_MODE, fogMode);
  93. glFogfv(GL_FOG_COLOR, fogColor);
  94. glFogf(GL_FOG_DENSITY, 0.35);
  95. glHint(GL_FOG_HINT, GL_DONT_CARE);
  96. glClearColor(0.5, 0.5, 0.5, 1.0);
  97. }
  98. }
  99. void
  100. renderRedTeapot(GLfloat x, GLfloat y, GLfloat z)
  101. {
  102. float mat[4];
  103. glPushMatrix();
  104. glTranslatef(x, y, z);
  105. mat[0] = 0.1745;
  106. mat[1] = 0.01175;
  107. mat[2] = 0.01175;
  108. mat[3] = 1.0;
  109. glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
  110. mat[0] = 0.61424;
  111. mat[1] = 0.04136;
  112. mat[2] = 0.04136;
  113. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
  114. mat[0] = 0.727811;
  115. mat[1] = 0.626959;
  116. mat[2] = 0.626959;
  117. glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
  118. glMaterialf(GL_FRONT, GL_SHININESS, 0.6 * 128.0);
  119. glutSolidTeapot(1.0);
  120. glPopMatrix();
  121. }
  122. /* display() draws 5 teapots at different z positions.
  123. */
  124. void
  125. display(void)
  126. {
  127. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  128. renderRedTeapot(-4.0, -0.5, -1.0);
  129. renderRedTeapot(-2.0, -0.5, -2.0);
  130. renderRedTeapot(0.0, -0.5, -3.0);
  131. renderRedTeapot(2.0, -0.5, -4.0);
  132. renderRedTeapot(4.0, -0.5, -5.0);
  133. glFlush();
  134. }
  135. void
  136. myReshape(int w, int h)
  137. {
  138. glViewport(0, 0, w, h);
  139. glMatrixMode(GL_PROJECTION);
  140. glLoadIdentity();
  141. if (w <= (h * 3))
  142. glOrtho(-6.0, 6.0, -2.0 * ((GLfloat) h * 3) / (GLfloat) w,
  143. 2.0 * ((GLfloat) h * 3) / (GLfloat) w, 0.0, 10.0);
  144. else
  145. glOrtho(-6.0 * (GLfloat) w / ((GLfloat) h * 3),
  146. 6.0 * (GLfloat) w / ((GLfloat) h * 3), -2.0, 2.0, 0.0, 10.0);
  147. glMatrixMode(GL_MODELVIEW);
  148. glLoadIdentity();
  149. }
  150. static void
  151. key(unsigned char k, int x, int y)
  152. {
  153. switch (k) {
  154. case 27: /* Escape */
  155. exit(0);
  156. break;
  157. default:
  158. return;
  159. }
  160. glutPostRedisplay();
  161. }
  162. /* Main Loop
  163. * Open window with initial window size, title bar,
  164. * RGBA display mode, depth buffer, and handle input events.
  165. */
  166. int
  167. main(int argc, char **argv)
  168. {
  169. glutInit(&argc, argv);
  170. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  171. glutInitWindowSize(450, 150);
  172. glutCreateWindow(argv[0]);
  173. myinit();
  174. glutReshapeFunc(myReshape);
  175. glutDisplayFunc(display);
  176. glutCreateMenu(selectFog);
  177. glutAddMenuEntry("Fog EXP", GL_EXP);
  178. glutAddMenuEntry("Fog EXP2", /*GL_EXP2*/ 0xffff);
  179. glutAddMenuEntry("Fog LINEAR", GL_LINEAR);
  180. glutAddMenuEntry("Quit", 0);
  181. glutAttachMenu(GLUT_RIGHT_BUTTON);
  182. glutKeyboardFunc(key);
  183. glutMainLoop();
  184. return 0; /* ANSI C requires main to return int. */
  185. }