Merge commit 'origin/master' into gallium-0.2
[mesa.git] / progs / demos / cubemap.c
1 /*
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
37 #include <assert.h>
38 #include <math.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include "GL/glut.h"
43 #include "readtex.h"
44
45
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;
52
53 #define eps1 0.99
54 #define br 20.0 /* box radius */
55
56 static const GLfloat tex_coords[] = {
57 /* +X side */
58 1.0, -eps1, -eps1,
59 1.0, -eps1, eps1,
60 1.0, eps1, eps1,
61 1.0, eps1, -eps1,
62
63 /* -X side */
64 -1.0, eps1, -eps1,
65 -1.0, eps1, eps1,
66 -1.0, -eps1, eps1,
67 -1.0, -eps1, -eps1,
68
69 /* +Y side */
70 -eps1, 1.0, -eps1,
71 -eps1, 1.0, eps1,
72 eps1, 1.0, eps1,
73 eps1, 1.0, -eps1,
74
75 /* -Y side */
76 -eps1, -1.0, -eps1,
77 -eps1, -1.0, eps1,
78 eps1, -1.0, eps1,
79 eps1, -1.0, -eps1,
80
81 /* +Z side */
82 eps1, -eps1, 1.0,
83 -eps1, -eps1, 1.0,
84 -eps1, eps1, 1.0,
85 eps1, eps1, 1.0,
86
87 /* -Z side */
88 eps1, eps1, -1.0,
89 -eps1, eps1, -1.0,
90 -eps1, -eps1, -1.0,
91 eps1, -eps1, -1.0,
92 };
93
94 static const GLfloat vtx_coords[] = {
95 /* +X side */
96 br, -br, -br,
97 br, -br, br,
98 br, br, br,
99 br, br, -br,
100
101 /* -X side */
102 -br, br, -br,
103 -br, br, br,
104 -br, -br, br,
105 -br, -br, -br,
106
107 /* +Y side */
108 -br, br, -br,
109 -br, br, br,
110 br, br, br,
111 br, br, -br,
112
113 /* -Y side */
114 -br, -br, -br,
115 -br, -br, br,
116 br, -br, br,
117 br, -br, -br,
118
119 /* +Z side */
120 br, -br, br,
121 -br, -br, br,
122 -br, br, br,
123 br, br, br,
124
125 /* -Z side */
126 br, br, -br,
127 -br, br, -br,
128 -br, -br, -br,
129 br, -br, -br,
130 };
131
132 static void draw_skybox( void )
133 {
134 if ( use_vertex_arrays ) {
135 glTexCoordPointer( 3, GL_FLOAT, 0, tex_coords );
136 glVertexPointer( 3, GL_FLOAT, 0, vtx_coords );
137
138 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
139 glEnableClientState( GL_VERTEX_ARRAY );
140
141 glDrawArrays( GL_QUADS, 0, 24 );
142
143 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
144 glDisableClientState( GL_VERTEX_ARRAY );
145 }
146 else {
147 unsigned i;
148
149 glBegin(GL_QUADS);
150 for ( i = 0 ; i < 24 ; i++ ) {
151 glTexCoord3fv( & tex_coords[ i * 3 ] );
152 glVertex3fv ( & vtx_coords[ i * 3 ] );
153 }
154 glEnd();
155 }
156 }
157
158
159 static void draw( void )
160 {
161 if (NoClear) {
162 /* This demonstrates how we can avoid calling glClear.
163 * This method only works if every pixel in the window is painted for
164 * every frame.
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]
169 * with GL_GREATER.
170 */
171 FrameParity = 1 - FrameParity;
172 if (FrameParity) {
173 glDepthRange(0.0, 0.5);
174 glDepthFunc(GL_LESS);
175 }
176 else {
177 glDepthRange(1.0, 0.5);
178 glDepthFunc(GL_GREATER);
179 }
180 }
181 else {
182 /* ordinary clearing */
183 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
184 }
185
186 glPushMatrix(); /*MODELVIEW*/
187 glTranslatef( 0.0, 0.0, -EyeDist );
188
189 /* skybox */
190 glDisable(GL_TEXTURE_GEN_S);
191 glDisable(GL_TEXTURE_GEN_T);
192 glDisable(GL_TEXTURE_GEN_R);
193
194 glMatrixMode(GL_MODELVIEW);
195 glPushMatrix();
196 glRotatef(Xrot, 1, 0, 0);
197 glRotatef(Yrot, 0, 1, 0);
198 draw_skybox();
199 glPopMatrix();
200
201 /* sphere */
202 glMatrixMode(GL_TEXTURE);
203 glLoadIdentity();
204 glRotatef(-Yrot, 0, 1, 0);
205 glRotatef(-Xrot, 1, 0, 0);
206
207 glEnable(GL_TEXTURE_GEN_S);
208 glEnable(GL_TEXTURE_GEN_T);
209 glEnable(GL_TEXTURE_GEN_R);
210 glutSolidSphere(2.0, 20, 20);
211
212 glLoadIdentity(); /* texture */
213
214 glMatrixMode(GL_MODELVIEW);
215 glPopMatrix();
216
217 glutSwapBuffers();
218 }
219
220
221 static void idle(void)
222 {
223 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
224 Yrot = t;
225 glutPostRedisplay();
226 }
227
228
229 static void set_mode(GLuint mode)
230 {
231 if (mode == 0) {
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");
236 }
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");
242 }
243 }
244
245
246 static void key(unsigned char k, int x, int y)
247 {
248 static GLuint mode = 0;
249 (void) x;
250 (void) y;
251 switch (k) {
252 case ' ':
253 anim = !anim;
254 if (anim)
255 glutIdleFunc(idle);
256 else
257 glutIdleFunc(NULL);
258 break;
259 case 'm':
260 mode = !mode;
261 set_mode(mode);
262 break;
263 case 'v':
264 use_vertex_arrays = ! use_vertex_arrays;
265 printf( "Vertex arrays are %sabled\n",
266 (use_vertex_arrays) ? "en" : "dis" );
267 break;
268 case 'z':
269 EyeDist -= 0.5;
270 if (EyeDist < 6.0)
271 EyeDist = 6.0;
272 break;
273 case 'Z':
274 EyeDist += 0.5;
275 if (EyeDist > 90.0)
276 EyeDist = 90;
277 break;
278 case 27:
279 exit(0);
280 }
281 glutPostRedisplay();
282 }
283
284
285 static void specialkey(int key, int x, int y)
286 {
287 GLfloat step = 5;
288 (void) x;
289 (void) y;
290 switch (key) {
291 case GLUT_KEY_UP:
292 Xrot += step;
293 break;
294 case GLUT_KEY_DOWN:
295 Xrot -= step;
296 break;
297 case GLUT_KEY_LEFT:
298 Yrot -= step;
299 break;
300 case GLUT_KEY_RIGHT:
301 Yrot += step;
302 break;
303 }
304 glutPostRedisplay();
305 }
306
307
308 /* new window size or exposure */
309 static void reshape(int width, int height)
310 {
311 GLfloat ar = (float) width / (float) height;
312 glViewport(0, 0, (GLint)width, (GLint)height);
313 glMatrixMode(GL_PROJECTION);
314 glLoadIdentity();
315 glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
316 glMatrixMode(GL_MODELVIEW);
317 glLoadIdentity();
318 }
319
320
321 static void init_checkers( void )
322 {
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 */
332 };
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
340 };
341
342 GLint i, j, f;
343
344 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
345
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];
354 }
355 else {
356 image[i][j][0] = 255;
357 image[i][j][1] = 255;
358 image[i][j][2] = 255;
359 }
360 }
361 }
362
363 glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
364 GL_RGB, GL_UNSIGNED_BYTE, image);
365 }
366 }
367
368
369 static void load(GLenum target, const char *filename,
370 GLboolean flipTB, GLboolean flipLR)
371 {
372 GLint w, h;
373 GLenum format;
374 GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
375 if (!img) {
376 printf("Error: couldn't load texture image %s\n", filename);
377 exit(1);
378 }
379 assert(format == GL_RGB);
380
381 /* <sigh> the way the texture cube mapping works, we have to flip
382 * images to make things look right.
383 */
384 if (flipTB) {
385 const int stride = 3 * w;
386 GLubyte temp[3*1024];
387 int i;
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);
392 }
393 }
394 if (flipLR) {
395 const int stride = 3 * w;
396 GLubyte temp[3];
397 GLubyte *row;
398 int i, j;
399 for (i = 0; i < h; i++) {
400 row = img + i * stride;
401 for (j = 0; j < w / 2; j++) {
402 int k = w - j - 1;
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];
412 }
413 }
414 }
415
416 gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
417 free(img);
418 }
419
420
421 static void load_envmaps(void)
422 {
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);
429 }
430
431
432 static void init( GLboolean useImageFiles )
433 {
434 GLenum filter;
435
436 /* check for extension */
437 {
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");
441 exit(0);
442 }
443 }
444 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
445
446 if (useImageFiles) {
447 load_envmaps();
448 filter = GL_LINEAR;
449 }
450 else {
451 init_checkers();
452 filter = GL_NEAREST;
453 }
454
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);
459
460 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
461 glEnable(GL_DEPTH_TEST);
462
463 glClearColor(.3, .3, .3, 0);
464 glColor3f( 1.0, 1.0, 1.0 );
465
466 set_mode(0);
467 }
468
469
470 static void usage(void)
471 {
472 printf("keys:\n");
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");
477 }
478
479
480 static void parse_args(int argc, char *argv[])
481 {
482 int initFlag = 0;
483 int i;
484
485 for (i = 1; i < argc; i++) {
486 if (strcmp(argv[i], "-i") == 0)
487 initFlag = 1;
488 else if (strcmp(argv[i], "--noclear") == 0)
489 NoClear = GL_TRUE;
490 else {
491 fprintf(stderr, "Bad option: %s\n", argv[i]);
492 exit(1);
493 }
494 }
495 init (initFlag);
496 }
497
498 int main( int argc, char *argv[] )
499 {
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 );
509 if (anim)
510 glutIdleFunc(idle);
511 parse_args(argc, argv);
512 usage();
513 glutMainLoop();
514 return 0;
515 }