Revert "option to test mipmapping"
[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
51 #define eps1 0.99
52 #define br 20.0 /* box radius */
53
54 static const GLfloat tex_coords[] = {
55 /* +X side */
56 1.0, -eps1, -eps1,
57 1.0, -eps1, eps1,
58 1.0, eps1, eps1,
59 1.0, eps1, -eps1,
60
61 /* -X side */
62 -1.0, eps1, -eps1,
63 -1.0, eps1, eps1,
64 -1.0, -eps1, eps1,
65 -1.0, -eps1, -eps1,
66
67 /* +Y side */
68 -eps1, 1.0, -eps1,
69 -eps1, 1.0, eps1,
70 eps1, 1.0, eps1,
71 eps1, 1.0, -eps1,
72
73 /* -Y side */
74 -eps1, -1.0, -eps1,
75 -eps1, -1.0, eps1,
76 eps1, -1.0, eps1,
77 eps1, -1.0, -eps1,
78
79 /* +Z side */
80 eps1, -eps1, 1.0,
81 -eps1, -eps1, 1.0,
82 -eps1, eps1, 1.0,
83 eps1, eps1, 1.0,
84
85 /* -Z side */
86 eps1, eps1, -1.0,
87 -eps1, eps1, -1.0,
88 -eps1, -eps1, -1.0,
89 eps1, -eps1, -1.0,
90 };
91
92 static const GLfloat vtx_coords[] = {
93 /* +X side */
94 br, -br, -br,
95 br, -br, br,
96 br, br, br,
97 br, br, -br,
98
99 /* -X side */
100 -br, br, -br,
101 -br, br, br,
102 -br, -br, br,
103 -br, -br, -br,
104
105 /* +Y side */
106 -br, br, -br,
107 -br, br, br,
108 br, br, br,
109 br, br, -br,
110
111 /* -Y side */
112 -br, -br, -br,
113 -br, -br, br,
114 br, -br, br,
115 br, -br, -br,
116
117 /* +Z side */
118 br, -br, br,
119 -br, -br, br,
120 -br, br, br,
121 br, br, br,
122
123 /* -Z side */
124 br, br, -br,
125 -br, br, -br,
126 -br, -br, -br,
127 br, -br, -br,
128 };
129
130 static void draw_skybox( void )
131 {
132 if ( use_vertex_arrays ) {
133 glTexCoordPointer( 3, GL_FLOAT, 0, tex_coords );
134 glVertexPointer( 3, GL_FLOAT, 0, vtx_coords );
135
136 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
137 glEnableClientState( GL_VERTEX_ARRAY );
138
139 glDrawArrays( GL_QUADS, 0, 24 );
140
141 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
142 glDisableClientState( GL_VERTEX_ARRAY );
143 }
144 else {
145 unsigned i;
146
147 glBegin(GL_QUADS);
148 for ( i = 0 ; i < 24 ; i++ ) {
149 glTexCoord3fv( & tex_coords[ i * 3 ] );
150 glVertex3fv ( & vtx_coords[ i * 3 ] );
151 }
152 glEnd();
153 }
154 }
155
156
157 static void draw( void )
158 {
159 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
160
161 glPushMatrix(); /*MODELVIEW*/
162 glTranslatef( 0.0, 0.0, -EyeDist );
163
164 /* skybox */
165 glDisable(GL_TEXTURE_GEN_S);
166 glDisable(GL_TEXTURE_GEN_T);
167 glDisable(GL_TEXTURE_GEN_R);
168
169 glMatrixMode(GL_MODELVIEW);
170 glPushMatrix();
171 glRotatef(Xrot, 1, 0, 0);
172 glRotatef(Yrot, 0, 1, 0);
173 draw_skybox();
174 glPopMatrix();
175
176 /* sphere */
177 glMatrixMode(GL_TEXTURE);
178 glLoadIdentity();
179 glRotatef(-Yrot, 0, 1, 0);
180 glRotatef(-Xrot, 1, 0, 0);
181
182 glEnable(GL_TEXTURE_GEN_S);
183 glEnable(GL_TEXTURE_GEN_T);
184 glEnable(GL_TEXTURE_GEN_R);
185 glutSolidSphere(2.0, 20, 20);
186
187 glLoadIdentity(); /* texture */
188
189 glMatrixMode(GL_MODELVIEW);
190 glPopMatrix();
191
192 glutSwapBuffers();
193 }
194
195
196 static void idle(void)
197 {
198 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
199 Yrot = t;
200 glutPostRedisplay();
201 }
202
203
204 static void set_mode(GLuint mode)
205 {
206 if (mode == 0) {
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");
211 }
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");
217 }
218 }
219
220
221 static void key(unsigned char k, int x, int y)
222 {
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;
238 case 'v':
239 use_vertex_arrays = ! use_vertex_arrays;
240 printf( "Vertex arrays are %sabled\n",
241 (use_vertex_arrays) ? "en" : "dis" );
242 break;
243 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;
253 case 27:
254 exit(0);
255 }
256 glutPostRedisplay();
257 }
258
259
260 static void specialkey(int key, int x, int y)
261 {
262 GLfloat step = 5;
263 (void) x;
264 (void) y;
265 switch (key) {
266 case GLUT_KEY_UP:
267 Xrot += step;
268 break;
269 case GLUT_KEY_DOWN:
270 Xrot -= step;
271 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 */
284 static void reshape(int width, int height)
285 {
286 GLfloat ar = (float) width / (float) height;
287 glViewport(0, 0, (GLint)width, (GLint)height);
288 glMatrixMode(GL_PROJECTION);
289 glLoadIdentity();
290 glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
291 glMatrixMode(GL_MODELVIEW);
292 glLoadIdentity();
293 }
294
295
296 static void init_checkers( void )
297 {
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 */
307 };
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
319 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 }
341 }
342
343
344 static 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
396 static 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
407 static 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);
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);
434
435 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
436 glEnable(GL_DEPTH_TEST);
437
438 glClearColor(.3, .3, .3, 0);
439 glColor3f( 1.0, 1.0, 1.0 );
440
441 set_mode(0);
442 }
443
444
445 static 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");
451 printf(" z/Z - change viewing distance\n");
452 }
453
454
455 int main( int argc, char *argv[] )
456 {
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");
462
463 if (argc > 1 && strcmp(argv[1] , "-i") == 0)
464 init( 1 );
465 else
466 init( 0 );
467 glutReshapeFunc( reshape );
468 glutKeyboardFunc( key );
469 glutSpecialFunc( specialkey );
470 glutDisplayFunc( draw );
471 if (anim)
472 glutIdleFunc(idle);
473 usage();
474 glutMainLoop();
475 return 0;
476 }