blob: 3e79d6a55890ce513ee6a0425a459c7642b3196c [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>
Brian Paul0e28e1a2009-03-09 11:49:46 -060042#include <GL/glew.h>
Brian Pauld04d2092000-05-30 01:18:29 +000043#include "GL/glut.h"
Brian Paul0fe7f402005-01-09 17:06:22 +000044#include "readtex.h"
Brian Paula4f0b682002-10-25 17:20:26 +000045
Ian Romanick8b0b3352009-08-14 16:26:59 -070046#ifndef GL_TEXTURE_CUBE_MAP_SEAMLESS
47#define GL_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
48#endif
Brian Pauld04d2092000-05-30 01:18:29 +000049
50static GLfloat Xrot = 0, Yrot = 0;
Brian Paula4f0b682002-10-25 17:20:26 +000051static GLfloat EyeDist = 10;
Ian Romanickcd6ca582004-05-05 20:17:19 +000052static GLboolean use_vertex_arrays = GL_FALSE;
Brian Paul52a5cc02004-08-10 15:39:00 +000053static GLboolean anim = GL_TRUE;
Brian3a484cd2007-08-27 12:00:19 -060054static GLboolean NoClear = GL_FALSE;
55static GLint FrameParity = 0;
Brian Paul0e28e1a2009-03-09 11:49:46 -060056static GLenum FilterIndex = 0;
57static GLint ClampIndex = 0;
Roland Scheidegger7a5c5b92009-06-20 00:24:03 +020058static GLboolean supportFBO = GL_FALSE;
Ian Romanick8b0b3352009-08-14 16:26:59 -070059static GLboolean supportSeamless = GL_FALSE;
60static GLboolean seamless = GL_FALSE;
Brian Paul0b0fc402009-09-15 17:13:49 -060061static GLuint TexObj = 0;
Keith Whitwell75312b62009-08-23 19:02:17 +010062static GLint T0 = 0;
63static GLint Frames = 0;
Brian Paul0e28e1a2009-03-09 11:49:46 -060064
65
66static struct {
67 GLenum mode;
68 const char *name;
69} ClampModes[] = {
70 { GL_CLAMP_TO_EDGE, "GL_CLAMP_TO_EDGE" },
71 { GL_CLAMP_TO_BORDER, "GL_CLAMP_TO_BORDER" },
72 { GL_CLAMP, "GL_CLAMP" },
73 { GL_REPEAT, "GL_REPEAT" }
74};
75
76#define NUM_CLAMP_MODES (sizeof(ClampModes) / sizeof(ClampModes[0]))
77
78
79static struct {
80 GLenum mag_mode, min_mode;
81 const char *name;
82} FilterModes[] = {
83 { GL_NEAREST, GL_NEAREST, "GL_NEAREST, GL_NEAREST" },
84 { GL_NEAREST, GL_LINEAR, "GL_NEAREST, GL_LINEAR" },
85 { GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST, "GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST" },
86 { GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR, "GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR" },
87 { GL_NEAREST, GL_LINEAR_MIPMAP_NEAREST, "GL_NEAREST, GL_LINEAR_MIPMAP_NEAREST" },
88 { GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR, "GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR" },
89
90 { GL_LINEAR, GL_NEAREST, "GL_LINEAR, GL_NEAREST" },
91 { GL_LINEAR, GL_LINEAR, "GL_LINEAR, GL_LINEAR" },
92 { GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, "GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST" },
93 { GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR, "GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR" },
94 { GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST, "GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST" },
95 { GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, "GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR" }
96};
97
98#define NUM_FILTER_MODES (sizeof(FilterModes) / sizeof(FilterModes[0]))
99
100
Brian Paula4f0b682002-10-25 17:20:26 +0000101
Ian Romanick8b0b3352009-08-14 16:26:59 -0700102/* The effects of GL_ARB_seamless_cube_map don't show up unless eps1 is 1.0.
103 */
104#define eps1 1.0 /*0.99*/
Ian Romanickcd6ca582004-05-05 20:17:19 +0000105#define br 20.0 /* box radius */
106
107static const GLfloat tex_coords[] = {
108 /* +X side */
109 1.0, -eps1, -eps1,
110 1.0, -eps1, eps1,
111 1.0, eps1, eps1,
112 1.0, eps1, -eps1,
113
114 /* -X side */
115 -1.0, eps1, -eps1,
116 -1.0, eps1, eps1,
117 -1.0, -eps1, eps1,
118 -1.0, -eps1, -eps1,
119
120 /* +Y side */
121 -eps1, 1.0, -eps1,
122 -eps1, 1.0, eps1,
123 eps1, 1.0, eps1,
124 eps1, 1.0, -eps1,
125
126 /* -Y side */
127 -eps1, -1.0, -eps1,
128 -eps1, -1.0, eps1,
129 eps1, -1.0, eps1,
130 eps1, -1.0, -eps1,
131
132 /* +Z side */
133 eps1, -eps1, 1.0,
134 -eps1, -eps1, 1.0,
135 -eps1, eps1, 1.0,
136 eps1, eps1, 1.0,
137
138 /* -Z side */
139 eps1, eps1, -1.0,
140 -eps1, eps1, -1.0,
141 -eps1, -eps1, -1.0,
142 eps1, -eps1, -1.0,
143};
144
145static const GLfloat vtx_coords[] = {
146 /* +X side */
147 br, -br, -br,
148 br, -br, br,
149 br, br, br,
150 br, br, -br,
151
152 /* -X side */
153 -br, br, -br,
154 -br, br, br,
155 -br, -br, br,
156 -br, -br, -br,
157
158 /* +Y side */
159 -br, br, -br,
160 -br, br, br,
161 br, br, br,
162 br, br, -br,
163
164 /* -Y side */
165 -br, -br, -br,
166 -br, -br, br,
167 br, -br, br,
168 br, -br, -br,
169
170 /* +Z side */
171 br, -br, br,
172 -br, -br, br,
173 -br, br, br,
174 br, br, br,
175
176 /* -Z side */
177 br, br, -br,
178 -br, br, -br,
179 -br, -br, -br,
180 br, -br, -br,
181};
Brian Paula4f0b682002-10-25 17:20:26 +0000182
183static void draw_skybox( void )
184{
Ian Romanickcd6ca582004-05-05 20:17:19 +0000185 if ( use_vertex_arrays ) {
186 glTexCoordPointer( 3, GL_FLOAT, 0, tex_coords );
187 glVertexPointer( 3, GL_FLOAT, 0, vtx_coords );
Brian Paula4f0b682002-10-25 17:20:26 +0000188
Ian Romanickcd6ca582004-05-05 20:17:19 +0000189 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
190 glEnableClientState( GL_VERTEX_ARRAY );
Brian Paula4f0b682002-10-25 17:20:26 +0000191
Ian Romanickcd6ca582004-05-05 20:17:19 +0000192 glDrawArrays( GL_QUADS, 0, 24 );
Brian Paula4f0b682002-10-25 17:20:26 +0000193
Ian Romanickcd6ca582004-05-05 20:17:19 +0000194 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
195 glDisableClientState( GL_VERTEX_ARRAY );
196 }
197 else {
198 unsigned i;
Brian Paula4f0b682002-10-25 17:20:26 +0000199
Ian Romanickcd6ca582004-05-05 20:17:19 +0000200 glBegin(GL_QUADS);
201 for ( i = 0 ; i < 24 ; i++ ) {
202 glTexCoord3fv( & tex_coords[ i * 3 ] );
203 glVertex3fv ( & vtx_coords[ i * 3 ] );
204 }
205 glEnd();
206 }
Brian Paula4f0b682002-10-25 17:20:26 +0000207}
Brian Pauld04d2092000-05-30 01:18:29 +0000208
209
210static void draw( void )
211{
Brian Paul0e28e1a2009-03-09 11:49:46 -0600212 GLenum wrap;
213
Brian3a484cd2007-08-27 12:00:19 -0600214 if (NoClear) {
215 /* This demonstrates how we can avoid calling glClear.
216 * This method only works if every pixel in the window is painted for
217 * every frame.
218 * We can simply skip clearing of the color buffer in this case.
219 * For the depth buffer, we alternately use a different subrange of
220 * the depth buffer for each frame. For the odd frame use the range
221 * [0, 0.5] with GL_LESS. For the even frames, use the range [1, 0.5]
222 * with GL_GREATER.
223 */
224 FrameParity = 1 - FrameParity;
225 if (FrameParity) {
226 glDepthRange(0.0, 0.5);
227 glDepthFunc(GL_LESS);
228 }
229 else {
230 glDepthRange(1.0, 0.5);
231 glDepthFunc(GL_GREATER);
232 }
233 }
234 else {
235 /* ordinary clearing */
236 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
237 }
Brian Pauld04d2092000-05-30 01:18:29 +0000238
Brian Paul0e28e1a2009-03-09 11:49:46 -0600239 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER,
240 FilterModes[FilterIndex].min_mode);
241 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER,
242 FilterModes[FilterIndex].mag_mode);
243
Ian Romanick8b0b3352009-08-14 16:26:59 -0700244 if (supportSeamless) {
245 if (seamless) {
246 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
247 } else {
248 glDisable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
249 }
250 }
Brian Paul0e28e1a2009-03-09 11:49:46 -0600251 wrap = ClampModes[ClampIndex].mode;
252 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, wrap);
253 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, wrap);
254 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, wrap);
255
Brian Paula4f0b682002-10-25 17:20:26 +0000256 glPushMatrix(); /*MODELVIEW*/
257 glTranslatef( 0.0, 0.0, -EyeDist );
258
259 /* skybox */
260 glDisable(GL_TEXTURE_GEN_S);
261 glDisable(GL_TEXTURE_GEN_T);
262 glDisable(GL_TEXTURE_GEN_R);
263
264 glMatrixMode(GL_MODELVIEW);
265 glPushMatrix();
266 glRotatef(Xrot, 1, 0, 0);
267 glRotatef(Yrot, 0, 1, 0);
268 draw_skybox();
269 glPopMatrix();
270
271 /* sphere */
272 glMatrixMode(GL_TEXTURE);
273 glLoadIdentity();
274 glRotatef(-Yrot, 0, 1, 0);
275 glRotatef(-Xrot, 1, 0, 0);
276
277 glEnable(GL_TEXTURE_GEN_S);
278 glEnable(GL_TEXTURE_GEN_T);
279 glEnable(GL_TEXTURE_GEN_R);
280 glutSolidSphere(2.0, 20, 20);
281
282 glLoadIdentity(); /* texture */
283
284 glMatrixMode(GL_MODELVIEW);
285 glPopMatrix();
Brian Pauld04d2092000-05-30 01:18:29 +0000286
287 glutSwapBuffers();
Keith Whitwell75312b62009-08-23 19:02:17 +0100288
289 Frames++;
290
291 {
292 GLint t = glutGet(GLUT_ELAPSED_TIME);
293 if (t - T0 >= 5000) {
294 GLfloat seconds = (t - T0) / 1000.0;
295 GLfloat fps = Frames / seconds;
296 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
297 fflush(stdout);
298 T0 = t;
299 Frames = 0;
300 }
301 }
Brian Pauld04d2092000-05-30 01:18:29 +0000302}
303
304
305static void idle(void)
306{
Brian Paula4f0b682002-10-25 17:20:26 +0000307 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
308 Yrot = t;
Brian Pauld04d2092000-05-30 01:18:29 +0000309 glutPostRedisplay();
310}
311
312
313static void set_mode(GLuint mode)
314{
315 if (mode == 0) {
316 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
317 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
318 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
319 printf("GL_REFLECTION_MAP_ARB mode\n");
320 }
321 else if (mode == 1) {
322 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
323 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
324 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
325 printf("GL_NORMAL_MAP_ARB mode\n");
326 }
Brian Pauld04d2092000-05-30 01:18:29 +0000327}
328
329
330static void key(unsigned char k, int x, int y)
331{
Brian Pauld04d2092000-05-30 01:18:29 +0000332 static GLuint mode = 0;
333 (void) x;
334 (void) y;
335 switch (k) {
336 case ' ':
337 anim = !anim;
338 if (anim)
339 glutIdleFunc(idle);
340 else
341 glutIdleFunc(NULL);
342 break;
Brian Paul0e28e1a2009-03-09 11:49:46 -0600343 case 'f':
344 FilterIndex = (FilterIndex + 1) % NUM_FILTER_MODES;
345 printf("Tex filter: %s\n", FilterModes[FilterIndex].name);
346 break;
347 case 'c':
348 ClampIndex = (ClampIndex + 1) % NUM_CLAMP_MODES;
349 printf("Tex wrap mode: %s\n", ClampModes[ClampIndex].name);
350 break;
Brian Pauld04d2092000-05-30 01:18:29 +0000351 case 'm':
352 mode = !mode;
353 set_mode(mode);
354 break;
Ian Romanick8b0b3352009-08-14 16:26:59 -0700355 case 's':
356 seamless = ! seamless;
357 printf("Seamless cube map filtering is %sabled\n",
358 (seamless) ? "en" : "dis" );
359 break;
Ian Romanickcd6ca582004-05-05 20:17:19 +0000360 case 'v':
361 use_vertex_arrays = ! use_vertex_arrays;
362 printf( "Vertex arrays are %sabled\n",
363 (use_vertex_arrays) ? "en" : "dis" );
364 break;
Brian Paula4f0b682002-10-25 17:20:26 +0000365 case 'z':
366 EyeDist -= 0.5;
367 if (EyeDist < 6.0)
368 EyeDist = 6.0;
369 break;
370 case 'Z':
371 EyeDist += 0.5;
372 if (EyeDist > 90.0)
373 EyeDist = 90;
374 break;
Brian Pauld04d2092000-05-30 01:18:29 +0000375 case 27:
376 exit(0);
377 }
378 glutPostRedisplay();
379}
380
381
382static void specialkey(int key, int x, int y)
383{
Brian Paula4f0b682002-10-25 17:20:26 +0000384 GLfloat step = 5;
Brian Pauld04d2092000-05-30 01:18:29 +0000385 (void) x;
386 (void) y;
387 switch (key) {
388 case GLUT_KEY_UP:
Brian Paula4f0b682002-10-25 17:20:26 +0000389 Xrot += step;
Brian Pauld04d2092000-05-30 01:18:29 +0000390 break;
391 case GLUT_KEY_DOWN:
Brian Paula4f0b682002-10-25 17:20:26 +0000392 Xrot -= step;
Brian Pauld04d2092000-05-30 01:18:29 +0000393 break;
394 case GLUT_KEY_LEFT:
395 Yrot -= step;
396 break;
397 case GLUT_KEY_RIGHT:
398 Yrot += step;
399 break;
400 }
401 glutPostRedisplay();
402}
403
404
405/* new window size or exposure */
406static void reshape(int width, int height)
407{
Brian Paula4f0b682002-10-25 17:20:26 +0000408 GLfloat ar = (float) width / (float) height;
Brian Pauld04d2092000-05-30 01:18:29 +0000409 glViewport(0, 0, (GLint)width, (GLint)height);
410 glMatrixMode(GL_PROJECTION);
411 glLoadIdentity();
Brian Paula4f0b682002-10-25 17:20:26 +0000412 glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
Brian Pauld04d2092000-05-30 01:18:29 +0000413 glMatrixMode(GL_MODELVIEW);
414 glLoadIdentity();
Brian Pauld04d2092000-05-30 01:18:29 +0000415}
416
417
Brian Paula4f0b682002-10-25 17:20:26 +0000418static void init_checkers( void )
Brian Pauld04d2092000-05-30 01:18:29 +0000419{
420#define CUBE_TEX_SIZE 64
Brian Paul0e28e1a2009-03-09 11:49:46 -0600421 GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][4];
Brian Pauld04d2092000-05-30 01:18:29 +0000422 static const GLubyte colors[6][3] = {
Keith Whitwell493e6e12004-02-05 17:36:02 +0000423 { 255, 0, 0 }, /* face 0 - red */
424 { 0, 255, 255 }, /* face 1 - cyan */
425 { 0, 255, 0 }, /* face 2 - green */
426 { 255, 0, 255 }, /* face 3 - purple */
427 { 0, 0, 255 }, /* face 4 - blue */
428 { 255, 255, 0 } /* face 5 - yellow */
Brian Pauld04d2092000-05-30 01:18:29 +0000429 };
430 static const GLenum targets[6] = {
431 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
432 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
433 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
434 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
435 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
436 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
437 };
438
439 GLint i, j, f;
440
Brian Pauld04d2092000-05-30 01:18:29 +0000441 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
442
Roland Scheidegger7a5c5b92009-06-20 00:24:03 +0200443 if (!supportFBO)
444 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
445
446
Brian Pauld04d2092000-05-30 01:18:29 +0000447 /* make colored checkerboard cube faces */
448 for (f = 0; f < 6; f++) {
449 for (i = 0; i < CUBE_TEX_SIZE; i++) {
450 for (j = 0; j < CUBE_TEX_SIZE; j++) {
451 if ((i/4 + j/4) & 1) {
Brian Paul0e28e1a2009-03-09 11:49:46 -0600452 image[i][j][0] = colors[f][2];
Brian Pauld04d2092000-05-30 01:18:29 +0000453 image[i][j][1] = colors[f][1];
Brian Paul0e28e1a2009-03-09 11:49:46 -0600454 image[i][j][2] = colors[f][0];
455 image[i][j][3] = 255;
Brian Pauld04d2092000-05-30 01:18:29 +0000456 }
457 else {
458 image[i][j][0] = 255;
459 image[i][j][1] = 255;
460 image[i][j][2] = 255;
Brian Paul0e28e1a2009-03-09 11:49:46 -0600461 image[i][j][3] = 255;
Brian Pauld04d2092000-05-30 01:18:29 +0000462 }
463 }
464 }
465
Brian Paul0e28e1a2009-03-09 11:49:46 -0600466 glTexImage2D(targets[f], 0, GL_RGBA8, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
467 GL_BGRA, GL_UNSIGNED_BYTE, image);
Brian Pauld04d2092000-05-30 01:18:29 +0000468 }
Brian Paul0e28e1a2009-03-09 11:49:46 -0600469
Roland Scheidegger7a5c5b92009-06-20 00:24:03 +0200470 if (supportFBO)
471 glGenerateMipmapEXT(GL_TEXTURE_CUBE_MAP_ARB);
Brian Paula4f0b682002-10-25 17:20:26 +0000472}
Brian Pauld04d2092000-05-30 01:18:29 +0000473
Brian Paula4f0b682002-10-25 17:20:26 +0000474
475static void load(GLenum target, const char *filename,
476 GLboolean flipTB, GLboolean flipLR)
477{
478 GLint w, h;
479 GLenum format;
480 GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
481 if (!img) {
482 printf("Error: couldn't load texture image %s\n", filename);
483 exit(1);
484 }
485 assert(format == GL_RGB);
486
487 /* <sigh> the way the texture cube mapping works, we have to flip
488 * images to make things look right.
489 */
490 if (flipTB) {
491 const int stride = 3 * w;
492 GLubyte temp[3*1024];
493 int i;
494 for (i = 0; i < h / 2; i++) {
495 memcpy(temp, img + i * stride, stride);
496 memcpy(img + i * stride, img + (h - i - 1) * stride, stride);
497 memcpy(img + (h - i - 1) * stride, temp, stride);
498 }
499 }
500 if (flipLR) {
501 const int stride = 3 * w;
502 GLubyte temp[3];
503 GLubyte *row;
504 int i, j;
505 for (i = 0; i < h; i++) {
506 row = img + i * stride;
507 for (j = 0; j < w / 2; j++) {
508 int k = w - j - 1;
509 temp[0] = row[j*3+0];
510 temp[1] = row[j*3+1];
511 temp[2] = row[j*3+2];
512 row[j*3+0] = row[k*3+0];
513 row[j*3+1] = row[k*3+1];
514 row[j*3+2] = row[k*3+2];
515 row[k*3+0] = temp[0];
516 row[k*3+1] = temp[1];
517 row[k*3+2] = temp[2];
518 }
519 }
520 }
521
522 gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
523 free(img);
524}
525
526
527static void load_envmaps(void)
528{
529 load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, "right.rgb", GL_TRUE, GL_FALSE);
530 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, "left.rgb", GL_TRUE, GL_FALSE);
531 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, "top.rgb", GL_FALSE, GL_TRUE);
532 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, "bottom.rgb", GL_FALSE, GL_TRUE);
533 load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, "front.rgb", GL_TRUE, GL_FALSE);
534 load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, "back.rgb", GL_TRUE, GL_FALSE);
535}
536
537
538static void init( GLboolean useImageFiles )
539{
Brian Paul6aa6ae82009-03-12 09:31:59 -0600540 /* check for extensions */
Ian Romanick8b0b3352009-08-14 16:26:59 -0700541 if (!GLEW_ARB_texture_cube_map) {
542 printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
543 exit(0);
Brian Paula4f0b682002-10-25 17:20:26 +0000544 }
Ian Romanick8b0b3352009-08-14 16:26:59 -0700545
546 /* Needed for glGenerateMipmapEXT / auto mipmapping
547 */
548 supportFBO = GLEW_EXT_framebuffer_object;
549
550 if (!supportFBO && !GLEW_SGIS_generate_mipmap) {
551 printf("Sorry, this demo requires GL_EXT_framebuffer_object or "
552 "GL_SGIS_generate_mipmap\n");
553 exit(0);
554 }
555
556 /* GLEW doesn't know about this extension yet, so use the old GLUT function
557 * to check for availability.
558 */
559 supportSeamless = glutExtensionSupported("GL_ARB_seamless_cube_map");
560
Brian Paula4f0b682002-10-25 17:20:26 +0000561 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
562
Brian Paul0b0fc402009-09-15 17:13:49 -0600563
564 glGenTextures(1, &TexObj);
565 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, TexObj);
566
Brian Paula4f0b682002-10-25 17:20:26 +0000567 if (useImageFiles) {
568 load_envmaps();
Brian Paula4f0b682002-10-25 17:20:26 +0000569 }
570 else {
571 init_checkers();
Brian Paula4f0b682002-10-25 17:20:26 +0000572 }
573
Brian Pauld04d2092000-05-30 01:18:29 +0000574 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
Brian Paula4f0b682002-10-25 17:20:26 +0000575 glEnable(GL_DEPTH_TEST);
Brian Pauld04d2092000-05-30 01:18:29 +0000576
577 glClearColor(.3, .3, .3, 0);
578 glColor3f( 1.0, 1.0, 1.0 );
579
580 set_mode(0);
581}
582
583
584static void usage(void)
585{
586 printf("keys:\n");
587 printf(" SPACE - toggle animation\n");
588 printf(" CURSOR KEYS - rotation\n");
Brian Paul0e28e1a2009-03-09 11:49:46 -0600589 printf(" c - toggle texture clamp/wrap mode\n");
590 printf(" f - toggle texture filter mode\n");
Brian Pauld04d2092000-05-30 01:18:29 +0000591 printf(" m - toggle texgen reflection mode\n");
Brian Paula4f0b682002-10-25 17:20:26 +0000592 printf(" z/Z - change viewing distance\n");
Brian Pauld04d2092000-05-30 01:18:29 +0000593}
594
595
Brian3a484cd2007-08-27 12:00:19 -0600596static void parse_args(int argc, char *argv[])
597{
598 int initFlag = 0;
599 int i;
600
601 for (i = 1; i < argc; i++) {
602 if (strcmp(argv[i], "-i") == 0)
603 initFlag = 1;
604 else if (strcmp(argv[i], "--noclear") == 0)
605 NoClear = GL_TRUE;
606 else {
607 fprintf(stderr, "Bad option: %s\n", argv[i]);
608 exit(1);
609 }
610 }
611 init (initFlag);
612}
613
Brian Pauld04d2092000-05-30 01:18:29 +0000614int main( int argc, char *argv[] )
615{
Brian Paula4f0b682002-10-25 17:20:26 +0000616 glutInitWindowSize(600, 500);
Brian Paul263f4322009-12-18 08:12:55 -0700617 glutInit(&argc, argv);
Brian Pauld04d2092000-05-30 01:18:29 +0000618 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
Brian Paul0b77a1c2003-04-09 21:50:08 +0000619 glutCreateWindow("Texture Cube Mapping");
Brian Paul0e28e1a2009-03-09 11:49:46 -0600620 glewInit();
Brian Pauld04d2092000-05-30 01:18:29 +0000621 glutReshapeFunc( reshape );
622 glutKeyboardFunc( key );
623 glutSpecialFunc( specialkey );
Brian Pauld04d2092000-05-30 01:18:29 +0000624 glutDisplayFunc( draw );
Brian Paul52a5cc02004-08-10 15:39:00 +0000625 if (anim)
626 glutIdleFunc(idle);
Brian3a484cd2007-08-27 12:00:19 -0600627 parse_args(argc, argv);
Brian Pauld04d2092000-05-30 01:18:29 +0000628 usage();
629 glutMainLoop();
630 return 0;
631}