blob: 53605cdfa4ff05ae78f227b87d420743d0b4c604 [file] [log] [blame]
Brian Pauld04d2092000-05-30 01:18:29 +00001/*
2 * GL_ARB_texture_cube_map demo
3 *
4 * Brian Paul
5 * May 2000
6 *
7 *
8 * Copyright (C) 2000 Brian Paul All Rights Reserved.
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
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.
26 */
27
28
29/*
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).
34 */
35
36
Brian Paula4f0b682002-10-25 17:20:26 +000037#include <assert.h>
Brian Pauld04d2092000-05-30 01:18:29 +000038#include <math.h>
Brian Paul02e8a032000-06-27 17:04:43 +000039#include <stdio.h>
Brian Pauld04d2092000-05-30 01:18:29 +000040#include <stdlib.h>
41#include <string.h>
42#include "GL/glut.h"
Brian Paula4f0b682002-10-25 17:20:26 +000043#include "../util/readtex.c" /* a hack */
44
Brian Pauld04d2092000-05-30 01:18:29 +000045
46static GLfloat Xrot = 0, Yrot = 0;
Brian Paula4f0b682002-10-25 17:20:26 +000047static GLfloat EyeDist = 10;
Ian Romanickcd6ca582004-05-05 20:17:19 +000048static GLboolean use_vertex_arrays = GL_FALSE;
Brian Paula4f0b682002-10-25 17:20:26 +000049
Ian Romanickcd6ca582004-05-05 20:17:19 +000050#define eps1 0.99
51#define br 20.0 /* box radius */
52
53static const GLfloat tex_coords[] = {
54 /* +X side */
55 1.0, -eps1, -eps1,
56 1.0, -eps1, eps1,
57 1.0, eps1, eps1,
58 1.0, eps1, -eps1,
59
60 /* -X side */
61 -1.0, eps1, -eps1,
62 -1.0, eps1, eps1,
63 -1.0, -eps1, eps1,
64 -1.0, -eps1, -eps1,
65
66 /* +Y side */
67 -eps1, 1.0, -eps1,
68 -eps1, 1.0, eps1,
69 eps1, 1.0, eps1,
70 eps1, 1.0, -eps1,
71
72 /* -Y side */
73 -eps1, -1.0, -eps1,
74 -eps1, -1.0, eps1,
75 eps1, -1.0, eps1,
76 eps1, -1.0, -eps1,
77
78 /* +Z side */
79 eps1, -eps1, 1.0,
80 -eps1, -eps1, 1.0,
81 -eps1, eps1, 1.0,
82 eps1, eps1, 1.0,
83
84 /* -Z side */
85 eps1, eps1, -1.0,
86 -eps1, eps1, -1.0,
87 -eps1, -eps1, -1.0,
88 eps1, -eps1, -1.0,
89};
90
91static const GLfloat vtx_coords[] = {
92 /* +X side */
93 br, -br, -br,
94 br, -br, br,
95 br, br, br,
96 br, br, -br,
97
98 /* -X side */
99 -br, br, -br,
100 -br, br, br,
101 -br, -br, br,
102 -br, -br, -br,
103
104 /* +Y side */
105 -br, br, -br,
106 -br, br, br,
107 br, br, br,
108 br, br, -br,
109
110 /* -Y side */
111 -br, -br, -br,
112 -br, -br, br,
113 br, -br, br,
114 br, -br, -br,
115
116 /* +Z side */
117 br, -br, br,
118 -br, -br, br,
119 -br, br, br,
120 br, br, br,
121
122 /* -Z side */
123 br, br, -br,
124 -br, br, -br,
125 -br, -br, -br,
126 br, -br, -br,
127};
Brian Paula4f0b682002-10-25 17:20:26 +0000128
129static void draw_skybox( void )
130{
Ian Romanickcd6ca582004-05-05 20:17:19 +0000131 if ( use_vertex_arrays ) {
132 glTexCoordPointer( 3, GL_FLOAT, 0, tex_coords );
133 glVertexPointer( 3, GL_FLOAT, 0, vtx_coords );
Brian Paula4f0b682002-10-25 17:20:26 +0000134
Ian Romanickcd6ca582004-05-05 20:17:19 +0000135 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
136 glEnableClientState( GL_VERTEX_ARRAY );
Brian Paula4f0b682002-10-25 17:20:26 +0000137
Ian Romanickcd6ca582004-05-05 20:17:19 +0000138 glDrawArrays( GL_QUADS, 0, 24 );
Brian Paula4f0b682002-10-25 17:20:26 +0000139
Ian Romanickcd6ca582004-05-05 20:17:19 +0000140 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
141 glDisableClientState( GL_VERTEX_ARRAY );
142 }
143 else {
144 unsigned i;
Brian Paula4f0b682002-10-25 17:20:26 +0000145
Ian Romanickcd6ca582004-05-05 20:17:19 +0000146 glBegin(GL_QUADS);
147 for ( i = 0 ; i < 24 ; i++ ) {
148 glTexCoord3fv( & tex_coords[ i * 3 ] );
149 glVertex3fv ( & vtx_coords[ i * 3 ] );
150 }
151 glEnd();
152 }
Brian Paula4f0b682002-10-25 17:20:26 +0000153}
Brian Pauld04d2092000-05-30 01:18:29 +0000154
155
156static void draw( void )
157{
158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
159
Brian Paula4f0b682002-10-25 17:20:26 +0000160 glPushMatrix(); /*MODELVIEW*/
161 glTranslatef( 0.0, 0.0, -EyeDist );
162
163 /* skybox */
164 glDisable(GL_TEXTURE_GEN_S);
165 glDisable(GL_TEXTURE_GEN_T);
166 glDisable(GL_TEXTURE_GEN_R);
167
168 glMatrixMode(GL_MODELVIEW);
169 glPushMatrix();
170 glRotatef(Xrot, 1, 0, 0);
171 glRotatef(Yrot, 0, 1, 0);
172 draw_skybox();
173 glPopMatrix();
174
175 /* sphere */
176 glMatrixMode(GL_TEXTURE);
177 glLoadIdentity();
178 glRotatef(-Yrot, 0, 1, 0);
179 glRotatef(-Xrot, 1, 0, 0);
180
181 glEnable(GL_TEXTURE_GEN_S);
182 glEnable(GL_TEXTURE_GEN_T);
183 glEnable(GL_TEXTURE_GEN_R);
184 glutSolidSphere(2.0, 20, 20);
185
186 glLoadIdentity(); /* texture */
187
188 glMatrixMode(GL_MODELVIEW);
189 glPopMatrix();
Brian Pauld04d2092000-05-30 01:18:29 +0000190
191 glutSwapBuffers();
192}
193
194
195static void idle(void)
196{
Brian Paula4f0b682002-10-25 17:20:26 +0000197 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
198 Yrot = t;
Brian Pauld04d2092000-05-30 01:18:29 +0000199 glutPostRedisplay();
200}
201
202
203static void set_mode(GLuint mode)
204{
205 if (mode == 0) {
206 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
207 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
208 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
209 printf("GL_REFLECTION_MAP_ARB mode\n");
210 }
211 else if (mode == 1) {
212 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
213 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
214 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
215 printf("GL_NORMAL_MAP_ARB mode\n");
216 }
Brian Pauld04d2092000-05-30 01:18:29 +0000217}
218
219
220static void key(unsigned char k, int x, int y)
221{
222 static GLboolean anim = GL_TRUE;
223 static GLuint mode = 0;
224 (void) x;
225 (void) y;
226 switch (k) {
227 case ' ':
228 anim = !anim;
229 if (anim)
230 glutIdleFunc(idle);
231 else
232 glutIdleFunc(NULL);
233 break;
234 case 'm':
235 mode = !mode;
236 set_mode(mode);
237 break;
Ian Romanickcd6ca582004-05-05 20:17:19 +0000238 case 'v':
239 use_vertex_arrays = ! use_vertex_arrays;
240 printf( "Vertex arrays are %sabled\n",
241 (use_vertex_arrays) ? "en" : "dis" );
242 break;
Brian Paula4f0b682002-10-25 17:20:26 +0000243 case 'z':
244 EyeDist -= 0.5;
245 if (EyeDist < 6.0)
246 EyeDist = 6.0;
247 break;
248 case 'Z':
249 EyeDist += 0.5;
250 if (EyeDist > 90.0)
251 EyeDist = 90;
252 break;
Brian Pauld04d2092000-05-30 01:18:29 +0000253 case 27:
254 exit(0);
255 }
256 glutPostRedisplay();
257}
258
259
260static void specialkey(int key, int x, int y)
261{
Brian Paula4f0b682002-10-25 17:20:26 +0000262 GLfloat step = 5;
Brian Pauld04d2092000-05-30 01:18:29 +0000263 (void) x;
264 (void) y;
265 switch (key) {
266 case GLUT_KEY_UP:
Brian Paula4f0b682002-10-25 17:20:26 +0000267 Xrot += step;
Brian Pauld04d2092000-05-30 01:18:29 +0000268 break;
269 case GLUT_KEY_DOWN:
Brian Paula4f0b682002-10-25 17:20:26 +0000270 Xrot -= step;
Brian Pauld04d2092000-05-30 01:18:29 +0000271 break;
272 case GLUT_KEY_LEFT:
273 Yrot -= step;
274 break;
275 case GLUT_KEY_RIGHT:
276 Yrot += step;
277 break;
278 }
279 glutPostRedisplay();
280}
281
282
283/* new window size or exposure */
284static void reshape(int width, int height)
285{
Brian Paula4f0b682002-10-25 17:20:26 +0000286 GLfloat ar = (float) width / (float) height;
Brian Pauld04d2092000-05-30 01:18:29 +0000287 glViewport(0, 0, (GLint)width, (GLint)height);
288 glMatrixMode(GL_PROJECTION);
289 glLoadIdentity();
Brian Paula4f0b682002-10-25 17:20:26 +0000290 glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
Brian Pauld04d2092000-05-30 01:18:29 +0000291 glMatrixMode(GL_MODELVIEW);
292 glLoadIdentity();
Brian Pauld04d2092000-05-30 01:18:29 +0000293}
294
295
Brian Paula4f0b682002-10-25 17:20:26 +0000296static void init_checkers( void )
Brian Pauld04d2092000-05-30 01:18:29 +0000297{
298#define CUBE_TEX_SIZE 64
299 GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3];
300 static const GLubyte colors[6][3] = {
Keith Whitwell493e6e12004-02-05 17:36:02 +0000301 { 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 */
Brian Pauld04d2092000-05-30 01:18:29 +0000307 };
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
315 };
316
317 GLint i, j, f;
318
Brian Pauld04d2092000-05-30 01:18:29 +0000319 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
320
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];
329 }
330 else {
331 image[i][j][0] = 255;
332 image[i][j][1] = 255;
333 image[i][j][2] = 255;
334 }
335 }
336 }
337
338 glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
339 GL_RGB, GL_UNSIGNED_BYTE, image);
340 }
Brian Paula4f0b682002-10-25 17:20:26 +0000341}
Brian Pauld04d2092000-05-30 01:18:29 +0000342
Brian Paula4f0b682002-10-25 17:20:26 +0000343
344static void load(GLenum target, const char *filename,
345 GLboolean flipTB, GLboolean flipLR)
346{
347 GLint w, h;
348 GLenum format;
349 GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
350 if (!img) {
351 printf("Error: couldn't load texture image %s\n", filename);
352 exit(1);
353 }
354 assert(format == GL_RGB);
355
356 /* <sigh> the way the texture cube mapping works, we have to flip
357 * images to make things look right.
358 */
359 if (flipTB) {
360 const int stride = 3 * w;
361 GLubyte temp[3*1024];
362 int i;
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);
367 }
368 }
369 if (flipLR) {
370 const int stride = 3 * w;
371 GLubyte temp[3];
372 GLubyte *row;
373 int i, j;
374 for (i = 0; i < h; i++) {
375 row = img + i * stride;
376 for (j = 0; j < w / 2; j++) {
377 int k = w - j - 1;
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];
387 }
388 }
389 }
390
391 gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
392 free(img);
393}
394
395
396static void load_envmaps(void)
397{
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);
404}
405
406
407static void init( GLboolean useImageFiles )
408{
409 GLenum filter;
410
411 /* check for extension */
412 {
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");
416 exit(0);
417 }
418 }
419 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
420
421 if (useImageFiles) {
422 load_envmaps();
423 filter = GL_LINEAR;
424 }
425 else {
426 init_checkers();
427 filter = GL_NEAREST;
428 }
429
430 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, filter);
431 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, filter);
Brian Pauld27e2562000-06-13 18:45:54 +0000432 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);
Brian Pauld04d2092000-05-30 01:18:29 +0000434
435 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
Brian Paula4f0b682002-10-25 17:20:26 +0000436 glEnable(GL_DEPTH_TEST);
Brian Pauld04d2092000-05-30 01:18:29 +0000437
438 glClearColor(.3, .3, .3, 0);
439 glColor3f( 1.0, 1.0, 1.0 );
440
441 set_mode(0);
442}
443
444
445static void usage(void)
446{
447 printf("keys:\n");
448 printf(" SPACE - toggle animation\n");
449 printf(" CURSOR KEYS - rotation\n");
450 printf(" m - toggle texgen reflection mode\n");
Brian Paula4f0b682002-10-25 17:20:26 +0000451 printf(" z/Z - change viewing distance\n");
Brian Pauld04d2092000-05-30 01:18:29 +0000452}
453
454
455int main( int argc, char *argv[] )
456{
457 glutInitWindowPosition(0, 0);
Brian Paula4f0b682002-10-25 17:20:26 +0000458 glutInitWindowSize(600, 500);
Brian Pauld04d2092000-05-30 01:18:29 +0000459 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
Brian Paul0b77a1c2003-04-09 21:50:08 +0000460 glutCreateWindow("Texture Cube Mapping");
Brian Paula4f0b682002-10-25 17:20:26 +0000461
462 if (argc > 1 && strcmp(argv[1] , "-i") == 0)
463 init( 1 );
464 else
465 init( 0 );
Brian Pauld04d2092000-05-30 01:18:29 +0000466 glutReshapeFunc( reshape );
467 glutKeyboardFunc( key );
468 glutSpecialFunc( specialkey );
469 glutIdleFunc( idle );
470 glutDisplayFunc( draw );
471 usage();
472 glutMainLoop();
473 return 0;
474}