/opengles/redbook/fog.c

http://ftk.googlecode.com/ · C · 182 lines · 116 code · 14 blank · 52 comment · 10 complexity · dd653fc2efc8c0400ee39c6444f9701b MD5 · raw file

  1. /*
  2. * License Applicability. Except to the extent portions of this file are
  3. * made subject to an alternative license as permitted in the SGI Free
  4. * Software License B, Version 1.1 (the "License"), the contents of this
  5. * file are subject only to the provisions of the License. You may not use
  6. * this file except in compliance with the License. You may obtain a copy
  7. * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
  8. * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
  9. *
  10. * http://oss.sgi.com/projects/FreeB
  11. *
  12. * Note that, as provided in the License, the Software is distributed on an
  13. * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
  14. * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
  15. * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
  16. * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. *
  18. * Original Code. The Original Code is: OpenGL Sample Implementation,
  19. * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
  20. * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
  21. * Copyright in any portions created by third parties is as indicated
  22. * elsewhere herein. All Rights Reserved.
  23. *
  24. * Additional Notice Provisions: The application programming interfaces
  25. * established by SGI in conjunction with the Original Code are The
  26. * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
  27. * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
  28. * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
  29. * Window System(R) (Version 1.3), released October 19, 1998. This software
  30. * was created using the OpenGL(R) version 1.2.1 Sample Implementation
  31. * published by SGI, but has not been independently verified as being
  32. * compliant with the OpenGL(R) version 1.2.1 Specification.
  33. *
  34. */
  35. /*
  36. * fog.c
  37. * This program draws 5 red spheres, each at a different
  38. * z distance from the eye, in different types of fog.
  39. * Pressing the f key chooses between 3 types of
  40. * fog: exponential, exponential squared, and linear.
  41. * In this program, there is a fixed density value, as well
  42. * as fixed start and end values for the linear fog.
  43. */
  44. #include <math.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include "ug.h"
  48. static GLint fogMode;
  49. /* Initialize depth buffer, fog, light source,
  50. * material property, and lighting model.
  51. */
  52. static void init(void)
  53. {
  54. GLfloat position[] = { 0.5, 0.5, 3.0, 0.0 };
  55. glEnable(GL_DEPTH_TEST);
  56. glLightfv(GL_LIGHT0, GL_POSITION, position);
  57. glEnable(GL_LIGHTING);
  58. glEnable(GL_LIGHT0);
  59. {
  60. GLfloat mat[3] = {0.1745, 0.01175, 0.01175};
  61. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, mat);
  62. mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;
  63. glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, mat);
  64. mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
  65. glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, mat);
  66. glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 0.6*128.0);
  67. }
  68. glEnable(GL_FOG);
  69. {
  70. GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
  71. fogMode = GL_EXP;
  72. glFogx (GL_FOG_MODE, fogMode);
  73. glFogfv (GL_FOG_COLOR, fogColor);
  74. glFogf (GL_FOG_DENSITY, 0.35);
  75. glHint (GL_FOG_HINT, GL_DONT_CARE);
  76. glFogf (GL_FOG_START, 1.0);
  77. glFogf (GL_FOG_END, 5.0);
  78. }
  79. glClearColor(0.5, 0.5, 0.5, 1.0); /* fog color */
  80. }
  81. static void renderSphere (GLfloat x, GLfloat y, GLfloat z)
  82. {
  83. glPushMatrix();
  84. glTranslatef (x, y, z);
  85. ugSolidSpheref(0.4, 32, 32);
  86. glPopMatrix();
  87. }
  88. /* display() draws 5 spheres at different z positions.
  89. */
  90. static void display(UGWindow uwin)
  91. {
  92. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  93. renderSphere (-2., -0.5, -1.0);
  94. renderSphere (-1., -0.5, -2.0);
  95. renderSphere (0., -0.5, -3.0);
  96. renderSphere (1., -0.5, -4.0);
  97. renderSphere (2., -0.5, -5.0);
  98. glFlush();
  99. ugSwapBuffers(uwin);
  100. }
  101. static void reshape(UGWindow uwin, int w, int h)
  102. {
  103. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  104. glMatrixMode(GL_PROJECTION);
  105. glLoadIdentity();
  106. if (w <= h)
  107. glOrthof(-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
  108. 2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
  109. else
  110. glOrthof(-2.5*(GLfloat)w/(GLfloat)h,
  111. 2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
  112. glMatrixMode(GL_MODELVIEW);
  113. glLoadIdentity ();
  114. }
  115. static void keyboard(UGWindow uwin, int key, int x, int y)
  116. {
  117. switch (key) {
  118. case 'f':
  119. case 'F':
  120. if (fogMode == GL_EXP) {
  121. fogMode = GL_EXP2;
  122. printf ("Fog mode is GL_EXP2\n");
  123. }
  124. else if (fogMode == GL_EXP2) {
  125. fogMode = GL_LINEAR;
  126. printf ("Fog mode is GL_LINEAR\n");
  127. }
  128. else if (fogMode == GL_LINEAR) {
  129. fogMode = GL_EXP;
  130. printf ("Fog mode is GL_EXP\n");
  131. }
  132. glFogx(GL_FOG_MODE, fogMode);
  133. ugPostRedisplay(uwin);
  134. break;
  135. case 27:
  136. exit(0);
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. /* Main Loop
  143. * Open window with initial window size, title bar,
  144. * RGBA display mode, depth buffer, and handle input events.
  145. */
  146. #ifdef FTK_AS_PLUGIN
  147. #include "ftk_app_demo.h"
  148. FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
  149. FtkApp* ftk_app_demo_fog_create()
  150. {
  151. return ftk_app_demo_create(_("fog"), ftk_main);
  152. }
  153. #else
  154. #define FTK_HIDE extern
  155. #endif /*FTK_AS_PLUGIN*/
  156. FTK_HIDE int FTK_MAIN(int argc, char* argv[])
  157. {
  158. UGCtx ug = ugInit();
  159. UGWindow uwin = ugCreateWindow (ug, "UG_DEPTH", "fog", 500, 500, 100, 100);
  160. init();
  161. ugDisplayFunc(uwin, display);
  162. ugReshapeFunc(uwin, reshape);
  163. ugKeyboardFunc(uwin, keyboard);
  164. #ifndef FTK_AS_PLUGIN
  165. ugMainLoop(ug);
  166. #endif
  167. return 0;
  168. }