1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* Sierpinski3D --- 3D sierpinski gasket */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)sierpinski3D.c 00.01 99/11/04 xlockmore";
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
23 * 1999: written by Tim Robinson <the_luggage@bigfoot.com>
24 * a 3-D representation of the Sierpinski gasket fractal.
26 * 10-Dec-99 jwz rewrote to draw a set of tetrahedrons instead of a
27 * random scattering of points.
31 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
32 * otherwise caddr_t is not defined correctly
35 #include <X11/Intrinsic.h>
38 # define PROGCLASS "Sierpinski3D"
39 # define HACK_INIT init_gasket
40 # define HACK_DRAW draw_gasket
41 # define HACK_RESHAPE reshape_gasket
42 # define gasket_opts xlockmore_opts
43 # define DEFAULTS "*delay: 20000 \n" \
46 "*showFPS: False \n" \
47 "*wireframe: False \n"
48 # include "xlockmore.h" /* from the xscreensaver distribution */
49 #else /* !STANDALONE */
50 # include "xlock.h" /* from the xlockmore distribution */
51 #endif /* !STANDALONE */
56 #define countof(x) (sizeof((x))/sizeof((*x)))
60 static XrmOptionDescRec opts[] = {
61 {"-depth", ".sierpinski3d.maxDepth", XrmoptionSepArg, (caddr_t) 0 },
62 {"-speed", ".sierpinski3d.speed", XrmoptionSepArg, (caddr_t) 0 }
65 static argtype vars[] = {
66 {(caddr_t *) &max_depth, "maxDepth", "MaxDepth", "5", t_Int},
67 {(caddr_t *) &speed, "speed", "Speed", "150", t_Int},
71 ModeSpecOpt gasket_opts = {countof(opts), opts, countof(vars), vars, NULL};
74 ModStruct gasket_description =
75 {"gasket", "init_gasket", "draw_gasket", "release_gasket",
76 "draw_gasket", "init_gasket", NULL, &gasket_opts,
77 1000, 1, 2, 1, 4, 1.0, "",
78 "Shows GL's Sierpinski gasket", 0, NULL};
89 GLfloat rotx, roty, rotz; /* current object rotation */
90 GLfloat dx, dy, dz; /* current rotational velocity */
91 GLfloat ddx, ddy, ddz; /* current rotational acceleration */
92 GLfloat d_max; /* max velocity */
95 GLuint gasket0, gasket1, gasket2, gasket3;
96 GLXContext *glx_context;
110 static gasketstruct *gasket = NULL;
114 static GLfloat normals[4][3];
119 triangle (GLfloat x1, GLfloat y1, GLfloat z1,
120 GLfloat x2, GLfloat y2, GLfloat z2,
121 GLfloat x3, GLfloat y3, GLfloat z3,
125 glBegin (GL_LINE_LOOP);
127 glBegin (GL_TRIANGLES);
128 glVertex3f (x1, y1, z1);
129 glVertex3f (x2, y2, z2);
130 glVertex3f (x3, y3, z3);
135 four_tetras (GL_VECTOR *outer, Bool wireframe_p, int countdown, int which)
141 glNormal3f (normals[0][0], normals[0][1], normals[0][2]);
142 triangle (outer[0].x, outer[0].y, outer[0].z,
143 outer[1].x, outer[1].y, outer[1].z,
144 outer[2].x, outer[2].y, outer[2].z,
149 glNormal3f (normals[1][0], normals[1][1], normals[1][2]);
150 triangle (outer[0].x, outer[0].y, outer[0].z,
151 outer[3].x, outer[3].y, outer[3].z,
152 outer[1].x, outer[1].y, outer[1].z,
157 glNormal3f (normals[2][0], normals[2][1], normals[2][2]);
158 triangle (outer[0].x, outer[0].y, outer[0].z,
159 outer[2].x, outer[2].y, outer[2].z,
160 outer[3].x, outer[3].y, outer[3].z,
165 glNormal3f (normals[3][0], normals[3][1], normals[3][2]);
166 triangle (outer[1].x, outer[1].y, outer[1].z,
167 outer[3].x, outer[3].y, outer[3].z,
168 outer[2].x, outer[2].y, outer[2].z,
180 GL_VECTOR inner[M23+1];
183 inner[M01].x = (outer[0].x + outer[1].x) / 2.0;
184 inner[M01].y = (outer[0].y + outer[1].y) / 2.0;
185 inner[M01].z = (outer[0].z + outer[1].z) / 2.0;
187 inner[M02].x = (outer[0].x + outer[2].x) / 2.0;
188 inner[M02].y = (outer[0].y + outer[2].y) / 2.0;
189 inner[M02].z = (outer[0].z + outer[2].z) / 2.0;
191 inner[M03].x = (outer[0].x + outer[3].x) / 2.0;
192 inner[M03].y = (outer[0].y + outer[3].y) / 2.0;
193 inner[M03].z = (outer[0].z + outer[3].z) / 2.0;
195 inner[M12].x = (outer[1].x + outer[2].x) / 2.0;
196 inner[M12].y = (outer[1].y + outer[2].y) / 2.0;
197 inner[M12].z = (outer[1].z + outer[2].z) / 2.0;
199 inner[M13].x = (outer[1].x + outer[3].x) / 2.0;
200 inner[M13].y = (outer[1].y + outer[3].y) / 2.0;
201 inner[M13].z = (outer[1].z + outer[3].z) / 2.0;
203 inner[M23].x = (outer[2].x + outer[3].x) / 2.0;
204 inner[M23].y = (outer[2].y + outer[3].y) / 2.0;
205 inner[M23].z = (outer[2].z + outer[3].z) / 2.0;
209 corner[0] = outer[0];
210 corner[1] = inner[M01];
211 corner[2] = inner[M02];
212 corner[3] = inner[M03];
213 four_tetras (corner, wireframe_p, countdown, which);
215 corner[0] = inner[M01];
216 corner[1] = outer[1];
217 corner[2] = inner[M12];
218 corner[3] = inner[M13];
219 four_tetras (corner, wireframe_p, countdown, which);
221 corner[0] = inner[M02];
222 corner[1] = inner[M12];
223 corner[2] = outer[2];
224 corner[3] = inner[M23];
225 four_tetras (corner, wireframe_p, countdown, which);
227 corner[0] = inner[M03];
228 corner[1] = inner[M13];
229 corner[2] = inner[M23];
230 corner[3] = outer[3];
231 four_tetras (corner, wireframe_p, countdown, which);
237 compile_gasket(ModeInfo *mi, int which)
239 Bool wireframe_p = MI_IS_WIREFRAME(mi);
240 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
246 normals[0][2] = -sqrt(2.0 / 3.0);
249 normals[1][1] = -sqrt(0.75);
250 normals[1][2] = sqrt(2.0 / 3.0) / 3.0;
252 normals[2][0] = sqrt (0.5);
253 normals[2][1] = sqrt(0.75) / 2.0;
254 normals[2][2] = normals[1][2];
256 normals[3][0] = -normals[2][0];
257 normals[3][1] = normals[2][1];
258 normals[3][2] = normals[1][2];
261 /* define verticies */
263 vertex[0].y = -(1.0/3.0)*sqrt((2.0/3.0));
264 vertex[0].z = -sqrt(3.0)/6.0;
267 vertex[1].y = -(1.0/3.0)*sqrt((2.0/3.0));
268 vertex[1].z = -sqrt(3.0)/6.0;
271 vertex[2].y = (2.0/3.0)*sqrt((2.0/3.0));
272 vertex[2].z = -sqrt(3.0)/6.0;
276 vertex[3].z = sqrt(3.0)/3.0;
282 four_tetras (vertex, wireframe_p,
283 (gp->current_depth < 0
284 ? -gp->current_depth : gp->current_depth),
291 Bool wireframe_p = MI_IS_WIREFRAME(mi);
292 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
293 static int tick = 999999;
295 static GLfloat pos[4] = {-4.0, 3.0, 10.0, 1.0};
296 static float white[] = {1.0, 1.0, 1.0, 1.0};
297 static float color0[] = {0.0, 0.0, 0.0, 1.0};
298 static float color1[] = {0.0, 0.0, 0.0, 1.0};
299 static float color2[] = {0.0, 0.0, 0.0, 1.0};
300 static float color3[] = {0.0, 0.0, 0.0, 1.0};
302 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
308 glLightfv(GL_LIGHT0, GL_POSITION, pos);
310 color0[0] = gp->colors[gp->ccolor0].red / 65536.0;
311 color0[1] = gp->colors[gp->ccolor0].green / 65536.0;
312 color0[2] = gp->colors[gp->ccolor0].blue / 65536.0;
314 color1[0] = gp->colors[gp->ccolor1].red / 65536.0;
315 color1[1] = gp->colors[gp->ccolor1].green / 65536.0;
316 color1[2] = gp->colors[gp->ccolor1].blue / 65536.0;
318 color2[0] = gp->colors[gp->ccolor2].red / 65536.0;
319 color2[1] = gp->colors[gp->ccolor2].green / 65536.0;
320 color2[2] = gp->colors[gp->ccolor2].blue / 65536.0;
322 color3[0] = gp->colors[gp->ccolor3].red / 65536.0;
323 color3[1] = gp->colors[gp->ccolor3].green / 65536.0;
324 color3[2] = gp->colors[gp->ccolor3].blue / 65536.0;
330 if (gp->ccolor0 >= gp->ncolors) gp->ccolor0 = 0;
331 if (gp->ccolor1 >= gp->ncolors) gp->ccolor1 = 0;
332 if (gp->ccolor2 >= gp->ncolors) gp->ccolor2 = 0;
333 if (gp->ccolor3 >= gp->ncolors) gp->ccolor3 = 0;
335 glShadeModel(GL_SMOOTH);
337 glEnable(GL_LIGHTING);
341 glEnable(GL_DEPTH_TEST);
342 glEnable(GL_NORMALIZE);
343 glEnable(GL_CULL_FACE);
348 static int frame = 0;
351 # define SINOID(SCALE,SIZE) \
352 ((((1 + sin((frame * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
353 x = SINOID(0.0071, 8.0);
354 y = SINOID(0.0053, 6.0);
355 z = SINOID(0.0037, 15.0);
357 glTranslatef(x, y, z);
362 if (x < 0) x = 1 - (x + 1);
363 if (y < 0) y = 1 - (y + 1);
364 if (z < 0) z = 1 - (z + 1);
365 glRotatef(x * 360, 1.0, 0.0, 0.0);
366 glRotatef(y * 360, 0.0, 1.0, 0.0);
367 glRotatef(z * 360, 0.0, 0.0, 1.0);
370 glScalef( 8.0, 8.0, 8.0 );
372 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color0);
373 glCallList(gp->gasket0);
374 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color1);
375 glCallList(gp->gasket1);
376 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color2);
377 glCallList(gp->gasket2);
378 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color3);
379 glCallList(gp->gasket3);
387 if (gp->current_depth >= max_depth)
388 gp->current_depth = -max_depth;
391 /* We make four different lists so that each face of the tetrahedrons
392 can have a different color (all triangles facing in the same
393 direction have the same color, which is different from all
394 triangles facing in other directions.)
396 glDeleteLists (gp->gasket0, 1);
397 glDeleteLists (gp->gasket1, 1);
398 glDeleteLists (gp->gasket2, 1);
399 glDeleteLists (gp->gasket3, 1);
400 glNewList (gp->gasket0, GL_COMPILE); compile_gasket (mi, 0); glEndList();
401 glNewList (gp->gasket1, GL_COMPILE); compile_gasket (mi, 1); glEndList();
402 glNewList (gp->gasket2, GL_COMPILE); compile_gasket (mi, 2); glEndList();
403 glNewList (gp->gasket3, GL_COMPILE); compile_gasket (mi, 3); glEndList();
409 /* new window size or exposure */
411 reshape_gasket(ModeInfo *mi, int width, int height)
413 GLfloat h = (GLfloat) height / (GLfloat) width;
415 glViewport(0, 0, (GLint) width, (GLint) height);
416 glMatrixMode(GL_PROJECTION);
419 gluPerspective( 30.0, 1/h, 1.0, 100.0 );
420 gluLookAt( 0.0, 0.0, 15.0,
423 glMatrixMode(GL_MODELVIEW);
425 glTranslatef(0.0, 0.0, -15.0);
427 glClear(GL_COLOR_BUFFER_BIT);
433 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
435 /* draw the gasket */
436 gp->gasket0 = glGenLists(1);
437 gp->gasket1 = glGenLists(1);
438 gp->gasket2 = glGenLists(1);
439 gp->gasket3 = glGenLists(1);
440 gp->current_depth = 1; /* start out at level 1, not 0 */
445 /* lifted from lament.c */
446 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
447 #define RANDSIGN() ((random() & 1) ? 1 : -1)
450 rotate(GLfloat *pos, GLfloat *v, GLfloat *dv, GLfloat max_v)
465 if (ppos < 0) abort();
466 if (ppos > 1.0) abort();
467 *pos = (*pos > 0 ? ppos : -ppos);
473 if (*v > max_v || *v < -max_v)
477 /* If it stops, start it going in the other direction. */
484 /* keep going in the same direction */
499 /* Alter direction of rotational acceleration randomly. */
500 if (! (random() % 120))
503 /* Change acceleration very occasionally. */
504 if (! (random() % 200))
508 else if (random() & 1)
517 init_gasket(ModeInfo *mi)
519 int screen = MI_SCREEN(mi);
524 if ((gasket = (gasketstruct *) calloc(MI_NUM_SCREENS(mi),
525 sizeof (gasketstruct))) == NULL)
528 gp = &gasket[screen];
530 gp->window = MI_WINDOW(mi);
532 gp->rotx = frand(1.0) * RANDSIGN();
533 gp->roty = frand(1.0) * RANDSIGN();
534 gp->rotz = frand(1.0) * RANDSIGN();
536 /* bell curve from 0-1.5 degrees, avg 0.75 */
537 gp->dx = (frand(1) + frand(1) + frand(1)) / (360*2);
538 gp->dy = (frand(1) + frand(1) + frand(1)) / (360*2);
539 gp->dz = (frand(1) + frand(1) + frand(1)) / (360*2);
541 gp->d_max = gp->dx * 2;
543 gp->ddx = 0.00006 + frand(0.00003);
544 gp->ddy = 0.00006 + frand(0.00003);
545 gp->ddz = 0.00006 + frand(0.00003);
548 gp->colors = (XColor *) calloc(gp->ncolors, sizeof(XColor));
549 make_smooth_colormap (0, 0, 0,
550 gp->colors, &gp->ncolors,
553 gp->ccolor1 = gp->ncolors * 0.25;
554 gp->ccolor2 = gp->ncolors * 0.5;
555 gp->ccolor3 = gp->ncolors * 0.75;
557 if ((gp->glx_context = init_GL(mi)) != NULL)
559 reshape_gasket(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
569 draw_gasket(ModeInfo * mi)
571 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
572 Display *display = MI_DISPLAY(mi);
573 Window window = MI_WINDOW(mi);
576 if (!gp->glx_context) return;
578 glDrawBuffer(GL_BACK);
583 glXMakeCurrent(display, window, *(gp->glx_context));
587 gp->angle = (int) (gp->angle + angle_incr) % 360;
589 rotate(&gp->rotx, &gp->dx, &gp->ddx, gp->d_max);
590 rotate(&gp->roty, &gp->dy, &gp->ddy, gp->d_max);
591 rotate(&gp->rotz, &gp->dz, &gp->ddz, gp->d_max);
593 if (mi->fps_p) do_fps (mi);
595 glXSwapBuffers(display, window);
599 release_gasket(ModeInfo * mi)
605 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
607 gasketstruct *gp = &gasket[screen];
611 /* Display lists MUST be freed while their glXContext is current. */
612 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
614 if (glIsList(gp->gasket0)) glDeleteLists(gp->gasket0, 1);
615 if (glIsList(gp->gasket1)) glDeleteLists(gp->gasket1, 1);
616 if (glIsList(gp->gasket2)) glDeleteLists(gp->gasket2, 1);
617 if (glIsList(gp->gasket3)) glDeleteLists(gp->gasket3, 1);
620 (void) free((void *) gasket);
627 /*********************************************************/