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.

cubemap.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * GL_ARB_texture_cube_map demo
  3. *
  4. * Brian Paul
  5. * May 2000
  6. *
  7. *
  8. * Copyright (C) 2000 Brian Paul All Rights Reserved.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, including without limitation
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. * and/or sell copies of the Software, and to permit persons to whom the
  15. * Software is furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included
  18. * in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  21. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  24. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  25. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. /*
  28. * This is a pretty minimalistic demo for now. Eventually, use some
  29. * interesting cube map textures and 3D objects.
  30. * For now, we use 6 checkerboard "walls" and a sphere (good for
  31. * verification purposes).
  32. */
  33. #include <assert.h>
  34. #include <math.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <GL/glew.h>
  39. #include "GL/glut.h"
  40. #include "readtex.h"
  41. static GLfloat Xrot = 0, Yrot = 0;
  42. static GLfloat EyeDist = 10;
  43. static GLboolean use_vertex_arrays = GL_FALSE;
  44. static GLboolean anim = GL_TRUE;
  45. static GLboolean NoClear = GL_FALSE;
  46. static GLint FrameParity = 0;
  47. static GLenum FilterIndex = 0;
  48. static GLint ClampIndex = 0;
  49. static struct {
  50. GLenum mode;
  51. const char *name;
  52. } ClampModes[] = {
  53. { GL_CLAMP_TO_EDGE, "GL_CLAMP_TO_EDGE" },
  54. { GL_CLAMP_TO_BORDER, "GL_CLAMP_TO_BORDER" },
  55. { GL_CLAMP, "GL_CLAMP" },
  56. { GL_REPEAT, "GL_REPEAT" }
  57. };
  58. #define NUM_CLAMP_MODES (sizeof(ClampModes) / sizeof(ClampModes[0]))
  59. static struct {
  60. GLenum mag_mode, min_mode;
  61. const char *name;
  62. } FilterModes[] = {
  63. { GL_NEAREST, GL_NEAREST, "GL_NEAREST, GL_NEAREST" },
  64. { GL_NEAREST, GL_LINEAR, "GL_NEAREST, GL_LINEAR" },
  65. { GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST, "GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST" },
  66. { GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR, "GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR" },
  67. { GL_NEAREST, GL_LINEAR_MIPMAP_NEAREST, "GL_NEAREST, GL_LINEAR_MIPMAP_NEAREST" },
  68. { GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR, "GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR" },
  69. { GL_LINEAR, GL_NEAREST, "GL_LINEAR, GL_NEAREST" },
  70. { GL_LINEAR, GL_LINEAR, "GL_LINEAR, GL_LINEAR" },
  71. { GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, "GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST" },
  72. { GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR, "GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR" },
  73. { GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST, "GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST" },
  74. { GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, "GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR" }
  75. };
  76. #define NUM_FILTER_MODES (sizeof(FilterModes) / sizeof(FilterModes[0]))
  77. #define eps1 0.99
  78. #define br 20.0 /* box radius */
  79. static const GLfloat tex_coords[] = {
  80. /* +X side */
  81. 1.0, -eps1, -eps1,
  82. 1.0, -eps1, eps1,
  83. 1.0, eps1, eps1,
  84. 1.0, eps1, -eps1,
  85. /* -X side */
  86. -1.0, eps1, -eps1,
  87. -1.0, eps1, eps1,
  88. -1.0, -eps1, eps1,
  89. -1.0, -eps1, -eps1,
  90. /* +Y side */
  91. -eps1, 1.0, -eps1,
  92. -eps1, 1.0, eps1,
  93. eps1, 1.0, eps1,
  94. eps1, 1.0, -eps1,
  95. /* -Y side */
  96. -eps1, -1.0, -eps1,
  97. -eps1, -1.0, eps1,
  98. eps1, -1.0, eps1,
  99. eps1, -1.0, -eps1,
  100. /* +Z side */
  101. eps1, -eps1, 1.0,
  102. -eps1, -eps1, 1.0,
  103. -eps1, eps1, 1.0,
  104. eps1, eps1, 1.0,
  105. /* -Z side */
  106. eps1, eps1, -1.0,
  107. -eps1, eps1, -1.0,
  108. -eps1, -eps1, -1.0,
  109. eps1, -eps1, -1.0,
  110. };
  111. static const GLfloat vtx_coords[] = {
  112. /* +X side */
  113. br, -br, -br,
  114. br, -br, br,
  115. br, br, br,
  116. br, br, -br,
  117. /* -X side */
  118. -br, br, -br,
  119. -br, br, br,
  120. -br, -br, br,
  121. -br, -br, -br,
  122. /* +Y side */
  123. -br, br, -br,
  124. -br, br, br,
  125. br, br, br,
  126. br, br, -br,
  127. /* -Y side */
  128. -br, -br, -br,
  129. -br, -br, br,
  130. br, -br, br,
  131. br, -br, -br,
  132. /* +Z side */
  133. br, -br, br,
  134. -br, -br, br,
  135. -br, br, br,
  136. br, br, br,
  137. /* -Z side */
  138. br, br, -br,
  139. -br, br, -br,
  140. -br, -br, -br,
  141. br, -br, -br,
  142. };
  143. static void draw_skybox( void )
  144. {
  145. if ( use_vertex_arrays ) {
  146. glTexCoordPointer( 3, GL_FLOAT, 0, tex_coords );
  147. glVertexPointer( 3, GL_FLOAT, 0, vtx_coords );
  148. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  149. glEnableClientState( GL_VERTEX_ARRAY );
  150. glDrawArrays( GL_QUADS, 0, 24 );
  151. glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  152. glDisableClientState( GL_VERTEX_ARRAY );
  153. }
  154. else {
  155. unsigned i;
  156. glBegin(GL_QUADS);
  157. for ( i = 0 ; i < 24 ; i++ ) {
  158. glTexCoord3fv( & tex_coords[ i * 3 ] );
  159. glVertex3fv ( & vtx_coords[ i * 3 ] );
  160. }
  161. glEnd();
  162. }
  163. }
  164. static void draw( void )
  165. {
  166. GLenum wrap;
  167. if (NoClear) {
  168. /* This demonstrates how we can avoid calling glClear.
  169. * This method only works if every pixel in the window is painted for
  170. * every frame.
  171. * We can simply skip clearing of the color buffer in this case.
  172. * For the depth buffer, we alternately use a different subrange of
  173. * the depth buffer for each frame. For the odd frame use the range
  174. * [0, 0.5] with GL_LESS. For the even frames, use the range [1, 0.5]
  175. * with GL_GREATER.
  176. */
  177. FrameParity = 1 - FrameParity;
  178. if (FrameParity) {
  179. glDepthRange(0.0, 0.5);
  180. glDepthFunc(GL_LESS);
  181. }
  182. else {
  183. glDepthRange(1.0, 0.5);
  184. glDepthFunc(GL_GREATER);
  185. }
  186. }
  187. else {
  188. /* ordinary clearing */
  189. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  190. }
  191. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER,
  192. FilterModes[FilterIndex].min_mode);
  193. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER,
  194. FilterModes[FilterIndex].mag_mode);
  195. wrap = ClampModes[ClampIndex].mode;
  196. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, wrap);
  197. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, wrap);
  198. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, wrap);
  199. glPushMatrix(); /*MODELVIEW*/
  200. glTranslatef( 0.0, 0.0, -EyeDist );
  201. /* skybox */
  202. glDisable(GL_TEXTURE_GEN_S);
  203. glDisable(GL_TEXTURE_GEN_T);
  204. glDisable(GL_TEXTURE_GEN_R);
  205. glMatrixMode(GL_MODELVIEW);
  206. glPushMatrix();
  207. glRotatef(Xrot, 1, 0, 0);
  208. glRotatef(Yrot, 0, 1, 0);
  209. draw_skybox();
  210. glPopMatrix();
  211. /* sphere */
  212. glMatrixMode(GL_TEXTURE);
  213. glLoadIdentity();
  214. glRotatef(-Yrot, 0, 1, 0);
  215. glRotatef(-Xrot, 1, 0, 0);
  216. glEnable(GL_TEXTURE_GEN_S);
  217. glEnable(GL_TEXTURE_GEN_T);
  218. glEnable(GL_TEXTURE_GEN_R);
  219. glutSolidSphere(2.0, 20, 20);
  220. glLoadIdentity(); /* texture */
  221. glMatrixMode(GL_MODELVIEW);
  222. glPopMatrix();
  223. glutSwapBuffers();
  224. }
  225. static void idle(void)
  226. {
  227. GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
  228. Yrot = t;
  229. glutPostRedisplay();
  230. }
  231. static void set_mode(GLuint mode)
  232. {
  233. if (mode == 0) {
  234. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  235. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  236. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  237. printf("GL_REFLECTION_MAP_ARB mode\n");
  238. }
  239. else if (mode == 1) {
  240. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  241. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  242. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  243. printf("GL_NORMAL_MAP_ARB mode\n");
  244. }
  245. }
  246. static void key(unsigned char k, int x, int y)
  247. {
  248. static GLuint mode = 0;
  249. (void) x;
  250. (void) y;
  251. switch (k) {
  252. case ' ':
  253. anim = !anim;
  254. if (anim)
  255. glutIdleFunc(idle);
  256. else
  257. glutIdleFunc(NULL);
  258. break;
  259. case 'f':
  260. FilterIndex = (FilterIndex + 1) % NUM_FILTER_MODES;
  261. printf("Tex filter: %s\n", FilterModes[FilterIndex].name);
  262. break;
  263. case 'c':
  264. ClampIndex = (ClampIndex + 1) % NUM_CLAMP_MODES;
  265. printf("Tex wrap mode: %s\n", ClampModes[ClampIndex].name);
  266. break;
  267. case 'm':
  268. mode = !mode;
  269. set_mode(mode);
  270. break;
  271. case 'v':
  272. use_vertex_arrays = ! use_vertex_arrays;
  273. printf( "Vertex arrays are %sabled\n",
  274. (use_vertex_arrays) ? "en" : "dis" );
  275. break;
  276. case 'z':
  277. EyeDist -= 0.5;
  278. if (EyeDist < 6.0)
  279. EyeDist = 6.0;
  280. break;
  281. case 'Z':
  282. EyeDist += 0.5;
  283. if (EyeDist > 90.0)
  284. EyeDist = 90;
  285. break;
  286. case 27:
  287. exit(0);
  288. }
  289. glutPostRedisplay();
  290. }
  291. static void specialkey(int key, int x, int y)
  292. {
  293. GLfloat step = 5;
  294. (void) x;
  295. (void) y;
  296. switch (key) {
  297. case GLUT_KEY_UP:
  298. Xrot += step;
  299. break;
  300. case GLUT_KEY_DOWN:
  301. Xrot -= step;
  302. break;
  303. case GLUT_KEY_LEFT:
  304. Yrot -= step;
  305. break;
  306. case GLUT_KEY_RIGHT:
  307. Yrot += step;
  308. break;
  309. }
  310. glutPostRedisplay();
  311. }
  312. /* new window size or exposure */
  313. static void reshape(int width, int height)
  314. {
  315. GLfloat ar = (float) width / (float) height;
  316. glViewport(0, 0, (GLint)width, (GLint)height);
  317. glMatrixMode(GL_PROJECTION);
  318. glLoadIdentity();
  319. glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
  320. glMatrixMode(GL_MODELVIEW);
  321. glLoadIdentity();
  322. }
  323. static void init_checkers( void )
  324. {
  325. #define CUBE_TEX_SIZE 64
  326. GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][4];
  327. static const GLubyte colors[6][3] = {
  328. { 255, 0, 0 }, /* face 0 - red */
  329. { 0, 255, 255 }, /* face 1 - cyan */
  330. { 0, 255, 0 }, /* face 2 - green */
  331. { 255, 0, 255 }, /* face 3 - purple */
  332. { 0, 0, 255 }, /* face 4 - blue */
  333. { 255, 255, 0 } /* face 5 - yellow */
  334. };
  335. static const GLenum targets[6] = {
  336. GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
  337. GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
  338. GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
  339. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
  340. GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
  341. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
  342. };
  343. GLint i, j, f;
  344. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  345. /* make colored checkerboard cube faces */
  346. for (f = 0; f < 6; f++) {
  347. for (i = 0; i < CUBE_TEX_SIZE; i++) {
  348. for (j = 0; j < CUBE_TEX_SIZE; j++) {
  349. if ((i/4 + j/4) & 1) {
  350. image[i][j][0] = colors[f][2];
  351. image[i][j][1] = colors[f][1];
  352. image[i][j][2] = colors[f][0];
  353. image[i][j][3] = 255;
  354. }
  355. else {
  356. image[i][j][0] = 255;
  357. image[i][j][1] = 255;
  358. image[i][j][2] = 255;
  359. image[i][j][3] = 255;
  360. }
  361. }
  362. }
  363. glTexImage2D(targets[f], 0, GL_RGBA8, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
  364. GL_BGRA, GL_UNSIGNED_BYTE, image);
  365. }
  366. glGenerateMipmapEXT(GL_TEXTURE_CUBE_MAP_ARB);
  367. }
  368. static void load(GLenum target, const char *filename,
  369. GLboolean flipTB, GLboolean flipLR)
  370. {
  371. GLint w, h;
  372. GLenum format;
  373. GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
  374. if (!img) {
  375. printf("Error: couldn't load texture image %s\n", filename);
  376. exit(1);
  377. }
  378. assert(format == GL_RGB);
  379. /* <sigh> the way the texture cube mapping works, we have to flip
  380. * images to make things look right.
  381. */
  382. if (flipTB) {
  383. const int stride = 3 * w;
  384. GLubyte temp[3*1024];
  385. int i;
  386. for (i = 0; i < h / 2; i++) {
  387. memcpy(temp, img + i * stride, stride);
  388. memcpy(img + i * stride, img + (h - i - 1) * stride, stride);
  389. memcpy(img + (h - i - 1) * stride, temp, stride);
  390. }
  391. }
  392. if (flipLR) {
  393. const int stride = 3 * w;
  394. GLubyte temp[3];
  395. GLubyte *row;
  396. int i, j;
  397. for (i = 0; i < h; i++) {
  398. row = img + i * stride;
  399. for (j = 0; j < w / 2; j++) {
  400. int k = w - j - 1;
  401. temp[0] = row[j*3+0];
  402. temp[1] = row[j*3+1];
  403. temp[2] = row[j*3+2];
  404. row[j*3+0] = row[k*3+0];
  405. row[j*3+1] = row[k*3+1];
  406. row[j*3+2] = row[k*3+2];
  407. row[k*3+0] = temp[0];
  408. row[k*3+1] = temp[1];
  409. row[k*3+2] = temp[2];
  410. }
  411. }
  412. }
  413. gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
  414. free(img);
  415. }
  416. static void load_envmaps(void)
  417. {
  418. load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, "right.rgb", GL_TRUE, GL_FALSE);
  419. load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, "left.rgb", GL_TRUE, GL_FALSE);
  420. load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, "top.rgb", GL_FALSE, GL_TRUE);
  421. load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, "bottom.rgb", GL_FALSE, GL_TRUE);
  422. load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, "front.rgb", GL_TRUE, GL_FALSE);
  423. load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, "back.rgb", GL_TRUE, GL_FALSE);
  424. }
  425. static void init( GLboolean useImageFiles )
  426. {
  427. /* check for extensions */
  428. {
  429. char *exten = (char *) glGetString(GL_EXTENSIONS);
  430. if (!strstr(exten, "GL_ARB_texture_cube_map")) {
  431. printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
  432. exit(0);
  433. }
  434. /* Needed for glGenerateMipmapEXT
  435. */
  436. if (!strstr(exten, "GL_EXT_framebuffer_object")) {
  437. printf("Sorry, this demo requires GL_EXT_framebuffer_object\n");
  438. exit(0);
  439. }
  440. }
  441. printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
  442. if (useImageFiles) {
  443. load_envmaps();
  444. }
  445. else {
  446. init_checkers();
  447. }
  448. glEnable(GL_TEXTURE_CUBE_MAP_ARB);
  449. glEnable(GL_DEPTH_TEST);
  450. glClearColor(.3, .3, .3, 0);
  451. glColor3f( 1.0, 1.0, 1.0 );
  452. set_mode(0);
  453. }
  454. static void usage(void)
  455. {
  456. printf("keys:\n");
  457. printf(" SPACE - toggle animation\n");
  458. printf(" CURSOR KEYS - rotation\n");
  459. printf(" c - toggle texture clamp/wrap mode\n");
  460. printf(" f - toggle texture filter mode\n");
  461. printf(" m - toggle texgen reflection mode\n");
  462. printf(" z/Z - change viewing distance\n");
  463. }
  464. static void parse_args(int argc, char *argv[])
  465. {
  466. int initFlag = 0;
  467. int i;
  468. for (i = 1; i < argc; i++) {
  469. if (strcmp(argv[i], "-i") == 0)
  470. initFlag = 1;
  471. else if (strcmp(argv[i], "--noclear") == 0)
  472. NoClear = GL_TRUE;
  473. else {
  474. fprintf(stderr, "Bad option: %s\n", argv[i]);
  475. exit(1);
  476. }
  477. }
  478. init (initFlag);
  479. }
  480. int main( int argc, char *argv[] )
  481. {
  482. glutInit(&argc, argv);
  483. glutInitWindowPosition(0, 0);
  484. glutInitWindowSize(600, 500);
  485. glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  486. glutCreateWindow("Texture Cube Mapping");
  487. glewInit();
  488. glutReshapeFunc( reshape );
  489. glutKeyboardFunc( key );
  490. glutSpecialFunc( specialkey );
  491. glutDisplayFunc( draw );
  492. if (anim)
  493. glutIdleFunc(idle);
  494. parse_args(argc, argv);
  495. usage();
  496. glutMainLoop();
  497. return 0;
  498. }