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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* $Id: cubemap.c,v 1.4 2002/10/25 17:20:26 brianp Exp $ */
  2. /*
  3. * GL_ARB_texture_cube_map demo
  4. *
  5. * Brian Paul
  6. * May 2000
  7. *
  8. *
  9. * Copyright (C) 2000 Brian Paul All Rights Reserved.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included
  19. * in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  25. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. /*
  29. * This is a pretty minimalistic demo for now. Eventually, use some
  30. * interesting cube map textures and 3D objects.
  31. * For now, we use 6 checkerboard "walls" and a sphere (good for
  32. * verification purposes).
  33. */
  34. #include <assert.h>
  35. #include <math.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "GL/glut.h"
  40. #include "../util/readtex.c" /* a hack */
  41. static GLfloat Xrot = 0, Yrot = 0;
  42. static GLfloat EyeDist = 10;
  43. static void draw_skybox( void )
  44. {
  45. const GLfloat eps1 = 0.99;
  46. const GLfloat br = 20.0; /* box radius */
  47. glBegin(GL_QUADS);
  48. /* +X side */
  49. glTexCoord3f(1.0, -eps1, -eps1); glVertex3f(br, -br, -br);
  50. glTexCoord3f(1.0, -eps1, eps1); glVertex3f(br, -br, br);
  51. glTexCoord3f(1.0, eps1, eps1); glVertex3f(br, br, br);
  52. glTexCoord3f(1.0, eps1, -eps1); glVertex3f(br, br, -br);
  53. /* -X side */
  54. glTexCoord3f(-1.0, eps1, -eps1); glVertex3f(-br, br, -br);
  55. glTexCoord3f(-1.0, eps1, eps1); glVertex3f(-br, br, br);
  56. glTexCoord3f(-1.0, -eps1, eps1); glVertex3f(-br, -br, br);
  57. glTexCoord3f(-1.0, -eps1, -eps1); glVertex3f(-br, -br, -br);
  58. /* +Y side */
  59. glTexCoord3f(-eps1, 1.0, -eps1); glVertex3f(-br, br, -br);
  60. glTexCoord3f(-eps1, 1.0, eps1); glVertex3f(-br, br, br);
  61. glTexCoord3f( eps1, 1.0, eps1); glVertex3f( br, br, br);
  62. glTexCoord3f( eps1, 1.0, -eps1); glVertex3f( br, br, -br);
  63. /* -Y side */
  64. glTexCoord3f(-eps1, -1.0, -eps1); glVertex3f(-br, -br, -br);
  65. glTexCoord3f(-eps1, -1.0, eps1); glVertex3f(-br, -br, br);
  66. glTexCoord3f( eps1, -1.0, eps1); glVertex3f( br, -br, br);
  67. glTexCoord3f( eps1, -1.0, -eps1); glVertex3f( br, -br, -br);
  68. /* +Z side */
  69. glTexCoord3f( eps1, -eps1, 1.0); glVertex3f( br, -br, br);
  70. glTexCoord3f(-eps1, -eps1, 1.0); glVertex3f(-br, -br, br);
  71. glTexCoord3f(-eps1, eps1, 1.0); glVertex3f(-br, br, br);
  72. glTexCoord3f( eps1, eps1, 1.0); glVertex3f( br, br, br);
  73. /* -Z side */
  74. glTexCoord3f( eps1, eps1, -1.0); glVertex3f( br, br, -br);
  75. glTexCoord3f(-eps1, eps1, -1.0); glVertex3f(-br, br, -br);
  76. glTexCoord3f(-eps1, -eps1, -1.0); glVertex3f(-br, -br, -br);
  77. glTexCoord3f( eps1, -eps1, -1.0); glVertex3f( br, -br, -br);
  78. glEnd();
  79. }
  80. static void draw( void )
  81. {
  82. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  83. glPushMatrix(); /*MODELVIEW*/
  84. glTranslatef( 0.0, 0.0, -EyeDist );
  85. /* skybox */
  86. glDisable(GL_TEXTURE_GEN_S);
  87. glDisable(GL_TEXTURE_GEN_T);
  88. glDisable(GL_TEXTURE_GEN_R);
  89. glMatrixMode(GL_MODELVIEW);
  90. glPushMatrix();
  91. glRotatef(Xrot, 1, 0, 0);
  92. glRotatef(Yrot, 0, 1, 0);
  93. draw_skybox();
  94. glPopMatrix();
  95. /* sphere */
  96. glMatrixMode(GL_TEXTURE);
  97. glLoadIdentity();
  98. glRotatef(-Yrot, 0, 1, 0);
  99. glRotatef(-Xrot, 1, 0, 0);
  100. glEnable(GL_TEXTURE_GEN_S);
  101. glEnable(GL_TEXTURE_GEN_T);
  102. glEnable(GL_TEXTURE_GEN_R);
  103. glutSolidSphere(2.0, 20, 20);
  104. glLoadIdentity(); /* texture */
  105. glMatrixMode(GL_MODELVIEW);
  106. glPopMatrix();
  107. glutSwapBuffers();
  108. }
  109. static void idle(void)
  110. {
  111. GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
  112. Yrot = t;
  113. glutPostRedisplay();
  114. }
  115. static void set_mode(GLuint mode)
  116. {
  117. if (mode == 0) {
  118. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  119. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  120. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  121. printf("GL_REFLECTION_MAP_ARB mode\n");
  122. }
  123. else if (mode == 1) {
  124. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  125. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  126. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  127. printf("GL_NORMAL_MAP_ARB mode\n");
  128. }
  129. }
  130. static void key(unsigned char k, int x, int y)
  131. {
  132. static GLboolean anim = GL_TRUE;
  133. static GLuint mode = 0;
  134. (void) x;
  135. (void) y;
  136. switch (k) {
  137. case ' ':
  138. anim = !anim;
  139. if (anim)
  140. glutIdleFunc(idle);
  141. else
  142. glutIdleFunc(NULL);
  143. break;
  144. case 'm':
  145. mode = !mode;
  146. set_mode(mode);
  147. break;
  148. case 'z':
  149. EyeDist -= 0.5;
  150. if (EyeDist < 6.0)
  151. EyeDist = 6.0;
  152. break;
  153. case 'Z':
  154. EyeDist += 0.5;
  155. if (EyeDist > 90.0)
  156. EyeDist = 90;
  157. break;
  158. case 27:
  159. exit(0);
  160. }
  161. glutPostRedisplay();
  162. }
  163. static void specialkey(int key, int x, int y)
  164. {
  165. GLfloat step = 5;
  166. (void) x;
  167. (void) y;
  168. switch (key) {
  169. case GLUT_KEY_UP:
  170. Xrot += step;
  171. break;
  172. case GLUT_KEY_DOWN:
  173. Xrot -= step;
  174. break;
  175. case GLUT_KEY_LEFT:
  176. Yrot -= step;
  177. break;
  178. case GLUT_KEY_RIGHT:
  179. Yrot += step;
  180. break;
  181. }
  182. glutPostRedisplay();
  183. }
  184. /* new window size or exposure */
  185. static void reshape(int width, int height)
  186. {
  187. GLfloat ar = (float) width / (float) height;
  188. glViewport(0, 0, (GLint)width, (GLint)height);
  189. glMatrixMode(GL_PROJECTION);
  190. glLoadIdentity();
  191. glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
  192. glMatrixMode(GL_MODELVIEW);
  193. glLoadIdentity();
  194. }
  195. static void init_checkers( void )
  196. {
  197. #define CUBE_TEX_SIZE 64
  198. GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3];
  199. static const GLubyte colors[6][3] = {
  200. { 255, 0, 0 },
  201. { 0, 255, 255 },
  202. { 0, 255, 0 },
  203. { 255, 0, 255 },
  204. { 0, 0, 255 },
  205. { 255, 255, 0 }
  206. };
  207. static const GLenum targets[6] = {
  208. GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
  209. GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
  210. GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
  211. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
  212. GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
  213. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
  214. };
  215. GLint i, j, f;
  216. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  217. /* make colored checkerboard cube faces */
  218. for (f = 0; f < 6; f++) {
  219. for (i = 0; i < CUBE_TEX_SIZE; i++) {
  220. for (j = 0; j < CUBE_TEX_SIZE; j++) {
  221. if ((i/4 + j/4) & 1) {
  222. image[i][j][0] = colors[f][0];
  223. image[i][j][1] = colors[f][1];
  224. image[i][j][2] = colors[f][2];
  225. }
  226. else {
  227. image[i][j][0] = 255;
  228. image[i][j][1] = 255;
  229. image[i][j][2] = 255;
  230. }
  231. }
  232. }
  233. glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
  234. GL_RGB, GL_UNSIGNED_BYTE, image);
  235. }
  236. }
  237. static void load(GLenum target, const char *filename,
  238. GLboolean flipTB, GLboolean flipLR)
  239. {
  240. GLint w, h;
  241. GLenum format;
  242. GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
  243. if (!img) {
  244. printf("Error: couldn't load texture image %s\n", filename);
  245. exit(1);
  246. }
  247. assert(format == GL_RGB);
  248. /* <sigh> the way the texture cube mapping works, we have to flip
  249. * images to make things look right.
  250. */
  251. if (flipTB) {
  252. const int stride = 3 * w;
  253. GLubyte temp[3*1024];
  254. int i;
  255. for (i = 0; i < h / 2; i++) {
  256. memcpy(temp, img + i * stride, stride);
  257. memcpy(img + i * stride, img + (h - i - 1) * stride, stride);
  258. memcpy(img + (h - i - 1) * stride, temp, stride);
  259. }
  260. }
  261. if (flipLR) {
  262. const int stride = 3 * w;
  263. GLubyte temp[3];
  264. GLubyte *row;
  265. int i, j;
  266. for (i = 0; i < h; i++) {
  267. row = img + i * stride;
  268. for (j = 0; j < w / 2; j++) {
  269. int k = w - j - 1;
  270. temp[0] = row[j*3+0];
  271. temp[1] = row[j*3+1];
  272. temp[2] = row[j*3+2];
  273. row[j*3+0] = row[k*3+0];
  274. row[j*3+1] = row[k*3+1];
  275. row[j*3+2] = row[k*3+2];
  276. row[k*3+0] = temp[0];
  277. row[k*3+1] = temp[1];
  278. row[k*3+2] = temp[2];
  279. }
  280. }
  281. }
  282. gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
  283. free(img);
  284. }
  285. static void load_envmaps(void)
  286. {
  287. load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, "right.rgb", GL_TRUE, GL_FALSE);
  288. load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, "left.rgb", GL_TRUE, GL_FALSE);
  289. load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, "top.rgb", GL_FALSE, GL_TRUE);
  290. load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, "bottom.rgb", GL_FALSE, GL_TRUE);
  291. load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, "front.rgb", GL_TRUE, GL_FALSE);
  292. load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, "back.rgb", GL_TRUE, GL_FALSE);
  293. }
  294. static void init( GLboolean useImageFiles )
  295. {
  296. GLenum filter;
  297. /* check for extension */
  298. {
  299. char *exten = (char *) glGetString(GL_EXTENSIONS);
  300. if (!strstr(exten, "GL_ARB_texture_cube_map")) {
  301. printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
  302. exit(0);
  303. }
  304. }
  305. printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
  306. if (useImageFiles) {
  307. load_envmaps();
  308. filter = GL_LINEAR;
  309. }
  310. else {
  311. init_checkers();
  312. filter = GL_NEAREST;
  313. }
  314. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, filter);
  315. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, filter);
  316. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  317. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  318. glEnable(GL_TEXTURE_CUBE_MAP_ARB);
  319. glEnable(GL_DEPTH_TEST);
  320. glClearColor(.3, .3, .3, 0);
  321. glColor3f( 1.0, 1.0, 1.0 );
  322. set_mode(0);
  323. }
  324. static void usage(void)
  325. {
  326. printf("keys:\n");
  327. printf(" SPACE - toggle animation\n");
  328. printf(" CURSOR KEYS - rotation\n");
  329. printf(" m - toggle texgen reflection mode\n");
  330. printf(" z/Z - change viewing distance\n");
  331. }
  332. int main( int argc, char *argv[] )
  333. {
  334. glutInitWindowPosition(0, 0);
  335. glutInitWindowSize(600, 500);
  336. glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  337. glutCreateWindow("Texture Cube Maping");
  338. if (argc > 1 && strcmp(argv[1] , "-i") == 0)
  339. init( 1 );
  340. else
  341. init( 0 );
  342. glutReshapeFunc( reshape );
  343. glutKeyboardFunc( key );
  344. glutSpecialFunc( specialkey );
  345. glutIdleFunc( idle );
  346. glutDisplayFunc( draw );
  347. usage();
  348. glutMainLoop();
  349. return 0;
  350. }