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
;
52 #define br 20.0 /* box radius */
54 static const GLfloat tex_coords
[] = {
92 static const GLfloat vtx_coords
[] = {
130 static void draw_skybox( void )
132 if ( use_vertex_arrays
) {
133 glTexCoordPointer( 3, GL_FLOAT
, 0, tex_coords
);
134 glVertexPointer( 3, GL_FLOAT
, 0, vtx_coords
);
136 glEnableClientState( GL_TEXTURE_COORD_ARRAY
);
137 glEnableClientState( GL_VERTEX_ARRAY
);
139 glDrawArrays( GL_QUADS
, 0, 24 );
141 glDisableClientState( GL_TEXTURE_COORD_ARRAY
);
142 glDisableClientState( GL_VERTEX_ARRAY
);
148 for ( i
= 0 ; i
< 24 ; i
++ ) {
149 glTexCoord3fv( & tex_coords
[ i
* 3 ] );
150 glVertex3fv ( & vtx_coords
[ i
* 3 ] );
157 static void draw( void )
159 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
161 glPushMatrix(); /*MODELVIEW*/
162 glTranslatef( 0.0, 0.0, -EyeDist
);
165 glDisable(GL_TEXTURE_GEN_S
);
166 glDisable(GL_TEXTURE_GEN_T
);
167 glDisable(GL_TEXTURE_GEN_R
);
169 glMatrixMode(GL_MODELVIEW
);
171 glRotatef(Xrot
, 1, 0, 0);
172 glRotatef(Yrot
, 0, 1, 0);
177 glMatrixMode(GL_TEXTURE
);
179 glRotatef(-Yrot
, 0, 1, 0);
180 glRotatef(-Xrot
, 1, 0, 0);
182 glEnable(GL_TEXTURE_GEN_S
);
183 glEnable(GL_TEXTURE_GEN_T
);
184 glEnable(GL_TEXTURE_GEN_R
);
185 glutSolidSphere(2.0, 20, 20);
187 glLoadIdentity(); /* texture */
189 glMatrixMode(GL_MODELVIEW
);
196 static void idle(void)
198 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
204 static void set_mode(GLuint mode
)
207 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
208 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
209 glTexGeni(GL_R
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
210 printf("GL_REFLECTION_MAP_ARB mode\n");
212 else if (mode
== 1) {
213 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
214 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
215 glTexGeni(GL_R
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
216 printf("GL_NORMAL_MAP_ARB mode\n");
221 static void key(unsigned char k
, int x
, int y
)
223 static GLuint mode
= 0;
239 use_vertex_arrays
= ! use_vertex_arrays
;
240 printf( "Vertex arrays are %sabled\n",
241 (use_vertex_arrays
) ? "en" : "dis" );
260 static void specialkey(int key
, int x
, int y
)
283 /* new window size or exposure */
284 static void reshape(int width
, int height
)
286 GLfloat ar
= (float) width
/ (float) height
;
287 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
288 glMatrixMode(GL_PROJECTION
);
290 glFrustum( -2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0 );
291 glMatrixMode(GL_MODELVIEW
);
296 static void init_checkers( void )
298 #define CUBE_TEX_SIZE 64
299 GLubyte image
[CUBE_TEX_SIZE
][CUBE_TEX_SIZE
][3];
300 static const GLubyte colors
[6][3] = {
301 { 255, 0, 0 }, /* face 0 - red */
302 { 0, 255, 255 }, /* face 1 - cyan */
303 { 0, 255, 0 }, /* face 2 - green */
304 { 255, 0, 255 }, /* face 3 - purple */
305 { 0, 0, 255 }, /* face 4 - blue */
306 { 255, 255, 0 } /* face 5 - yellow */
308 static const GLenum targets
[6] = {
309 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
310 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
311 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
312 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
313 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
314 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
319 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
321 /* make colored checkerboard cube faces */
322 for (f
= 0; f
< 6; f
++) {
323 for (i
= 0; i
< CUBE_TEX_SIZE
; i
++) {
324 for (j
= 0; j
< CUBE_TEX_SIZE
; j
++) {
325 if ((i
/4 + j
/4) & 1) {
326 image
[i
][j
][0] = colors
[f
][0];
327 image
[i
][j
][1] = colors
[f
][1];
328 image
[i
][j
][2] = colors
[f
][2];
331 image
[i
][j
][0] = 255;
332 image
[i
][j
][1] = 255;
333 image
[i
][j
][2] = 255;
338 glTexImage2D(targets
[f
], 0, GL_RGB
, CUBE_TEX_SIZE
, CUBE_TEX_SIZE
, 0,
339 GL_RGB
, GL_UNSIGNED_BYTE
, image
);
344 static void load(GLenum target
, const char *filename
,
345 GLboolean flipTB
, GLboolean flipLR
)
349 GLubyte
*img
= LoadRGBImage( filename
, &w
, &h
, &format
);
351 printf("Error: couldn't load texture image %s\n", filename
);
354 assert(format
== GL_RGB
);
356 /* <sigh> the way the texture cube mapping works, we have to flip
357 * images to make things look right.
360 const int stride
= 3 * w
;
361 GLubyte temp
[3*1024];
363 for (i
= 0; i
< h
/ 2; i
++) {
364 memcpy(temp
, img
+ i
* stride
, stride
);
365 memcpy(img
+ i
* stride
, img
+ (h
- i
- 1) * stride
, stride
);
366 memcpy(img
+ (h
- i
- 1) * stride
, temp
, stride
);
370 const int stride
= 3 * w
;
374 for (i
= 0; i
< h
; i
++) {
375 row
= img
+ i
* stride
;
376 for (j
= 0; j
< w
/ 2; j
++) {
378 temp
[0] = row
[j
*3+0];
379 temp
[1] = row
[j
*3+1];
380 temp
[2] = row
[j
*3+2];
381 row
[j
*3+0] = row
[k
*3+0];
382 row
[j
*3+1] = row
[k
*3+1];
383 row
[j
*3+2] = row
[k
*3+2];
384 row
[k
*3+0] = temp
[0];
385 row
[k
*3+1] = temp
[1];
386 row
[k
*3+2] = temp
[2];
391 gluBuild2DMipmaps(target
, GL_RGB
, w
, h
, format
, GL_UNSIGNED_BYTE
, img
);
396 static void load_envmaps(void)
398 load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
, "right.rgb", GL_TRUE
, GL_FALSE
);
399 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
, "left.rgb", GL_TRUE
, GL_FALSE
);
400 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
, "top.rgb", GL_FALSE
, GL_TRUE
);
401 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
, "bottom.rgb", GL_FALSE
, GL_TRUE
);
402 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
, "front.rgb", GL_TRUE
, GL_FALSE
);
403 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
, "back.rgb", GL_TRUE
, GL_FALSE
);
407 static void init( GLboolean useImageFiles
)
411 /* check for extension */
413 char *exten
= (char *) glGetString(GL_EXTENSIONS
);
414 if (!strstr(exten
, "GL_ARB_texture_cube_map")) {
415 printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
419 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER
));
430 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MIN_FILTER
, filter
);
431 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MAG_FILTER
, filter
);
432 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
433 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
435 glEnable(GL_TEXTURE_CUBE_MAP_ARB
);
436 glEnable(GL_DEPTH_TEST
);
438 glClearColor(.3, .3, .3, 0);
439 glColor3f( 1.0, 1.0, 1.0 );
445 static void usage(void)
448 printf(" SPACE - toggle animation\n");
449 printf(" CURSOR KEYS - rotation\n");
450 printf(" m - toggle texgen reflection mode\n");
451 printf(" z/Z - change viewing distance\n");
455 int main( int argc
, char *argv
[] )
457 glutInit(&argc
, argv
);
458 glutInitWindowPosition(0, 0);
459 glutInitWindowSize(600, 500);
460 glutInitDisplayMode( GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
461 glutCreateWindow("Texture Cube Mapping");
463 if (argc
> 1 && strcmp(argv
[1] , "-i") == 0)
467 glutReshapeFunc( reshape
);
468 glutKeyboardFunc( key
);
469 glutSpecialFunc( specialkey
);
470 glutDisplayFunc( draw
);