1 /* $Id: cubemap.c,v 1.4 2002/10/25 17:20:26 brianp Exp $ */
4 * GL_ARB_texture_cube_map demo
10 * Copyright (C) 2000 Brian Paul All Rights Reserved.
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 * This is a pretty minimalistic demo for now. Eventually, use some
33 * interesting cube map textures and 3D objects.
34 * For now, we use 6 checkerboard "walls" and a sphere (good for
35 * verification purposes).
45 #include "../util/readtex.c" /* a hack */
48 static GLfloat Xrot
= 0, Yrot
= 0;
49 static GLfloat EyeDist
= 10;
52 static void draw_skybox( void )
54 const GLfloat eps1
= 0.99;
55 const GLfloat br
= 20.0; /* box radius */
60 glTexCoord3f(1.0, -eps1
, -eps1
); glVertex3f(br
, -br
, -br
);
61 glTexCoord3f(1.0, -eps1
, eps1
); glVertex3f(br
, -br
, br
);
62 glTexCoord3f(1.0, eps1
, eps1
); glVertex3f(br
, br
, br
);
63 glTexCoord3f(1.0, eps1
, -eps1
); glVertex3f(br
, br
, -br
);
66 glTexCoord3f(-1.0, eps1
, -eps1
); glVertex3f(-br
, br
, -br
);
67 glTexCoord3f(-1.0, eps1
, eps1
); glVertex3f(-br
, br
, br
);
68 glTexCoord3f(-1.0, -eps1
, eps1
); glVertex3f(-br
, -br
, br
);
69 glTexCoord3f(-1.0, -eps1
, -eps1
); glVertex3f(-br
, -br
, -br
);
72 glTexCoord3f(-eps1
, 1.0, -eps1
); glVertex3f(-br
, br
, -br
);
73 glTexCoord3f(-eps1
, 1.0, eps1
); glVertex3f(-br
, br
, br
);
74 glTexCoord3f( eps1
, 1.0, eps1
); glVertex3f( br
, br
, br
);
75 glTexCoord3f( eps1
, 1.0, -eps1
); glVertex3f( br
, br
, -br
);
78 glTexCoord3f(-eps1
, -1.0, -eps1
); glVertex3f(-br
, -br
, -br
);
79 glTexCoord3f(-eps1
, -1.0, eps1
); glVertex3f(-br
, -br
, br
);
80 glTexCoord3f( eps1
, -1.0, eps1
); glVertex3f( br
, -br
, br
);
81 glTexCoord3f( eps1
, -1.0, -eps1
); glVertex3f( br
, -br
, -br
);
84 glTexCoord3f( eps1
, -eps1
, 1.0); glVertex3f( br
, -br
, br
);
85 glTexCoord3f(-eps1
, -eps1
, 1.0); glVertex3f(-br
, -br
, br
);
86 glTexCoord3f(-eps1
, eps1
, 1.0); glVertex3f(-br
, br
, br
);
87 glTexCoord3f( eps1
, eps1
, 1.0); glVertex3f( br
, br
, br
);
90 glTexCoord3f( eps1
, eps1
, -1.0); glVertex3f( br
, br
, -br
);
91 glTexCoord3f(-eps1
, eps1
, -1.0); glVertex3f(-br
, br
, -br
);
92 glTexCoord3f(-eps1
, -eps1
, -1.0); glVertex3f(-br
, -br
, -br
);
93 glTexCoord3f( eps1
, -eps1
, -1.0); glVertex3f( br
, -br
, -br
);
99 static void draw( void )
101 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
103 glPushMatrix(); /*MODELVIEW*/
104 glTranslatef( 0.0, 0.0, -EyeDist
);
107 glDisable(GL_TEXTURE_GEN_S
);
108 glDisable(GL_TEXTURE_GEN_T
);
109 glDisable(GL_TEXTURE_GEN_R
);
111 glMatrixMode(GL_MODELVIEW
);
113 glRotatef(Xrot
, 1, 0, 0);
114 glRotatef(Yrot
, 0, 1, 0);
119 glMatrixMode(GL_TEXTURE
);
121 glRotatef(-Yrot
, 0, 1, 0);
122 glRotatef(-Xrot
, 1, 0, 0);
124 glEnable(GL_TEXTURE_GEN_S
);
125 glEnable(GL_TEXTURE_GEN_T
);
126 glEnable(GL_TEXTURE_GEN_R
);
127 glutSolidSphere(2.0, 20, 20);
129 glLoadIdentity(); /* texture */
131 glMatrixMode(GL_MODELVIEW
);
138 static void idle(void)
140 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
146 static void set_mode(GLuint mode
)
149 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
150 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
151 glTexGeni(GL_R
, GL_TEXTURE_GEN_MODE
, GL_REFLECTION_MAP_ARB
);
152 printf("GL_REFLECTION_MAP_ARB mode\n");
154 else if (mode
== 1) {
155 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
156 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
157 glTexGeni(GL_R
, GL_TEXTURE_GEN_MODE
, GL_NORMAL_MAP_ARB
);
158 printf("GL_NORMAL_MAP_ARB mode\n");
163 static void key(unsigned char k
, int x
, int y
)
165 static GLboolean anim
= GL_TRUE
;
166 static GLuint mode
= 0;
198 static void specialkey(int key
, int x
, int y
)
221 /* new window size or exposure */
222 static void reshape(int width
, int height
)
224 GLfloat ar
= (float) width
/ (float) height
;
225 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
226 glMatrixMode(GL_PROJECTION
);
228 glFrustum( -2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0 );
229 glMatrixMode(GL_MODELVIEW
);
234 static void init_checkers( void )
236 #define CUBE_TEX_SIZE 64
237 GLubyte image
[CUBE_TEX_SIZE
][CUBE_TEX_SIZE
][3];
238 static const GLubyte colors
[6][3] = {
246 static const GLenum targets
[6] = {
247 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
248 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
249 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
250 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
251 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
252 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
257 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
259 /* make colored checkerboard cube faces */
260 for (f
= 0; f
< 6; f
++) {
261 for (i
= 0; i
< CUBE_TEX_SIZE
; i
++) {
262 for (j
= 0; j
< CUBE_TEX_SIZE
; j
++) {
263 if ((i
/4 + j
/4) & 1) {
264 image
[i
][j
][0] = colors
[f
][0];
265 image
[i
][j
][1] = colors
[f
][1];
266 image
[i
][j
][2] = colors
[f
][2];
269 image
[i
][j
][0] = 255;
270 image
[i
][j
][1] = 255;
271 image
[i
][j
][2] = 255;
276 glTexImage2D(targets
[f
], 0, GL_RGB
, CUBE_TEX_SIZE
, CUBE_TEX_SIZE
, 0,
277 GL_RGB
, GL_UNSIGNED_BYTE
, image
);
282 static void load(GLenum target
, const char *filename
,
283 GLboolean flipTB
, GLboolean flipLR
)
287 GLubyte
*img
= LoadRGBImage( filename
, &w
, &h
, &format
);
289 printf("Error: couldn't load texture image %s\n", filename
);
292 assert(format
== GL_RGB
);
294 /* <sigh> the way the texture cube mapping works, we have to flip
295 * images to make things look right.
298 const int stride
= 3 * w
;
299 GLubyte temp
[3*1024];
301 for (i
= 0; i
< h
/ 2; i
++) {
302 memcpy(temp
, img
+ i
* stride
, stride
);
303 memcpy(img
+ i
* stride
, img
+ (h
- i
- 1) * stride
, stride
);
304 memcpy(img
+ (h
- i
- 1) * stride
, temp
, stride
);
308 const int stride
= 3 * w
;
312 for (i
= 0; i
< h
; i
++) {
313 row
= img
+ i
* stride
;
314 for (j
= 0; j
< w
/ 2; j
++) {
316 temp
[0] = row
[j
*3+0];
317 temp
[1] = row
[j
*3+1];
318 temp
[2] = row
[j
*3+2];
319 row
[j
*3+0] = row
[k
*3+0];
320 row
[j
*3+1] = row
[k
*3+1];
321 row
[j
*3+2] = row
[k
*3+2];
322 row
[k
*3+0] = temp
[0];
323 row
[k
*3+1] = temp
[1];
324 row
[k
*3+2] = temp
[2];
329 gluBuild2DMipmaps(target
, GL_RGB
, w
, h
, format
, GL_UNSIGNED_BYTE
, img
);
334 static void load_envmaps(void)
336 load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
, "right.rgb", GL_TRUE
, GL_FALSE
);
337 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
, "left.rgb", GL_TRUE
, GL_FALSE
);
338 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
, "top.rgb", GL_FALSE
, GL_TRUE
);
339 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
, "bottom.rgb", GL_FALSE
, GL_TRUE
);
340 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
, "front.rgb", GL_TRUE
, GL_FALSE
);
341 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
, "back.rgb", GL_TRUE
, GL_FALSE
);
345 static void init( GLboolean useImageFiles
)
349 /* check for extension */
351 char *exten
= (char *) glGetString(GL_EXTENSIONS
);
352 if (!strstr(exten
, "GL_ARB_texture_cube_map")) {
353 printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
357 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER
));
368 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MIN_FILTER
, filter
);
369 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MAG_FILTER
, filter
);
370 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
371 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
373 glEnable(GL_TEXTURE_CUBE_MAP_ARB
);
374 glEnable(GL_DEPTH_TEST
);
376 glClearColor(.3, .3, .3, 0);
377 glColor3f( 1.0, 1.0, 1.0 );
383 static void usage(void)
386 printf(" SPACE - toggle animation\n");
387 printf(" CURSOR KEYS - rotation\n");
388 printf(" m - toggle texgen reflection mode\n");
389 printf(" z/Z - change viewing distance\n");
393 int main( int argc
, char *argv
[] )
395 glutInitWindowPosition(0, 0);
396 glutInitWindowSize(600, 500);
397 glutInitDisplayMode( GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
398 glutCreateWindow("Texture Cube Maping");
400 if (argc
> 1 && strcmp(argv
[1] , "-i") == 0)
404 glutReshapeFunc( reshape
);
405 glutKeyboardFunc( key
);
406 glutSpecialFunc( specialkey
);
407 glutIdleFunc( idle
);
408 glutDisplayFunc( draw
);