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
;
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;
55 scalFac
= 1 / (outerRadius
* 2);
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
;
65 v0
[0] = cos(theta1
) * (outerRadius
+ innerRadius
* cos(phi1
));
66 v0
[1] = -sin(theta1
) * (outerRadius
+ innerRadius
* cos(phi1
));
67 v0
[2] = innerRadius
* sin(phi1
);
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
);
76 v3
[0] = cos(theta1
) * (outerRadius
+ innerRadius
* cos(phi2
));
77 v3
[1] = -sin(theta1
) * (outerRadius
+ innerRadius
* cos(phi2
));
78 v3
[2] = innerRadius
* sin(phi2
);
80 n0
[0] = cos(theta1
) * (cos(phi1
));
81 n0
[1] = -sin(theta1
) * (cos(phi1
));
84 n1
[0] = cos(theta2
) * (cos(phi1
));
85 n1
[1] = -sin(theta2
) * (cos(phi1
));
88 n2
[0] = cos(theta2
) * (cos(phi2
));
89 n2
[1] = -sin(theta2
) * (cos(phi2
));
92 n3
[0] = cos(theta1
) * (cos(phi2
));
93 n3
[1] = -sin(theta1
) * (cos(phi2
));
96 t0
[0] = v0
[0] * scalFac
+ 0.5;
97 t0
[1] = v0
[1] * scalFac
+ 0.5;
98 t0
[2] = v0
[2] * scalFac
+ 0.5;
100 t1
[0] = v1
[0] * scalFac
+ 0.5;
101 t1
[1] = v1
[1] * scalFac
+ 0.5;
102 t1
[2] = v1
[2] * scalFac
+ 0.5;
104 t2
[0] = v2
[0] * scalFac
+ 0.5;
105 t2
[1] = v2
[1] * scalFac
+ 0.5;
106 t2
[2] = v2
[2] * scalFac
+ 0.5;
108 t3
[0] = v3
[0] * scalFac
+ 0.5;
109 t3
[1] = v3
[1] * scalFac
+ 0.5;
110 t3
[2] = v3
[2] * scalFac
+ 0.5;
132 /*--------------------------------------------------------------------
133 noise function over R3 - implemented by a pseudorandom tricubic spline
134 EXCERPTED FROM SIGGRAPH 92, COURSE 23
138 ----------------------------------------------------------------------*/
141 #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
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); \
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
;
159 setup(0, bx0
, bx1
, rx0
, rx1
);
160 setup(1, by0
, by1
, ry0
, ry1
);
161 setup(2, bz0
, bz1
, rz0
, rz1
);
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) )
180 u
= at(rx0
, ry0
, rz0
);
182 v
= at(rx1
, ry0
, rz0
);
186 u
= at(rx0
, ry1
, rz0
);
188 v
= at(rx1
, ry1
, rz0
);
191 c
= lerp(sy
, a
, b
); /* interpolate in y at lo x */
194 u
= at(rx0
, ry0
, rz1
);
196 v
= at(rx1
, ry0
, rz1
);
200 u
= at(rx0
, ry1
, rz1
);
202 v
= at(rx1
, ry1
, rz1
);
205 d
= lerp(sy
, a
, b
); /* interpolate in y at hi x */
207 return 1.5 * lerp(sz
, c
, d
); /* interpolate in z */
217 /* Create an array of random gradient vectors uniformly on the unit sphere */
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
;
225 } while (s
> 1.0); /* If not in sphere try again */
227 for (j
= 0; j
< 3; j
++) /* Else normalize */
231 /* Create a pseudorandom permutation of [1..B] */
232 for (i
= 0; i
< B
; i
++)
234 for (i
= B
; i
> 0; i
-= 2) {
236 p
[i
] = p
[j
= rand() % B
];
240 /* Extend g and p arrays to allow for faster indexing */
241 for (i
= 0; i
< B
+ 2; i
++) {
243 for (j
= 0; j
< 3; j
++)
244 g
[B
+ i
][j
] = g
[i
][j
];
250 turbulence(float point
[3], float lofreq
, float hifreq
)
254 p
[0] = point
[0] + 123.456;
259 for (freq
= lofreq
; freq
< hifreq
; freq
*= 2.) {
260 t
+= fabs(noise3(p
)) / freq
;
265 return t
- 0.3; /* readjust to make mean value = 0.0 */
270 create3Dtexture(void)
272 unsigned char *voxels
= NULL
;
278 printf("creating 3d textures...\n");
281 malloc((size_t) (4 * tex_width
* tex_height
* tex_depth
));
283 for (i
= 0; i
< tex_width
; i
++) {
285 for (j
= 0; j
< tex_height
; j
++) {
287 for (k
= 0; k
< tex_depth
; k
++) {
289 tmp
= (sin(k
* i
* j
+ turbulence(vec
, 0.01, 1)) + 1) * 127.5;
298 printf("setting up 3d texture...\n");
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
);
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
);
315 printf("finished setting up 3d texture image.\n");
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");
340 parseCmdLine(int argc
, char **argv
)
344 for (i
= 1; i
< argc
; i
++) {
345 if (strcmp(argv
[i
], "-help") == 0) {
349 else if (strstr(argv
[i
], "-w") != NULL
) {
350 tex_width
= atoi((argv
[i
]) + 2);
352 else if (strstr(argv
[i
], "-h") != NULL
) {
353 tex_height
= atoi((argv
[i
]) + 2);
355 else if (strstr(argv
[i
], "-d") != NULL
) {
356 tex_depth
= atoi((argv
[i
]) + 2);
359 printf("%s (Bad option).\n", argv
[i
]);
364 if (tex_width
== 0 || tex_height
== 0 || tex_depth
== 0) {
365 printf("%s (Bad option).\n", "size parameters can't be 0");
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 };
380 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
383 glTexGenfv(GL_S
, GL_EYE_PLANE
, sPlane
);
384 glTexGenfv(GL_T
, GL_EYE_PLANE
, tPlane
);
385 glTexGenfv(GL_R
, GL_EYE_PLANE
, rPlane
);
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);
393 glTexGenfv(GL_S
, GL_EYE_PLANE
, sPlane
);
394 glTexGenfv(GL_T
, GL_EYE_PLANE
, tPlane
);
395 glTexGenfv(GL_R
, GL_EYE_PLANE
, rPlane
);
399 glEnable(GL_TEXTURE_GEN_S
);
400 glEnable(GL_TEXTURE_GEN_T
);
401 glEnable(GL_TEXTURE_GEN_R
);
404 glDisable(GL_TEXTURE_GEN_S
);
405 glDisable(GL_TEXTURE_GEN_T
);
406 glDisable(GL_TEXTURE_GEN_R
);
409 glCallList(CurObject
);
419 float ar
= (float) w
/ (float) h
;
422 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
423 glMatrixMode(GL_PROJECTION
);
425 glFrustum(-ax
, ax
, -ay
, ay
, 2, 20);
426 /*glOrtho(-2, 2, -2, 2, -10, 10);*/
427 glMatrixMode(GL_MODELVIEW
);
429 glTranslatef(0, 0, -4);
436 float t
= glutGet(GLUT_ELAPSED_TIME
);
445 SpecialKey(int k
, int x
, int y
)
468 KeyHandler(unsigned char key
, int x
, int y
)
470 static const char *mode
[] = {
471 "glTexCoord3f (no texgen)",
472 "texgen fixed to object coords",
473 "texgen fixed to eye coords"
480 case 'Q': /* quit game. */
492 glShadeModel(GL_SMOOTH
);
494 glShadeModel(GL_FLAT
);
500 printf("Texgen: %s\n", mode
[texgen
]);
503 if (CurObject
== TORUS
)
509 if (CurTexture
== NOISE_TEXTURE
)
510 CurTexture
= GRADIENT_TEXTURE
;
512 CurTexture
= NOISE_TEXTURE
;
513 glBindTexture(GL_TEXTURE_3D
, CurTexture
);
523 wireframe
= !wireframe
;
525 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
527 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
537 create3Dgradient(void)
541 unsigned char *voxels
= NULL
;
543 voxels
= (unsigned char *) malloc(4 * tex_width
* tex_height
* tex_depth
);
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);
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
);
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
);
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 };
590 /* see if we have OpenGL 1.2 or later, for 3D texturing */
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");
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
);
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
);
616 glClearColor(.5, .5, .5, 0);
621 gluQuadricTexture( q
, GL_TRUE
);
622 glNewList(SPHERE
, GL_COMPILE
);
623 gluSphere( q
, 0.95, 30, 15 );
636 glEnable(GL_TEXTURE_3D
);
639 glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
642 glEnable(GL_DEPTH_TEST
);
644 glColor3f(0.6, 0.7, 0.8);
649 main(int argc
, char **argv
)
651 glutInit(&argc
, argv
);
653 if (parseCmdLine(argc
, argv
) == GL_FALSE
) {
657 glutInitWindowPosition(0, 0);
658 glutInitWindowSize(400, 400);
659 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
661 if (glutCreateWindow("stex3d") <= 0) {
671 glutReshapeFunc(resize
);
672 glutKeyboardFunc(KeyHandler
);
673 glutSpecialFunc(SpecialKey
);
674 glutDisplayFunc(drawScene
);