~ubuntu-branches/ubuntu/quantal/mesa-glw/quantal

« back to all changes in this revision

Viewing changes to progs/demos/stex3d.c

  • Committer: Bazaar Package Importer
  • Author(s): Morten Kjeldgaard
  • Date: 2008-05-06 16:19:15 UTC
  • Revision ID: james.westby@ubuntu.com-20080506161915-uynz7nftmfixu6bq
Tags: upstream-7.0.3
ImportĀ upstreamĀ versionĀ 7.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*----------------------------- 
 
2
 * stex3d.c GL example of the mesa 3d-texture extention to simulate procedural
 
3
 *            texturing, it uses a perlin noise and turbulence functions.
 
4
 * 
 
5
 * Author:   Daniel Barrero
 
6
 *           barrero@irit.fr
 
7
 *           dbarrero@pegasus.uniandes.edu.co
 
8
 *
 
9
 * Converted to GLUT by brianp on 1/1/98
 
10
 * Massive clean-up on 2002/10/23 by brianp
 
11
 *
 
12
 *      
 
13
 * cc stex3d.c -o stex3d -lglut -lMesaGLU -lMesaGL -lX11 -lXext -lm 
 
14
 *
 
15
 *---------------------------- */
 
16
 
 
17
#include <string.h>
 
18
#include <stdio.h>
 
19
#include <stdlib.h>
 
20
#include <math.h>
 
21
#include <GL/gl.h>
 
22
#include <GL/glut.h>
 
23
 
 
24
 
 
25
#ifndef M_PI
 
26
#define M_PI            3.14159265358979323846
 
27
#endif
 
28
 
 
29
#define NOISE_TEXTURE 1
 
30
#define GRADIENT_TEXTURE 2
 
31
 
 
32
#define TORUS 1
 
33
#define SPHERE 2
 
34
 
 
35
static int tex_width=64, tex_height=64, tex_depth=64;
 
36
static float angx=0, angy=0, angz=0;
 
37
static int texgen = 2, animate = 1, smooth = 1, wireframe = 0;
 
38
static int CurTexture = NOISE_TEXTURE, CurObject = TORUS;
 
39
 
 
40
 
 
41
static void
 
42
BuildTorus(void)
 
43
{
 
44
   GLint i, j;
 
45
   float theta1, phi1, theta2, phi2, rings, sides;
 
46
   float v0[03], v1[3], v2[3], v3[3];
 
47
   float t0[03], t1[3], t2[3], t3[3];
 
48
   float n0[3], n1[3], n2[3], n3[3];
 
49
   float innerRadius = 0.25;
 
50
   float outerRadius = 0.5;
 
51
   float scalFac;
 
52
 
 
53
   rings = 16;
 
54
   sides = 12;
 
55
   scalFac = 1 / (outerRadius * 2);
 
56
 
 
57
   glNewList(TORUS, GL_COMPILE);
 
58
   for (i = 0; i < rings; i++) {
 
59
      theta1 = (float) i *2.0 * M_PI / rings;
 
60
      theta2 = (float) (i + 1) * 2.0 * M_PI / rings;
 
61
      for (j = 0; j < sides; j++) {
 
62
         phi1 = (float) j *2.0 * M_PI / sides;
 
63
         phi2 = (float) (j + 1) * 2.0 * M_PI / sides;
 
64
 
 
65
         v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
 
66
         v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
 
67
         v0[2] = innerRadius * sin(phi1);
 
68
 
 
69
         v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
 
70
         v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
 
71
         v1[2] = innerRadius * sin(phi1);
 
72
         v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
 
73
         v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
 
74
         v2[2] = innerRadius * sin(phi2);
 
75
 
 
76
         v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
 
77
         v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
 
78
         v3[2] = innerRadius * sin(phi2);
 
79
 
 
80
         n0[0] = cos(theta1) * (cos(phi1));
 
81
         n0[1] = -sin(theta1) * (cos(phi1));
 
82
         n0[2] = sin(phi1);
 
83
 
 
84
         n1[0] = cos(theta2) * (cos(phi1));
 
85
         n1[1] = -sin(theta2) * (cos(phi1));
 
86
         n1[2] = sin(phi1);
 
87
 
 
88
         n2[0] = cos(theta2) * (cos(phi2));
 
89
         n2[1] = -sin(theta2) * (cos(phi2));
 
90
         n2[2] = sin(phi2);
 
91
 
 
92
         n3[0] = cos(theta1) * (cos(phi2));
 
93
         n3[1] = -sin(theta1) * (cos(phi2));
 
94
         n3[2] = sin(phi2);
 
95
 
 
96
         t0[0] = v0[0] * scalFac + 0.5;
 
97
         t0[1] = v0[1] * scalFac + 0.5;
 
98
         t0[2] = v0[2] * scalFac + 0.5;
 
99
 
 
100
         t1[0] = v1[0] * scalFac + 0.5;
 
101
         t1[1] = v1[1] * scalFac + 0.5;
 
102
         t1[2] = v1[2] * scalFac + 0.5;
 
103
 
 
104
         t2[0] = v2[0] * scalFac + 0.5;
 
105
         t2[1] = v2[1] * scalFac + 0.5;
 
106
         t2[2] = v2[2] * scalFac + 0.5;
 
107
 
 
108
         t3[0] = v3[0] * scalFac + 0.5;
 
109
         t3[1] = v3[1] * scalFac + 0.5;
 
110
         t3[2] = v3[2] * scalFac + 0.5;
 
111
 
 
112
         glBegin(GL_POLYGON);
 
113
         glNormal3fv(n3);
 
114
         glTexCoord3fv(t3);
 
115
         glVertex3fv(v3);
 
116
         glNormal3fv(n2);
 
117
         glTexCoord3fv(t2);
 
118
         glVertex3fv(v2);
 
119
         glNormal3fv(n1);
 
120
         glTexCoord3fv(t1);
 
121
         glVertex3fv(v1);
 
122
         glNormal3fv(n0);
 
123
         glTexCoord3fv(t0);
 
124
         glVertex3fv(v0);
 
125
         glEnd();
 
126
      }
 
127
   }
 
128
   glEndList();
 
129
}
 
130
 
 
131
 
 
132
/*--------------------------------------------------------------------
 
133
 noise function over R3 - implemented by a pseudorandom tricubic spline 
 
134
              EXCERPTED FROM SIGGRAPH 92, COURSE 23
 
135
                        PROCEDURAL MODELING
 
136
                             Ken Perlin
 
137
                           New York University
 
138
----------------------------------------------------------------------*/
 
139
 
 
140
 
 
141
#define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
 
142
#define B 128
 
143
static int p[B + B + 2];
 
144
static float g[B + B + 2][3];
 
145
#define setup(i,b0,b1,r0,r1) \
 
146
        t = vec[i] + 10000.; \
 
147
        b0 = ((int)t) & (B-1); \
 
148
        b1 = (b0+1) & (B-1); \
 
149
        r0 = t - (int)t; \
 
150
        r1 = r0 - 1.;
 
151
 
 
152
static float
 
153
noise3(float vec[3])
 
154
{
 
155
   int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
 
156
   float rx0, rx1, ry0, ry1, rz0, rz1, *q, sx, sy, sz, a, b, c, d, t, u, v;
 
157
   register int i, j;
 
158
 
 
159
   setup(0, bx0, bx1, rx0, rx1);
 
160
   setup(1, by0, by1, ry0, ry1);
 
161
   setup(2, bz0, bz1, rz0, rz1);
 
162
 
 
163
   i = p[bx0];
 
164
   j = p[bx1];
 
165
 
 
166
   b00 = p[i + by0];
 
167
   b10 = p[j + by0];
 
168
   b01 = p[i + by1];
 
169
   b11 = p[j + by1];
 
170
 
 
171
#define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
 
172
#define surve(t) ( t * t * (3. - 2. * t) )
 
173
#define lerp(t, a, b) ( a + t * (b - a) )
 
174
 
 
175
   sx = surve(rx0);
 
176
   sy = surve(ry0);
 
177
   sz = surve(rz0);
 
178
 
 
179
   q = g[b00 + bz0];
 
180
   u = at(rx0, ry0, rz0);
 
181
   q = g[b10 + bz0];
 
182
   v = at(rx1, ry0, rz0);
 
183
   a = lerp(sx, u, v);
 
184
 
 
185
   q = g[b01 + bz0];
 
186
   u = at(rx0, ry1, rz0);
 
187
   q = g[b11 + bz0];
 
188
   v = at(rx1, ry1, rz0);
 
189
   b = lerp(sx, u, v);
 
190
 
 
191
   c = lerp(sy, a, b);          /* interpolate in y at lo x */
 
192
 
 
193
   q = g[b00 + bz1];
 
194
   u = at(rx0, ry0, rz1);
 
195
   q = g[b10 + bz1];
 
196
   v = at(rx1, ry0, rz1);
 
197
   a = lerp(sx, u, v);
 
198
 
 
199
   q = g[b01 + bz1];
 
200
   u = at(rx0, ry1, rz1);
 
201
   q = g[b11 + bz1];
 
202
   v = at(rx1, ry1, rz1);
 
203
   b = lerp(sx, u, v);
 
204
 
 
205
   d = lerp(sy, a, b);          /* interpolate in y at hi x */
 
206
 
 
207
   return 1.5 * lerp(sz, c, d); /* interpolate in z */
 
208
}
 
209
 
 
210
static void
 
211
initNoise(void)
 
212
{
 
213
   /*long random(); */
 
214
   int i, j, k;
 
215
   float v[3], s;
 
216
 
 
217
   /* Create an array of random gradient vectors uniformly on the unit sphere */
 
218
   /*srandom(1); */
 
219
   srand(1);
 
220
   for (i = 0; i < B; i++) {
 
221
      do {                      /* Choose uniformly in a cube */
 
222
         for (j = 0; j < 3; j++)
 
223
            v[j] = (float) ((rand() % (B + B)) - B) / B;
 
224
         s = DOT(v, v);
 
225
      } while (s > 1.0);        /* If not in sphere try again */
 
226
      s = sqrt(s);
 
227
      for (j = 0; j < 3; j++)   /* Else normalize */
 
228
         g[i][j] = v[j] / s;
 
229
   }
 
230
 
 
231
   /* Create a pseudorandom permutation of [1..B] */
 
232
   for (i = 0; i < B; i++)
 
233
      p[i] = i;
 
234
   for (i = B; i > 0; i -= 2) {
 
235
      k = p[i];
 
236
      p[i] = p[j = rand() % B];
 
237
      p[j] = k;
 
238
   }
 
239
 
 
240
   /* Extend g and p arrays to allow for faster indexing */
 
241
   for (i = 0; i < B + 2; i++) {
 
242
      p[B + i] = p[i];
 
243
      for (j = 0; j < 3; j++)
 
244
         g[B + i][j] = g[i][j];
 
245
   }
 
246
}
 
247
 
 
248
 
 
249
static float
 
250
turbulence(float point[3], float lofreq, float hifreq)
 
251
{
 
252
   float freq, t, p[3];
 
253
 
 
254
   p[0] = point[0] + 123.456;
 
255
   p[1] = point[1];
 
256
   p[2] = point[2];
 
257
 
 
258
   t = 0;
 
259
   for (freq = lofreq; freq < hifreq; freq *= 2.) {
 
260
      t += fabs(noise3(p)) / freq;
 
261
      p[0] *= 2.;
 
262
      p[1] *= 2.;
 
263
      p[2] *= 2.;
 
264
   }
 
265
   return t - 0.3;              /* readjust to make mean value = 0.0 */
 
266
}
 
267
 
 
268
 
 
269
static void
 
270
create3Dtexture(void)
 
271
{
 
272
   unsigned char *voxels = NULL;
 
273
   int i, j, k;
 
274
   unsigned char *vp;
 
275
   float vec[3];
 
276
   int tmp;
 
277
 
 
278
   printf("creating 3d textures...\n");
 
279
   voxels =
 
280
      (unsigned char *)
 
281
      malloc((size_t) (4 * tex_width * tex_height * tex_depth));
 
282
   vp = voxels;
 
283
   for (i = 0; i < tex_width; i++) {
 
284
      vec[0] = i;
 
285
      for (j = 0; j < tex_height; j++) {
 
286
         vec[1] = j;
 
287
         for (k = 0; k < tex_depth; k++) {
 
288
            vec[2] = k;
 
289
            tmp = (sin(k * i * j + turbulence(vec, 0.01, 1)) + 1) * 127.5;
 
290
            *vp++ = 0;
 
291
            *vp++ = 0;
 
292
            *vp++ = tmp;
 
293
            *vp++ = tmp + 128;
 
294
         }
 
295
      }
 
296
   }
 
297
 
 
298
   printf("setting up 3d texture...\n");
 
299
 
 
300
   glBindTexture(GL_TEXTURE_3D, NOISE_TEXTURE);
 
301
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
302
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
303
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 
304
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
305
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
 
306
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
 
307
 
 
308
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
309
   glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
 
310
                tex_width, tex_height, tex_depth,
 
311
                0, GL_RGBA, GL_UNSIGNED_BYTE, voxels);
 
312
 
 
313
   free(voxels);
 
314
 
 
315
   printf("finished setting up 3d texture image.\n");
 
316
}
 
317
 
 
318
 
 
319
static void
 
320
printHelp(void)
 
321
{
 
322
   printf("\nUsage: stex3d  <cmd line options>\n");
 
323
   printf(" cmd line options:\n");
 
324
   printf("      -wxxx   Width of the texture (Default=64)\n");
 
325
   printf("      -hxxx   Height of the texture (Default=64)\n");
 
326
   printf("      -dxxx   Depth of the texture (Default=64)\n");
 
327
   printf(" Keyboard Options:\n");
 
328
   printf("    up/down   rotate around X\n");
 
329
   printf("  left/right  rotate around Y\n");
 
330
   printf("      z/Z     rotate around Z\n");
 
331
   printf("       a      toggle animation\n");
 
332
   printf("       s      toggle smooth shading\n");
 
333
   printf("       t      toggle texgen mode\n");
 
334
   printf("       o      toggle object: torus/sphere\n");
 
335
   printf("       i      toggle texture image: noise/gradient\n");
 
336
}
 
337
 
 
338
 
 
339
static GLenum
 
340
parseCmdLine(int argc, char **argv)
 
341
{
 
342
   GLint i;
 
343
 
 
344
   for (i = 1; i < argc; i++) {
 
345
      if (strcmp(argv[i], "-help") == 0) {
 
346
         printHelp();
 
347
         return GL_FALSE;
 
348
      }
 
349
      else if (strstr(argv[i], "-w") != NULL) {
 
350
         tex_width = atoi((argv[i]) + 2);
 
351
      }
 
352
      else if (strstr(argv[i], "-h") != NULL) {
 
353
         tex_height = atoi((argv[i]) + 2);
 
354
      }
 
355
      else if (strstr(argv[i], "-d") != NULL) {
 
356
         tex_depth = atoi((argv[i]) + 2);
 
357
      }
 
358
      else {
 
359
         printf("%s (Bad option).\n", argv[i]);
 
360
         printHelp();
 
361
         return GL_FALSE;
 
362
      }
 
363
   }
 
364
   if (tex_width == 0 || tex_height == 0 || tex_depth == 0) {
 
365
      printf("%s (Bad option).\n", "size parameters can't be 0");
 
366
      printHelp();
 
367
      return GL_FALSE;
 
368
   }
 
369
   return GL_TRUE;
 
370
}
 
371
 
 
372
 
 
373
static void
 
374
drawScene(void)
 
375
{
 
376
   static const GLfloat sPlane[4] = { 0.5, 0, 0, -.5 };
 
377
   static const GLfloat tPlane[4] = { 0, 0.5, 0, -.5 };
 
378
   static const GLfloat rPlane[4] = { 0, 0, 0.5, -.5 };
 
379
 
 
380
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
381
   glPushMatrix();
 
382
   if (texgen == 2) {
 
383
      glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
 
384
      glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
 
385
      glTexGenfv(GL_R, GL_EYE_PLANE, rPlane);
 
386
   }
 
387
 
 
388
   glRotatef(angx, 1.0, 0.0, 0.0);
 
389
   glRotatef(angy, 0.0, 1.0, 0.0);
 
390
   glRotatef(angz, 0.0, 0.0, 1.0);
 
391
 
 
392
   if (texgen == 1) {
 
393
      glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
 
394
      glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
 
395
      glTexGenfv(GL_R, GL_EYE_PLANE, rPlane);
 
396
   }
 
397
 
 
398
   if (texgen) {
 
399
      glEnable(GL_TEXTURE_GEN_S);
 
400
      glEnable(GL_TEXTURE_GEN_T);
 
401
      glEnable(GL_TEXTURE_GEN_R);
 
402
   }
 
403
   else {
 
404
      glDisable(GL_TEXTURE_GEN_S);
 
405
      glDisable(GL_TEXTURE_GEN_T);
 
406
      glDisable(GL_TEXTURE_GEN_R);
 
407
   }
 
408
 
 
409
   glCallList(CurObject);
 
410
   glPopMatrix();
 
411
 
 
412
   glutSwapBuffers();
 
413
}
 
414
 
 
415
 
 
416
static void
 
417
resize(int w, int h)
 
418
{
 
419
   float ar = (float) w / (float) h;
 
420
   float ax = 0.6 * ar;
 
421
   float ay = 0.6;
 
422
   glViewport(0, 0, (GLint) w, (GLint) h);
 
423
   glMatrixMode(GL_PROJECTION);
 
424
   glLoadIdentity();
 
425
   glFrustum(-ax, ax, -ay, ay, 2, 20);
 
426
   /*glOrtho(-2, 2, -2, 2, -10, 10);*/
 
427
   glMatrixMode(GL_MODELVIEW);
 
428
   glLoadIdentity();
 
429
   glTranslatef(0, 0, -4);
 
430
}
 
431
 
 
432
 
 
433
static void
 
434
Idle(void)
 
435
{
 
436
   float t = glutGet(GLUT_ELAPSED_TIME);
 
437
   angx = 0.01 * t;
 
438
   angy = 0.03 * t;
 
439
   angz += 0;
 
440
   glutPostRedisplay();
 
441
}
 
442
 
 
443
 
 
444
static void
 
445
SpecialKey(int k, int x, int y)
 
446
{
 
447
  switch (k) {
 
448
  case GLUT_KEY_UP:
 
449
    angx += 5.0;
 
450
    break;
 
451
  case GLUT_KEY_DOWN:
 
452
    angx -= 5.0;
 
453
    break;
 
454
  case GLUT_KEY_LEFT:
 
455
    angy += 5.0;
 
456
    break;
 
457
  case GLUT_KEY_RIGHT:
 
458
    angy -= 5.0;
 
459
    break;
 
460
  default:
 
461
    return;
 
462
  }
 
463
  glutPostRedisplay();
 
464
}
 
465
 
 
466
 
 
467
static void
 
468
KeyHandler(unsigned char key, int x, int y)
 
469
{
 
470
   static const char *mode[] = {
 
471
      "glTexCoord3f (no texgen)",
 
472
      "texgen fixed to object coords",
 
473
      "texgen fixed to eye coords"
 
474
   };
 
475
   (void) x;
 
476
   (void) y;
 
477
   switch (key) {
 
478
   case 27:
 
479
   case 'q':
 
480
   case 'Q':                    /* quit game. */
 
481
      exit(0);
 
482
      break;
 
483
   case 'z':
 
484
      angz += 10;
 
485
      break;
 
486
   case 'Z':
 
487
      angz -= 10;
 
488
      break;
 
489
   case 's':
 
490
      smooth = !smooth;
 
491
      if (smooth)
 
492
         glShadeModel(GL_SMOOTH);
 
493
      else
 
494
         glShadeModel(GL_FLAT);
 
495
      break;
 
496
   case 't':
 
497
      texgen++;
 
498
      if (texgen > 2)
 
499
         texgen = 0;
 
500
      printf("Texgen: %s\n", mode[texgen]);
 
501
      break;
 
502
   case 'o':
 
503
      if (CurObject == TORUS)
 
504
         CurObject = SPHERE;
 
505
      else
 
506
         CurObject = TORUS;
 
507
      break;
 
508
   case 'i':
 
509
      if (CurTexture == NOISE_TEXTURE)
 
510
         CurTexture = GRADIENT_TEXTURE;
 
511
      else
 
512
         CurTexture = NOISE_TEXTURE;
 
513
      glBindTexture(GL_TEXTURE_3D, CurTexture);
 
514
      break;
 
515
   case 'a':
 
516
      animate = !animate;
 
517
      if (animate)
 
518
         glutIdleFunc(Idle);
 
519
      else
 
520
         glutIdleFunc(NULL);
 
521
      break;
 
522
   case 'w':
 
523
      wireframe = !wireframe;
 
524
      if (wireframe)
 
525
         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 
526
      else
 
527
         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 
528
      break;
 
529
   default:
 
530
      break;
 
531
   }
 
532
   glutPostRedisplay();
 
533
}
 
534
 
 
535
 
 
536
static void
 
537
create3Dgradient(void)
 
538
{
 
539
   unsigned char *v;
 
540
   int i, j, k;
 
541
   unsigned char *voxels = NULL;
 
542
 
 
543
   voxels = (unsigned char *) malloc(4 * tex_width * tex_height * tex_depth);
 
544
   v = voxels;
 
545
 
 
546
   for (i = 0; i < tex_depth; i++) {
 
547
      for (j = 0; j < tex_height; j++) {
 
548
         for (k = 0; k < tex_width; k++) {
 
549
            GLint r = (255 * i) / (tex_depth - 1);
 
550
            GLint g = (255 * j) / (tex_height - 1);
 
551
            GLint b = (255 * k) / (tex_width - 1);
 
552
            *v++ = r;
 
553
            *v++ = g;
 
554
            *v++ = b;
 
555
            *v++ = 255;
 
556
         }
 
557
      }
 
558
   }
 
559
 
 
560
 
 
561
   glBindTexture(GL_TEXTURE_3D, GRADIENT_TEXTURE);
 
562
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
563
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
564
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 
565
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
566
   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
 
567
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
 
568
 
 
569
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
570
   glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
 
571
                tex_width, tex_height, tex_depth,
 
572
                0, GL_RGBA, GL_UNSIGNED_BYTE, voxels);
 
573
 
 
574
   free(voxels);
 
575
}
 
576
 
 
577
 
 
578
 
 
579
static void
 
580
init(void)
 
581
{
 
582
   static const GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
 
583
   static const GLfloat mat_shininess[] = { 25.0 };
 
584
   static const GLfloat gray[] = { 0.6, 0.6, 0.6, 0.0 };
 
585
   static const GLfloat white[] = { 1.0, 1.0, 1.0, 0.0 };
 
586
   static const GLfloat light_position[] = { 0.0, 1.0, 1.0, 0.0 };
 
587
 
 
588
   int max;
 
589
 
 
590
   /* see if we have OpenGL 1.2 or later, for 3D texturing */
 
591
   {
 
592
      const char *version = (const char *) glGetString(GL_VERSION);
 
593
      if (strncmp(version, "1.0", 3) == 0 || strncmp(version, "1.1", 3) == 0) {
 
594
         printf("Sorry, OpenGL 1.2 or later is required\n");
 
595
         exit(1);
 
596
      }
 
597
   }
 
598
   printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
 
599
   glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &max);
 
600
   printf("GL_MAX_3D_TEXTURE_SIZE: %d\n", max);
 
601
   printf("Current 3D texture size: %d x %d x %d\n",
 
602
          tex_width, tex_height, tex_depth);
 
603
 
 
604
   /* init light */
 
605
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
 
606
   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
 
607
   glLightfv(GL_LIGHT1, GL_POSITION, light_position);
 
608
   glLightfv(GL_LIGHT1, GL_AMBIENT, gray);
 
609
   glLightfv(GL_LIGHT1, GL_DIFFUSE, white);
 
610
   glLightfv(GL_LIGHT1, GL_SPECULAR, white);
 
611
   glColorMaterial(GL_FRONT, GL_DIFFUSE);
 
612
   glEnable(GL_COLOR_MATERIAL);
 
613
   glEnable(GL_LIGHTING);
 
614
   glEnable(GL_LIGHT1);
 
615
 
 
616
   glClearColor(.5, .5, .5, 0);
 
617
 
 
618
   {
 
619
      GLUquadricObj *q;
 
620
      q = gluNewQuadric();
 
621
      gluQuadricTexture( q, GL_TRUE );
 
622
      glNewList(SPHERE, GL_COMPILE);
 
623
      gluSphere( q, 0.95, 30, 15 );
 
624
      glEndList();
 
625
      gluDeleteQuadric(q);
 
626
   }
 
627
 
 
628
   BuildTorus();
 
629
 
 
630
 
 
631
   create3Dgradient();
 
632
 
 
633
   initNoise();
 
634
   create3Dtexture();
 
635
 
 
636
   glEnable(GL_TEXTURE_3D);
 
637
 
 
638
   /*
 
639
      glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
 
640
      glEnable(GL_BLEND); 
 
641
    */
 
642
   glEnable(GL_DEPTH_TEST);
 
643
 
 
644
   glColor3f(0.6, 0.7, 0.8);
 
645
}
 
646
 
 
647
 
 
648
int
 
649
main(int argc, char **argv)
 
650
{
 
651
   glutInit(&argc, argv);
 
652
 
 
653
   if (parseCmdLine(argc, argv) == GL_FALSE) {
 
654
      exit(0);
 
655
   }
 
656
 
 
657
   glutInitWindowPosition(0, 0);
 
658
   glutInitWindowSize(400, 400);
 
659
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
 
660
 
 
661
   if (glutCreateWindow("stex3d") <= 0) {
 
662
      exit(0);
 
663
   }
 
664
 
 
665
   init();
 
666
 
 
667
   printHelp();
 
668
 
 
669
   glutReshapeFunc(resize);
 
670
   glutKeyboardFunc(KeyHandler);
 
671
   glutSpecialFunc(SpecialKey);
 
672
   glutDisplayFunc(drawScene);
 
673
   if (animate)
 
674
      glutIdleFunc(Idle);
 
675
   glutMainLoop();
 
676
   return 0;
 
677
}
 
678