Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cubemap.c 11KB

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