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