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.

fogcoord.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * EXT_fog_coord.
  3. *
  4. * Based on glutskel.c by Brian Paul
  5. * and NeHe's Volumetric fog tutorial!
  6. *
  7. * Daniel Borca
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glew.h>
  13. #include <GL/glut.h>
  14. #define DEPTH 5.0f
  15. static GLfloat camz;
  16. static GLint fogMode;
  17. static GLboolean fogCoord;
  18. static GLfloat fogDensity = 0.75;
  19. static GLfloat fogStart = 1.0, fogEnd = DEPTH;
  20. static GLfloat fogColor[4] = {0.6f, 0.3f, 0.0f, 1.0f};
  21. static const char *ModeStr = NULL;
  22. static GLboolean Arrays = GL_FALSE;
  23. static GLboolean Texture = GL_TRUE;
  24. static void
  25. Reset(void)
  26. {
  27. fogMode = 1;
  28. fogCoord = 1;
  29. fogDensity = 0.75;
  30. fogStart = 1.0;
  31. fogEnd = DEPTH;
  32. Arrays = GL_FALSE;
  33. Texture = GL_TRUE;
  34. }
  35. static void
  36. glFogCoordf_ext (GLfloat f)
  37. {
  38. if (fogCoord)
  39. glFogCoordfEXT(f);
  40. }
  41. static void
  42. PrintString(const char *s)
  43. {
  44. while (*s) {
  45. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  46. s++;
  47. }
  48. }
  49. static void
  50. PrintInfo(void)
  51. {
  52. char s[100];
  53. glDisable(GL_FOG);
  54. glColor3f(0, 1, 1);
  55. sprintf(s, "Mode(m): %s Start(s/S): %g End(e/E): %g Density(d/D): %g",
  56. ModeStr, fogStart, fogEnd, fogDensity);
  57. glWindowPos2iARB(5, 20);
  58. PrintString(s);
  59. sprintf(s, "Arrays(a): %s glFogCoord(c): %s EyeZ(z/z): %g",
  60. (Arrays ? "Yes" : "No"),
  61. (fogCoord ? "Yes" : "No"),
  62. camz);
  63. glWindowPos2iARB(5, 5);
  64. PrintString(s);
  65. }
  66. static int
  67. SetFogMode(GLint fogMode)
  68. {
  69. fogMode &= 3;
  70. switch (fogMode) {
  71. case 0:
  72. ModeStr = "Off";
  73. glDisable(GL_FOG);
  74. break;
  75. case 1:
  76. ModeStr = "GL_LINEAR";
  77. glEnable(GL_FOG);
  78. glFogi(GL_FOG_MODE, GL_LINEAR);
  79. glFogf(GL_FOG_START, fogStart);
  80. glFogf(GL_FOG_END, fogEnd);
  81. break;
  82. case 2:
  83. ModeStr = "GL_EXP";
  84. glEnable(GL_FOG);
  85. glFogi(GL_FOG_MODE, GL_EXP);
  86. glFogf(GL_FOG_DENSITY, fogDensity);
  87. break;
  88. case 3:
  89. ModeStr = "GL_EXP2";
  90. glEnable(GL_FOG);
  91. glFogi(GL_FOG_MODE, GL_EXP2);
  92. glFogf(GL_FOG_DENSITY, fogDensity);
  93. break;
  94. }
  95. return fogMode;
  96. }
  97. static GLboolean
  98. SetFogCoord(GLboolean fogCoord)
  99. {
  100. if (!GLEW_EXT_fog_coord) {
  101. return GL_FALSE;
  102. }
  103. if (fogCoord) {
  104. glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
  105. }
  106. else {
  107. glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
  108. }
  109. return fogCoord;
  110. }
  111. /* could reuse vertices */
  112. static GLuint vertex_index[] = {
  113. /* Back */
  114. 0, 1, 2, 3,
  115. /* Floor */
  116. 4, 5, 6, 7,
  117. /* Roof */
  118. 8, 9, 10, 11,
  119. /* Right */
  120. 12, 13, 14, 15,
  121. /* Left */
  122. 16, 17, 18, 19
  123. };
  124. static GLfloat vertex_pointer[][3] = {
  125. /* Back */
  126. {-1.0f,-1.0f,-DEPTH}, { 1.0f,-1.0f,-DEPTH}, { 1.0f, 1.0f,-DEPTH}, {-1.0f, 1.0f,-DEPTH},
  127. /* Floor */
  128. {-1.0f,-1.0f,-DEPTH}, { 1.0f,-1.0f,-DEPTH}, { 1.0f,-1.0f, 0.0}, {-1.0f,-1.0f, 0.0},
  129. /* Roof */
  130. {-1.0f, 1.0f,-DEPTH}, { 1.0f, 1.0f,-DEPTH}, { 1.0f, 1.0f, 0.0}, {-1.0f, 1.0f, 0.0},
  131. /* Right */
  132. { 1.0f,-1.0f, 0.0}, { 1.0f, 1.0f, 0.0}, { 1.0f, 1.0f,-DEPTH}, { 1.0f,-1.0f,-DEPTH},
  133. /* Left */
  134. {-1.0f,-1.0f, 0.0}, {-1.0f, 1.0f, 0.0}, {-1.0f, 1.0f,-DEPTH}, {-1.0f,-1.0f,-DEPTH}
  135. };
  136. static GLfloat texcoord_pointer[][2] = {
  137. /* Back */
  138. {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f},
  139. /* Floor */
  140. {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, DEPTH}, {0.0f, DEPTH},
  141. /* Roof */
  142. {1.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, DEPTH}, {1.0f, DEPTH},
  143. /* Right */
  144. {0.0f, 1.0f}, {0.0f, 0.0f}, {DEPTH, 0.0f}, {DEPTH, 1.0f},
  145. /* Left */
  146. {0.0f, 0.0f}, {0.0f, 1.0f}, {DEPTH, 1.0f}, {DEPTH, 0.0f}
  147. };
  148. static GLfloat fogcoord_pointer[] = {
  149. /* Back */
  150. DEPTH, DEPTH, DEPTH, DEPTH,
  151. /* Floor */
  152. DEPTH, DEPTH, 0.0, 0.0,
  153. /* Roof */
  154. DEPTH, DEPTH, 0.0, 0.0,
  155. /* Right */
  156. 0.0, 0.0, DEPTH, DEPTH,
  157. /* Left */
  158. 0.0, 0.0, DEPTH, DEPTH
  159. };
  160. static void
  161. Display( void )
  162. {
  163. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  164. glLoadIdentity ();
  165. glTranslatef(0.0f, 0.0f, -camz);
  166. SetFogMode(fogMode);
  167. glColor3f(1, 1, 1);
  168. if (Texture)
  169. glEnable(GL_TEXTURE_2D);
  170. if (Arrays) {
  171. glEnableClientState(GL_VERTEX_ARRAY);
  172. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  173. glDrawElements(GL_QUADS, sizeof(vertex_index) / sizeof(vertex_index[0]),
  174. GL_UNSIGNED_INT, vertex_index);
  175. glDisableClientState(GL_VERTEX_ARRAY);
  176. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  177. }
  178. else {
  179. /* Back */
  180. glBegin(GL_QUADS);
  181. glFogCoordf_ext(DEPTH); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-DEPTH);
  182. glFogCoordf_ext(DEPTH); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-DEPTH);
  183. glFogCoordf_ext(DEPTH); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f,-DEPTH);
  184. glFogCoordf_ext(DEPTH); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-DEPTH);
  185. glEnd();
  186. /* Floor */
  187. glBegin(GL_QUADS);
  188. glFogCoordf_ext(DEPTH); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-DEPTH);
  189. glFogCoordf_ext(DEPTH); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-DEPTH);
  190. glFogCoordf_ext(0.0f); glTexCoord2f(1.0f, DEPTH); glVertex3f( 1.0f,-1.0f,0.0);
  191. glFogCoordf_ext(0.0f); glTexCoord2f(0.0f, DEPTH); glVertex3f(-1.0f,-1.0f,0.0);
  192. glEnd();
  193. /* Roof */
  194. glBegin(GL_QUADS);
  195. glFogCoordf_ext(DEPTH); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f,-DEPTH);
  196. glFogCoordf_ext(DEPTH); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f,-DEPTH);
  197. glFogCoordf_ext(0.0f); glTexCoord2f(0.0f, DEPTH); glVertex3f( 1.0f, 1.0f,0.0);
  198. glFogCoordf_ext(0.0f); glTexCoord2f(1.0f, DEPTH); glVertex3f(-1.0f, 1.0f,0.0);
  199. glEnd();
  200. /* Right */
  201. glBegin(GL_QUADS);
  202. glFogCoordf_ext(0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,-1.0f,0.0);
  203. glFogCoordf_ext(0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f,0.0);
  204. glFogCoordf_ext(DEPTH); glTexCoord2f(DEPTH, 0.0f); glVertex3f( 1.0f, 1.0f,-DEPTH);
  205. glFogCoordf_ext(DEPTH); glTexCoord2f(DEPTH, 1.0f); glVertex3f( 1.0f,-1.0f,-DEPTH);
  206. glEnd();
  207. /* Left */
  208. glBegin(GL_QUADS);
  209. glFogCoordf_ext(0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f,0.0);
  210. glFogCoordf_ext(0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f,0.0);
  211. glFogCoordf_ext(DEPTH); glTexCoord2f(DEPTH, 1.0f); glVertex3f(-1.0f, 1.0f,-DEPTH);
  212. glFogCoordf_ext(DEPTH); glTexCoord2f(DEPTH, 0.0f); glVertex3f(-1.0f,-1.0f,-DEPTH);
  213. glEnd();
  214. }
  215. glDisable(GL_TEXTURE_2D);
  216. PrintInfo();
  217. glutSwapBuffers();
  218. }
  219. static void
  220. Reshape( int width, int height )
  221. {
  222. glViewport(0, 0, width, height);
  223. glMatrixMode(GL_PROJECTION);
  224. glLoadIdentity();
  225. glFrustum(-1, 1, -1, 1, 1.0, 100);
  226. glMatrixMode(GL_MODELVIEW);
  227. glLoadIdentity();
  228. }
  229. static void
  230. Key( unsigned char key, int x, int y )
  231. {
  232. (void) x;
  233. (void) y;
  234. switch (key) {
  235. case 'a':
  236. Arrays = !Arrays;
  237. break;
  238. case 'f':
  239. case 'm':
  240. fogMode = SetFogMode(fogMode + 1);
  241. break;
  242. case 'D':
  243. fogDensity += 0.05;
  244. SetFogMode(fogMode);
  245. break;
  246. case 'd':
  247. if (fogDensity > 0.0) {
  248. fogDensity -= 0.05;
  249. }
  250. SetFogMode(fogMode);
  251. break;
  252. case 's':
  253. if (fogStart > 0.0) {
  254. fogStart -= 0.25;
  255. }
  256. SetFogMode(fogMode);
  257. break;
  258. case 'S':
  259. if (fogStart < 100.0) {
  260. fogStart += 0.25;
  261. }
  262. SetFogMode(fogMode);
  263. break;
  264. case 'e':
  265. if (fogEnd > 0.0) {
  266. fogEnd -= 0.25;
  267. }
  268. SetFogMode(fogMode);
  269. break;
  270. case 'E':
  271. if (fogEnd < 100.0) {
  272. fogEnd += 0.25;
  273. }
  274. SetFogMode(fogMode);
  275. break;
  276. case 'c':
  277. fogCoord = SetFogCoord(fogCoord ^ GL_TRUE);
  278. break;
  279. case 't':
  280. Texture = !Texture;
  281. break;
  282. case 'z':
  283. camz -= 0.1;
  284. break;
  285. case 'Z':
  286. camz += 0.1;
  287. break;
  288. case 'r':
  289. Reset();
  290. break;
  291. case 27:
  292. exit(0);
  293. break;
  294. }
  295. glutPostRedisplay();
  296. }
  297. static void
  298. Init(void)
  299. {
  300. static const GLubyte teximage[2][2][4] = {
  301. { { 255, 255, 255, 255}, { 128, 128, 128, 255} },
  302. { { 128, 128, 128, 255}, { 255, 255, 255, 255} }
  303. };
  304. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  305. if (!GLEW_EXT_fog_coord) {
  306. printf("GL_EXT_fog_coord not supported!\n");
  307. }
  308. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0,
  309. GL_RGBA, GL_UNSIGNED_BYTE, teximage);
  310. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  311. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  312. glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
  313. glDepthFunc(GL_LEQUAL);
  314. glEnable(GL_DEPTH_TEST);
  315. glShadeModel(GL_SMOOTH);
  316. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  317. glFogfv(GL_FOG_COLOR, fogColor);
  318. glHint(GL_FOG_HINT, GL_NICEST);
  319. fogCoord = SetFogCoord(GL_TRUE); /* try to enable fog_coord */
  320. fogMode = SetFogMode(1);
  321. glEnableClientState(GL_VERTEX_ARRAY);
  322. glVertexPointer(3, GL_FLOAT, 0, vertex_pointer);
  323. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  324. glTexCoordPointer(2, GL_FLOAT, 0, texcoord_pointer);
  325. if (GLEW_EXT_fog_coord) {
  326. glEnableClientState(GL_FOG_COORDINATE_ARRAY_EXT);
  327. glFogCoordPointerEXT(GL_FLOAT, 0, fogcoord_pointer);
  328. }
  329. Reset();
  330. }
  331. int
  332. main( int argc, char *argv[] )
  333. {
  334. glutInitWindowSize( 600, 600 );
  335. glutInit( &argc, argv );
  336. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  337. glutCreateWindow(argv[0]);
  338. glewInit();
  339. glutReshapeFunc( Reshape );
  340. glutKeyboardFunc( Key );
  341. glutDisplayFunc( Display );
  342. Init();
  343. glutMainLoop();
  344. return 0;
  345. }