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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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/glut.h"
  39. #include "readtex.h"
  40. static GLfloat Xrot = 0, Yrot = 0;
  41. static GLfloat EyeDist = 10;
  42. static GLboolean use_vertex_arrays = GL_FALSE;
  43. static GLboolean anim = GL_TRUE;
  44. #define eps1 0.99
  45. #define br 20.0 /* box radius */
  46. static const GLfloat tex_coords[] = {
  47. /* +X side */
  48. 1.0, -eps1, -eps1,
  49. 1.0, -eps1, eps1,
  50. 1.0, eps1, eps1,
  51. 1.0, eps1, -eps1,
  52. /* -X side */
  53. -1.0, eps1, -eps1,
  54. -1.0, eps1, eps1,
  55. -1.0, -eps1, eps1,
  56. -1.0, -eps1, -eps1,
  57. /* +Y side */
  58. -eps1, 1.0, -eps1,
  59. -eps1, 1.0, eps1,
  60. eps1, 1.0, eps1,
  61. eps1, 1.0, -eps1,
  62. /* -Y side */
  63. -eps1, -1.0, -eps1,
  64. -eps1, -1.0, eps1,
  65. eps1, -1.0, eps1,
  66. eps1, -1.0, -eps1,
  67. /* +Z side */
  68. eps1, -eps1, 1.0,
  69. -eps1, -eps1, 1.0,
  70. -eps1, eps1, 1.0,
  71. eps1, eps1, 1.0,
  72. /* -Z side */
  73. eps1, eps1, -1.0,
  74. -eps1, eps1, -1.0,
  75. -eps1, -eps1, -1.0,
  76. eps1, -eps1, -1.0,
  77. };
  78. static const GLfloat vtx_coords[] = {
  79. /* +X side */
  80. br, -br, -br,
  81. br, -br, br,
  82. br, br, br,
  83. br, br, -br,
  84. /* -X side */
  85. -br, br, -br,
  86. -br, br, br,
  87. -br, -br, br,
  88. -br, -br, -br,
  89. /* +Y side */
  90. -br, br, -br,
  91. -br, br, br,
  92. br, br, br,
  93. br, br, -br,
  94. /* -Y side */
  95. -br, -br, -br,
  96. -br, -br, br,
  97. br, -br, br,
  98. br, -br, -br,
  99. /* +Z side */
  100. br, -br, br,
  101. -br, -br, br,
  102. -br, br, br,
  103. br, br, br,
  104. /* -Z side */
  105. br, br, -br,
  106. -br, br, -br,
  107. -br, -br, -br,
  108. br, -br, -br,
  109. };
  110. static void draw_skybox( void )
  111. {
  112. if ( use_vertex_arrays ) {
  113. glTexCoordPointer( 3, GL_FLOAT, 0, tex_coords );
  114. glVertexPointer( 3, GL_FLOAT, 0, vtx_coords );
  115. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  116. glEnableClientState( GL_VERTEX_ARRAY );
  117. glDrawArrays( GL_QUADS, 0, 24 );
  118. glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  119. glDisableClientState( GL_VERTEX_ARRAY );
  120. }
  121. else {
  122. unsigned i;
  123. glBegin(GL_QUADS);
  124. for ( i = 0 ; i < 24 ; i++ ) {
  125. glTexCoord3fv( & tex_coords[ i * 3 ] );
  126. glVertex3fv ( & vtx_coords[ i * 3 ] );
  127. }
  128. glEnd();
  129. }
  130. }
  131. static void draw( void )
  132. {
  133. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  134. glPushMatrix(); /*MODELVIEW*/
  135. glTranslatef( 0.0, 0.0, -EyeDist );
  136. /* skybox */
  137. glDisable(GL_TEXTURE_GEN_S);
  138. glDisable(GL_TEXTURE_GEN_T);
  139. glDisable(GL_TEXTURE_GEN_R);
  140. glMatrixMode(GL_MODELVIEW);
  141. glPushMatrix();
  142. glRotatef(Xrot, 1, 0, 0);
  143. glRotatef(Yrot, 0, 1, 0);
  144. draw_skybox();
  145. glPopMatrix();
  146. /* sphere */
  147. glMatrixMode(GL_TEXTURE);
  148. glLoadIdentity();
  149. glRotatef(-Yrot, 0, 1, 0);
  150. glRotatef(-Xrot, 1, 0, 0);
  151. glEnable(GL_TEXTURE_GEN_S);
  152. glEnable(GL_TEXTURE_GEN_T);
  153. glEnable(GL_TEXTURE_GEN_R);
  154. glutSolidSphere(2.0, 20, 20);
  155. glLoadIdentity(); /* texture */
  156. glMatrixMode(GL_MODELVIEW);
  157. glPopMatrix();
  158. glutSwapBuffers();
  159. }
  160. static void idle(void)
  161. {
  162. GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
  163. Yrot = t;
  164. glutPostRedisplay();
  165. }
  166. static void set_mode(GLuint mode)
  167. {
  168. if (mode == 0) {
  169. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  170. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  171. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  172. printf("GL_REFLECTION_MAP_ARB mode\n");
  173. }
  174. else if (mode == 1) {
  175. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  176. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  177. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  178. printf("GL_NORMAL_MAP_ARB mode\n");
  179. }
  180. }
  181. static void key(unsigned char k, int x, int y)
  182. {
  183. static GLuint mode = 0;
  184. (void) x;
  185. (void) y;
  186. switch (k) {
  187. case ' ':
  188. anim = !anim;
  189. if (anim)
  190. glutIdleFunc(idle);
  191. else
  192. glutIdleFunc(NULL);
  193. break;
  194. case 'm':
  195. mode = !mode;
  196. set_mode(mode);
  197. break;
  198. case 'v':
  199. use_vertex_arrays = ! use_vertex_arrays;
  200. printf( "Vertex arrays are %sabled\n",
  201. (use_vertex_arrays) ? "en" : "dis" );
  202. break;
  203. case 'z':
  204. EyeDist -= 0.5;
  205. if (EyeDist < 6.0)
  206. EyeDist = 6.0;
  207. break;
  208. case 'Z':
  209. EyeDist += 0.5;
  210. if (EyeDist > 90.0)
  211. EyeDist = 90;
  212. break;
  213. case 27:
  214. exit(0);
  215. }
  216. glutPostRedisplay();
  217. }
  218. static void specialkey(int key, int x, int y)
  219. {
  220. GLfloat step = 5;
  221. (void) x;
  222. (void) y;
  223. switch (key) {
  224. case GLUT_KEY_UP:
  225. Xrot += step;
  226. break;
  227. case GLUT_KEY_DOWN:
  228. Xrot -= step;
  229. break;
  230. case GLUT_KEY_LEFT:
  231. Yrot -= step;
  232. break;
  233. case GLUT_KEY_RIGHT:
  234. Yrot += step;
  235. break;
  236. }
  237. glutPostRedisplay();
  238. }
  239. /* new window size or exposure */
  240. static void reshape(int width, int height)
  241. {
  242. GLfloat ar = (float) width / (float) height;
  243. glViewport(0, 0, (GLint)width, (GLint)height);
  244. glMatrixMode(GL_PROJECTION);
  245. glLoadIdentity();
  246. glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
  247. glMatrixMode(GL_MODELVIEW);
  248. glLoadIdentity();
  249. }
  250. static void init_checkers( void )
  251. {
  252. #define CUBE_TEX_SIZE 64
  253. GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3];
  254. static const GLubyte colors[6][3] = {
  255. { 255, 0, 0 }, /* face 0 - red */
  256. { 0, 255, 255 }, /* face 1 - cyan */
  257. { 0, 255, 0 }, /* face 2 - green */
  258. { 255, 0, 255 }, /* face 3 - purple */
  259. { 0, 0, 255 }, /* face 4 - blue */
  260. { 255, 255, 0 } /* face 5 - yellow */
  261. };
  262. static const GLenum targets[6] = {
  263. GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
  264. GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
  265. GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
  266. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
  267. GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
  268. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
  269. };
  270. GLint i, j, f;
  271. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  272. /* make colored checkerboard cube faces */
  273. for (f = 0; f < 6; f++) {
  274. for (i = 0; i < CUBE_TEX_SIZE; i++) {
  275. for (j = 0; j < CUBE_TEX_SIZE; j++) {
  276. if ((i/4 + j/4) & 1) {
  277. image[i][j][0] = colors[f][0];
  278. image[i][j][1] = colors[f][1];
  279. image[i][j][2] = colors[f][2];
  280. }
  281. else {
  282. image[i][j][0] = 255;
  283. image[i][j][1] = 255;
  284. image[i][j][2] = 255;
  285. }
  286. }
  287. }
  288. glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
  289. GL_RGB, GL_UNSIGNED_BYTE, image);
  290. }
  291. }
  292. static void load(GLenum target, const char *filename,
  293. GLboolean flipTB, GLboolean flipLR)
  294. {
  295. GLint w, h;
  296. GLenum format;
  297. GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
  298. if (!img) {
  299. printf("Error: couldn't load texture image %s\n", filename);
  300. exit(1);
  301. }
  302. assert(format == GL_RGB);
  303. /* <sigh> the way the texture cube mapping works, we have to flip
  304. * images to make things look right.
  305. */
  306. if (flipTB) {
  307. const int stride = 3 * w;
  308. GLubyte temp[3*1024];
  309. int i;
  310. for (i = 0; i < h / 2; i++) {
  311. memcpy(temp, img + i * stride, stride);
  312. memcpy(img + i * stride, img + (h - i - 1) * stride, stride);
  313. memcpy(img + (h - i - 1) * stride, temp, stride);
  314. }
  315. }
  316. if (flipLR) {
  317. const int stride = 3 * w;
  318. GLubyte temp[3];
  319. GLubyte *row;
  320. int i, j;
  321. for (i = 0; i < h; i++) {
  322. row = img + i * stride;
  323. for (j = 0; j < w / 2; j++) {
  324. int k = w - j - 1;
  325. temp[0] = row[j*3+0];
  326. temp[1] = row[j*3+1];
  327. temp[2] = row[j*3+2];
  328. row[j*3+0] = row[k*3+0];
  329. row[j*3+1] = row[k*3+1];
  330. row[j*3+2] = row[k*3+2];
  331. row[k*3+0] = temp[0];
  332. row[k*3+1] = temp[1];
  333. row[k*3+2] = temp[2];
  334. }
  335. }
  336. }
  337. gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
  338. free(img);
  339. }
  340. static void load_envmaps(void)
  341. {
  342. load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, "right.rgb", GL_TRUE, GL_FALSE);
  343. load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, "left.rgb", GL_TRUE, GL_FALSE);
  344. load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, "top.rgb", GL_FALSE, GL_TRUE);
  345. load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, "bottom.rgb", GL_FALSE, GL_TRUE);
  346. load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, "front.rgb", GL_TRUE, GL_FALSE);
  347. load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, "back.rgb", GL_TRUE, GL_FALSE);
  348. }
  349. static void init( GLboolean useImageFiles )
  350. {
  351. GLenum filter;
  352. /* check for extension */
  353. {
  354. char *exten = (char *) glGetString(GL_EXTENSIONS);
  355. if (!strstr(exten, "GL_ARB_texture_cube_map")) {
  356. printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
  357. exit(0);
  358. }
  359. }
  360. printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
  361. if (useImageFiles) {
  362. load_envmaps();
  363. filter = GL_LINEAR;
  364. }
  365. else {
  366. init_checkers();
  367. filter = GL_NEAREST;
  368. }
  369. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, filter);
  370. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, filter);
  371. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  372. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  373. glEnable(GL_TEXTURE_CUBE_MAP_ARB);
  374. glEnable(GL_DEPTH_TEST);
  375. glClearColor(.3, .3, .3, 0);
  376. glColor3f( 1.0, 1.0, 1.0 );
  377. set_mode(0);
  378. }
  379. static void usage(void)
  380. {
  381. printf("keys:\n");
  382. printf(" SPACE - toggle animation\n");
  383. printf(" CURSOR KEYS - rotation\n");
  384. printf(" m - toggle texgen reflection mode\n");
  385. printf(" z/Z - change viewing distance\n");
  386. }
  387. int main( int argc, char *argv[] )
  388. {
  389. glutInit(&argc, argv);
  390. glutInitWindowPosition(0, 0);
  391. glutInitWindowSize(600, 500);
  392. glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  393. glutCreateWindow("Texture Cube Mapping");
  394. if (argc > 1 && strcmp(argv[1] , "-i") == 0)
  395. init( 1 );
  396. else
  397. init( 0 );
  398. glutReshapeFunc( reshape );
  399. glutKeyboardFunc( key );
  400. glutSpecialFunc( specialkey );
  401. glutDisplayFunc( draw );
  402. if (anim)
  403. glutIdleFunc(idle);
  404. usage();
  405. glutMainLoop();
  406. return 0;
  407. }