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 static GLfloat Xrot
= 0, Yrot
= 0;
47 static GLfloat EyeDist
= 10;
48 static GLboolean use_vertex_arrays
= GL_FALSE
;
49 static GLboolean anim
= GL_TRUE
;
50 static GLboolean NoClear
= GL_FALSE
;
51 static GLint FrameParity
= 0;
54 #define br 20.0 /* box radius */
56 static const GLfloat tex_coords
[] = {
94 static const GLfloat vtx_coords
[] = {
132 static void draw_skybox( void )
134 if ( use_vertex_arrays
) {
135 glTexCoordPointer( 3, GL_FLOAT
, 0, tex_coords
);
136 glVertexPointer( 3, GL_FLOAT
, 0, vtx_coords
);
138 glEnableClientState( GL_TEXTURE_COORD_ARRAY
);
139 glEnableClientState( GL_VERTEX_ARRAY
);
141 glDrawArrays( GL_QUADS
, 0, 24 );
143 glDisableClientState( GL_TEXTURE_COORD_ARRAY
);
144 glDisableClientState( GL_VERTEX_ARRAY
);
150 for ( i
= 0 ; i
< 24 ; i
++ ) {
151 glTexCoord3fv( & tex_coords
[ i
* 3 ] );
152 glVertex3fv ( & vtx_coords
[ i
* 3 ] );
159 static void draw( void )
162 /* This demonstrates how we can avoid calling glClear.
163 * This method only works if every pixel in the window is painted for
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]
171 FrameParity
= 1 - FrameParity
;
173 glDepthRange(0.0, 0.5);
174 glDepthFunc(GL_LESS
);
177 glDepthRange(1.0, 0.5);
178 glDepthFunc(GL_GREATER
);
182 /* ordinary clearing */
183 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
186 glPushMatrix(); /*MODELVIEW*/
187 glTranslatef( 0.0, 0.0, -EyeDist
);
190 glDisable(GL_TEXTURE_GEN_S
);
191 glDisable(GL_TEXTURE_GEN_T
);
192 glDisable(GL_TEXTURE_GEN_R
);
194 glMatrixMode(GL_MODELVIEW
);
196 glRotatef(Xrot
, 1, 0, 0);
197 glRotatef(Yrot
, 0, 1, 0);
202 glMatrixMode(GL_TEXTURE
);
204 glRotatef(-Yrot
, 0, 1, 0);
205 glRotatef(-Xrot
, 1, 0, 0);
207 glEnable(GL_TEXTURE_GEN_S
);
208 glEnable(GL_TEXTURE_GEN_T
);
209 glEnable(GL_TEXTURE_GEN_R
);
210 glutSolidSphere(2.0, 20, 20);
212 glLoadIdentity(); /* texture */
214 glMatrixMode(GL_MODELVIEW
);
221 static void idle(void)
223 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
229 static void set_mode(GLuint mode
)
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");
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");
246 static void key(unsigned char k
, int x
, int y
)
248 static GLuint mode
= 0;
264 use_vertex_arrays
= ! use_vertex_arrays
;
265 printf( "Vertex arrays are %sabled\n",
266 (use_vertex_arrays
) ? "en" : "dis" );
285 static void specialkey(int key
, int x
, int y
)
308 /* new window size or exposure */
309 static void reshape(int width
, int height
)
311 GLfloat ar
= (float) width
/ (float) height
;
312 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
313 glMatrixMode(GL_PROJECTION
);
315 glFrustum( -2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0 );
316 glMatrixMode(GL_MODELVIEW
);
321 static void init_checkers( void )
323 #define CUBE_TEX_SIZE 64
324 GLubyte image
[CUBE_TEX_SIZE
][CUBE_TEX_SIZE
][3];
325 static const GLubyte colors
[6][3] = {
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 */
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
344 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
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];
356 image
[i
][j
][0] = 255;
357 image
[i
][j
][1] = 255;
358 image
[i
][j
][2] = 255;
363 glTexImage2D(targets
[f
], 0, GL_RGB
, CUBE_TEX_SIZE
, CUBE_TEX_SIZE
, 0,
364 GL_RGB
, GL_UNSIGNED_BYTE
, image
);
369 static void load(GLenum target
, const char *filename
,
370 GLboolean flipTB
, GLboolean flipLR
)
374 GLubyte
*img
= LoadRGBImage( filename
, &w
, &h
, &format
);
376 printf("Error: couldn't load texture image %s\n", filename
);
379 assert(format
== GL_RGB
);
381 /* <sigh> the way the texture cube mapping works, we have to flip
382 * images to make things look right.
385 const int stride
= 3 * w
;
386 GLubyte temp
[3*1024];
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
);
395 const int stride
= 3 * w
;
399 for (i
= 0; i
< h
; i
++) {
400 row
= img
+ i
* stride
;
401 for (j
= 0; j
< w
/ 2; j
++) {
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];
416 gluBuild2DMipmaps(target
, GL_RGB
, w
, h
, format
, GL_UNSIGNED_BYTE
, img
);
421 static void load_envmaps(void)
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
);
432 static void init( GLboolean useImageFiles
)
436 /* check for extension */
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");
444 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER
));
455 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MIN_FILTER
, filter
);
456 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MAG_FILTER
, filter
);
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
);
460 glEnable(GL_TEXTURE_CUBE_MAP_ARB
);
461 glEnable(GL_DEPTH_TEST
);
463 glClearColor(.3, .3, .3, 0);
464 glColor3f( 1.0, 1.0, 1.0 );
470 static void usage(void)
473 printf(" SPACE - toggle animation\n");
474 printf(" CURSOR KEYS - rotation\n");
475 printf(" m - toggle texgen reflection mode\n");
476 printf(" z/Z - change viewing distance\n");
480 static void parse_args(int argc
, char *argv
[])
485 for (i
= 1; i
< argc
; i
++) {
486 if (strcmp(argv
[i
], "-i") == 0)
488 else if (strcmp(argv
[i
], "--noclear") == 0)
491 fprintf(stderr
, "Bad option: %s\n", argv
[i
]);
498 int main( int argc
, char *argv
[] )
500 glutInit(&argc
, argv
);
501 glutInitWindowPosition(0, 0);
502 glutInitWindowSize(600, 500);
503 glutInitDisplayMode( GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
504 glutCreateWindow("Texture Cube Mapping");
505 glutReshapeFunc( reshape
);
506 glutKeyboardFunc( key
);
507 glutSpecialFunc( specialkey
);
508 glutDisplayFunc( draw
);
511 parse_args(argc
, argv
);