Merge branch 'mesa_7_6_branch'
[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/glew.h>
43 #include "GL/glut.h"
44 #include "readtex.h"
45
46 #ifndef GL_TEXTURE_CUBE_MAP_SEAMLESS
47 #define GL_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
48 #endif
49
50 static GLfloat Xrot = 0, Yrot = 0;
51 static GLfloat EyeDist = 10;
52 static GLboolean use_vertex_arrays = GL_FALSE;
53 static GLboolean anim = GL_TRUE;
54 static GLboolean NoClear = GL_FALSE;
55 static GLint FrameParity = 0;
56 static GLenum FilterIndex = 0;
57 static GLint ClampIndex = 0;
58 static GLboolean supportFBO = GL_FALSE;
59 static GLboolean supportSeamless = GL_FALSE;
60 static GLboolean seamless = GL_FALSE;
61 static GLuint TexObj = 0;
62
63
64 static struct {
65 GLenum mode;
66 const char *name;
67 } ClampModes[] = {
68 { GL_CLAMP_TO_EDGE, "GL_CLAMP_TO_EDGE" },
69 { GL_CLAMP_TO_BORDER, "GL_CLAMP_TO_BORDER" },
70 { GL_CLAMP, "GL_CLAMP" },
71 { GL_REPEAT, "GL_REPEAT" }
72 };
73
74 #define NUM_CLAMP_MODES (sizeof(ClampModes) / sizeof(ClampModes[0]))
75
76
77 static struct {
78 GLenum mag_mode, min_mode;
79 const char *name;
80 } FilterModes[] = {
81 { GL_NEAREST, GL_NEAREST, "GL_NEAREST, GL_NEAREST" },
82 { GL_NEAREST, GL_LINEAR, "GL_NEAREST, GL_LINEAR" },
83 { GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST, "GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST" },
84 { GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR, "GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR" },
85 { GL_NEAREST, GL_LINEAR_MIPMAP_NEAREST, "GL_NEAREST, GL_LINEAR_MIPMAP_NEAREST" },
86 { GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR, "GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR" },
87
88 { GL_LINEAR, GL_NEAREST, "GL_LINEAR, GL_NEAREST" },
89 { GL_LINEAR, GL_LINEAR, "GL_LINEAR, GL_LINEAR" },
90 { GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, "GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST" },
91 { GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR, "GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR" },
92 { GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST, "GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST" },
93 { GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, "GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR" }
94 };
95
96 #define NUM_FILTER_MODES (sizeof(FilterModes) / sizeof(FilterModes[0]))
97
98
99
100 /* The effects of GL_ARB_seamless_cube_map don't show up unless eps1 is 1.0.
101 */
102 #define eps1 1.0 /*0.99*/
103 #define br 20.0 /* box radius */
104
105 static const GLfloat tex_coords[] = {
106 /* +X side */
107 1.0, -eps1, -eps1,
108 1.0, -eps1, eps1,
109 1.0, eps1, eps1,
110 1.0, eps1, -eps1,
111
112 /* -X side */
113 -1.0, eps1, -eps1,
114 -1.0, eps1, eps1,
115 -1.0, -eps1, eps1,
116 -1.0, -eps1, -eps1,
117
118 /* +Y side */
119 -eps1, 1.0, -eps1,
120 -eps1, 1.0, eps1,
121 eps1, 1.0, eps1,
122 eps1, 1.0, -eps1,
123
124 /* -Y side */
125 -eps1, -1.0, -eps1,
126 -eps1, -1.0, eps1,
127 eps1, -1.0, eps1,
128 eps1, -1.0, -eps1,
129
130 /* +Z side */
131 eps1, -eps1, 1.0,
132 -eps1, -eps1, 1.0,
133 -eps1, eps1, 1.0,
134 eps1, eps1, 1.0,
135
136 /* -Z side */
137 eps1, eps1, -1.0,
138 -eps1, eps1, -1.0,
139 -eps1, -eps1, -1.0,
140 eps1, -eps1, -1.0,
141 };
142
143 static const GLfloat vtx_coords[] = {
144 /* +X side */
145 br, -br, -br,
146 br, -br, br,
147 br, br, br,
148 br, br, -br,
149
150 /* -X side */
151 -br, br, -br,
152 -br, br, br,
153 -br, -br, br,
154 -br, -br, -br,
155
156 /* +Y side */
157 -br, br, -br,
158 -br, br, br,
159 br, br, br,
160 br, br, -br,
161
162 /* -Y side */
163 -br, -br, -br,
164 -br, -br, br,
165 br, -br, br,
166 br, -br, -br,
167
168 /* +Z side */
169 br, -br, br,
170 -br, -br, br,
171 -br, br, br,
172 br, br, br,
173
174 /* -Z side */
175 br, br, -br,
176 -br, br, -br,
177 -br, -br, -br,
178 br, -br, -br,
179 };
180
181 static void draw_skybox( void )
182 {
183 if ( use_vertex_arrays ) {
184 glTexCoordPointer( 3, GL_FLOAT, 0, tex_coords );
185 glVertexPointer( 3, GL_FLOAT, 0, vtx_coords );
186
187 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
188 glEnableClientState( GL_VERTEX_ARRAY );
189
190 glDrawArrays( GL_QUADS, 0, 24 );
191
192 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
193 glDisableClientState( GL_VERTEX_ARRAY );
194 }
195 else {
196 unsigned i;
197
198 glBegin(GL_QUADS);
199 for ( i = 0 ; i < 24 ; i++ ) {
200 glTexCoord3fv( & tex_coords[ i * 3 ] );
201 glVertex3fv ( & vtx_coords[ i * 3 ] );
202 }
203 glEnd();
204 }
205 }
206
207
208 static void draw( void )
209 {
210 GLenum wrap;
211
212 if (NoClear) {
213 /* This demonstrates how we can avoid calling glClear.
214 * This method only works if every pixel in the window is painted for
215 * every frame.
216 * We can simply skip clearing of the color buffer in this case.
217 * For the depth buffer, we alternately use a different subrange of
218 * the depth buffer for each frame. For the odd frame use the range
219 * [0, 0.5] with GL_LESS. For the even frames, use the range [1, 0.5]
220 * with GL_GREATER.
221 */
222 FrameParity = 1 - FrameParity;
223 if (FrameParity) {
224 glDepthRange(0.0, 0.5);
225 glDepthFunc(GL_LESS);
226 }
227 else {
228 glDepthRange(1.0, 0.5);
229 glDepthFunc(GL_GREATER);
230 }
231 }
232 else {
233 /* ordinary clearing */
234 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
235 }
236
237 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER,
238 FilterModes[FilterIndex].min_mode);
239 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER,
240 FilterModes[FilterIndex].mag_mode);
241
242 if (supportSeamless) {
243 if (seamless) {
244 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
245 } else {
246 glDisable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
247 }
248 }
249 wrap = ClampModes[ClampIndex].mode;
250 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, wrap);
251 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, wrap);
252 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, wrap);
253
254 glPushMatrix(); /*MODELVIEW*/
255 glTranslatef( 0.0, 0.0, -EyeDist );
256
257 /* skybox */
258 glDisable(GL_TEXTURE_GEN_S);
259 glDisable(GL_TEXTURE_GEN_T);
260 glDisable(GL_TEXTURE_GEN_R);
261
262 glMatrixMode(GL_MODELVIEW);
263 glPushMatrix();
264 glRotatef(Xrot, 1, 0, 0);
265 glRotatef(Yrot, 0, 1, 0);
266 draw_skybox();
267 glPopMatrix();
268
269 /* sphere */
270 glMatrixMode(GL_TEXTURE);
271 glLoadIdentity();
272 glRotatef(-Yrot, 0, 1, 0);
273 glRotatef(-Xrot, 1, 0, 0);
274
275 glEnable(GL_TEXTURE_GEN_S);
276 glEnable(GL_TEXTURE_GEN_T);
277 glEnable(GL_TEXTURE_GEN_R);
278 glutSolidSphere(2.0, 20, 20);
279
280 glLoadIdentity(); /* texture */
281
282 glMatrixMode(GL_MODELVIEW);
283 glPopMatrix();
284
285 glutSwapBuffers();
286 }
287
288
289 static void idle(void)
290 {
291 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
292 Yrot = t;
293 glutPostRedisplay();
294 }
295
296
297 static void set_mode(GLuint mode)
298 {
299 if (mode == 0) {
300 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
301 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
302 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
303 printf("GL_REFLECTION_MAP_ARB mode\n");
304 }
305 else if (mode == 1) {
306 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
307 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
308 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
309 printf("GL_NORMAL_MAP_ARB mode\n");
310 }
311 }
312
313
314 static void key(unsigned char k, int x, int y)
315 {
316 static GLuint mode = 0;
317 (void) x;
318 (void) y;
319 switch (k) {
320 case ' ':
321 anim = !anim;
322 if (anim)
323 glutIdleFunc(idle);
324 else
325 glutIdleFunc(NULL);
326 break;
327 case 'f':
328 FilterIndex = (FilterIndex + 1) % NUM_FILTER_MODES;
329 printf("Tex filter: %s\n", FilterModes[FilterIndex].name);
330 break;
331 case 'c':
332 ClampIndex = (ClampIndex + 1) % NUM_CLAMP_MODES;
333 printf("Tex wrap mode: %s\n", ClampModes[ClampIndex].name);
334 break;
335 case 'm':
336 mode = !mode;
337 set_mode(mode);
338 break;
339 case 's':
340 seamless = ! seamless;
341 printf("Seamless cube map filtering is %sabled\n",
342 (seamless) ? "en" : "dis" );
343 break;
344 case 'v':
345 use_vertex_arrays = ! use_vertex_arrays;
346 printf( "Vertex arrays are %sabled\n",
347 (use_vertex_arrays) ? "en" : "dis" );
348 break;
349 case 'z':
350 EyeDist -= 0.5;
351 if (EyeDist < 6.0)
352 EyeDist = 6.0;
353 break;
354 case 'Z':
355 EyeDist += 0.5;
356 if (EyeDist > 90.0)
357 EyeDist = 90;
358 break;
359 case 27:
360 exit(0);
361 }
362 glutPostRedisplay();
363 }
364
365
366 static void specialkey(int key, int x, int y)
367 {
368 GLfloat step = 5;
369 (void) x;
370 (void) y;
371 switch (key) {
372 case GLUT_KEY_UP:
373 Xrot += step;
374 break;
375 case GLUT_KEY_DOWN:
376 Xrot -= step;
377 break;
378 case GLUT_KEY_LEFT:
379 Yrot -= step;
380 break;
381 case GLUT_KEY_RIGHT:
382 Yrot += step;
383 break;
384 }
385 glutPostRedisplay();
386 }
387
388
389 /* new window size or exposure */
390 static void reshape(int width, int height)
391 {
392 GLfloat ar = (float) width / (float) height;
393 glViewport(0, 0, (GLint)width, (GLint)height);
394 glMatrixMode(GL_PROJECTION);
395 glLoadIdentity();
396 glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
397 glMatrixMode(GL_MODELVIEW);
398 glLoadIdentity();
399 }
400
401
402 static void init_checkers( void )
403 {
404 #define CUBE_TEX_SIZE 64
405 GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][4];
406 static const GLubyte colors[6][3] = {
407 { 255, 0, 0 }, /* face 0 - red */
408 { 0, 255, 255 }, /* face 1 - cyan */
409 { 0, 255, 0 }, /* face 2 - green */
410 { 255, 0, 255 }, /* face 3 - purple */
411 { 0, 0, 255 }, /* face 4 - blue */
412 { 255, 255, 0 } /* face 5 - yellow */
413 };
414 static const GLenum targets[6] = {
415 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
416 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
417 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
418 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
419 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
420 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
421 };
422
423 GLint i, j, f;
424
425 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
426
427 if (!supportFBO)
428 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
429
430
431 /* make colored checkerboard cube faces */
432 for (f = 0; f < 6; f++) {
433 for (i = 0; i < CUBE_TEX_SIZE; i++) {
434 for (j = 0; j < CUBE_TEX_SIZE; j++) {
435 if ((i/4 + j/4) & 1) {
436 image[i][j][0] = colors[f][2];
437 image[i][j][1] = colors[f][1];
438 image[i][j][2] = colors[f][0];
439 image[i][j][3] = 255;
440 }
441 else {
442 image[i][j][0] = 255;
443 image[i][j][1] = 255;
444 image[i][j][2] = 255;
445 image[i][j][3] = 255;
446 }
447 }
448 }
449
450 glTexImage2D(targets[f], 0, GL_RGBA8, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
451 GL_BGRA, GL_UNSIGNED_BYTE, image);
452 }
453
454 if (supportFBO)
455 glGenerateMipmapEXT(GL_TEXTURE_CUBE_MAP_ARB);
456 }
457
458
459 static void load(GLenum target, const char *filename,
460 GLboolean flipTB, GLboolean flipLR)
461 {
462 GLint w, h;
463 GLenum format;
464 GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
465 if (!img) {
466 printf("Error: couldn't load texture image %s\n", filename);
467 exit(1);
468 }
469 assert(format == GL_RGB);
470
471 /* <sigh> the way the texture cube mapping works, we have to flip
472 * images to make things look right.
473 */
474 if (flipTB) {
475 const int stride = 3 * w;
476 GLubyte temp[3*1024];
477 int i;
478 for (i = 0; i < h / 2; i++) {
479 memcpy(temp, img + i * stride, stride);
480 memcpy(img + i * stride, img + (h - i - 1) * stride, stride);
481 memcpy(img + (h - i - 1) * stride, temp, stride);
482 }
483 }
484 if (flipLR) {
485 const int stride = 3 * w;
486 GLubyte temp[3];
487 GLubyte *row;
488 int i, j;
489 for (i = 0; i < h; i++) {
490 row = img + i * stride;
491 for (j = 0; j < w / 2; j++) {
492 int k = w - j - 1;
493 temp[0] = row[j*3+0];
494 temp[1] = row[j*3+1];
495 temp[2] = row[j*3+2];
496 row[j*3+0] = row[k*3+0];
497 row[j*3+1] = row[k*3+1];
498 row[j*3+2] = row[k*3+2];
499 row[k*3+0] = temp[0];
500 row[k*3+1] = temp[1];
501 row[k*3+2] = temp[2];
502 }
503 }
504 }
505
506 gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
507 free(img);
508 }
509
510
511 static void load_envmaps(void)
512 {
513 load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, "right.rgb", GL_TRUE, GL_FALSE);
514 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, "left.rgb", GL_TRUE, GL_FALSE);
515 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, "top.rgb", GL_FALSE, GL_TRUE);
516 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, "bottom.rgb", GL_FALSE, GL_TRUE);
517 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, "front.rgb", GL_TRUE, GL_FALSE);
518 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, "back.rgb", GL_TRUE, GL_FALSE);
519 }
520
521
522 static void init( GLboolean useImageFiles )
523 {
524 /* check for extensions */
525 if (!GLEW_ARB_texture_cube_map) {
526 printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
527 exit(0);
528 }
529
530 /* Needed for glGenerateMipmapEXT / auto mipmapping
531 */
532 supportFBO = GLEW_EXT_framebuffer_object;
533
534 if (!supportFBO && !GLEW_SGIS_generate_mipmap) {
535 printf("Sorry, this demo requires GL_EXT_framebuffer_object or "
536 "GL_SGIS_generate_mipmap\n");
537 exit(0);
538 }
539
540 /* GLEW doesn't know about this extension yet, so use the old GLUT function
541 * to check for availability.
542 */
543 supportSeamless = glutExtensionSupported("GL_ARB_seamless_cube_map");
544
545 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
546
547
548 glGenTextures(1, &TexObj);
549 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, TexObj);
550
551 if (useImageFiles) {
552 load_envmaps();
553 }
554 else {
555 init_checkers();
556 }
557
558 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
559 glEnable(GL_DEPTH_TEST);
560
561 glClearColor(.3, .3, .3, 0);
562 glColor3f( 1.0, 1.0, 1.0 );
563
564 set_mode(0);
565 }
566
567
568 static void usage(void)
569 {
570 printf("keys:\n");
571 printf(" SPACE - toggle animation\n");
572 printf(" CURSOR KEYS - rotation\n");
573 printf(" c - toggle texture clamp/wrap mode\n");
574 printf(" f - toggle texture filter mode\n");
575 printf(" m - toggle texgen reflection mode\n");
576 printf(" z/Z - change viewing distance\n");
577 }
578
579
580 static void parse_args(int argc, char *argv[])
581 {
582 int initFlag = 0;
583 int i;
584
585 for (i = 1; i < argc; i++) {
586 if (strcmp(argv[i], "-i") == 0)
587 initFlag = 1;
588 else if (strcmp(argv[i], "--noclear") == 0)
589 NoClear = GL_TRUE;
590 else {
591 fprintf(stderr, "Bad option: %s\n", argv[i]);
592 exit(1);
593 }
594 }
595 init (initFlag);
596 }
597
598 int main( int argc, char *argv[] )
599 {
600 glutInit(&argc, argv);
601 glutInitWindowPosition(0, 0);
602 glutInitWindowSize(600, 500);
603 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
604 glutCreateWindow("Texture Cube Mapping");
605 glewInit();
606 glutReshapeFunc( reshape );
607 glutKeyboardFunc( key );
608 glutSpecialFunc( specialkey );
609 glutDisplayFunc( draw );
610 if (anim)
611 glutIdleFunc(idle);
612 parse_args(argc, argv);
613 usage();
614 glutMainLoop();
615 return 0;
616 }