2350532c9815bff77c2d954dba1096cdce23f411
1 /* $Id: stex3d.c,v 1.4 2000/03/22 19:48:57 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.4 2000/03/22 19:48:57 brianp
21 * converted from GL_EXT_texture3D to GL 1.2
23 * Revision 1.3 1999/12/16 08:54:22 brianp
24 * added a cast to malloc call
26 * Revision 1.2 1999/09/17 12:27:01 brianp
27 * silenced some warnings
29 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
32 * Revision 3.1 1998/06/09 01:53:49 brianp
33 * main() should return an int
35 * Revision 3.0 1998/02/14 18:42:29 brianp
47 /* function declarations */
49 #define M_PI 3.14159265358979323846
54 create3Dtexture(void),
60 float turbulence(float point
[3], float lofreq
, float hifreq
);
62 void KeyHandler( unsigned char key
, int x
, int y
);
63 GLenum
parseCmdLine(int argc
, char **argv
);
64 float noise3(float vec
[3]);
66 /* global variables */
67 GLenum rgb
, doubleBuffer
, directRender
, windType
; /* visualization state*/
68 float tex_width
,tex_height
,tex_depth
; /* texture volume dimensions */
69 unsigned char *voxels
; /* texture data ptr */
73 /*function definitions */
74 int main(int argc
, char **argv
)
77 if (parseCmdLine(argc
, argv
) == GL_FALSE
) {
81 glutInitWindowPosition(0, 0);
82 glutInitWindowSize(400, 400);
83 windType
= (rgb
) ? GLUT_RGB
: GLUT_INDEX
;
84 windType
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
85 windType
|= GLUT_DEPTH
;
86 glutInitDisplayMode(windType
);
88 if (glutCreateWindow("stex3d") <= 0) {
94 glutReshapeFunc(resize
);
95 glutKeyboardFunc(KeyHandler
);
96 glutDisplayFunc(drawScene
);
104 GLfloat mat_specular
[] = { 1.0, 1.0, 1.0, 1.0 };
105 GLfloat mat_shininess
[] = { 25.0 };
106 GLfloat gray
[] = { 0.6, 0.6, 0.6, 0.0 };
107 GLfloat white
[] = { 1.0, 1.0, 1.0, 0.0 };
108 GLfloat light_position
[] = { 0.0, 1.0, 1.0, 0.0 };
110 glMaterialfv(GL_FRONT
, GL_SPECULAR
, mat_specular
);
111 glMaterialfv(GL_FRONT
, GL_SHININESS
, mat_shininess
);
112 glLightfv(GL_LIGHT1
, GL_POSITION
, light_position
);
113 glLightfv(GL_LIGHT1
, GL_AMBIENT
, gray
);
114 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, white
);
115 glLightfv(GL_LIGHT1
, GL_SPECULAR
, white
);
116 glColorMaterial(GL_FRONT
, GL_DIFFUSE
);
117 glEnable(GL_COLOR_MATERIAL
);
118 glEnable(GL_LIGHTING
);
121 /* create torus for texturing */
122 figure
=glGenLists(1);
124 /* tkSolidTorus(figure,0.3,1.2);*/
126 /* start the noise function variables */
129 /* see if we have OpenGL 1.2 or later, for 3D texturing */
131 const char *version
= (const char *) glGetString(GL_VERSION
);
132 if (strncmp(version
, "1.0", 3) == 0 ||
133 strncmp(version
, "1.1", 3) == 0) {
134 printf("Sorry, OpenGL 1.2 or later is required\n");
139 /* if texture is supported then generate the texture */
142 glEnable(GL_TEXTURE_3D
);
144 glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
147 glEnable(GL_DEPTH_TEST
);
149 glShadeModel(GL_FLAT
);
150 glColor3f(0.6,0.7,0.8);
153 void buildFigure(void)
155 float theta1
, phi1
, theta2
, phi2
, rings
, sides
;
156 float v0
[03], v1
[3], v2
[3], v3
[3];
157 float t0
[03], t1
[3], t2
[3], t3
[3];
158 float n0
[3], n1
[3], n2
[3], n3
[3];
159 float innerRadius
=0.4;
160 float outerRadius
=0.8;
165 scalFac
=1/(outerRadius
*2);
167 glNewList(figure
, GL_COMPILE
);
168 for (i
= 0; i
< rings
; i
++) {
169 theta1
= (float)i
* 2.0 * M_PI
/ rings
;
170 theta2
= (float)(i
+ 1) * 2.0 * M_PI
/ rings
;
171 for (j
= 0; j
< sides
; j
++) {
172 phi1
= (float)j
* 2.0 * M_PI
/ sides
;
173 phi2
= (float)(j
+ 1) * 2.0 * M_PI
/ sides
;
175 v0
[0] = cos(theta1
) * (outerRadius
+ innerRadius
* cos(phi1
));
176 v0
[1] = -sin(theta1
) * (outerRadius
+ innerRadius
* cos(phi1
));
177 v0
[2] = innerRadius
* sin(phi1
);
179 v1
[0] = cos(theta2
) * (outerRadius
+ innerRadius
* cos(phi1
));
180 v1
[1] = -sin(theta2
) * (outerRadius
+ innerRadius
* cos(phi1
));
181 v1
[2] = innerRadius
* sin(phi1
);
182 v2
[0] = cos(theta2
) * (outerRadius
+ innerRadius
* cos(phi2
));
183 v2
[1] = -sin(theta2
) * (outerRadius
+ innerRadius
* cos(phi2
));
184 v2
[2] = innerRadius
* sin(phi2
);
186 v3
[0] = cos(theta1
) * (outerRadius
+ innerRadius
* cos(phi2
));
187 v3
[1] = -sin(theta1
) * (outerRadius
+ innerRadius
* cos(phi2
));
188 v3
[2] = innerRadius
* sin(phi2
);
190 n0
[0] = cos(theta1
) * (cos(phi1
));
191 n0
[1] = -sin(theta1
) * (cos(phi1
));
194 n1
[0] = cos(theta2
) * (cos(phi1
));
195 n1
[1] = -sin(theta2
) * (cos(phi1
));
198 n2
[0] = cos(theta2
) * (cos(phi2
));
199 n2
[1] = -sin(theta2
) * (cos(phi2
));
202 n3
[0] = cos(theta1
) * (cos(phi2
));
203 n3
[1] = -sin(theta1
) * (cos(phi2
));
206 t0
[0] = v0
[0]*scalFac
+ 0.5;
207 t0
[1] = v0
[1]*scalFac
+ 0.5;
208 t0
[2] = v0
[2]*scalFac
+ 0.5;
210 t1
[0] = v1
[0]*scalFac
+ 0.5;
211 t1
[1] = v1
[1]*scalFac
+ 0.5;
212 t1
[2] = v1
[2]*scalFac
+ 0.5;
214 t2
[0] = v2
[0]*scalFac
+ 0.5;
215 t2
[1] = v2
[1]*scalFac
+ 0.5;
216 t2
[2] = v2
[2]*scalFac
+ 0.5;
218 t3
[0] = v3
[0]*scalFac
+ 0.5;
219 t3
[1] = v3
[1]*scalFac
+ 0.5;
220 t3
[2] = v3
[2]*scalFac
+ 0.5;
223 glNormal3fv(n3
); glTexCoord3fv(t3
); glVertex3fv(v3
);
224 glNormal3fv(n2
); glTexCoord3fv(t2
); glVertex3fv(v2
);
225 glNormal3fv(n1
); glTexCoord3fv(t1
); glVertex3fv(v1
);
226 glNormal3fv(n0
); glTexCoord3fv(t0
); glVertex3fv(v0
);
233 void create3Dtexture()
240 printf("creating 3d textures...\n");
241 voxels
= (unsigned char *) malloc((size_t)(4*tex_width
*tex_height
*tex_depth
));
243 for (i
=0;i
<tex_width
;i
++){
245 for (j
=0;j
<tex_height
;j
++) {
247 for (k
=0;k
<tex_depth
;k
++) {
249 tmp
=(sin(k
*i
*j
+turbulence(vec
,0.01,1))+1)*127.5;
258 printf("setting up 3d texture...\n");
259 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
260 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
261 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
262 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
263 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
264 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_R
, GL_REPEAT
);
265 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
267 glTexImage3D(GL_TEXTURE_3D
, 0, GL_RGBA
,
268 tex_width
, tex_height
, tex_depth
,
269 0, GL_RGBA
, GL_UNSIGNED_BYTE
, voxels
);
271 printf("finished setting up 3d texture image...\n");
276 printf("\nUsage: stex3d <cmd line options>\n");
277 printf(" cmd line options:\n");
278 printf(" -help print this help!\n");
279 printf(" -rgb RGBA mode. (Default)\n");
280 printf(" -ci Color index mode.\n");
281 printf(" -sb Single buffer mode. (Default)\n");
282 printf(" -db Double buffer mode. \n");
283 printf(" -dr Direct render mode.\n");
284 printf(" -ir Indirect render mode. (Default)\n");
285 printf(" -wxxx Width of the texture (Default=64)\n");
286 printf(" -hxxx Height of the texture (Default=64)\n");
287 printf(" -dxxx Depth of the texture (Default=64)\n");
288 printf(" Keyboard Options:\n");
289 printf(" 1 Object Texture coordinates (Default)\n");
290 printf(" 2 Eye Texture coordinates \n");
291 printf(" x rotate around x clockwise\n");
292 printf(" X rotate around x counter clockwise\n");
293 printf(" y rotate around y clockwise\n");
294 printf(" Y rotate around y counter clockwise\n");
295 printf(" z rotate around z clockwise\n");
296 printf(" Z rotate around z counter clockwise\n");
297 printf(" t enable 3-D texuring (Default)\n");
298 printf(" T disable 3-D texuring\n");
299 printf(" s smooth shading \n");
300 printf(" S flat shading (Default)\n");
305 /* visualization defaults */
307 doubleBuffer
= GL_FALSE
;
308 directRender
= GL_TRUE
;
318 GLenum
parseCmdLine(int argc
, char **argv
)
324 for (i
= 1; i
< argc
; i
++) {
325 if (strcmp(argv
[i
], "-ci") == 0) {
327 } else if (strcmp(argv
[i
], "-rgb") == 0) {
329 } else if (strcmp(argv
[i
], "-sb") == 0) {
330 doubleBuffer
= GL_FALSE
;
331 } else if (strcmp(argv
[i
], "-db") == 0) {
332 doubleBuffer
= GL_TRUE
;
333 } else if (strcmp(argv
[i
], "-dr") == 0) {
334 directRender
= GL_TRUE
;
335 } else if (strcmp(argv
[i
], "-ir") == 0) {
336 directRender
= GL_FALSE
;
337 } else if (strstr(argv
[i
], "-w") == 0) {
338 tex_width
=atoi((argv
[i
])+2);
339 } else if (strstr(argv
[i
], "-h") == 0) {
340 tex_height
=atoi((argv
[i
])+2);
341 } else if (strstr(argv
[i
], "-d") == 0) {
342 tex_depth
=atoi((argv
[i
])+2);
343 } else if (strcmp(argv
[i
], "-help") == 0) {
347 printf("%s (Bad option).\n", argv
[i
]);
352 if(tex_width
==0 || tex_height
==0 || tex_depth
==0) {
353 printf("%s (Bad option).\n", "size parameters can't be 0");
362 /* clear background, z buffer etc */
363 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
365 glRotatef(angx
,1.0,0.0,0.0);
366 glRotatef(angy
,0.0,1.0,0.0);
367 glRotatef(angz
,0.0,0.0,1.0);
377 void resize(int w
, int h
)
379 glViewport(0, 0, (GLint
)w
, (GLint
)h
);
380 glMatrixMode(GL_PROJECTION
);
382 glOrtho(-2,2,-2,2,-5,10);
383 glMatrixMode(GL_MODELVIEW
);
385 glTranslatef(0,0,-5);
388 void cleanEverything(void)
394 void KeyHandler( unsigned char key
, int x
, int y
)
401 case 'Q': /* quit game. */
424 glEnable(GL_TEXTURE_3D
);
427 glDisable(GL_TEXTURE_3D
);
430 glShadeModel(GL_SMOOTH
);
433 glShadeModel(GL_FLAT
);
436 glDisable(GL_TEXTURE_GEN_S
);
437 glDisable(GL_TEXTURE_GEN_T
);
438 glDisable(GL_TEXTURE_GEN_R
);
441 glEnable(GL_TEXTURE_GEN_S
);
442 glEnable(GL_TEXTURE_GEN_T
);
443 glEnable(GL_TEXTURE_GEN_R
);
451 /*--------------------------------------------------------------------
452 noise function over R3 - implemented by a pseudorandom tricubic spline
453 EXCERPTED FROM SIGGRAPH 92, COURSE 23
457 ----------------------------------------------------------------------*/
460 #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
462 static int p
[B
+ B
+ 2];
463 static float g
[B
+ B
+ 2][3];
464 #define setup(i,b0,b1,r0,r1) \
465 t = vec[i] + 10000.; \
466 b0 = ((int)t) & (B-1); \
467 b1 = (b0+1) & (B-1); \
471 float noise3(float vec
[3])
473 int bx0
, bx1
, by0
, by1
, bz0
, bz1
, b00
, b10
, b01
, b11
;
474 float rx0
, rx1
, ry0
, ry1
, rz0
, rz1
, *q
, sx
, sy
, sz
, a
, b
, c
, d
, t
, u
, v
;
477 setup(0, bx0
,bx1
, rx0
,rx1
);
478 setup(1, by0
,by1
, ry0
,ry1
);
479 setup(2, bz0
,bz1
, rz0
,rz1
);
489 #define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
490 #define surve(t) ( t * t * (3. - 2. * t) )
491 #define lerp(t, a, b) ( a + t * (b - a) )
497 q
= g
[ b00
+ bz0
] ; u
= at(rx0
,ry0
,rz0
);
498 q
= g
[ b10
+ bz0
] ; v
= at(rx1
,ry0
,rz0
);
501 q
= g
[ b01
+ bz0
] ; u
= at(rx0
,ry1
,rz0
);
502 q
= g
[ b11
+ bz0
] ; v
= at(rx1
,ry1
,rz0
);
505 c
= lerp(sy
, a
, b
); /* interpolate in y at lo x */
507 q
= g
[ b00
+ bz1
] ; u
= at(rx0
,ry0
,rz1
);
508 q
= g
[ b10
+ bz1
] ; v
= at(rx1
,ry0
,rz1
);
511 q
= g
[ b01
+ bz1
] ; u
= at(rx0
,ry1
,rz1
);
512 q
= g
[ b11
+ bz1
] ; v
= at(rx1
,ry1
,rz1
);
515 d
= lerp(sy
, a
, b
); /* interpolate in y at hi x */
517 return 1.5 * lerp(sz
, c
, d
); /* interpolate in z */
526 /* Create an array of random gradient vectors uniformly on the unit sphere */
529 for (i
= 0 ; i
< B
; i
++) {
530 do { /* Choose uniformly in a cube */ for (j
=0 ; j
<3 ; j
++)
531 v
[j
] = (float)((rand() % (B
+ B
)) - B
) / B
;
533 } while (s
> 1.0); /* If not in sphere try again */ s
= sqrt(s
);
534 for (j
= 0 ; j
< 3 ; j
++) /* Else normalize */
538 /* Create a pseudorandom permutation of [1..B] */
539 for (i
= 0 ; i
< B
; i
++)
541 for (i
= B
; i
> 0 ; i
-= 2) {
543 p
[i
] = p
[j
= rand() % B
];
547 /* Extend g and p arrays to allow for faster indexing */
548 for (i
= 0 ; i
< B
+ 2 ; i
++) {
550 for (j
= 0 ; j
< 3 ; j
++)
551 g
[B
+ i
][j
] = g
[i
][j
];
555 float turbulence(float point
[3], float lofreq
, float hifreq
)
559 p
[0] = point
[0] + 123.456;
564 for (freq
= lofreq
; freq
< hifreq
; freq
*= 2.) {
565 t
+= fabs(noise3(p
)) / freq
;
570 return t
- 0.3; /* readjust to make mean value = 0.0 */