5818337c12679dc61592e461dafe817ffcefa086
[mesa.git] / progs / demos / cubemap.c
1 /* $Id: cubemap.c,v 1.4 2002/10/25 17:20:26 brianp Exp $ */
2
3 /*
4 * GL_ARB_texture_cube_map demo
5 *
6 * Brian Paul
7 * May 2000
8 *
9 *
10 * Copyright (C) 2000 Brian Paul All Rights Reserved.
11 *
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:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
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.
28 */
29
30
31 /*
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).
36 */
37
38
39 #include <assert.h>
40 #include <math.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include "GL/glut.h"
45 #include "../util/readtex.c" /* a hack */
46
47
48 static GLfloat Xrot = 0, Yrot = 0;
49 static GLfloat EyeDist = 10;
50
51
52 static void draw_skybox( void )
53 {
54 const GLfloat eps1 = 0.99;
55 const GLfloat br = 20.0; /* box radius */
56
57 glBegin(GL_QUADS);
58
59 /* +X side */
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);
64
65 /* -X side */
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);
70
71 /* +Y side */
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);
76
77 /* -Y side */
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);
82
83 /* +Z side */
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);
88
89 /* -Z side */
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);
94
95 glEnd();
96 }
97
98
99 static void draw( void )
100 {
101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
102
103 glPushMatrix(); /*MODELVIEW*/
104 glTranslatef( 0.0, 0.0, -EyeDist );
105
106 /* skybox */
107 glDisable(GL_TEXTURE_GEN_S);
108 glDisable(GL_TEXTURE_GEN_T);
109 glDisable(GL_TEXTURE_GEN_R);
110
111 glMatrixMode(GL_MODELVIEW);
112 glPushMatrix();
113 glRotatef(Xrot, 1, 0, 0);
114 glRotatef(Yrot, 0, 1, 0);
115 draw_skybox();
116 glPopMatrix();
117
118 /* sphere */
119 glMatrixMode(GL_TEXTURE);
120 glLoadIdentity();
121 glRotatef(-Yrot, 0, 1, 0);
122 glRotatef(-Xrot, 1, 0, 0);
123
124 glEnable(GL_TEXTURE_GEN_S);
125 glEnable(GL_TEXTURE_GEN_T);
126 glEnable(GL_TEXTURE_GEN_R);
127 glutSolidSphere(2.0, 20, 20);
128
129 glLoadIdentity(); /* texture */
130
131 glMatrixMode(GL_MODELVIEW);
132 glPopMatrix();
133
134 glutSwapBuffers();
135 }
136
137
138 static void idle(void)
139 {
140 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
141 Yrot = t;
142 glutPostRedisplay();
143 }
144
145
146 static void set_mode(GLuint mode)
147 {
148 if (mode == 0) {
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");
153 }
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");
159 }
160 }
161
162
163 static void key(unsigned char k, int x, int y)
164 {
165 static GLboolean anim = GL_TRUE;
166 static GLuint mode = 0;
167 (void) x;
168 (void) y;
169 switch (k) {
170 case ' ':
171 anim = !anim;
172 if (anim)
173 glutIdleFunc(idle);
174 else
175 glutIdleFunc(NULL);
176 break;
177 case 'm':
178 mode = !mode;
179 set_mode(mode);
180 break;
181 case 'z':
182 EyeDist -= 0.5;
183 if (EyeDist < 6.0)
184 EyeDist = 6.0;
185 break;
186 case 'Z':
187 EyeDist += 0.5;
188 if (EyeDist > 90.0)
189 EyeDist = 90;
190 break;
191 case 27:
192 exit(0);
193 }
194 glutPostRedisplay();
195 }
196
197
198 static void specialkey(int key, int x, int y)
199 {
200 GLfloat step = 5;
201 (void) x;
202 (void) y;
203 switch (key) {
204 case GLUT_KEY_UP:
205 Xrot += step;
206 break;
207 case GLUT_KEY_DOWN:
208 Xrot -= step;
209 break;
210 case GLUT_KEY_LEFT:
211 Yrot -= step;
212 break;
213 case GLUT_KEY_RIGHT:
214 Yrot += step;
215 break;
216 }
217 glutPostRedisplay();
218 }
219
220
221 /* new window size or exposure */
222 static void reshape(int width, int height)
223 {
224 GLfloat ar = (float) width / (float) height;
225 glViewport(0, 0, (GLint)width, (GLint)height);
226 glMatrixMode(GL_PROJECTION);
227 glLoadIdentity();
228 glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
229 glMatrixMode(GL_MODELVIEW);
230 glLoadIdentity();
231 }
232
233
234 static void init_checkers( void )
235 {
236 #define CUBE_TEX_SIZE 64
237 GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3];
238 static const GLubyte colors[6][3] = {
239 { 255, 0, 0 },
240 { 0, 255, 255 },
241 { 0, 255, 0 },
242 { 255, 0, 255 },
243 { 0, 0, 255 },
244 { 255, 255, 0 }
245 };
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
253 };
254
255 GLint i, j, f;
256
257 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
258
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];
267 }
268 else {
269 image[i][j][0] = 255;
270 image[i][j][1] = 255;
271 image[i][j][2] = 255;
272 }
273 }
274 }
275
276 glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
277 GL_RGB, GL_UNSIGNED_BYTE, image);
278 }
279 }
280
281
282 static void load(GLenum target, const char *filename,
283 GLboolean flipTB, GLboolean flipLR)
284 {
285 GLint w, h;
286 GLenum format;
287 GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
288 if (!img) {
289 printf("Error: couldn't load texture image %s\n", filename);
290 exit(1);
291 }
292 assert(format == GL_RGB);
293
294 /* <sigh> the way the texture cube mapping works, we have to flip
295 * images to make things look right.
296 */
297 if (flipTB) {
298 const int stride = 3 * w;
299 GLubyte temp[3*1024];
300 int i;
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);
305 }
306 }
307 if (flipLR) {
308 const int stride = 3 * w;
309 GLubyte temp[3];
310 GLubyte *row;
311 int i, j;
312 for (i = 0; i < h; i++) {
313 row = img + i * stride;
314 for (j = 0; j < w / 2; j++) {
315 int k = w - j - 1;
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];
325 }
326 }
327 }
328
329 gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
330 free(img);
331 }
332
333
334 static void load_envmaps(void)
335 {
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);
342 }
343
344
345 static void init( GLboolean useImageFiles )
346 {
347 GLenum filter;
348
349 /* check for extension */
350 {
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");
354 exit(0);
355 }
356 }
357 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
358
359 if (useImageFiles) {
360 load_envmaps();
361 filter = GL_LINEAR;
362 }
363 else {
364 init_checkers();
365 filter = GL_NEAREST;
366 }
367
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);
372
373 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
374 glEnable(GL_DEPTH_TEST);
375
376 glClearColor(.3, .3, .3, 0);
377 glColor3f( 1.0, 1.0, 1.0 );
378
379 set_mode(0);
380 }
381
382
383 static void usage(void)
384 {
385 printf("keys:\n");
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");
390 }
391
392
393 int main( int argc, char *argv[] )
394 {
395 glutInitWindowPosition(0, 0);
396 glutInitWindowSize(600, 500);
397 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
398 glutCreateWindow("Texture Cube Maping");
399
400 if (argc > 1 && strcmp(argv[1] , "-i") == 0)
401 init( 1 );
402 else
403 init( 0 );
404 glutReshapeFunc( reshape );
405 glutKeyboardFunc( key );
406 glutSpecialFunc( specialkey );
407 glutIdleFunc( idle );
408 glutDisplayFunc( draw );
409 usage();
410 glutMainLoop();
411 return 0;
412 }