1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* Sierpinski3D --- 3D sierpinski gasket */
5 static const char sccsid[] = "@(#)sierpinski3D.c 00.01 99/11/04 xlockmore";
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and that
12 * both that copyright notice and this permission notice appear in
13 * supporting documentation.
15 * This file is provided AS IS with no warranties of any kind. The author
16 * shall have no liability with respect to the infringement of copyrights,
17 * trade secrets or any patents by this file or any part thereof. In no
18 * event will the author be liable for any lost revenue or profits or
19 * other special, indirect and consequential damages.
22 * 1999: written by Tim Robinson <the_luggage@bigfoot.com>
23 * a 3-D representation of the Sierpinski gasket fractal.
25 * 10-Dec-99 jwz rewrote to draw a set of tetrahedrons instead of a
26 * random scattering of points.
30 # define DEFAULTS "*delay: 20000 \n" \
31 "*showFPS: False \n" \
32 "*wireframe: False \n" \
34 # define refresh_gasket 0
35 # include "xlockmore.h" /* from the xscreensaver distribution */
36 #else /* !STANDALONE */
37 # include "xlock.h" /* from the xlockmore distribution */
38 #endif /* !STANDALONE */
42 #define DEF_SPIN "True"
43 #define DEF_WANDER "True"
44 #define DEF_SPEED "150"
45 #define DEF_MAX_DEPTH "5"
48 #include "gltrackball.h"
51 #define countof(x) (sizeof((x))/sizeof((*x)))
56 static Bool do_wander;
58 static XrmOptionDescRec opts[] = {
59 {"-depth", ".sierpinski3d.maxDepth", XrmoptionSepArg, 0 },
60 {"-speed", ".sierpinski3d.speed", XrmoptionSepArg, 0 },
61 { "-spin", ".spin", XrmoptionNoArg, "True" },
62 { "+spin", ".spin", XrmoptionNoArg, "False" },
63 { "-wander", ".wander", XrmoptionNoArg, "True" },
64 { "+wander", ".wander", XrmoptionNoArg, "False" },
67 static argtype vars[] = {
68 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
69 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
70 {&speed, "speed", "Speed", DEF_SPEED, t_Int},
71 {&max_depth, "maxDepth", "MaxDepth", DEF_MAX_DEPTH, t_Int},
75 ENTRYPOINT ModeSpecOpt gasket_opts = {countof(opts), opts, countof(vars), vars, NULL};
78 ModStruct gasket_description =
79 {"gasket", "init_gasket", "draw_gasket", "release_gasket",
80 "draw_gasket", "init_gasket", NULL, &gasket_opts,
81 1000, 1, 2, 1, 4, 1.0, "",
82 "Shows GL's Sierpinski gasket", 0, NULL};
93 GLuint gasket0, gasket1, gasket2, gasket3;
94 GLXContext *glx_context;
97 trackball_state *trackball;
111 GLfloat normals[4][3];
115 static gasketstruct *gasket = NULL;
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 (gasketstruct *gp,
136 GL_VECTOR *outer, Bool wireframe_p, int countdown, int which,
144 glNormal3f (gp->normals[0][0], gp->normals[0][1], gp->normals[0][2]);
145 triangle (outer[0].x, outer[0].y, outer[0].z,
146 outer[1].x, outer[1].y, outer[1].z,
147 outer[2].x, outer[2].y, outer[2].z,
152 glNormal3f (gp->normals[1][0], gp->normals[1][1], gp->normals[1][2]);
153 triangle (outer[0].x, outer[0].y, outer[0].z,
154 outer[3].x, outer[3].y, outer[3].z,
155 outer[1].x, outer[1].y, outer[1].z,
160 glNormal3f (gp->normals[2][0], gp->normals[2][1], gp->normals[2][2]);
161 triangle (outer[0].x, outer[0].y, outer[0].z,
162 outer[2].x, outer[2].y, outer[2].z,
163 outer[3].x, outer[3].y, outer[3].z,
168 glNormal3f (gp->normals[3][0], gp->normals[3][1], gp->normals[3][2]);
169 triangle (outer[1].x, outer[1].y, outer[1].z,
170 outer[3].x, outer[3].y, outer[3].z,
171 outer[2].x, outer[2].y, outer[2].z,
183 GL_VECTOR inner[M23+1];
186 inner[M01].x = (outer[0].x + outer[1].x) / 2.0;
187 inner[M01].y = (outer[0].y + outer[1].y) / 2.0;
188 inner[M01].z = (outer[0].z + outer[1].z) / 2.0;
190 inner[M02].x = (outer[0].x + outer[2].x) / 2.0;
191 inner[M02].y = (outer[0].y + outer[2].y) / 2.0;
192 inner[M02].z = (outer[0].z + outer[2].z) / 2.0;
194 inner[M03].x = (outer[0].x + outer[3].x) / 2.0;
195 inner[M03].y = (outer[0].y + outer[3].y) / 2.0;
196 inner[M03].z = (outer[0].z + outer[3].z) / 2.0;
198 inner[M12].x = (outer[1].x + outer[2].x) / 2.0;
199 inner[M12].y = (outer[1].y + outer[2].y) / 2.0;
200 inner[M12].z = (outer[1].z + outer[2].z) / 2.0;
202 inner[M13].x = (outer[1].x + outer[3].x) / 2.0;
203 inner[M13].y = (outer[1].y + outer[3].y) / 2.0;
204 inner[M13].z = (outer[1].z + outer[3].z) / 2.0;
206 inner[M23].x = (outer[2].x + outer[3].x) / 2.0;
207 inner[M23].y = (outer[2].y + outer[3].y) / 2.0;
208 inner[M23].z = (outer[2].z + outer[3].z) / 2.0;
212 corner[0] = outer[0];
213 corner[1] = inner[M01];
214 corner[2] = inner[M02];
215 corner[3] = inner[M03];
216 four_tetras (gp, corner, wireframe_p, countdown, which, countP);
218 corner[0] = inner[M01];
219 corner[1] = outer[1];
220 corner[2] = inner[M12];
221 corner[3] = inner[M13];
222 four_tetras (gp, corner, wireframe_p, countdown, which, countP);
224 corner[0] = inner[M02];
225 corner[1] = inner[M12];
226 corner[2] = outer[2];
227 corner[3] = inner[M23];
228 four_tetras (gp, corner, wireframe_p, countdown, which, countP);
230 corner[0] = inner[M03];
231 corner[1] = inner[M13];
232 corner[2] = inner[M23];
233 corner[3] = outer[3];
234 four_tetras (gp, corner, wireframe_p, countdown, which, countP);
240 compile_gasket(ModeInfo *mi, int which)
242 Bool wireframe_p = MI_IS_WIREFRAME(mi);
243 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
248 gp->normals[0][0] = 0;
249 gp->normals[0][1] = 0;
250 gp->normals[0][2] = -sqrt(2.0 / 3.0);
252 gp->normals[1][0] = 0;
253 gp->normals[1][1] = -sqrt(0.75);
254 gp->normals[1][2] = sqrt(2.0 / 3.0) / 3.0;
256 gp->normals[2][0] = sqrt (0.5);
257 gp->normals[2][1] = sqrt(0.75) / 2.0;
258 gp->normals[2][2] = gp->normals[1][2];
260 gp->normals[3][0] = -gp->normals[2][0];
261 gp->normals[3][1] = gp->normals[2][1];
262 gp->normals[3][2] = gp->normals[1][2];
265 /* define verticies */
267 vertex[0].y = -(1.0/3.0)*sqrt((2.0/3.0));
268 vertex[0].z = -sqrt(3.0)/6.0;
271 vertex[1].y = -(1.0/3.0)*sqrt((2.0/3.0));
272 vertex[1].z = -sqrt(3.0)/6.0;
275 vertex[2].y = (2.0/3.0)*sqrt((2.0/3.0));
276 vertex[2].z = -sqrt(3.0)/6.0;
280 vertex[3].z = sqrt(3.0)/3.0;
287 four_tetras (gp, vertex, wireframe_p,
288 (gp->current_depth < 0
289 ? -gp->current_depth : gp->current_depth),
292 mi->polygon_count = count;
298 Bool wireframe_p = MI_IS_WIREFRAME(mi);
299 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
301 static const GLfloat pos[] = {-4.0, 3.0, 10.0, 1.0};
302 static const GLfloat white[] = {1.0, 1.0, 1.0, 1.0};
304 GLfloat color0[] = {0.0, 0.0, 0.0, 1.0};
305 GLfloat color1[] = {0.0, 0.0, 0.0, 1.0};
306 GLfloat color2[] = {0.0, 0.0, 0.0, 1.0};
307 GLfloat color3[] = {0.0, 0.0, 0.0, 1.0};
309 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
315 glLightfv(GL_LIGHT0, GL_POSITION, pos);
317 color0[0] = gp->colors[gp->ccolor0].red / 65536.0;
318 color0[1] = gp->colors[gp->ccolor0].green / 65536.0;
319 color0[2] = gp->colors[gp->ccolor0].blue / 65536.0;
321 color1[0] = gp->colors[gp->ccolor1].red / 65536.0;
322 color1[1] = gp->colors[gp->ccolor1].green / 65536.0;
323 color1[2] = gp->colors[gp->ccolor1].blue / 65536.0;
325 color2[0] = gp->colors[gp->ccolor2].red / 65536.0;
326 color2[1] = gp->colors[gp->ccolor2].green / 65536.0;
327 color2[2] = gp->colors[gp->ccolor2].blue / 65536.0;
329 color3[0] = gp->colors[gp->ccolor3].red / 65536.0;
330 color3[1] = gp->colors[gp->ccolor3].green / 65536.0;
331 color3[2] = gp->colors[gp->ccolor3].blue / 65536.0;
337 if (gp->ccolor0 >= gp->ncolors) gp->ccolor0 = 0;
338 if (gp->ccolor1 >= gp->ncolors) gp->ccolor1 = 0;
339 if (gp->ccolor2 >= gp->ncolors) gp->ccolor2 = 0;
340 if (gp->ccolor3 >= gp->ncolors) gp->ccolor3 = 0;
342 glShadeModel(GL_SMOOTH);
344 glEnable(GL_LIGHTING);
348 glEnable(GL_DEPTH_TEST);
349 glEnable(GL_NORMALIZE);
350 glEnable(GL_CULL_FACE);
356 get_position (gp->rot, &x, &y, &z, !gp->button_down_p);
357 glTranslatef((x - 0.5) * 10,
361 gltrackball_rotate (gp->trackball);
363 get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
364 glRotatef (x * 360, 1.0, 0.0, 0.0);
365 glRotatef (y * 360, 0.0, 1.0, 0.0);
366 glRotatef (z * 360, 0.0, 0.0, 1.0);
369 glScalef( 8.0, 8.0, 8.0 );
371 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color0);
372 glCallList(gp->gasket0);
373 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color1);
374 glCallList(gp->gasket1);
375 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color2);
376 glCallList(gp->gasket2);
377 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color3);
378 glCallList(gp->gasket3);
383 if (gp->tick++ >= speed)
386 if (gp->current_depth >= max_depth)
387 gp->current_depth = -max_depth;
390 /* We make four different lists so that each face of the tetrahedrons
391 can have a different color (all triangles facing in the same
392 direction have the same color, which is different from all
393 triangles facing in other directions.)
395 glDeleteLists (gp->gasket0, 1);
396 glDeleteLists (gp->gasket1, 1);
397 glDeleteLists (gp->gasket2, 1);
398 glDeleteLists (gp->gasket3, 1);
400 mi->polygon_count = 0;
401 glNewList (gp->gasket0, GL_COMPILE); compile_gasket (mi, 0); glEndList();
402 glNewList (gp->gasket1, GL_COMPILE); compile_gasket (mi, 1); glEndList();
403 glNewList (gp->gasket2, GL_COMPILE); compile_gasket (mi, 2); glEndList();
404 glNewList (gp->gasket3, GL_COMPILE); compile_gasket (mi, 3); glEndList();
410 /* new window size or exposure */
412 reshape_gasket(ModeInfo *mi, int width, int height)
414 GLfloat h = (GLfloat) height / (GLfloat) width;
416 glViewport(0, 0, (GLint) width, (GLint) height);
417 glMatrixMode(GL_PROJECTION);
419 gluPerspective (30.0, 1/h, 1.0, 100.0);
421 glMatrixMode(GL_MODELVIEW);
423 gluLookAt( 0.0, 0.0, 30.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 gasket_handle_event (ModeInfo *mi, XEvent *event)
447 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
449 if (event->xany.type == ButtonPress &&
450 event->xbutton.button == Button1)
452 gp->button_down_p = True;
453 gltrackball_start (gp->trackball,
454 event->xbutton.x, event->xbutton.y,
455 MI_WIDTH (mi), MI_HEIGHT (mi));
458 else if (event->xany.type == ButtonRelease &&
459 event->xbutton.button == Button1)
461 gp->button_down_p = False;
464 else if (event->xany.type == ButtonPress &&
465 (event->xbutton.button == Button4 ||
466 event->xbutton.button == Button5 ||
467 event->xbutton.button == Button6 ||
468 event->xbutton.button == Button7))
470 gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
471 !!event->xbutton.state);
474 else if (event->xany.type == MotionNotify &&
477 gltrackball_track (gp->trackball,
478 event->xmotion.x, event->xmotion.y,
479 MI_WIDTH (mi), MI_HEIGHT (mi));
488 init_gasket(ModeInfo *mi)
490 int screen = MI_SCREEN(mi);
495 if ((gasket = (gasketstruct *) calloc(MI_NUM_SCREENS(mi),
496 sizeof (gasketstruct))) == NULL)
499 gp = &gasket[screen];
501 gp->window = MI_WINDOW(mi);
504 double spin_speed = 1.0;
505 double wander_speed = 0.03;
506 gp->rot = make_rotator (do_spin ? spin_speed : 0,
507 do_spin ? spin_speed : 0,
508 do_spin ? spin_speed : 0,
510 do_wander ? wander_speed : 0,
512 gp->trackball = gltrackball_init ();
516 gp->colors = (XColor *) calloc(gp->ncolors, sizeof(XColor));
517 make_smooth_colormap (0, 0, 0,
518 gp->colors, &gp->ncolors,
521 gp->ccolor1 = gp->ncolors * 0.25;
522 gp->ccolor2 = gp->ncolors * 0.5;
523 gp->ccolor3 = gp->ncolors * 0.75;
526 if ((gp->glx_context = init_GL(mi)) != NULL)
528 reshape_gasket(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
538 draw_gasket(ModeInfo * mi)
540 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
541 Display *display = MI_DISPLAY(mi);
542 Window window = MI_WINDOW(mi);
544 if (!gp->glx_context) return;
546 glDrawBuffer(GL_BACK);
551 glXMakeCurrent(display, window, *(gp->glx_context));
553 if (mi->fps_p) do_fps (mi);
555 glXSwapBuffers(display, window);
559 release_gasket(ModeInfo * mi)
565 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
567 gasketstruct *gp = &gasket[screen];
571 /* Display lists MUST be freed while their glXContext is current. */
572 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
574 if (glIsList(gp->gasket0)) glDeleteLists(gp->gasket0, 1);
575 if (glIsList(gp->gasket1)) glDeleteLists(gp->gasket1, 1);
576 if (glIsList(gp->gasket2)) glDeleteLists(gp->gasket2, 1);
577 if (glIsList(gp->gasket3)) glDeleteLists(gp->gasket3, 1);
580 (void) free((void *) gasket);
586 XSCREENSAVER_MODULE_2 ("Sierpinski3D", sierpinski3d, gasket)
588 /*********************************************************/