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.7KB

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