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;
63 static GLint Frames
= 0;
70 { GL_CLAMP_TO_EDGE
, "GL_CLAMP_TO_EDGE" },
71 { GL_CLAMP_TO_BORDER
, "GL_CLAMP_TO_BORDER" },
72 { GL_CLAMP
, "GL_CLAMP" },
73 { GL_REPEAT
, "GL_REPEAT" }
76 #define NUM_CLAMP_MODES (sizeof(ClampModes) / sizeof(ClampModes[0]))
80 GLenum mag_mode
, min_mode
;
83 { GL_NEAREST
, GL_NEAREST
, "GL_NEAREST, GL_NEAREST" },
84 { GL_NEAREST
, GL_LINEAR
, "GL_NEAREST, GL_LINEAR" },
85 { GL_NEAREST
, GL_NEAREST_MIPMAP_NEAREST
, "GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST" },
86 { GL_NEAREST
, GL_NEAREST_MIPMAP_LINEAR
, "GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR" },
87 { GL_NEAREST
, GL_LINEAR_MIPMAP_NEAREST
, "GL_NEAREST, GL_LINEAR_MIPMAP_NEAREST" },
88 { GL_NEAREST
, GL_LINEAR_MIPMAP_LINEAR
, "GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR" },
90 { GL_LINEAR
, GL_NEAREST
, "GL_LINEAR, GL_NEAREST" },
91 { GL_LINEAR
, GL_LINEAR
, "GL_LINEAR, GL_LINEAR" },
92 { GL_LINEAR
, GL_NEAREST_MIPMAP_NEAREST
, "GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST" },
93 { GL_LINEAR
, GL_NEAREST_MIPMAP_LINEAR
, "GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR" },
94 { GL_LINEAR
, GL_LINEAR_MIPMAP_NEAREST
, "GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST" },
95 { GL_LINEAR
, GL_LINEAR_MIPMAP_LINEAR
, "GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR" }
98 #define NUM_FILTER_MODES (sizeof(FilterModes) / sizeof(FilterModes[0]))
102 /* The effects of GL_ARB_seamless_cube_map don't show up unless eps1 is 1.0.
104 #define eps1 1.0 /*0.99*/
105 #define br 20.0 /* box radius */
107 static const GLfloat tex_coords
[] = {
145 static const GLfloat vtx_coords
[] = {
183 static void draw_skybox( void )
185 if ( use_vertex_arrays
) {
186 glTexCoordPointer( 3, GL_FLOAT
, 0, tex_coords
);
187 glVertexPointer( 3, GL_FLOAT
, 0, vtx_coords
);
189 glEnableClientState( GL_TEXTURE_COORD_ARRAY
);
190 glEnableClientState( GL_VERTEX_ARRAY
);
192 glDrawArrays( GL_QUADS
, 0, 24 );
194 glDisableClientState( GL_TEXTURE_COORD_ARRAY
);
195 glDisableClientState( GL_VERTEX_ARRAY
);
201 for ( i
= 0 ; i
< 24 ; i
++ ) {
202 glTexCoord3fv( & tex_coords
[ i
* 3 ] );
203 glVertex3fv ( & vtx_coords
[ i
* 3 ] );
210 static void draw( void )
215 /* This demonstrates how we can avoid calling glClear.
216 * This method only works if every pixel in the window is painted for
218 * We can simply skip clearing of the color buffer in this case.
219 * For the depth buffer, we alternately use a different subrange of
220 * the depth buffer for each frame. For the odd frame use the range
221 * [0, 0.5] with GL_LESS. For the even frames, use the range [1, 0.5]
224 FrameParity
= 1 - FrameParity
;
226 glDepthRange(0.0, 0.5);
227 glDepthFunc(GL_LESS
);
230 glDepthRange(1.0, 0.5);
231 glDepthFunc(GL_GREATER
);
235 /* ordinary clearing */
236 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
239 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MIN_FILTER
,
240 FilterModes
[FilterIndex
].min_mode
);
241 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MAG_FILTER
,
242 FilterModes
[FilterIndex
].mag_mode
);
244 if (supportSeamless
) {
246 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
248 glDisable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
251 wrap
= ClampModes
[ClampIndex
].mode
;
252 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_S
, wrap
);
253 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_T
, wrap
);
254 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_R
, wrap
);
256 glPushMatrix(); /*MODELVIEW*/
257 glTranslatef( 0.0, 0.0, -EyeDist
);
260 glDisable(GL_TEXTURE_GEN_S
);
261 glDisable(GL_TEXTURE_GEN_T
);
262 glDisable(GL_TEXTURE_GEN_R
);
264 glMatrixMode(GL_MODELVIEW
);
266 glRotatef(Xrot
, 1, 0, 0);
267 glRotatef(Yrot
, 0, 1, 0);
272 glMatrixMode(GL_TEXTURE
);
274 glRotatef(-Yrot
, 0, 1, 0);
275 glRotatef(-Xrot
, 1, 0, 0);
277 glEnable(GL_TEXTURE_GEN_S
);
278 glEnable(GL_TEXTURE_GEN_T
);
279 glEnable(GL_TEXTURE_GEN_R
);
280 glutSolidSphere(2.0, 20, 20);
282 glLoadIdentity(); /* texture */
284 glMatrixMode(GL_MODELVIEW
);
292 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
293 if (t
- T0
>= 5000) {
294 GLfloat seconds
= (t
- T0
) / 1000.0;
295 GLfloat fps
= Frames
/ seconds
;
296 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
305 static void idle(void)
307 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
313 static void set_mode(GLuint mode
)
316 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
317 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
318 glTexGeni(GL_R
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
319 printf("GL_REFLECTION_MAP_ARB mode\n");
321 else if (mode
== 1) {
322 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
323 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
324 glTexGeni(GL_R
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
325 printf("GL_NORMAL_MAP_ARB mode\n");
330 static void key(unsigned char k
, int x
, int y
)
332 static GLuint mode
= 0;
344 FilterIndex
= (FilterIndex
+ 1) % NUM_FILTER_MODES
;
345 printf("Tex filter: %s\n", FilterModes
[FilterIndex
].name
);
348 ClampIndex
= (ClampIndex
+ 1) % NUM_CLAMP_MODES
;
349 printf("Tex wrap mode: %s\n", ClampModes
[ClampIndex
].name
);
356 seamless
= ! seamless
;
357 printf("Seamless cube map filtering is %sabled\n",
358 (seamless
) ? "en" : "dis" );
361 use_vertex_arrays
= ! use_vertex_arrays
;
362 printf( "Vertex arrays are %sabled\n",
363 (use_vertex_arrays
) ? "en" : "dis" );
382 static void specialkey(int key
, int x
, int y
)
405 /* new window size or exposure */
406 static void reshape(int width
, int height
)
408 GLfloat ar
= (float) width
/ (float) height
;
409 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
410 glMatrixMode(GL_PROJECTION
);
412 glFrustum( -2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0 );
413 glMatrixMode(GL_MODELVIEW
);
418 static void init_checkers( void )
420 #define CUBE_TEX_SIZE 64
421 GLubyte image
[CUBE_TEX_SIZE
][CUBE_TEX_SIZE
][4];
422 static const GLubyte colors
[6][3] = {
423 { 255, 0, 0 }, /* face 0 - red */
424 { 0, 255, 255 }, /* face 1 - cyan */
425 { 0, 255, 0 }, /* face 2 - green */
426 { 255, 0, 255 }, /* face 3 - purple */
427 { 0, 0, 255 }, /* face 4 - blue */
428 { 255, 255, 0 } /* face 5 - yellow */
430 static const GLenum targets
[6] = {
431 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
432 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
433 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
434 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
435 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
436 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
441 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
444 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
447 /* make colored checkerboard cube faces */
448 for (f
= 0; f
< 6; f
++) {
449 for (i
= 0; i
< CUBE_TEX_SIZE
; i
++) {
450 for (j
= 0; j
< CUBE_TEX_SIZE
; j
++) {
451 if ((i
/4 + j
/4) & 1) {
452 image
[i
][j
][0] = colors
[f
][2];
453 image
[i
][j
][1] = colors
[f
][1];
454 image
[i
][j
][2] = colors
[f
][0];
455 image
[i
][j
][3] = 255;
458 image
[i
][j
][0] = 255;
459 image
[i
][j
][1] = 255;
460 image
[i
][j
][2] = 255;
461 image
[i
][j
][3] = 255;
466 glTexImage2D(targets
[f
], 0, GL_RGBA8
, CUBE_TEX_SIZE
, CUBE_TEX_SIZE
, 0,
467 GL_BGRA
, GL_UNSIGNED_BYTE
, image
);
471 glGenerateMipmapEXT(GL_TEXTURE_CUBE_MAP_ARB
);
475 static void load(GLenum target
, const char *filename
,
476 GLboolean flipTB
, GLboolean flipLR
)
480 GLubyte
*img
= LoadRGBImage( filename
, &w
, &h
, &format
);
482 printf("Error: couldn't load texture image %s\n", filename
);
485 assert(format
== GL_RGB
);
487 /* <sigh> the way the texture cube mapping works, we have to flip
488 * images to make things look right.
491 const int stride
= 3 * w
;
492 GLubyte temp
[3*1024];
494 for (i
= 0; i
< h
/ 2; i
++) {
495 memcpy(temp
, img
+ i
* stride
, stride
);
496 memcpy(img
+ i
* stride
, img
+ (h
- i
- 1) * stride
, stride
);
497 memcpy(img
+ (h
- i
- 1) * stride
, temp
, stride
);
501 const int stride
= 3 * w
;
505 for (i
= 0; i
< h
; i
++) {
506 row
= img
+ i
* stride
;
507 for (j
= 0; j
< w
/ 2; j
++) {
509 temp
[0] = row
[j
*3+0];
510 temp
[1] = row
[j
*3+1];
511 temp
[2] = row
[j
*3+2];
512 row
[j
*3+0] = row
[k
*3+0];
513 row
[j
*3+1] = row
[k
*3+1];
514 row
[j
*3+2] = row
[k
*3+2];
515 row
[k
*3+0] = temp
[0];
516 row
[k
*3+1] = temp
[1];
517 row
[k
*3+2] = temp
[2];
522 gluBuild2DMipmaps(target
, GL_RGB
, w
, h
, format
, GL_UNSIGNED_BYTE
, img
);
527 static void load_envmaps(void)
529 load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
, "right.rgb", GL_TRUE
, GL_FALSE
);
530 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
, "left.rgb", GL_TRUE
, GL_FALSE
);
531 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
, "top.rgb", GL_FALSE
, GL_TRUE
);
532 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
, "bottom.rgb", GL_FALSE
, GL_TRUE
);
533 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
, "front.rgb", GL_TRUE
, GL_FALSE
);
534 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
, "back.rgb", GL_TRUE
, GL_FALSE
);
538 static void init( GLboolean useImageFiles
)
540 /* check for extensions */
541 if (!GLEW_ARB_texture_cube_map
) {
542 printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
546 /* Needed for glGenerateMipmapEXT / auto mipmapping
548 supportFBO
= GLEW_EXT_framebuffer_object
;
550 if (!supportFBO
&& !GLEW_SGIS_generate_mipmap
) {
551 printf("Sorry, this demo requires GL_EXT_framebuffer_object or "
552 "GL_SGIS_generate_mipmap\n");
556 /* GLEW doesn't know about this extension yet, so use the old GLUT function
557 * to check for availability.
559 supportSeamless
= glutExtensionSupported("GL_ARB_seamless_cube_map");
561 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER
));
564 glGenTextures(1, &TexObj
);
565 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB
, TexObj
);
574 glEnable(GL_TEXTURE_CUBE_MAP_ARB
);
575 glEnable(GL_DEPTH_TEST
);
577 glClearColor(.3, .3, .3, 0);
578 glColor3f( 1.0, 1.0, 1.0 );
584 static void usage(void)
587 printf(" SPACE - toggle animation\n");
588 printf(" CURSOR KEYS - rotation\n");
589 printf(" c - toggle texture clamp/wrap mode\n");
590 printf(" f - toggle texture filter mode\n");
591 printf(" m - toggle texgen reflection mode\n");
592 printf(" z/Z - change viewing distance\n");
596 static void parse_args(int argc
, char *argv
[])
601 for (i
= 1; i
< argc
; i
++) {
602 if (strcmp(argv
[i
], "-i") == 0)
604 else if (strcmp(argv
[i
], "--noclear") == 0)
607 fprintf(stderr
, "Bad option: %s\n", argv
[i
]);
614 int main( int argc
, char *argv
[] )
616 glutInitWindowSize(600, 500);
617 glutInit(&argc
, argv
);
618 glutInitDisplayMode( GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
619 glutCreateWindow("Texture Cube Mapping");
621 glutReshapeFunc( reshape
);
622 glutKeyboardFunc( key
);
623 glutSpecialFunc( specialkey
);
624 glutDisplayFunc( draw
);
627 parse_args(argc
, argv
);