2 * GL_ARB_texture_cube_map demo
8 * Copyright (C) 2000 Brian Paul All Rights Reserved.
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
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).
46 #ifndef GL_TEXTURE_CUBE_MAP_SEAMLESS
47 #define GL_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
50 static GLfloat Xrot
= 0, Yrot
= 0;
51 static GLfloat EyeDist
= 10;
52 static GLboolean use_vertex_arrays
= GL_FALSE
;
53 static GLboolean anim
= GL_TRUE
;
54 static GLboolean NoClear
= GL_FALSE
;
55 static GLint FrameParity
= 0;
56 static GLenum FilterIndex
= 0;
57 static GLint ClampIndex
= 0;
58 static GLboolean supportFBO
= GL_FALSE
;
59 static GLboolean supportSeamless
= GL_FALSE
;
60 static GLboolean seamless
= GL_FALSE
;
61 static GLuint TexObj
= 0;
68 { GL_CLAMP_TO_EDGE
, "GL_CLAMP_TO_EDGE" },
69 { GL_CLAMP_TO_BORDER
, "GL_CLAMP_TO_BORDER" },
70 { GL_CLAMP
, "GL_CLAMP" },
71 { GL_REPEAT
, "GL_REPEAT" }
74 #define NUM_CLAMP_MODES (sizeof(ClampModes) / sizeof(ClampModes[0]))
78 GLenum mag_mode
, min_mode
;
81 { GL_NEAREST
, GL_NEAREST
, "GL_NEAREST, GL_NEAREST" },
82 { GL_NEAREST
, GL_LINEAR
, "GL_NEAREST, GL_LINEAR" },
83 { GL_NEAREST
, GL_NEAREST_MIPMAP_NEAREST
, "GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST" },
84 { GL_NEAREST
, GL_NEAREST_MIPMAP_LINEAR
, "GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR" },
85 { GL_NEAREST
, GL_LINEAR_MIPMAP_NEAREST
, "GL_NEAREST, GL_LINEAR_MIPMAP_NEAREST" },
86 { GL_NEAREST
, GL_LINEAR_MIPMAP_LINEAR
, "GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR" },
88 { GL_LINEAR
, GL_NEAREST
, "GL_LINEAR, GL_NEAREST" },
89 { GL_LINEAR
, GL_LINEAR
, "GL_LINEAR, GL_LINEAR" },
90 { GL_LINEAR
, GL_NEAREST_MIPMAP_NEAREST
, "GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST" },
91 { GL_LINEAR
, GL_NEAREST_MIPMAP_LINEAR
, "GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR" },
92 { GL_LINEAR
, GL_LINEAR_MIPMAP_NEAREST
, "GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST" },
93 { GL_LINEAR
, GL_LINEAR_MIPMAP_LINEAR
, "GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR" }
96 #define NUM_FILTER_MODES (sizeof(FilterModes) / sizeof(FilterModes[0]))
100 /* The effects of GL_ARB_seamless_cube_map don't show up unless eps1 is 1.0.
102 #define eps1 1.0 /*0.99*/
103 #define br 20.0 /* box radius */
105 static const GLfloat tex_coords
[] = {
143 static const GLfloat vtx_coords
[] = {
181 static void draw_skybox( void )
183 if ( use_vertex_arrays
) {
184 glTexCoordPointer( 3, GL_FLOAT
, 0, tex_coords
);
185 glVertexPointer( 3, GL_FLOAT
, 0, vtx_coords
);
187 glEnableClientState( GL_TEXTURE_COORD_ARRAY
);
188 glEnableClientState( GL_VERTEX_ARRAY
);
190 glDrawArrays( GL_QUADS
, 0, 24 );
192 glDisableClientState( GL_TEXTURE_COORD_ARRAY
);
193 glDisableClientState( GL_VERTEX_ARRAY
);
199 for ( i
= 0 ; i
< 24 ; i
++ ) {
200 glTexCoord3fv( & tex_coords
[ i
* 3 ] );
201 glVertex3fv ( & vtx_coords
[ i
* 3 ] );
208 static void draw( void )
213 /* This demonstrates how we can avoid calling glClear.
214 * This method only works if every pixel in the window is painted for
216 * We can simply skip clearing of the color buffer in this case.
217 * For the depth buffer, we alternately use a different subrange of
218 * the depth buffer for each frame. For the odd frame use the range
219 * [0, 0.5] with GL_LESS. For the even frames, use the range [1, 0.5]
222 FrameParity
= 1 - FrameParity
;
224 glDepthRange(0.0, 0.5);
225 glDepthFunc(GL_LESS
);
228 glDepthRange(1.0, 0.5);
229 glDepthFunc(GL_GREATER
);
233 /* ordinary clearing */
234 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
237 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MIN_FILTER
,
238 FilterModes
[FilterIndex
].min_mode
);
239 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MAG_FILTER
,
240 FilterModes
[FilterIndex
].mag_mode
);
242 if (supportSeamless
) {
244 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
246 glDisable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
249 wrap
= ClampModes
[ClampIndex
].mode
;
250 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_S
, wrap
);
251 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_T
, wrap
);
252 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_R
, wrap
);
254 glPushMatrix(); /*MODELVIEW*/
255 glTranslatef( 0.0, 0.0, -EyeDist
);
258 glDisable(GL_TEXTURE_GEN_S
);
259 glDisable(GL_TEXTURE_GEN_T
);
260 glDisable(GL_TEXTURE_GEN_R
);
262 glMatrixMode(GL_MODELVIEW
);
264 glRotatef(Xrot
, 1, 0, 0);
265 glRotatef(Yrot
, 0, 1, 0);
270 glMatrixMode(GL_TEXTURE
);
272 glRotatef(-Yrot
, 0, 1, 0);
273 glRotatef(-Xrot
, 1, 0, 0);
275 glEnable(GL_TEXTURE_GEN_S
);
276 glEnable(GL_TEXTURE_GEN_T
);
277 glEnable(GL_TEXTURE_GEN_R
);
278 glutSolidSphere(2.0, 20, 20);
280 glLoadIdentity(); /* texture */
282 glMatrixMode(GL_MODELVIEW
);
289 static void idle(void)
291 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
297 static void set_mode(GLuint mode
)
300 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
301 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
302 glTexGeni(GL_R
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
303 printf("GL_REFLECTION_MAP_ARB mode\n");
305 else if (mode
== 1) {
306 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
307 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
308 glTexGeni(GL_R
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
309 printf("GL_NORMAL_MAP_ARB mode\n");
314 static void key(unsigned char k
, int x
, int y
)
316 static GLuint mode
= 0;
328 FilterIndex
= (FilterIndex
+ 1) % NUM_FILTER_MODES
;
329 printf("Tex filter: %s\n", FilterModes
[FilterIndex
].name
);
332 ClampIndex
= (ClampIndex
+ 1) % NUM_CLAMP_MODES
;
333 printf("Tex wrap mode: %s\n", ClampModes
[ClampIndex
].name
);
340 seamless
= ! seamless
;
341 printf("Seamless cube map filtering is %sabled\n",
342 (seamless
) ? "en" : "dis" );
345 use_vertex_arrays
= ! use_vertex_arrays
;
346 printf( "Vertex arrays are %sabled\n",
347 (use_vertex_arrays
) ? "en" : "dis" );
366 static void specialkey(int key
, int x
, int y
)
389 /* new window size or exposure */
390 static void reshape(int width
, int height
)
392 GLfloat ar
= (float) width
/ (float) height
;
393 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
394 glMatrixMode(GL_PROJECTION
);
396 glFrustum( -2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0 );
397 glMatrixMode(GL_MODELVIEW
);
402 static void init_checkers( void )
404 #define CUBE_TEX_SIZE 64
405 GLubyte image
[CUBE_TEX_SIZE
][CUBE_TEX_SIZE
][4];
406 static const GLubyte colors
[6][3] = {
407 { 255, 0, 0 }, /* face 0 - red */
408 { 0, 255, 255 }, /* face 1 - cyan */
409 { 0, 255, 0 }, /* face 2 - green */
410 { 255, 0, 255 }, /* face 3 - purple */
411 { 0, 0, 255 }, /* face 4 - blue */
412 { 255, 255, 0 } /* face 5 - yellow */
414 static const GLenum targets
[6] = {
415 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
416 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
417 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
418 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
419 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
420 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
425 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
428 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
431 /* make colored checkerboard cube faces */
432 for (f
= 0; f
< 6; f
++) {
433 for (i
= 0; i
< CUBE_TEX_SIZE
; i
++) {
434 for (j
= 0; j
< CUBE_TEX_SIZE
; j
++) {
435 if ((i
/4 + j
/4) & 1) {
436 image
[i
][j
][0] = colors
[f
][2];
437 image
[i
][j
][1] = colors
[f
][1];
438 image
[i
][j
][2] = colors
[f
][0];
439 image
[i
][j
][3] = 255;
442 image
[i
][j
][0] = 255;
443 image
[i
][j
][1] = 255;
444 image
[i
][j
][2] = 255;
445 image
[i
][j
][3] = 255;
450 glTexImage2D(targets
[f
], 0, GL_RGBA8
, CUBE_TEX_SIZE
, CUBE_TEX_SIZE
, 0,
451 GL_BGRA
, GL_UNSIGNED_BYTE
, image
);
455 glGenerateMipmapEXT(GL_TEXTURE_CUBE_MAP_ARB
);
459 static void load(GLenum target
, const char *filename
,
460 GLboolean flipTB
, GLboolean flipLR
)
464 GLubyte
*img
= LoadRGBImage( filename
, &w
, &h
, &format
);
466 printf("Error: couldn't load texture image %s\n", filename
);
469 assert(format
== GL_RGB
);
471 /* <sigh> the way the texture cube mapping works, we have to flip
472 * images to make things look right.
475 const int stride
= 3 * w
;
476 GLubyte temp
[3*1024];
478 for (i
= 0; i
< h
/ 2; i
++) {
479 memcpy(temp
, img
+ i
* stride
, stride
);
480 memcpy(img
+ i
* stride
, img
+ (h
- i
- 1) * stride
, stride
);
481 memcpy(img
+ (h
- i
- 1) * stride
, temp
, stride
);
485 const int stride
= 3 * w
;
489 for (i
= 0; i
< h
; i
++) {
490 row
= img
+ i
* stride
;
491 for (j
= 0; j
< w
/ 2; j
++) {
493 temp
[0] = row
[j
*3+0];
494 temp
[1] = row
[j
*3+1];
495 temp
[2] = row
[j
*3+2];
496 row
[j
*3+0] = row
[k
*3+0];
497 row
[j
*3+1] = row
[k
*3+1];
498 row
[j
*3+2] = row
[k
*3+2];
499 row
[k
*3+0] = temp
[0];
500 row
[k
*3+1] = temp
[1];
501 row
[k
*3+2] = temp
[2];
506 gluBuild2DMipmaps(target
, GL_RGB
, w
, h
, format
, GL_UNSIGNED_BYTE
, img
);
511 static void load_envmaps(void)
513 load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
, "right.rgb", GL_TRUE
, GL_FALSE
);
514 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
, "left.rgb", GL_TRUE
, GL_FALSE
);
515 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
, "top.rgb", GL_FALSE
, GL_TRUE
);
516 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
, "bottom.rgb", GL_FALSE
, GL_TRUE
);
517 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
, "front.rgb", GL_TRUE
, GL_FALSE
);
518 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
, "back.rgb", GL_TRUE
, GL_FALSE
);
522 static void init( GLboolean useImageFiles
)
524 /* check for extensions */
525 if (!GLEW_ARB_texture_cube_map
) {
526 printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
530 /* Needed for glGenerateMipmapEXT / auto mipmapping
532 supportFBO
= GLEW_EXT_framebuffer_object
;
534 if (!supportFBO
&& !GLEW_SGIS_generate_mipmap
) {
535 printf("Sorry, this demo requires GL_EXT_framebuffer_object or "
536 "GL_SGIS_generate_mipmap\n");
540 /* GLEW doesn't know about this extension yet, so use the old GLUT function
541 * to check for availability.
543 supportSeamless
= glutExtensionSupported("GL_ARB_seamless_cube_map");
545 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER
));
548 glGenTextures(1, &TexObj
);
549 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB
, TexObj
);
558 glEnable(GL_TEXTURE_CUBE_MAP_ARB
);
559 glEnable(GL_DEPTH_TEST
);
561 glClearColor(.3, .3, .3, 0);
562 glColor3f( 1.0, 1.0, 1.0 );
568 static void usage(void)
571 printf(" SPACE - toggle animation\n");
572 printf(" CURSOR KEYS - rotation\n");
573 printf(" c - toggle texture clamp/wrap mode\n");
574 printf(" f - toggle texture filter mode\n");
575 printf(" m - toggle texgen reflection mode\n");
576 printf(" z/Z - change viewing distance\n");
580 static void parse_args(int argc
, char *argv
[])
585 for (i
= 1; i
< argc
; i
++) {
586 if (strcmp(argv
[i
], "-i") == 0)
588 else if (strcmp(argv
[i
], "--noclear") == 0)
591 fprintf(stderr
, "Bad option: %s\n", argv
[i
]);
598 int main( int argc
, char *argv
[] )
600 glutInit(&argc
, argv
);
601 glutInitWindowPosition(0, 0);
602 glutInitWindowSize(600, 500);
603 glutInitDisplayMode( GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
604 glutCreateWindow("Texture Cube Mapping");
606 glutReshapeFunc( reshape
);
607 glutKeyboardFunc( key
);
608 glutSpecialFunc( specialkey
);
609 glutDisplayFunc( draw
);
612 parse_args(argc
, argv
);