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.
5
* Author: Daniel Barrero
7
* dbarrero@pegasus.uniandes.edu.co
9
* Converted to GLUT by brianp on 1/1/98
10
* Massive clean-up on 2002/10/23 by brianp
13
* cc stex3d.c -o stex3d -lglut -lMesaGLU -lMesaGL -lX11 -lXext -lm
15
*---------------------------- */
26
#define M_PI 3.14159265358979323846
29
#define NOISE_TEXTURE 1
30
#define GRADIENT_TEXTURE 2
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
static GLenum Filter = GL_LINEAR;
46
float theta1, phi1, theta2, phi2, rings, sides;
47
float v0[03], v1[3], v2[3], v3[3];
48
float t0[03], t1[3], t2[3], t3[3];
49
float n0[3], n1[3], n2[3], n3[3];
50
float innerRadius = 0.25;
51
float outerRadius = 0.5;
56
scalFac = 1 / (outerRadius * 2);
58
glNewList(TORUS, GL_COMPILE);
59
for (i = 0; i < rings; i++) {
60
theta1 = (float) i *2.0 * M_PI / rings;
61
theta2 = (float) (i + 1) * 2.0 * M_PI / rings;
62
for (j = 0; j < sides; j++) {
63
phi1 = (float) j *2.0 * M_PI / sides;
64
phi2 = (float) (j + 1) * 2.0 * M_PI / sides;
66
v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
67
v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
68
v0[2] = innerRadius * sin(phi1);
70
v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
71
v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
72
v1[2] = innerRadius * sin(phi1);
73
v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
74
v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
75
v2[2] = innerRadius * sin(phi2);
77
v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
78
v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
79
v3[2] = innerRadius * sin(phi2);
81
n0[0] = cos(theta1) * (cos(phi1));
82
n0[1] = -sin(theta1) * (cos(phi1));
85
n1[0] = cos(theta2) * (cos(phi1));
86
n1[1] = -sin(theta2) * (cos(phi1));
89
n2[0] = cos(theta2) * (cos(phi2));
90
n2[1] = -sin(theta2) * (cos(phi2));
93
n3[0] = cos(theta1) * (cos(phi2));
94
n3[1] = -sin(theta1) * (cos(phi2));
97
t0[0] = v0[0] * scalFac + 0.5;
98
t0[1] = v0[1] * scalFac + 0.5;
99
t0[2] = v0[2] * scalFac + 0.5;
101
t1[0] = v1[0] * scalFac + 0.5;
102
t1[1] = v1[1] * scalFac + 0.5;
103
t1[2] = v1[2] * scalFac + 0.5;
105
t2[0] = v2[0] * scalFac + 0.5;
106
t2[1] = v2[1] * scalFac + 0.5;
107
t2[2] = v2[2] * scalFac + 0.5;
109
t3[0] = v3[0] * scalFac + 0.5;
110
t3[1] = v3[1] * scalFac + 0.5;
111
t3[2] = v3[2] * scalFac + 0.5;
133
/*--------------------------------------------------------------------
134
noise function over R3 - implemented by a pseudorandom tricubic spline
135
EXCERPTED FROM SIGGRAPH 92, COURSE 23
139
----------------------------------------------------------------------*/
142
#define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
144
static int p[B + B + 2];
145
static float g[B + B + 2][3];
146
#define setup(i,b0,b1,r0,r1) \
147
t = vec[i] + 10000.; \
148
b0 = ((int)t) & (B-1); \
149
b1 = (b0+1) & (B-1); \
156
int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
157
float rx0, rx1, ry0, ry1, rz0, rz1, *q, sx, sy, sz, a, b, c, d, t, u, v;
160
setup(0, bx0, bx1, rx0, rx1);
161
setup(1, by0, by1, ry0, ry1);
162
setup(2, bz0, bz1, rz0, rz1);
172
#define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
173
#define surve(t) ( t * t * (3. - 2. * t) )
174
#define lerp(t, a, b) ( a + t * (b - a) )
181
u = at(rx0, ry0, rz0);
183
v = at(rx1, ry0, rz0);
187
u = at(rx0, ry1, rz0);
189
v = at(rx1, ry1, rz0);
192
c = lerp(sy, a, b); /* interpolate in y at lo x */
195
u = at(rx0, ry0, rz1);
197
v = at(rx1, ry0, rz1);
201
u = at(rx0, ry1, rz1);
203
v = at(rx1, ry1, rz1);
206
d = lerp(sy, a, b); /* interpolate in y at hi x */
208
return 1.5 * lerp(sz, c, d); /* interpolate in z */
218
/* Create an array of random gradient vectors uniformly on the unit sphere */
221
for (i = 0; i < B; i++) {
222
do { /* Choose uniformly in a cube */
223
for (j = 0; j < 3; j++)
224
v[j] = (float) ((rand() % (B + B)) - B) / B;
226
} while (s > 1.0); /* If not in sphere try again */
228
for (j = 0; j < 3; j++) /* Else normalize */
232
/* Create a pseudorandom permutation of [1..B] */
233
for (i = 0; i < B; i++)
235
for (i = B; i > 0; i -= 2) {
237
p[i] = p[j = rand() % B];
241
/* Extend g and p arrays to allow for faster indexing */
242
for (i = 0; i < B + 2; i++) {
244
for (j = 0; j < 3; j++)
245
g[B + i][j] = g[i][j];
251
turbulence(float point[3], float lofreq, float hifreq)
255
p[0] = point[0] + 123.456;
260
for (freq = lofreq; freq < hifreq; freq *= 2.) {
261
t += fabs(noise3(p)) / freq;
266
return t - 0.3; /* readjust to make mean value = 0.0 */
271
create3Dtexture(void)
273
unsigned char *voxels = NULL;
279
printf("creating 3d textures...\n");
282
malloc((size_t) (4 * tex_width * tex_height * tex_depth));
284
for (i = 0; i < tex_width; i++) {
286
for (j = 0; j < tex_height; j++) {
288
for (k = 0; k < tex_depth; k++) {
290
tmp = (sin(k * i * j + turbulence(vec, 0.01, 1)) + 1) * 127.5;
299
printf("setting up 3d texture...\n");
301
glBindTexture(GL_TEXTURE_3D, NOISE_TEXTURE);
302
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
303
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
304
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
305
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
307
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
308
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
309
tex_width, tex_height, tex_depth,
310
0, GL_RGBA, GL_UNSIGNED_BYTE, voxels);
314
printf("finished setting up 3d texture image.\n");
321
printf("\nUsage: stex3d <cmd line options>\n");
322
printf(" cmd line options:\n");
323
printf(" -wxxx Width of the texture (Default=64)\n");
324
printf(" -hxxx Height of the texture (Default=64)\n");
325
printf(" -dxxx Depth of the texture (Default=64)\n");
326
printf(" Keyboard Options:\n");
327
printf(" up/down rotate around X\n");
328
printf(" left/right rotate around Y\n");
329
printf(" z/Z rotate around Z\n");
330
printf(" a toggle animation\n");
331
printf(" s toggle smooth shading\n");
332
printf(" t toggle texgen mode\n");
333
printf(" o toggle object: torus/sphere\n");
334
printf(" i toggle texture image: noise/gradient\n");
339
parseCmdLine(int argc, char **argv)
343
for (i = 1; i < argc; i++) {
344
if (strcmp(argv[i], "-help") == 0) {
348
else if (strstr(argv[i], "-w") != NULL) {
349
tex_width = atoi((argv[i]) + 2);
351
else if (strstr(argv[i], "-h") != NULL) {
352
tex_height = atoi((argv[i]) + 2);
354
else if (strstr(argv[i], "-d") != NULL) {
355
tex_depth = atoi((argv[i]) + 2);
358
printf("%s (Bad option).\n", argv[i]);
363
if (tex_width == 0 || tex_height == 0 || tex_depth == 0) {
364
printf("%s (Bad option).\n", "size parameters can't be 0");
375
static const GLfloat sPlane[4] = { 0.5, 0, 0, -.5 };
376
static const GLfloat tPlane[4] = { 0, 0.5, 0, -.5 };
377
static const GLfloat rPlane[4] = { 0, 0, 0.5, -.5 };
379
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
382
glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
383
glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
384
glTexGenfv(GL_R, GL_EYE_PLANE, rPlane);
387
glRotatef(angx, 1.0, 0.0, 0.0);
388
glRotatef(angy, 0.0, 1.0, 0.0);
389
glRotatef(angz, 0.0, 0.0, 1.0);
392
glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
393
glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
394
glTexGenfv(GL_R, GL_EYE_PLANE, rPlane);
398
glEnable(GL_TEXTURE_GEN_S);
399
glEnable(GL_TEXTURE_GEN_T);
400
glEnable(GL_TEXTURE_GEN_R);
403
glDisable(GL_TEXTURE_GEN_S);
404
glDisable(GL_TEXTURE_GEN_T);
405
glDisable(GL_TEXTURE_GEN_R);
408
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, Filter);
409
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, Filter);
411
glCallList(CurObject);
421
float ar = (float) w / (float) h;
424
glViewport(0, 0, (GLint) w, (GLint) h);
425
glMatrixMode(GL_PROJECTION);
427
glFrustum(-ax, ax, -ay, ay, 2, 20);
428
/*glOrtho(-2, 2, -2, 2, -10, 10);*/
429
glMatrixMode(GL_MODELVIEW);
431
glTranslatef(0, 0, -4);
438
float t = glutGet(GLUT_ELAPSED_TIME);
447
SpecialKey(int k, int x, int y)
470
KeyHandler(unsigned char key, int x, int y)
472
static const char *mode[] = {
473
"glTexCoord3f (no texgen)",
474
"texgen fixed to object coords",
475
"texgen fixed to eye coords"
482
case 'Q': /* quit game. */
494
glShadeModel(GL_SMOOTH);
496
glShadeModel(GL_FLAT);
502
printf("Texgen: %s\n", mode[texgen]);
505
if (CurObject == TORUS)
511
if (Filter == GL_LINEAR)
517
if (CurTexture == NOISE_TEXTURE)
518
CurTexture = GRADIENT_TEXTURE;
520
CurTexture = NOISE_TEXTURE;
521
glBindTexture(GL_TEXTURE_3D, CurTexture);
532
wireframe = !wireframe;
534
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
536
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
546
create3Dgradient(void)
550
unsigned char *voxels = NULL;
552
voxels = (unsigned char *) malloc(4 * tex_width * tex_height * tex_depth);
555
for (i = 0; i < tex_depth; i++) {
556
for (j = 0; j < tex_height; j++) {
557
for (k = 0; k < tex_width; k++) {
558
GLint r = (255 * i) / (tex_depth - 1);
559
GLint g = (255 * j) / (tex_height - 1);
560
GLint b = (255 * k) / (tex_width - 1);
570
glBindTexture(GL_TEXTURE_3D, GRADIENT_TEXTURE);
571
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
572
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
573
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
574
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
576
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
577
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
578
tex_width, tex_height, tex_depth,
579
0, GL_RGBA, GL_UNSIGNED_BYTE, voxels);
589
static const GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
590
static const GLfloat mat_shininess[] = { 25.0 };
591
static const GLfloat gray[] = { 0.6, 0.6, 0.6, 0.0 };
592
static const GLfloat white[] = { 1.0, 1.0, 1.0, 0.0 };
593
static const GLfloat light_position[] = { 0.0, 1.0, 1.0, 0.0 };
597
/* see if we have OpenGL 1.2 or later, for 3D texturing */
599
const char *version = (const char *) glGetString(GL_VERSION);
600
if (strncmp(version, "1.0", 3) == 0 || strncmp(version, "1.1", 3) == 0) {
601
printf("Sorry, OpenGL 1.2 or later is required\n");
605
printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
606
glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &max);
607
printf("GL_MAX_3D_TEXTURE_SIZE: %d\n", max);
608
printf("Current 3D texture size: %d x %d x %d\n",
609
tex_width, tex_height, tex_depth);
612
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
613
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
614
glLightfv(GL_LIGHT1, GL_POSITION, light_position);
615
glLightfv(GL_LIGHT1, GL_AMBIENT, gray);
616
glLightfv(GL_LIGHT1, GL_DIFFUSE, white);
617
glLightfv(GL_LIGHT1, GL_SPECULAR, white);
618
glColorMaterial(GL_FRONT, GL_DIFFUSE);
619
glEnable(GL_COLOR_MATERIAL);
620
glEnable(GL_LIGHTING);
623
glClearColor(.5, .5, .5, 0);
628
gluQuadricTexture( q, GL_TRUE );
629
glNewList(SPHERE, GL_COMPILE);
630
gluSphere( q, 0.95, 30, 15 );
643
glEnable(GL_TEXTURE_3D);
646
glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
649
glEnable(GL_DEPTH_TEST);
651
glColor3f(0.6, 0.7, 0.8);
656
main(int argc, char **argv)
658
glutInit(&argc, argv);
660
if (parseCmdLine(argc, argv) == GL_FALSE) {
664
glutInitWindowPosition(0, 0);
665
glutInitWindowSize(400, 400);
666
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
668
if (glutCreateWindow("stex3d") <= 0) {
678
glutReshapeFunc(resize);
679
glutKeyboardFunc(KeyHandler);
680
glutSpecialFunc(SpecialKey);
681
glutDisplayFunc(drawScene);