Clone of mesa.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

fogcoord.c 10.0KB

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