1 /* $Id: stex3d.c,v 1.5 2000/06/27 17:04:43 brianp Exp $ */
3 /*-----------------------------
4 * stex3d.c GL example of the mesa 3d-texture extention to simulate procedural
5 * texturing, it uses a perlin noise and turbulence functions.
7 * Author: Daniel Barrero
9 * dbarrero@pegasus.uniandes.edu.co
11 * Converted to GLUT by brianp on 1/1/98
14 * cc stex3d.c -o stex3d -lglut -lMesaGLU -lMesaGL -lX11 -lXext -lm
16 *---------------------------- */
20 * Revision 1.5 2000/06/27 17:04:43 brianp
21 * fixed compiler warnings
23 * Revision 1.4 2000/03/22 19:48:57 brianp
24 * converted from GL_EXT_texture3D to GL 1.2
26 * Revision 1.3 1999/12/16 08:54:22 brianp
27 * added a cast to malloc call
29 * Revision 1.2 1999/09/17 12:27:01 brianp
30 * silenced some warnings
32 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
35 * Revision 3.1 1998/06/09 01:53:49 brianp
36 * main() should return an int
38 * Revision 3.0 1998/02/14 18:42:29 brianp
50 /* function declarations */
52 #define M_PI 3.14159265358979323846
57 create3Dtexture(void),
63 float turbulence(float point
[3], float lofreq
, float hifreq
);
65 void KeyHandler( unsigned char key
, int x
, int y
);
66 GLenum
parseCmdLine(int argc
, char **argv
);
67 float noise3(float vec
[3]);
69 /* global variables */
70 GLenum rgb
, doubleBuffer
, directRender
, windType
; /* visualization state*/
71 float tex_width
,tex_height
,tex_depth
; /* texture volume dimensions */
72 unsigned char *voxels
; /* texture data ptr */
76 /*function definitions */
77 int main(int argc
, char **argv
)
80 if (parseCmdLine(argc
, argv
) == GL_FALSE
) {
84 glutInitWindowPosition(0, 0);
85 glutInitWindowSize(400, 400);
86 windType
= (rgb
) ? GLUT_RGB
: GLUT_INDEX
;
87 windType
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
88 windType
|= GLUT_DEPTH
;
89 glutInitDisplayMode(windType
);
91 if (glutCreateWindow("stex3d") <= 0) {
97 glutReshapeFunc(resize
);
98 glutKeyboardFunc(KeyHandler
);
99 glutDisplayFunc(drawScene
);
107 GLfloat mat_specular
[] = { 1.0, 1.0, 1.0, 1.0 };
108 GLfloat mat_shininess
[] = { 25.0 };
109 GLfloat gray
[] = { 0.6, 0.6, 0.6, 0.0 };
110 GLfloat white
[] = { 1.0, 1.0, 1.0, 0.0 };
111 GLfloat light_position
[] = { 0.0, 1.0, 1.0, 0.0 };
113 glMaterialfv(GL_FRONT
, GL_SPECULAR
, mat_specular
);
114 glMaterialfv(GL_FRONT
, GL_SHININESS
, mat_shininess
);
115 glLightfv(GL_LIGHT1
, GL_POSITION
, light_position
);
116 glLightfv(GL_LIGHT1
, GL_AMBIENT
, gray
);
117 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, white
);
118 glLightfv(GL_LIGHT1
, GL_SPECULAR
, white
);
119 glColorMaterial(GL_FRONT
, GL_DIFFUSE
);
120 glEnable(GL_COLOR_MATERIAL
);
121 glEnable(GL_LIGHTING
);
124 /* create torus for texturing */
125 figure
=glGenLists(1);
127 /* tkSolidTorus(figure,0.3,1.2);*/
129 /* start the noise function variables */
132 /* see if we have OpenGL 1.2 or later, for 3D texturing */
134 const char *version
= (const char *) glGetString(GL_VERSION
);
135 if (strncmp(version
, "1.0", 3) == 0 ||
136 strncmp(version
, "1.1", 3) == 0) {
137 printf("Sorry, OpenGL 1.2 or later is required\n");
142 /* if texture is supported then generate the texture */
145 glEnable(GL_TEXTURE_3D
);
147 glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
150 glEnable(GL_DEPTH_TEST
);
152 glShadeModel(GL_FLAT
);
153 glColor3f(0.6,0.7,0.8);
156 void buildFigure(void)
158 float theta1
, phi1
, theta2
, phi2
, rings
, sides
;
159 float v0
[03], v1
[3], v2
[3], v3
[3];
160 float t0
[03], t1
[3], t2
[3], t3
[3];
161 float n0
[3], n1
[3], n2
[3], n3
[3];
162 float innerRadius
=0.4;
163 float outerRadius
=0.8;
168 scalFac
=1/(outerRadius
*2);
170 glNewList(figure
, GL_COMPILE
);
171 for (i
= 0; i
< rings
; i
++) {
172 theta1
= (float)i
* 2.0 * M_PI
/ rings
;
173 theta2
= (float)(i
+ 1) * 2.0 * M_PI
/ rings
;
174 for (j
= 0; j
< sides
; j
++) {
175 phi1
= (float)j
* 2.0 * M_PI
/ sides
;
176 phi2
= (float)(j
+ 1) * 2.0 * M_PI
/ sides
;
178 v0
[0] = cos(theta1
) * (outerRadius
+ innerRadius
* cos(phi1
));
179 v0
[1] = -sin(theta1
) * (outerRadius
+ innerRadius
* cos(phi1
));
180 v0
[2] = innerRadius
* sin(phi1
);
182 v1
[0] = cos(theta2
) * (outerRadius
+ innerRadius
* cos(phi1
));
183 v1
[1] = -sin(theta2
) * (outerRadius
+ innerRadius
* cos(phi1
));
184 v1
[2] = innerRadius
* sin(phi1
);
185 v2
[0] = cos(theta2
) * (outerRadius
+ innerRadius
* cos(phi2
));
186 v2
[1] = -sin(theta2
) * (outerRadius
+ innerRadius
* cos(phi2
));
187 v2
[2] = innerRadius
* sin(phi2
);
189 v3
[0] = cos(theta1
) * (outerRadius
+ innerRadius
* cos(phi2
));
190 v3
[1] = -sin(theta1
) * (outerRadius
+ innerRadius
* cos(phi2
));
191 v3
[2] = innerRadius
* sin(phi2
);
193 n0
[0] = cos(theta1
) * (cos(phi1
));
194 n0
[1] = -sin(theta1
) * (cos(phi1
));
197 n1
[0] = cos(theta2
) * (cos(phi1
));
198 n1
[1] = -sin(theta2
) * (cos(phi1
));
201 n2
[0] = cos(theta2
) * (cos(phi2
));
202 n2
[1] = -sin(theta2
) * (cos(phi2
));
205 n3
[0] = cos(theta1
) * (cos(phi2
));
206 n3
[1] = -sin(theta1
) * (cos(phi2
));
209 t0
[0] = v0
[0]*scalFac
+ 0.5;
210 t0
[1] = v0
[1]*scalFac
+ 0.5;
211 t0
[2] = v0
[2]*scalFac
+ 0.5;
213 t1
[0] = v1
[0]*scalFac
+ 0.5;
214 t1
[1] = v1
[1]*scalFac
+ 0.5;
215 t1
[2] = v1
[2]*scalFac
+ 0.5;
217 t2
[0] = v2
[0]*scalFac
+ 0.5;
218 t2
[1] = v2
[1]*scalFac
+ 0.5;
219 t2
[2] = v2
[2]*scalFac
+ 0.5;
221 t3
[0] = v3
[0]*scalFac
+ 0.5;
222 t3
[1] = v3
[1]*scalFac
+ 0.5;
223 t3
[2] = v3
[2]*scalFac
+ 0.5;
226 glNormal3fv(n3
); glTexCoord3fv(t3
); glVertex3fv(v3
);
227 glNormal3fv(n2
); glTexCoord3fv(t2
); glVertex3fv(v2
);
228 glNormal3fv(n1
); glTexCoord3fv(t1
); glVertex3fv(v1
);
229 glNormal3fv(n0
); glTexCoord3fv(t0
); glVertex3fv(v0
);
236 void create3Dtexture()
243 printf("creating 3d textures...\n");
244 voxels
= (unsigned char *) malloc((size_t)(4*tex_width
*tex_height
*tex_depth
));
246 for (i
=0;i
<tex_width
;i
++){
248 for (j
=0;j
<tex_height
;j
++) {
250 for (k
=0;k
<tex_depth
;k
++) {
252 tmp
=(sin(k
*i
*j
+turbulence(vec
,0.01,1))+1)*127.5;
261 printf("setting up 3d texture...\n");
262 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
263 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
264 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
265 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
266 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
267 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_R
, GL_REPEAT
);
268 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
270 glTexImage3D(GL_TEXTURE_3D
, 0, GL_RGBA
,
271 tex_width
, tex_height
, tex_depth
,
272 0, GL_RGBA
, GL_UNSIGNED_BYTE
, voxels
);
274 printf("finished setting up 3d texture image...\n");
279 printf("\nUsage: stex3d <cmd line options>\n");
280 printf(" cmd line options:\n");
281 printf(" -help print this help!\n");
282 printf(" -rgb RGBA mode. (Default)\n");
283 printf(" -ci Color index mode.\n");
284 printf(" -sb Single buffer mode. (Default)\n");
285 printf(" -db Double buffer mode. \n");
286 printf(" -dr Direct render mode.\n");
287 printf(" -ir Indirect render mode. (Default)\n");
288 printf(" -wxxx Width of the texture (Default=64)\n");
289 printf(" -hxxx Height of the texture (Default=64)\n");
290 printf(" -dxxx Depth of the texture (Default=64)\n");
291 printf(" Keyboard Options:\n");
292 printf(" 1 Object Texture coordinates (Default)\n");
293 printf(" 2 Eye Texture coordinates \n");
294 printf(" x rotate around x clockwise\n");
295 printf(" X rotate around x counter clockwise\n");
296 printf(" y rotate around y clockwise\n");
297 printf(" Y rotate around y counter clockwise\n");
298 printf(" z rotate around z clockwise\n");
299 printf(" Z rotate around z counter clockwise\n");
300 printf(" t enable 3-D texuring (Default)\n");
301 printf(" T disable 3-D texuring\n");
302 printf(" s smooth shading \n");
303 printf(" S flat shading (Default)\n");
308 /* visualization defaults */
310 doubleBuffer
= GL_FALSE
;
311 directRender
= GL_TRUE
;
321 GLenum
parseCmdLine(int argc
, char **argv
)
327 for (i
= 1; i
< argc
; i
++) {
328 if (strcmp(argv
[i
], "-ci") == 0) {
330 } else if (strcmp(argv
[i
], "-rgb") == 0) {
332 } else if (strcmp(argv
[i
], "-sb") == 0) {
333 doubleBuffer
= GL_FALSE
;
334 } else if (strcmp(argv
[i
], "-db") == 0) {
335 doubleBuffer
= GL_TRUE
;
336 } else if (strcmp(argv
[i
], "-dr") == 0) {
337 directRender
= GL_TRUE
;
338 } else if (strcmp(argv
[i
], "-ir") == 0) {
339 directRender
= GL_FALSE
;
340 } else if (strstr(argv
[i
], "-w") == 0) {
341 tex_width
=atoi((argv
[i
])+2);
342 } else if (strstr(argv
[i
], "-h") == 0) {
343 tex_height
=atoi((argv
[i
])+2);
344 } else if (strstr(argv
[i
], "-d") == 0) {
345 tex_depth
=atoi((argv
[i
])+2);
346 } else if (strcmp(argv
[i
], "-help") == 0) {
350 printf("%s (Bad option).\n", argv
[i
]);
355 if(tex_width
==0 || tex_height
==0 || tex_depth
==0) {
356 printf("%s (Bad option).\n", "size parameters can't be 0");
365 /* clear background, z buffer etc */
366 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
368 glRotatef(angx
,1.0,0.0,0.0);
369 glRotatef(angy
,0.0,1.0,0.0);
370 glRotatef(angz
,0.0,0.0,1.0);
380 void resize(int w
, int h
)
382 glViewport(0, 0, (GLint
)w
, (GLint
)h
);
383 glMatrixMode(GL_PROJECTION
);
385 glOrtho(-2,2,-2,2,-5,10);
386 glMatrixMode(GL_MODELVIEW
);
388 glTranslatef(0,0,-5);
391 static void cleanEverything(void)
397 void KeyHandler( unsigned char key
, int x
, int y
)
404 case 'Q': /* quit game. */
427 glEnable(GL_TEXTURE_3D
);
430 glDisable(GL_TEXTURE_3D
);
433 glShadeModel(GL_SMOOTH
);
436 glShadeModel(GL_FLAT
);
439 glDisable(GL_TEXTURE_GEN_S
);
440 glDisable(GL_TEXTURE_GEN_T
);
441 glDisable(GL_TEXTURE_GEN_R
);
444 glEnable(GL_TEXTURE_GEN_S
);
445 glEnable(GL_TEXTURE_GEN_T
);
446 glEnable(GL_TEXTURE_GEN_R
);
454 /*--------------------------------------------------------------------
455 noise function over R3 - implemented by a pseudorandom tricubic spline
456 EXCERPTED FROM SIGGRAPH 92, COURSE 23
460 ----------------------------------------------------------------------*/
463 #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
465 static int p
[B
+ B
+ 2];
466 static float g
[B
+ B
+ 2][3];
467 #define setup(i,b0,b1,r0,r1) \
468 t = vec[i] + 10000.; \
469 b0 = ((int)t) & (B-1); \
470 b1 = (b0+1) & (B-1); \
474 float noise3(float vec
[3])
476 int bx0
, bx1
, by0
, by1
, bz0
, bz1
, b00
, b10
, b01
, b11
;
477 float rx0
, rx1
, ry0
, ry1
, rz0
, rz1
, *q
, sx
, sy
, sz
, a
, b
, c
, d
, t
, u
, v
;
480 setup(0, bx0
,bx1
, rx0
,rx1
);
481 setup(1, by0
,by1
, ry0
,ry1
);
482 setup(2, bz0
,bz1
, rz0
,rz1
);
492 #define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
493 #define surve(t) ( t * t * (3. - 2. * t) )
494 #define lerp(t, a, b) ( a + t * (b - a) )
500 q
= g
[ b00
+ bz0
] ; u
= at(rx0
,ry0
,rz0
);
501 q
= g
[ b10
+ bz0
] ; v
= at(rx1
,ry0
,rz0
);
504 q
= g
[ b01
+ bz0
] ; u
= at(rx0
,ry1
,rz0
);
505 q
= g
[ b11
+ bz0
] ; v
= at(rx1
,ry1
,rz0
);
508 c
= lerp(sy
, a
, b
); /* interpolate in y at lo x */
510 q
= g
[ b00
+ bz1
] ; u
= at(rx0
,ry0
,rz1
);
511 q
= g
[ b10
+ bz1
] ; v
= at(rx1
,ry0
,rz1
);
514 q
= g
[ b01
+ bz1
] ; u
= at(rx0
,ry1
,rz1
);
515 q
= g
[ b11
+ bz1
] ; v
= at(rx1
,ry1
,rz1
);
518 d
= lerp(sy
, a
, b
); /* interpolate in y at hi x */
520 return 1.5 * lerp(sz
, c
, d
); /* interpolate in z */
529 /* Create an array of random gradient vectors uniformly on the unit sphere */
532 for (i
= 0 ; i
< B
; i
++) {
533 do { /* Choose uniformly in a cube */ for (j
=0 ; j
<3 ; j
++)
534 v
[j
] = (float)((rand() % (B
+ B
)) - B
) / B
;
536 } while (s
> 1.0); /* If not in sphere try again */ s
= sqrt(s
);
537 for (j
= 0 ; j
< 3 ; j
++) /* Else normalize */
541 /* Create a pseudorandom permutation of [1..B] */
542 for (i
= 0 ; i
< B
; i
++)
544 for (i
= B
; i
> 0 ; i
-= 2) {
546 p
[i
] = p
[j
= rand() % B
];
550 /* Extend g and p arrays to allow for faster indexing */
551 for (i
= 0 ; i
< B
+ 2 ; i
++) {
553 for (j
= 0 ; j
< 3 ; j
++)
554 g
[B
+ i
][j
] = g
[i
][j
];
558 float turbulence(float point
[3], float lofreq
, float hifreq
)
562 p
[0] = point
[0] + 123.456;
567 for (freq
= lofreq
; freq
< hifreq
; freq
*= 2.) {
568 t
+= fabs(noise3(p
)) / freq
;
573 return t
- 0.3; /* readjust to make mean value = 0.0 */