gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / demos / stex3d.c
blobde18480c25e6cc69221ee1146792f9281ba9e1a2
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.
4 *
5 * Author: Daniel Barrero
6 * barrero@irit.fr
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 *---------------------------- */
17 #include <string.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <math.h>
21 #include <GL/glew.h>
22 #include <GL/glut.h>
25 #ifndef M_PI
26 #define M_PI 3.14159265358979323846
27 #endif
29 #define NOISE_TEXTURE 1
30 #define GRADIENT_TEXTURE 2
32 #define TORUS 1
33 #define SPHERE 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;
42 static void
43 BuildTorus(void)
45 GLint i, j;
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;
52 float scalFac;
54 rings = 16;
55 sides = 12;
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));
83 n0[2] = sin(phi1);
85 n1[0] = cos(theta2) * (cos(phi1));
86 n1[1] = -sin(theta2) * (cos(phi1));
87 n1[2] = sin(phi1);
89 n2[0] = cos(theta2) * (cos(phi2));
90 n2[1] = -sin(theta2) * (cos(phi2));
91 n2[2] = sin(phi2);
93 n3[0] = cos(theta1) * (cos(phi2));
94 n3[1] = -sin(theta1) * (cos(phi2));
95 n3[2] = sin(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;
113 glBegin(GL_POLYGON);
114 glNormal3fv(n3);
115 glTexCoord3fv(t3);
116 glVertex3fv(v3);
117 glNormal3fv(n2);
118 glTexCoord3fv(t2);
119 glVertex3fv(v2);
120 glNormal3fv(n1);
121 glTexCoord3fv(t1);
122 glVertex3fv(v1);
123 glNormal3fv(n0);
124 glTexCoord3fv(t0);
125 glVertex3fv(v0);
126 glEnd();
129 glEndList();
133 /*--------------------------------------------------------------------
134 noise function over R3 - implemented by a pseudorandom tricubic spline
135 EXCERPTED FROM SIGGRAPH 92, COURSE 23
136 PROCEDURAL MODELING
137 Ken Perlin
138 New York University
139 ----------------------------------------------------------------------*/
142 #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
143 #define B 128
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); \
150 r0 = t - (int)t; \
151 r1 = r0 - 1.;
153 static float
154 noise3(float vec[3])
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;
158 register int i, j;
160 setup(0, bx0, bx1, rx0, rx1);
161 setup(1, by0, by1, ry0, ry1);
162 setup(2, bz0, bz1, rz0, rz1);
164 i = p[bx0];
165 j = p[bx1];
167 b00 = p[i + by0];
168 b10 = p[j + by0];
169 b01 = p[i + by1];
170 b11 = p[j + by1];
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) )
176 sx = surve(rx0);
177 sy = surve(ry0);
178 sz = surve(rz0);
180 q = g[b00 + bz0];
181 u = at(rx0, ry0, rz0);
182 q = g[b10 + bz0];
183 v = at(rx1, ry0, rz0);
184 a = lerp(sx, u, v);
186 q = g[b01 + bz0];
187 u = at(rx0, ry1, rz0);
188 q = g[b11 + bz0];
189 v = at(rx1, ry1, rz0);
190 b = lerp(sx, u, v);
192 c = lerp(sy, a, b); /* interpolate in y at lo x */
194 q = g[b00 + bz1];
195 u = at(rx0, ry0, rz1);
196 q = g[b10 + bz1];
197 v = at(rx1, ry0, rz1);
198 a = lerp(sx, u, v);
200 q = g[b01 + bz1];
201 u = at(rx0, ry1, rz1);
202 q = g[b11 + bz1];
203 v = at(rx1, ry1, rz1);
204 b = lerp(sx, u, v);
206 d = lerp(sy, a, b); /* interpolate in y at hi x */
208 return 1.5 * lerp(sz, c, d); /* interpolate in z */
211 static void
212 initNoise(void)
214 /*long random(); */
215 int i, j, k;
216 float v[3], s;
218 /* Create an array of random gradient vectors uniformly on the unit sphere */
219 /*srandom(1); */
220 srand(1);
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;
225 s = DOT(v, v);
226 } while (s > 1.0); /* If not in sphere try again */
227 s = sqrt(s);
228 for (j = 0; j < 3; j++) /* Else normalize */
229 g[i][j] = v[j] / s;
232 /* Create a pseudorandom permutation of [1..B] */
233 for (i = 0; i < B; i++)
234 p[i] = i;
235 for (i = B; i > 0; i -= 2) {
236 k = p[i];
237 p[i] = p[j = rand() % B];
238 p[j] = k;
241 /* Extend g and p arrays to allow for faster indexing */
242 for (i = 0; i < B + 2; i++) {
243 p[B + i] = p[i];
244 for (j = 0; j < 3; j++)
245 g[B + i][j] = g[i][j];
250 static float
251 turbulence(float point[3], float lofreq, float hifreq)
253 float freq, t, p[3];
255 p[0] = point[0] + 123.456;
256 p[1] = point[1];
257 p[2] = point[2];
259 t = 0;
260 for (freq = lofreq; freq < hifreq; freq *= 2.) {
261 t += fabs(noise3(p)) / freq;
262 p[0] *= 2.;
263 p[1] *= 2.;
264 p[2] *= 2.;
266 return t - 0.3; /* readjust to make mean value = 0.0 */
270 static void
271 create3Dtexture(void)
273 unsigned char *voxels = NULL;
274 int i, j, k;
275 unsigned char *vp;
276 float vec[3];
277 int tmp;
279 printf("creating 3d textures...\n");
280 voxels =
281 (unsigned char *)
282 malloc((size_t) (4 * tex_width * tex_height * tex_depth));
283 vp = voxels;
284 for (i = 0; i < tex_width; i++) {
285 vec[0] = i;
286 for (j = 0; j < tex_height; j++) {
287 vec[1] = j;
288 for (k = 0; k < tex_depth; k++) {
289 vec[2] = k;
290 tmp = (sin(k * i * j + turbulence(vec, 0.01, 1)) + 1) * 127.5;
291 *vp++ = 0;
292 *vp++ = 0;
293 *vp++ = tmp;
294 *vp++ = tmp + 128;
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);
312 free(voxels);
314 printf("finished setting up 3d texture image.\n");
318 static void
319 printHelp(void)
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");
338 static GLenum
339 parseCmdLine(int argc, char **argv)
341 GLint i;
343 for (i = 1; i < argc; i++) {
344 if (strcmp(argv[i], "-help") == 0) {
345 printHelp();
346 return GL_FALSE;
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);
357 else {
358 printf("%s (Bad option).\n", argv[i]);
359 printHelp();
360 return GL_FALSE;
363 if (tex_width == 0 || tex_height == 0 || tex_depth == 0) {
364 printf("%s (Bad option).\n", "size parameters can't be 0");
365 printHelp();
366 return GL_FALSE;
368 return GL_TRUE;
372 static void
373 drawScene(void)
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);
380 glPushMatrix();
381 if (texgen == 2) {
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);
391 if (texgen == 1) {
392 glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
393 glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
394 glTexGenfv(GL_R, GL_EYE_PLANE, rPlane);
397 if (texgen) {
398 glEnable(GL_TEXTURE_GEN_S);
399 glEnable(GL_TEXTURE_GEN_T);
400 glEnable(GL_TEXTURE_GEN_R);
402 else {
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);
412 glPopMatrix();
414 glutSwapBuffers();
418 static void
419 resize(int w, int h)
421 float ar = (float) w / (float) h;
422 float ax = 0.6 * ar;
423 float ay = 0.6;
424 glViewport(0, 0, (GLint) w, (GLint) h);
425 glMatrixMode(GL_PROJECTION);
426 glLoadIdentity();
427 glFrustum(-ax, ax, -ay, ay, 2, 20);
428 /*glOrtho(-2, 2, -2, 2, -10, 10);*/
429 glMatrixMode(GL_MODELVIEW);
430 glLoadIdentity();
431 glTranslatef(0, 0, -4);
435 static void
436 Idle(void)
438 float t = glutGet(GLUT_ELAPSED_TIME);
439 angx = 0.01 * t;
440 angy = 0.03 * t;
441 angz += 0;
442 glutPostRedisplay();
446 static void
447 SpecialKey(int k, int x, int y)
449 switch (k) {
450 case GLUT_KEY_UP:
451 angx += 5.0;
452 break;
453 case GLUT_KEY_DOWN:
454 angx -= 5.0;
455 break;
456 case GLUT_KEY_LEFT:
457 angy += 5.0;
458 break;
459 case GLUT_KEY_RIGHT:
460 angy -= 5.0;
461 break;
462 default:
463 return;
465 glutPostRedisplay();
469 static void
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"
477 (void) x;
478 (void) y;
479 switch (key) {
480 case 27:
481 case 'q':
482 case 'Q': /* quit game. */
483 exit(0);
484 break;
485 case 'z':
486 angz += 10;
487 break;
488 case 'Z':
489 angz -= 10;
490 break;
491 case 's':
492 smooth = !smooth;
493 if (smooth)
494 glShadeModel(GL_SMOOTH);
495 else
496 glShadeModel(GL_FLAT);
497 break;
498 case 't':
499 texgen++;
500 if (texgen > 2)
501 texgen = 0;
502 printf("Texgen: %s\n", mode[texgen]);
503 break;
504 case 'o':
505 if (CurObject == TORUS)
506 CurObject = SPHERE;
507 else
508 CurObject = TORUS;
509 break;
510 case 'f':
511 if (Filter == GL_LINEAR)
512 Filter = GL_NEAREST;
513 else
514 Filter = GL_LINEAR;
515 break;
516 case 'i':
517 if (CurTexture == NOISE_TEXTURE)
518 CurTexture = GRADIENT_TEXTURE;
519 else
520 CurTexture = NOISE_TEXTURE;
521 glBindTexture(GL_TEXTURE_3D, CurTexture);
522 break;
523 case 'a':
524 case ' ':
525 animate = !animate;
526 if (animate)
527 glutIdleFunc(Idle);
528 else
529 glutIdleFunc(NULL);
530 break;
531 case 'w':
532 wireframe = !wireframe;
533 if (wireframe)
534 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
535 else
536 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
537 break;
538 default:
539 break;
541 glutPostRedisplay();
545 static void
546 create3Dgradient(void)
548 unsigned char *v;
549 int i, j, k;
550 unsigned char *voxels = NULL;
552 voxels = (unsigned char *) malloc(4 * tex_width * tex_height * tex_depth);
553 v = voxels;
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);
561 *v++ = r;
562 *v++ = g;
563 *v++ = b;
564 *v++ = 255;
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);
581 free(voxels);
586 static void
587 init(void)
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 };
595 int max;
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");
602 exit(1);
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);
611 /* init light */
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);
621 glEnable(GL_LIGHT1);
623 glClearColor(.5, .5, .5, 0);
626 GLUquadricObj *q;
627 q = gluNewQuadric();
628 gluQuadricTexture( q, GL_TRUE );
629 glNewList(SPHERE, GL_COMPILE);
630 gluSphere( q, 0.95, 30, 15 );
631 glEndList();
632 gluDeleteQuadric(q);
635 BuildTorus();
638 create3Dgradient();
640 initNoise();
641 create3Dtexture();
643 glEnable(GL_TEXTURE_3D);
646 glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
647 glEnable(GL_BLEND);
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) {
661 exit(0);
664 glutInitWindowPosition(0, 0);
665 glutInitWindowSize(400, 400);
666 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
668 if (glutCreateWindow("stex3d") <= 0) {
669 exit(0);
672 glewInit();
674 init();
676 printHelp();
678 glutReshapeFunc(resize);
679 glutKeyboardFunc(KeyHandler);
680 glutSpecialFunc(SpecialKey);
681 glutDisplayFunc(drawScene);
682 if (animate)
683 glutIdleFunc(Idle);
684 glutMainLoop();
685 return 0;