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 HACK_HANDLE_EVENT gasket_handle_event
43 # define EVENT_MASK PointerMotionMask
44 # define gasket_opts xlockmore_opts
47 #define DEF_SPIN "True"
48 #define DEF_WANDER "True"
49 #define DEF_SPEED "150"
50 #define DEF_MAXDEPTH "5"
52 # define DEFAULTS "*delay: 20000 \n" \
53 "*showFPS: False \n" \
54 "*wireframe: False \n" \
55 "*maxDepth: " DEF_MAXDEPTH "\n" \
56 "*speed: " DEF_SPEED "\n" \
57 "*spin: " DEF_SPIN "\n" \
58 "*wander: " DEF_WANDER "\n" \
60 # include "xlockmore.h" /* from the xscreensaver distribution */
61 #else /* !STANDALONE */
62 # include "xlock.h" /* from the xlockmore distribution */
63 #endif /* !STANDALONE */
69 #include "gltrackball.h"
72 #define countof(x) (sizeof((x))/sizeof((*x)))
77 static Bool do_wander;
79 static XrmOptionDescRec opts[] = {
80 {"-depth", ".sierpinski3d.maxDepth", XrmoptionSepArg, (caddr_t) 0 },
81 {"-speed", ".sierpinski3d.speed", XrmoptionSepArg, (caddr_t) 0 },
82 { "-spin", ".spin", XrmoptionNoArg, "True" },
83 { "+spin", ".spin", XrmoptionNoArg, "False" },
84 { "-wander", ".wander", XrmoptionNoArg, "True" },
85 { "+wander", ".wander", XrmoptionNoArg, "False" },
88 static argtype vars[] = {
89 {(caddr_t *) &do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
90 {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
91 {(caddr_t *) &speed, "speed", "Speed", DEF_SPEED, t_Int},
92 {(caddr_t *) &max_depth, "maxDepth", "MaxDepth", DEF_MAXDEPTH, t_Int},
96 ModeSpecOpt gasket_opts = {countof(opts), opts, countof(vars), vars, NULL};
99 ModStruct gasket_description =
100 {"gasket", "init_gasket", "draw_gasket", "release_gasket",
101 "draw_gasket", "init_gasket", NULL, &gasket_opts,
102 1000, 1, 2, 1, 4, 1.0, "",
103 "Shows GL's Sierpinski gasket", 0, NULL};
114 GLuint gasket0, gasket1, gasket2, gasket3;
115 GLXContext *glx_context;
118 trackball_state *trackball;
132 static gasketstruct *gasket = NULL;
134 static GLfloat normals[4][3];
139 triangle (GLfloat x1, GLfloat y1, GLfloat z1,
140 GLfloat x2, GLfloat y2, GLfloat z2,
141 GLfloat x3, GLfloat y3, GLfloat z3,
145 glBegin (GL_LINE_LOOP);
147 glBegin (GL_TRIANGLES);
148 glVertex3f (x1, y1, z1);
149 glVertex3f (x2, y2, z2);
150 glVertex3f (x3, y3, z3);
155 four_tetras (GL_VECTOR *outer, Bool wireframe_p, int countdown, int which,
163 glNormal3f (normals[0][0], normals[0][1], normals[0][2]);
164 triangle (outer[0].x, outer[0].y, outer[0].z,
165 outer[1].x, outer[1].y, outer[1].z,
166 outer[2].x, outer[2].y, outer[2].z,
171 glNormal3f (normals[1][0], normals[1][1], normals[1][2]);
172 triangle (outer[0].x, outer[0].y, outer[0].z,
173 outer[3].x, outer[3].y, outer[3].z,
174 outer[1].x, outer[1].y, outer[1].z,
179 glNormal3f (normals[2][0], normals[2][1], normals[2][2]);
180 triangle (outer[0].x, outer[0].y, outer[0].z,
181 outer[2].x, outer[2].y, outer[2].z,
182 outer[3].x, outer[3].y, outer[3].z,
187 glNormal3f (normals[3][0], normals[3][1], normals[3][2]);
188 triangle (outer[1].x, outer[1].y, outer[1].z,
189 outer[3].x, outer[3].y, outer[3].z,
190 outer[2].x, outer[2].y, outer[2].z,
202 GL_VECTOR inner[M23+1];
205 inner[M01].x = (outer[0].x + outer[1].x) / 2.0;
206 inner[M01].y = (outer[0].y + outer[1].y) / 2.0;
207 inner[M01].z = (outer[0].z + outer[1].z) / 2.0;
209 inner[M02].x = (outer[0].x + outer[2].x) / 2.0;
210 inner[M02].y = (outer[0].y + outer[2].y) / 2.0;
211 inner[M02].z = (outer[0].z + outer[2].z) / 2.0;
213 inner[M03].x = (outer[0].x + outer[3].x) / 2.0;
214 inner[M03].y = (outer[0].y + outer[3].y) / 2.0;
215 inner[M03].z = (outer[0].z + outer[3].z) / 2.0;
217 inner[M12].x = (outer[1].x + outer[2].x) / 2.0;
218 inner[M12].y = (outer[1].y + outer[2].y) / 2.0;
219 inner[M12].z = (outer[1].z + outer[2].z) / 2.0;
221 inner[M13].x = (outer[1].x + outer[3].x) / 2.0;
222 inner[M13].y = (outer[1].y + outer[3].y) / 2.0;
223 inner[M13].z = (outer[1].z + outer[3].z) / 2.0;
225 inner[M23].x = (outer[2].x + outer[3].x) / 2.0;
226 inner[M23].y = (outer[2].y + outer[3].y) / 2.0;
227 inner[M23].z = (outer[2].z + outer[3].z) / 2.0;
231 corner[0] = outer[0];
232 corner[1] = inner[M01];
233 corner[2] = inner[M02];
234 corner[3] = inner[M03];
235 four_tetras (corner, wireframe_p, countdown, which, countP);
237 corner[0] = inner[M01];
238 corner[1] = outer[1];
239 corner[2] = inner[M12];
240 corner[3] = inner[M13];
241 four_tetras (corner, wireframe_p, countdown, which, countP);
243 corner[0] = inner[M02];
244 corner[1] = inner[M12];
245 corner[2] = outer[2];
246 corner[3] = inner[M23];
247 four_tetras (corner, wireframe_p, countdown, which, countP);
249 corner[0] = inner[M03];
250 corner[1] = inner[M13];
251 corner[2] = inner[M23];
252 corner[3] = outer[3];
253 four_tetras (corner, wireframe_p, countdown, which, countP);
259 compile_gasket(ModeInfo *mi, int which)
261 Bool wireframe_p = MI_IS_WIREFRAME(mi);
262 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
269 normals[0][2] = -sqrt(2.0 / 3.0);
272 normals[1][1] = -sqrt(0.75);
273 normals[1][2] = sqrt(2.0 / 3.0) / 3.0;
275 normals[2][0] = sqrt (0.5);
276 normals[2][1] = sqrt(0.75) / 2.0;
277 normals[2][2] = normals[1][2];
279 normals[3][0] = -normals[2][0];
280 normals[3][1] = normals[2][1];
281 normals[3][2] = normals[1][2];
284 /* define verticies */
286 vertex[0].y = -(1.0/3.0)*sqrt((2.0/3.0));
287 vertex[0].z = -sqrt(3.0)/6.0;
290 vertex[1].y = -(1.0/3.0)*sqrt((2.0/3.0));
291 vertex[1].z = -sqrt(3.0)/6.0;
294 vertex[2].y = (2.0/3.0)*sqrt((2.0/3.0));
295 vertex[2].z = -sqrt(3.0)/6.0;
299 vertex[3].z = sqrt(3.0)/3.0;
306 four_tetras (vertex, wireframe_p,
307 (gp->current_depth < 0
308 ? -gp->current_depth : gp->current_depth),
311 mi->polygon_count = count;
317 Bool wireframe_p = MI_IS_WIREFRAME(mi);
318 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
319 static int tick = 999999;
321 static GLfloat pos[4] = {-4.0, 3.0, 10.0, 1.0};
322 static float white[] = {1.0, 1.0, 1.0, 1.0};
323 static float color0[] = {0.0, 0.0, 0.0, 1.0};
324 static float color1[] = {0.0, 0.0, 0.0, 1.0};
325 static float color2[] = {0.0, 0.0, 0.0, 1.0};
326 static float color3[] = {0.0, 0.0, 0.0, 1.0};
328 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
334 glLightfv(GL_LIGHT0, GL_POSITION, pos);
336 color0[0] = gp->colors[gp->ccolor0].red / 65536.0;
337 color0[1] = gp->colors[gp->ccolor0].green / 65536.0;
338 color0[2] = gp->colors[gp->ccolor0].blue / 65536.0;
340 color1[0] = gp->colors[gp->ccolor1].red / 65536.0;
341 color1[1] = gp->colors[gp->ccolor1].green / 65536.0;
342 color1[2] = gp->colors[gp->ccolor1].blue / 65536.0;
344 color2[0] = gp->colors[gp->ccolor2].red / 65536.0;
345 color2[1] = gp->colors[gp->ccolor2].green / 65536.0;
346 color2[2] = gp->colors[gp->ccolor2].blue / 65536.0;
348 color3[0] = gp->colors[gp->ccolor3].red / 65536.0;
349 color3[1] = gp->colors[gp->ccolor3].green / 65536.0;
350 color3[2] = gp->colors[gp->ccolor3].blue / 65536.0;
356 if (gp->ccolor0 >= gp->ncolors) gp->ccolor0 = 0;
357 if (gp->ccolor1 >= gp->ncolors) gp->ccolor1 = 0;
358 if (gp->ccolor2 >= gp->ncolors) gp->ccolor2 = 0;
359 if (gp->ccolor3 >= gp->ncolors) gp->ccolor3 = 0;
361 glShadeModel(GL_SMOOTH);
363 glEnable(GL_LIGHTING);
367 glEnable(GL_DEPTH_TEST);
368 glEnable(GL_NORMALIZE);
369 glEnable(GL_CULL_FACE);
375 get_position (gp->rot, &x, &y, &z, !gp->button_down_p);
376 glTranslatef((x - 0.5) * 10,
380 gltrackball_rotate (gp->trackball);
382 get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
383 glRotatef (x * 360, 1.0, 0.0, 0.0);
384 glRotatef (y * 360, 0.0, 1.0, 0.0);
385 glRotatef (z * 360, 0.0, 0.0, 1.0);
388 glScalef( 8.0, 8.0, 8.0 );
390 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color0);
391 glCallList(gp->gasket0);
392 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color1);
393 glCallList(gp->gasket1);
394 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color2);
395 glCallList(gp->gasket2);
396 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color3);
397 glCallList(gp->gasket3);
405 if (gp->current_depth >= max_depth)
406 gp->current_depth = -max_depth;
409 /* We make four different lists so that each face of the tetrahedrons
410 can have a different color (all triangles facing in the same
411 direction have the same color, which is different from all
412 triangles facing in other directions.)
414 glDeleteLists (gp->gasket0, 1);
415 glDeleteLists (gp->gasket1, 1);
416 glDeleteLists (gp->gasket2, 1);
417 glDeleteLists (gp->gasket3, 1);
419 mi->polygon_count = 0;
420 glNewList (gp->gasket0, GL_COMPILE); compile_gasket (mi, 0); glEndList();
421 glNewList (gp->gasket1, GL_COMPILE); compile_gasket (mi, 1); glEndList();
422 glNewList (gp->gasket2, GL_COMPILE); compile_gasket (mi, 2); glEndList();
423 glNewList (gp->gasket3, GL_COMPILE); compile_gasket (mi, 3); glEndList();
429 /* new window size or exposure */
431 reshape_gasket(ModeInfo *mi, int width, int height)
433 GLfloat h = (GLfloat) height / (GLfloat) width;
435 glViewport(0, 0, (GLint) width, (GLint) height);
436 glMatrixMode(GL_PROJECTION);
438 gluPerspective (30.0, 1/h, 1.0, 100.0);
440 glMatrixMode(GL_MODELVIEW);
442 gluLookAt( 0.0, 0.0, 30.0,
446 glClear(GL_COLOR_BUFFER_BIT);
452 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
454 /* draw the gasket */
455 gp->gasket0 = glGenLists(1);
456 gp->gasket1 = glGenLists(1);
457 gp->gasket2 = glGenLists(1);
458 gp->gasket3 = glGenLists(1);
459 gp->current_depth = 1; /* start out at level 1, not 0 */
464 gasket_handle_event (ModeInfo *mi, XEvent *event)
466 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
468 if (event->xany.type == ButtonPress &&
469 event->xbutton.button & Button1)
471 gp->button_down_p = True;
472 gltrackball_start (gp->trackball,
473 event->xbutton.x, event->xbutton.y,
474 MI_WIDTH (mi), MI_HEIGHT (mi));
477 else if (event->xany.type == ButtonRelease &&
478 event->xbutton.button & Button1)
480 gp->button_down_p = False;
483 else if (event->xany.type == MotionNotify &&
486 gltrackball_track (gp->trackball,
487 event->xmotion.x, event->xmotion.y,
488 MI_WIDTH (mi), MI_HEIGHT (mi));
497 init_gasket(ModeInfo *mi)
499 int screen = MI_SCREEN(mi);
504 if ((gasket = (gasketstruct *) calloc(MI_NUM_SCREENS(mi),
505 sizeof (gasketstruct))) == NULL)
508 gp = &gasket[screen];
510 gp->window = MI_WINDOW(mi);
513 double spin_speed = 1.0;
514 double wander_speed = 0.03;
515 gp->rot = make_rotator (do_spin ? spin_speed : 0,
516 do_spin ? spin_speed : 0,
517 do_spin ? spin_speed : 0,
519 do_wander ? wander_speed : 0,
521 gp->trackball = gltrackball_init ();
525 gp->colors = (XColor *) calloc(gp->ncolors, sizeof(XColor));
526 make_smooth_colormap (0, 0, 0,
527 gp->colors, &gp->ncolors,
530 gp->ccolor1 = gp->ncolors * 0.25;
531 gp->ccolor2 = gp->ncolors * 0.5;
532 gp->ccolor3 = gp->ncolors * 0.75;
534 if ((gp->glx_context = init_GL(mi)) != NULL)
536 reshape_gasket(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
546 draw_gasket(ModeInfo * mi)
548 gasketstruct *gp = &gasket[MI_SCREEN(mi)];
549 Display *display = MI_DISPLAY(mi);
550 Window window = MI_WINDOW(mi);
552 if (!gp->glx_context) return;
554 glDrawBuffer(GL_BACK);
559 glXMakeCurrent(display, window, *(gp->glx_context));
561 if (mi->fps_p) do_fps (mi);
563 glXSwapBuffers(display, window);
567 release_gasket(ModeInfo * mi)
573 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
575 gasketstruct *gp = &gasket[screen];
579 /* Display lists MUST be freed while their glXContext is current. */
580 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
582 if (glIsList(gp->gasket0)) glDeleteLists(gp->gasket0, 1);
583 if (glIsList(gp->gasket1)) glDeleteLists(gp->gasket1, 1);
584 if (glIsList(gp->gasket2)) glDeleteLists(gp->gasket2, 1);
585 if (glIsList(gp->gasket3)) glDeleteLists(gp->gasket3, 1);
588 (void) free((void *) gasket);
595 /*********************************************************/