1 /* glknots, Copyright (c) 2003-2008 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * Generates some 3D knots (closed loops).
12 * Inspired by Paul Bourke <pbourke@swin.edu.au> at
13 * http://astronomy.swin.edu.au/~pbourke/curves/knot/
16 #define DEFAULTS "*delay: 30000 \n" \
17 "*showFPS: False \n" \
18 "*wireframe: False \n" \
20 # define refresh_knot 0
21 # define release_knot 0
23 #define countof(x) (sizeof((x))/sizeof((*x)))
25 #include "xlockmore.h"
29 #include "gltrackball.h"
32 #ifdef USE_GL /* whole file */
34 #define DEF_SPIN "XYZ"
35 #define DEF_WANDER "True"
36 #define DEF_SPEED "1.0"
37 #define DEF_THICKNESS "0.3"
38 #define DEF_SEGMENTS "800"
39 #define DEF_DURATION "8"
42 GLXContext *glx_context;
44 trackball_state *trackball;
53 int mode; /* 0 = normal, 1 = out, 2 = in */
62 static knot_configuration *bps = NULL;
66 static Bool do_wander;
67 static GLfloat thickness;
68 static unsigned int segments;
71 static XrmOptionDescRec opts[] = {
72 { "-spin", ".spin", XrmoptionSepArg, 0 },
73 { "+spin", ".spin", XrmoptionNoArg, "" },
74 { "-wander", ".wander", XrmoptionNoArg, "True" },
75 { "+wander", ".wander", XrmoptionNoArg, "False" },
76 { "-speed", ".speed", XrmoptionSepArg, 0 },
77 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
78 { "-segments", ".segments", XrmoptionSepArg, 0 },
79 { "-duration", ".duration", XrmoptionSepArg, 0 },
82 static argtype vars[] = {
83 {&do_spin, "spin", "Spin", DEF_SPIN, t_String},
84 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
85 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
86 {&thickness, "thickness", "Thickness", DEF_THICKNESS, t_Float},
87 {&segments, "segments", "Segments", DEF_SEGMENTS, t_Int},
88 {&duration, "duration", "Duration", DEF_DURATION, t_Int},
91 ENTRYPOINT ModeSpecOpt knot_opts = {countof(opts), opts, countof(vars), vars, NULL};
95 make_knot (ModeInfo *mi)
97 int wire = MI_IS_WIREFRAME(mi);
99 GLfloat diam = (4 * thickness);
100 int faces = (wire ? 3 : 6);
103 double x, y, z, ox=0, oy=0, oz=0;
108 Bool blobby_p = (0 == (random() % 5));
109 Bool type = (random() % 2);
111 for (i = 0; i < countof(p); i++)
113 p[i] = 1 + (random() % 4);
114 if (! (random() % 3))
115 p[i] += (random() % 5);
121 p[1] *= ((p[0] + p[0]) / 10);
125 mi->polygon_count = 0;
127 for (i = 0; i <= segments; i++)
131 mu = i * (M_PI * 2) / segments;
132 x = 10 * (cos(mu) + cos(p[0]*mu)) + cos(p[1]*mu) + cos(p[2]*mu);
133 y = 6 * sin(mu) + 10 * sin(p[3]*mu);
134 z = 16 * sin(p[4]*mu) * sin(p[5]*mu/2) + p[6]*sin(p[7]*mu) -
139 mu = i * (M_PI * 2) * p[0] / (double) segments;
140 x = 10 * cos(mu) * (1 + cos(p[1] * mu/ p[0]) / 2.0);
141 y = 25 * sin(p[1] * mu / p[0]) / 2.0;
142 z = 10 * sin(mu) * (1 + cos(p[1] * mu/ p[0]) / 2.0);
149 GLfloat dist = sqrt ((x-ox)*(x-ox) +
157 di = dist * (segments / 500.0);
161 mi->polygon_count += tube (ox, oy, oz,
164 faces, True, wire, wire);
174 /* Window management, etc
177 reshape_knot (ModeInfo *mi, int width, int height)
179 GLfloat h = (GLfloat) height / (GLfloat) width;
181 glViewport (0, 0, (GLint) width, (GLint) height);
183 glMatrixMode(GL_PROJECTION);
185 gluPerspective (30.0, 1/h, 1.0, 100.0);
187 glMatrixMode(GL_MODELVIEW);
189 gluLookAt( 0.0, 0.0, 30.0,
193 glClear(GL_COLOR_BUFFER_BIT);
198 new_knot (ModeInfo *mi)
200 knot_configuration *bp = &bps[MI_SCREEN(mi)];
203 bp->clear_p = !!(random() % 15);
206 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
207 make_smooth_colormap (0, 0, 0,
208 bp->colors, &bp->ncolors,
211 for (i = 0; i < bp->ncolors; i++)
213 /* make colors twice as bright */
214 bp->colors[i].red = (bp->colors[i].red >> 2) + 0x7FFF;
215 bp->colors[i].green = (bp->colors[i].green >> 2) + 0x7FFF;
216 bp->colors[i].blue = (bp->colors[i].blue >> 2) + 0x7FFF;
219 glNewList (bp->knot_list, GL_COMPILE);
226 knot_handle_event (ModeInfo *mi, XEvent *event)
228 knot_configuration *bp = &bps[MI_SCREEN(mi)];
230 if (event->xany.type == ButtonPress &&
231 event->xbutton.button == Button1)
233 bp->button_down_p = True;
234 gltrackball_start (bp->trackball,
235 event->xbutton.x, event->xbutton.y,
236 MI_WIDTH (mi), MI_HEIGHT (mi));
239 else if (event->xany.type == ButtonRelease &&
240 event->xbutton.button == Button1)
242 bp->button_down_p = False;
245 else if (event->xany.type == ButtonPress &&
246 (event->xbutton.button == Button4 ||
247 event->xbutton.button == Button5 ||
248 event->xbutton.button == Button6 ||
249 event->xbutton.button == Button7))
251 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 5,
252 !!event->xbutton.state);
255 else if (event->xany.type == MotionNotify &&
258 gltrackball_track (bp->trackball,
259 event->xmotion.x, event->xmotion.y,
260 MI_WIDTH (mi), MI_HEIGHT (mi));
270 init_knot (ModeInfo *mi)
272 knot_configuration *bp;
273 int wire = MI_IS_WIREFRAME(mi);
276 bps = (knot_configuration *)
277 calloc (MI_NUM_SCREENS(mi), sizeof (knot_configuration));
279 fprintf(stderr, "%s: out of memory\n", progname);
283 bp = &bps[MI_SCREEN(mi)];
286 bp = &bps[MI_SCREEN(mi)];
288 bp->glx_context = init_GL(mi);
290 if (thickness <= 0) thickness = 0.001;
291 else if (thickness > 1) thickness = 1;
293 if (segments < 10) segments = 10;
295 reshape_knot (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
299 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
300 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
301 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
302 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
304 glEnable(GL_LIGHTING);
306 glEnable(GL_DEPTH_TEST);
307 glEnable(GL_CULL_FACE);
309 glLightfv(GL_LIGHT0, GL_POSITION, pos);
310 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
311 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
312 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
316 Bool spinx=False, spiny=False, spinz=False;
317 double spin_speed = 2.0;
318 double wander_speed = 0.05;
319 double spin_accel = 0.2;
324 if (*s == 'x' || *s == 'X') spinx = True;
325 else if (*s == 'y' || *s == 'Y') spiny = True;
326 else if (*s == 'z' || *s == 'Z') spinz = True;
327 else if (*s == '0') ;
331 "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
338 bp->rot = make_rotator (spinx ? spin_speed : 0,
339 spiny ? spin_speed : 0,
340 spinz ? spin_speed : 0,
342 do_wander ? wander_speed : 0,
343 (spinx && spiny && spinz));
344 bp->trackball = gltrackball_init ();
347 bp->knot_list = glGenLists (1);
350 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
355 draw_knot (ModeInfo *mi)
357 knot_configuration *bp = &bps[MI_SCREEN(mi)];
358 Display *dpy = MI_DISPLAY(mi);
359 Window window = MI_WINDOW(mi);
361 GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
362 GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
363 GLfloat bshiny = 128.0;
365 if (!bp->glx_context)
368 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
372 if (bp->draw_tick++ > 10)
374 time_t now = time((time_t *) 0);
375 if (bp->last_time == 0) bp->last_time = now;
377 if (!bp->button_down_p &&
378 now - bp->last_time >= duration)
380 bp->mode = 1; /* go out */
381 bp->mode_tick = 10 * speed;
386 else if (bp->mode == 1) /* out */
388 if (--bp->mode_tick <= 0)
391 bp->mode_tick = 10 * speed;
392 bp->mode = 2; /* go in */
395 else if (bp->mode == 2) /* in */
397 if (--bp->mode_tick <= 0)
398 bp->mode = 0; /* normal */
403 glShadeModel(GL_SMOOTH);
405 glEnable(GL_DEPTH_TEST);
406 glEnable(GL_NORMALIZE);
407 glEnable(GL_CULL_FACE);
410 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
416 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
417 glTranslatef((x - 0.5) * 8,
421 gltrackball_rotate (bp->trackball);
423 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
425 glRotatef (x * 360, 1.0, 0.0, 0.0);
426 glRotatef (y * 360, 0.0, 1.0, 0.0);
427 glRotatef (z * 360, 0.0, 0.0, 1.0);
430 bcolor[0] = bp->colors[bp->ccolor].red / 65536.0;
431 bcolor[1] = bp->colors[bp->ccolor].green / 65536.0;
432 bcolor[2] = bp->colors[bp->ccolor].blue / 65536.0;
434 if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
436 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
437 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
438 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
440 glScalef(0.25, 0.25, 0.25);
444 GLfloat s = (bp->mode == 1
445 ? bp->mode_tick / (10 * speed)
446 : ((10 * speed) - bp->mode_tick + 1) / (10 * speed));
450 glCallList (bp->knot_list);
454 if (mi->fps_p) do_fps (mi);
457 glXSwapBuffers(dpy, window);
460 XSCREENSAVER_MODULE_2 ("GLKnots", glknots, knot)