1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/* $Id: cubemap.c,v 1.4 2002/10/25 17:20:26 brianp Exp $ */
/*
* GL_ARB_texture_cube_map demo
*
* Brian Paul
* May 2000
*
*
* Copyright (C) 2000 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* This is a pretty minimalistic demo for now. Eventually, use some
* interesting cube map textures and 3D objects.
* For now, we use 6 checkerboard "walls" and a sphere (good for
* verification purposes).
*/
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "GL/glut.h"
#include "../util/readtex.c" /* a hack */
static GLfloat Xrot = 0, Yrot = 0;
static GLfloat EyeDist = 10;
static void draw_skybox( void )
{
const GLfloat eps1 = 0.99;
const GLfloat br = 20.0; /* box radius */
glBegin(GL_QUADS);
/* +X side */
glTexCoord3f(1.0, -eps1, -eps1); glVertex3f(br, -br, -br);
glTexCoord3f(1.0, -eps1, eps1); glVertex3f(br, -br, br);
glTexCoord3f(1.0, eps1, eps1); glVertex3f(br, br, br);
glTexCoord3f(1.0, eps1, -eps1); glVertex3f(br, br, -br);
/* -X side */
glTexCoord3f(-1.0, eps1, -eps1); glVertex3f(-br, br, -br);
glTexCoord3f(-1.0, eps1, eps1); glVertex3f(-br, br, br);
glTexCoord3f(-1.0, -eps1, eps1); glVertex3f(-br, -br, br);
glTexCoord3f(-1.0, -eps1, -eps1); glVertex3f(-br, -br, -br);
/* +Y side */
glTexCoord3f(-eps1, 1.0, -eps1); glVertex3f(-br, br, -br);
glTexCoord3f(-eps1, 1.0, eps1); glVertex3f(-br, br, br);
glTexCoord3f( eps1, 1.0, eps1); glVertex3f( br, br, br);
glTexCoord3f( eps1, 1.0, -eps1); glVertex3f( br, br, -br);
/* -Y side */
glTexCoord3f(-eps1, -1.0, -eps1); glVertex3f(-br, -br, -br);
glTexCoord3f(-eps1, -1.0, eps1); glVertex3f(-br, -br, br);
glTexCoord3f( eps1, -1.0, eps1); glVertex3f( br, -br, br);
glTexCoord3f( eps1, -1.0, -eps1); glVertex3f( br, -br, -br);
/* +Z side */
glTexCoord3f( eps1, -eps1, 1.0); glVertex3f( br, -br, br);
glTexCoord3f(-eps1, -eps1, 1.0); glVertex3f(-br, -br, br);
glTexCoord3f(-eps1, eps1, 1.0); glVertex3f(-br, br, br);
glTexCoord3f( eps1, eps1, 1.0); glVertex3f( br, br, br);
/* -Z side */
glTexCoord3f( eps1, eps1, -1.0); glVertex3f( br, br, -br);
glTexCoord3f(-eps1, eps1, -1.0); glVertex3f(-br, br, -br);
glTexCoord3f(-eps1, -eps1, -1.0); glVertex3f(-br, -br, -br);
glTexCoord3f( eps1, -eps1, -1.0); glVertex3f( br, -br, -br);
glEnd();
}
static void draw( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); /*MODELVIEW*/
glTranslatef( 0.0, 0.0, -EyeDist );
/* skybox */
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glRotatef(Xrot, 1, 0, 0);
glRotatef(Yrot, 0, 1, 0);
draw_skybox();
glPopMatrix();
/* sphere */
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glRotatef(-Yrot, 0, 1, 0);
glRotatef(-Xrot, 1, 0, 0);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
glutSolidSphere(2.0, 20, 20);
glLoadIdentity(); /* texture */
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glutSwapBuffers();
}
static void idle(void)
{
GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
Yrot = t;
glutPostRedisplay();
}
static void set_mode(GLuint mode)
{
if (mode == 0) {
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
printf("GL_REFLECTION_MAP_ARB mode\n");
}
else if (mode == 1) {
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
printf("GL_NORMAL_MAP_ARB mode\n");
}
}
static void key(unsigned char k, int x, int y)
{
static GLboolean anim = GL_TRUE;
static GLuint mode = 0;
(void) x;
(void) y;
switch (k) {
case ' ':
anim = !anim;
if (anim)
glutIdleFunc(idle);
else
glutIdleFunc(NULL);
break;
case 'm':
mode = !mode;
set_mode(mode);
break;
case 'z':
EyeDist -= 0.5;
if (EyeDist < 6.0)
EyeDist = 6.0;
break;
case 'Z':
EyeDist += 0.5;
if (EyeDist > 90.0)
EyeDist = 90;
break;
case 27:
exit(0);
}
glutPostRedisplay();
}
static void specialkey(int key, int x, int y)
{
GLfloat step = 5;
(void) x;
(void) y;
switch (key) {
case GLUT_KEY_UP:
Xrot += step;
break;
case GLUT_KEY_DOWN:
Xrot -= step;
break;
case GLUT_KEY_LEFT:
Yrot -= step;
break;
case GLUT_KEY_RIGHT:
Yrot += step;
break;
}
glutPostRedisplay();
}
/* new window size or exposure */
static void reshape(int width, int height)
{
GLfloat ar = (float) width / (float) height;
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum( -2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void init_checkers( void )
{
#define CUBE_TEX_SIZE 64
GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3];
static const GLubyte colors[6][3] = {
{ 255, 0, 0 },
{ 0, 255, 255 },
{ 0, 255, 0 },
{ 255, 0, 255 },
{ 0, 0, 255 },
{ 255, 255, 0 }
};
static const GLenum targets[6] = {
GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
};
GLint i, j, f;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/* make colored checkerboard cube faces */
for (f = 0; f < 6; f++) {
for (i = 0; i < CUBE_TEX_SIZE; i++) {
for (j = 0; j < CUBE_TEX_SIZE; j++) {
if ((i/4 + j/4) & 1) {
image[i][j][0] = colors[f][0];
image[i][j][1] = colors[f][1];
image[i][j][2] = colors[f][2];
}
else {
image[i][j][0] = 255;
image[i][j][1] = 255;
image[i][j][2] = 255;
}
}
}
glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
GL_RGB, GL_UNSIGNED_BYTE, image);
}
}
static void load(GLenum target, const char *filename,
GLboolean flipTB, GLboolean flipLR)
{
GLint w, h;
GLenum format;
GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
if (!img) {
printf("Error: couldn't load texture image %s\n", filename);
exit(1);
}
assert(format == GL_RGB);
/* <sigh> the way the texture cube mapping works, we have to flip
* images to make things look right.
*/
if (flipTB) {
const int stride = 3 * w;
GLubyte temp[3*1024];
int i;
for (i = 0; i < h / 2; i++) {
memcpy(temp, img + i * stride, stride);
memcpy(img + i * stride, img + (h - i - 1) * stride, stride);
memcpy(img + (h - i - 1) * stride, temp, stride);
}
}
if (flipLR) {
const int stride = 3 * w;
GLubyte temp[3];
GLubyte *row;
int i, j;
for (i = 0; i < h; i++) {
row = img + i * stride;
for (j = 0; j < w / 2; j++) {
int k = w - j - 1;
temp[0] = row[j*3+0];
temp[1] = row[j*3+1];
temp[2] = row[j*3+2];
row[j*3+0] = row[k*3+0];
row[j*3+1] = row[k*3+1];
row[j*3+2] = row[k*3+2];
row[k*3+0] = temp[0];
row[k*3+1] = temp[1];
row[k*3+2] = temp[2];
}
}
}
gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
free(img);
}
static void load_envmaps(void)
{
load(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, "right.rgb", GL_TRUE, GL_FALSE);
load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, "left.rgb", GL_TRUE, GL_FALSE);
load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, "top.rgb", GL_FALSE, GL_TRUE);
load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, "bottom.rgb", GL_FALSE, GL_TRUE);
load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, "front.rgb", GL_TRUE, GL_FALSE);
load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, "back.rgb", GL_TRUE, GL_FALSE);
}
static void init( GLboolean useImageFiles )
{
GLenum filter;
/* check for extension */
{
char *exten = (char *) glGetString(GL_EXTENSIONS);
if (!strstr(exten, "GL_ARB_texture_cube_map")) {
printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
exit(0);
}
}
printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
if (useImageFiles) {
load_envmaps();
filter = GL_LINEAR;
}
else {
init_checkers();
filter = GL_NEAREST;
}
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glEnable(GL_DEPTH_TEST);
glClearColor(.3, .3, .3, 0);
glColor3f( 1.0, 1.0, 1.0 );
set_mode(0);
}
static void usage(void)
{
printf("keys:\n");
printf(" SPACE - toggle animation\n");
printf(" CURSOR KEYS - rotation\n");
printf(" m - toggle texgen reflection mode\n");
printf(" z/Z - change viewing distance\n");
}
int main( int argc, char *argv[] )
{
glutInitWindowPosition(0, 0);
glutInitWindowSize(600, 500);
glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
glutCreateWindow("Texture Cube Maping");
if (argc > 1 && strcmp(argv[1] , "-i") == 0)
init( 1 );
else
init( 0 );
glutReshapeFunc( reshape );
glutKeyboardFunc( key );
glutSpecialFunc( specialkey );
glutIdleFunc( idle );
glutDisplayFunc( draw );
usage();
glutMainLoop();
return 0;
}
|