2a35ca161f5ad36424fe31f2be3e45e29a894ce7
1 /* $Id: stex3d.c,v 1.2 1999/09/17 12:27:01 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.2 1999/09/17 12:27:01 brianp
21 * silenced some warnings
23 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
26 * Revision 3.1 1998/06/09 01:53:49 brianp
27 * main() should return an int
29 * Revision 3.0 1998/02/14 18:42:29 brianp
41 /* function declarations */
43 #define M_PI 3.14159265358979323846
48 create3Dtexture(void),
54 float turbulence(float point
[3], float lofreq
, float hifreq
);
56 int isExtSupported(char *ext
);
57 void KeyHandler( unsigned char key
, int x
, int y
);
58 GLenum
parseCmdLine(int argc
, char **argv
);
59 float noise3(float vec
[3]);
61 /* global variables */
62 GLenum rgb
, doubleBuffer
, directRender
, windType
; /* visualization state*/
63 float tex_width
,tex_height
,tex_depth
; /* texture volume dimensions */
64 unsigned char *voxels
; /* texture data ptr */
68 /*function definitions */
69 int main(int argc
, char **argv
)
72 if (parseCmdLine(argc
, argv
) == GL_FALSE
) {
76 glutInitWindowPosition(0, 0);
77 glutInitWindowSize(400, 400);
78 windType
= (rgb
) ? GLUT_RGB
: GLUT_INDEX
;
79 windType
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
80 windType
|= GLUT_DEPTH
;
81 glutInitDisplayMode(windType
);
83 if (glutCreateWindow("stex3d") <= 0) {
89 glutReshapeFunc(resize
);
90 glutKeyboardFunc(KeyHandler
);
91 glutDisplayFunc(drawScene
);
99 GLfloat mat_specular
[] = { 1.0, 1.0, 1.0, 1.0 };
100 GLfloat mat_shininess
[] = { 25.0 };
101 GLfloat gray
[] = { 0.6, 0.6, 0.6, 0.0 };
102 GLfloat white
[] = { 1.0, 1.0, 1.0, 0.0 };
103 GLfloat light_position
[] = { 0.0, 1.0, 1.0, 0.0 };
105 glMaterialfv(GL_FRONT
, GL_SPECULAR
, mat_specular
);
106 glMaterialfv(GL_FRONT
, GL_SHININESS
, mat_shininess
);
107 glLightfv(GL_LIGHT1
, GL_POSITION
, light_position
);
108 glLightfv(GL_LIGHT1
, GL_AMBIENT
, gray
);
109 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, white
);
110 glLightfv(GL_LIGHT1
, GL_SPECULAR
, white
);
111 glColorMaterial(GL_FRONT
, GL_DIFFUSE
);
112 glEnable(GL_COLOR_MATERIAL
);
113 glEnable(GL_LIGHTING
);
116 /* create torus for texturing */
117 figure
=glGenLists(1);
119 /* tkSolidTorus(figure,0.3,1.2);*/
121 /* start the noise function variables */
124 /* see if the texture 3d extention is supported */
125 if (!isExtSupported("GL_EXT_texture3D")) {
126 printf("Sorry this GL implementation (%s) does not support 3d texture extentions\n",
127 (char *)(glGetString(GL_RENDERER
)));
131 /* if texture is supported then generate the texture */
134 glEnable(GL_TEXTURE_3D_EXT
);
136 glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
139 glEnable(GL_DEPTH_TEST
);
141 glShadeModel(GL_FLAT
);
142 glColor3f(0.6,0.7,0.8);
145 void buildFigure(void)
147 float theta1
, phi1
, theta2
, phi2
, rings
, sides
;
148 float v0
[03], v1
[3], v2
[3], v3
[3];
149 float t0
[03], t1
[3], t2
[3], t3
[3];
150 float n0
[3], n1
[3], n2
[3], n3
[3];
151 float innerRadius
=0.4;
152 float outerRadius
=0.8;
157 scalFac
=1/(outerRadius
*2);
159 glNewList(figure
, GL_COMPILE
);
160 for (i
= 0; i
< rings
; i
++) {
161 theta1
= (float)i
* 2.0 * M_PI
/ rings
;
162 theta2
= (float)(i
+ 1) * 2.0 * M_PI
/ rings
;
163 for (j
= 0; j
< sides
; j
++) {
164 phi1
= (float)j
* 2.0 * M_PI
/ sides
;
165 phi2
= (float)(j
+ 1) * 2.0 * M_PI
/ sides
;
167 v0
[0] = cos(theta1
) * (outerRadius
+ innerRadius
* cos(phi1
));
168 v0
[1] = -sin(theta1
) * (outerRadius
+ innerRadius
* cos(phi1
));
169 v0
[2] = innerRadius
* sin(phi1
);
171 v1
[0] = cos(theta2
) * (outerRadius
+ innerRadius
* cos(phi1
));
172 v1
[1] = -sin(theta2
) * (outerRadius
+ innerRadius
* cos(phi1
));
173 v1
[2] = innerRadius
* sin(phi1
);
174 v2
[0] = cos(theta2
) * (outerRadius
+ innerRadius
* cos(phi2
));
175 v2
[1] = -sin(theta2
) * (outerRadius
+ innerRadius
* cos(phi2
));
176 v2
[2] = innerRadius
* sin(phi2
);
178 v3
[0] = cos(theta1
) * (outerRadius
+ innerRadius
* cos(phi2
));
179 v3
[1] = -sin(theta1
) * (outerRadius
+ innerRadius
* cos(phi2
));
180 v3
[2] = innerRadius
* sin(phi2
);
182 n0
[0] = cos(theta1
) * (cos(phi1
));
183 n0
[1] = -sin(theta1
) * (cos(phi1
));
186 n1
[0] = cos(theta2
) * (cos(phi1
));
187 n1
[1] = -sin(theta2
) * (cos(phi1
));
190 n2
[0] = cos(theta2
) * (cos(phi2
));
191 n2
[1] = -sin(theta2
) * (cos(phi2
));
194 n3
[0] = cos(theta1
) * (cos(phi2
));
195 n3
[1] = -sin(theta1
) * (cos(phi2
));
198 t0
[0] = v0
[0]*scalFac
+ 0.5;
199 t0
[1] = v0
[1]*scalFac
+ 0.5;
200 t0
[2] = v0
[2]*scalFac
+ 0.5;
202 t1
[0] = v1
[0]*scalFac
+ 0.5;
203 t1
[1] = v1
[1]*scalFac
+ 0.5;
204 t1
[2] = v1
[2]*scalFac
+ 0.5;
206 t2
[0] = v2
[0]*scalFac
+ 0.5;
207 t2
[1] = v2
[1]*scalFac
+ 0.5;
208 t2
[2] = v2
[2]*scalFac
+ 0.5;
210 t3
[0] = v3
[0]*scalFac
+ 0.5;
211 t3
[1] = v3
[1]*scalFac
+ 0.5;
212 t3
[2] = v3
[2]*scalFac
+ 0.5;
215 glNormal3fv(n3
); glTexCoord3fv(t3
); glVertex3fv(v3
);
216 glNormal3fv(n2
); glTexCoord3fv(t2
); glVertex3fv(v2
);
217 glNormal3fv(n1
); glTexCoord3fv(t1
); glVertex3fv(v1
);
218 glNormal3fv(n0
); glTexCoord3fv(t0
); glVertex3fv(v0
);
225 void create3Dtexture()
232 printf("creating 3d textures...\n");
233 voxels
= (unsigned char *) malloc((4*tex_width
*tex_height
*tex_depth
));
235 for (i
=0;i
<tex_width
;i
++){
237 for (j
=0;j
<tex_height
;j
++) {
239 for (k
=0;k
<tex_depth
;k
++) {
241 tmp
=(sin(k
*i
*j
+turbulence(vec
,0.01,1))+1)*127.5;
250 printf("setting up 3d texture...\n");
251 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
252 glTexParameteri(GL_TEXTURE_3D_EXT
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
253 glTexParameteri(GL_TEXTURE_3D_EXT
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
254 glTexParameteri(GL_TEXTURE_3D_EXT
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
255 glTexParameteri(GL_TEXTURE_3D_EXT
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
256 glTexParameteri(GL_TEXTURE_3D_EXT
, GL_TEXTURE_WRAP_R_EXT
, GL_REPEAT
);
257 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
259 glTexImage3DEXT(GL_TEXTURE_3D_EXT
, 0, GL_RGBA
,
260 tex_width
, tex_height
, tex_depth
,
261 0, GL_RGBA
, GL_UNSIGNED_BYTE
, voxels
);
263 printf("finished setting up 3d texture image...\n");
266 int isExtSupported(char *ext
)
268 /* routine to find whether a specified OpenGL extension is supported */
272 char *allext
= (char *)(glGetString(GL_EXTENSIONS
));
275 if (len
<= 0) return 0;
279 if (!strncmp(c
,ext
,len
)) return 1;
287 printf("\nUsage: stex3d <cmd line options>\n");
288 printf(" cmd line options:\n");
289 printf(" -help print this help!\n");
290 printf(" -rgb RGBA mode. (Default)\n");
291 printf(" -ci Color index mode.\n");
292 printf(" -sb Single buffer mode. (Default)\n");
293 printf(" -db Double buffer mode. \n");
294 printf(" -dr Direct render mode.\n");
295 printf(" -ir Indirect render mode. (Default)\n");
296 printf(" -wxxx Width of the texture (Default=64)\n");
297 printf(" -hxxx Height of the texture (Default=64)\n");
298 printf(" -dxxx Depth of the texture (Default=64)\n");
299 printf(" Keyboard Options:\n");
300 printf(" 1 Object Texture coordinates (Default)\n");
301 printf(" 2 Eye Texture coordinates \n");
302 printf(" x rotate around x clockwise\n");
303 printf(" X rotate around x counter clockwise\n");
304 printf(" y rotate around y clockwise\n");
305 printf(" Y rotate around y counter clockwise\n");
306 printf(" z rotate around z clockwise\n");
307 printf(" Z rotate around z counter clockwise\n");
308 printf(" t enable 3-D texuring (Default)\n");
309 printf(" T disable 3-D texuring\n");
310 printf(" s smooth shading \n");
311 printf(" S flat shading (Default)\n");
316 /* visualization defaults */
318 doubleBuffer
= GL_FALSE
;
319 directRender
= GL_TRUE
;
329 GLenum
parseCmdLine(int argc
, char **argv
)
335 for (i
= 1; i
< argc
; i
++) {
336 if (strcmp(argv
[i
], "-ci") == 0) {
338 } else if (strcmp(argv
[i
], "-rgb") == 0) {
340 } else if (strcmp(argv
[i
], "-sb") == 0) {
341 doubleBuffer
= GL_FALSE
;
342 } else if (strcmp(argv
[i
], "-db") == 0) {
343 doubleBuffer
= GL_TRUE
;
344 } else if (strcmp(argv
[i
], "-dr") == 0) {
345 directRender
= GL_TRUE
;
346 } else if (strcmp(argv
[i
], "-ir") == 0) {
347 directRender
= GL_FALSE
;
348 } else if (strstr(argv
[i
], "-w") == 0) {
349 tex_width
=atoi((argv
[i
])+2);
350 } else if (strstr(argv
[i
], "-h") == 0) {
351 tex_height
=atoi((argv
[i
])+2);
352 } else if (strstr(argv
[i
], "-d") == 0) {
353 tex_depth
=atoi((argv
[i
])+2);
354 } else if (strcmp(argv
[i
], "-help") == 0) {
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");
373 /* clear background, z buffer etc */
374 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
376 glRotatef(angx
,1.0,0.0,0.0);
377 glRotatef(angy
,0.0,1.0,0.0);
378 glRotatef(angz
,0.0,0.0,1.0);
388 void resize(int w
, int h
)
390 glViewport(0, 0, (GLint
)w
, (GLint
)h
);
391 glMatrixMode(GL_PROJECTION
);
393 glOrtho(-2,2,-2,2,-5,10);
394 glMatrixMode(GL_MODELVIEW
);
396 glTranslatef(0,0,-5);
399 void cleanEverything(void)
405 void KeyHandler( unsigned char key
, int x
, int y
)
412 case 'Q': /* quit game. */
435 glEnable(GL_TEXTURE_3D_EXT
);
438 glDisable(GL_TEXTURE_3D_EXT
);
441 glShadeModel(GL_SMOOTH
);
444 glShadeModel(GL_FLAT
);
447 glDisable(GL_TEXTURE_GEN_S
);
448 glDisable(GL_TEXTURE_GEN_T
);
449 glDisable(GL_TEXTURE_GEN_R
);
452 glEnable(GL_TEXTURE_GEN_S
);
453 glEnable(GL_TEXTURE_GEN_T
);
454 glEnable(GL_TEXTURE_GEN_R
);
462 /*--------------------------------------------------------------------
463 noise function over R3 - implemented by a pseudorandom tricubic spline
464 EXCERPTED FROM SIGGRAPH 92, COURSE 23
468 ----------------------------------------------------------------------*/
471 #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
473 static int p
[B
+ B
+ 2];
474 static float g
[B
+ B
+ 2][3];
475 #define setup(i,b0,b1,r0,r1) \
476 t = vec[i] + 10000.; \
477 b0 = ((int)t) & (B-1); \
478 b1 = (b0+1) & (B-1); \
482 float noise3(float vec
[3])
484 int bx0
, bx1
, by0
, by1
, bz0
, bz1
, b00
, b10
, b01
, b11
;
485 float rx0
, rx1
, ry0
, ry1
, rz0
, rz1
, *q
, sx
, sy
, sz
, a
, b
, c
, d
, t
, u
, v
;
488 setup(0, bx0
,bx1
, rx0
,rx1
);
489 setup(1, by0
,by1
, ry0
,ry1
);
490 setup(2, bz0
,bz1
, rz0
,rz1
);
500 #define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
501 #define surve(t) ( t * t * (3. - 2. * t) )
502 #define lerp(t, a, b) ( a + t * (b - a) )
508 q
= g
[ b00
+ bz0
] ; u
= at(rx0
,ry0
,rz0
);
509 q
= g
[ b10
+ bz0
] ; v
= at(rx1
,ry0
,rz0
);
512 q
= g
[ b01
+ bz0
] ; u
= at(rx0
,ry1
,rz0
);
513 q
= g
[ b11
+ bz0
] ; v
= at(rx1
,ry1
,rz0
);
516 c
= lerp(sy
, a
, b
); /* interpolate in y at lo x */
518 q
= g
[ b00
+ bz1
] ; u
= at(rx0
,ry0
,rz1
);
519 q
= g
[ b10
+ bz1
] ; v
= at(rx1
,ry0
,rz1
);
522 q
= g
[ b01
+ bz1
] ; u
= at(rx0
,ry1
,rz1
);
523 q
= g
[ b11
+ bz1
] ; v
= at(rx1
,ry1
,rz1
);
526 d
= lerp(sy
, a
, b
); /* interpolate in y at hi x */
528 return 1.5 * lerp(sz
, c
, d
); /* interpolate in z */
537 /* Create an array of random gradient vectors uniformly on the unit sphere */
540 for (i
= 0 ; i
< B
; i
++) {
541 do { /* Choose uniformly in a cube */ for (j
=0 ; j
<3 ; j
++)
542 v
[j
] = (float)((rand() % (B
+ B
)) - B
) / B
;
544 } while (s
> 1.0); /* If not in sphere try again */ s
= sqrt(s
);
545 for (j
= 0 ; j
< 3 ; j
++) /* Else normalize */
549 /* Create a pseudorandom permutation of [1..B] */
550 for (i
= 0 ; i
< B
; i
++)
552 for (i
= B
; i
> 0 ; i
-= 2) {
554 p
[i
] = p
[j
= rand() % B
];
558 /* Extend g and p arrays to allow for faster indexing */
559 for (i
= 0 ; i
< B
+ 2 ; i
++) {
561 for (j
= 0 ; j
< 3 ; j
++)
562 g
[B
+ i
][j
] = g
[i
][j
];
566 float turbulence(float point
[3], float lofreq
, float hifreq
)
570 p
[0] = point
[0] + 123.456;
575 for (freq
= lofreq
; freq
< hifreq
; freq
*= 2.) {
576 t
+= fabs(noise3(p
)) / freq
;
581 return t
- 0.3; /* readjust to make mean value = 0.0 */