255ebb75e8500fb7d16ff6ead4d04c785791140f
[xscreensaver] / hacks / glx / lockward.c
1 /*
2  * lockward.c:  First attempt at an Xscreensaver.
3  *
4  * Leo L. Schwab                                        2007.08.17
5  ****
6  * Copyright (c) 2007 Leo L. Schwab
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to permit
13  * persons to whom the Software is furnished to do so, subject to the
14  * following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  * 
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
22  * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #include <ctype.h>
28 #include <strings.h>
29
30 #include "xlockmore.h"
31 #include "colors.h"
32
33
34 /***************************************************************************
35  * #defines
36  */
37 #ifdef USE_GL /* whole file */
38
39 #define DEFAULTS        "*delay:        20000   \n"\
40                         "*showFPS:      False   \n"
41
42 #define refresh_lockward        0
43
44
45 #define NUMOF(x)        (sizeof ((x)) / sizeof ((*x)))
46
47 #define NBLADES         12
48 #define NSPINNERS       4
49 #define NRADII          8
50 #define COLORIDX_SHF    4
51
52
53 /***************************************************************************
54  * Structure definitions.
55  */
56 struct lockward_context;                        /*  Forward declaration.  */
57
58 #define int8_t   char
59 #define int16_t  short
60 #define int32_t  int
61 #define uint8_t  unsigned char
62 #define uint16_t unsigned short
63 #define uint32_t unsigned int
64
65 typedef struct bladestate {
66         uint8_t         outer, inner;   /*  Radii  */
67 } bladestate;
68
69 typedef struct spinnerstate {
70         GLfloat         rot;    /*  Terminal rotation after count expires */
71         GLfloat         rotinc; /*  Per-frame increment to rot.  */
72         XColor          *colors;
73         bladestate      *bladeidx;
74         int             ncolors;        /*  n.4 fixed-point  */
75         int             ccolor;         /*  n.4 fixed-point  */
76         int             colorinc;       /*  n.4 fixed-point  */
77         int             rotcount;
78         uint8_t         nblades;
79 } spinnerstate;
80
81 typedef struct blinkstate {
82         int             (*drawfunc) (struct lockward_context *ctx,
83                                      struct blinkstate *bs);
84         uint32_t        *noise; /*  For draw_blink_segment_scatter()  */
85         GLfloat         color[4];
86         uint32_t        val;
87         int16_t         dwell;  /*  <0: sharp  >0: decay  */
88         int16_t         dwellcnt;
89         uint8_t         type;
90         int8_t          counter;
91         int8_t          direction;
92         int8_t          radius;
93 } blinkstate;
94
95 enum blinktype {
96         BTYPE_RADIAL_SINGLE = 0,
97         BTYPE_RADIAL_RANDOM,
98         BTYPE_RADIAL_SEQ,
99         BTYPE_RADIAL_DOUBLESEQ,
100         BTYPE_SEGMENT_SINGLE,
101         BTYPE_SEGMENT_RANDOM,
102         BTYPE_CONCENTRIC_SINGLE,
103         BTYPE_CONCENTRIC_RANDOM,
104         BTYPE_CONCENTRIC_SEQ,
105         BTYPE_SEGMENT_SCATTER,
106         MAX_BTYPE
107 };
108
109 typedef struct lockward_context {
110         GLXContext      *glx_context;
111
112         spinnerstate    spinners[NSPINNERS];
113         blinkstate      blink;
114
115         GLuint          blades_outer, blades_inner;
116         GLuint          rings;
117         Bool            blendmode;
118         int             nextblink;
119         int             fps;
120 } lockward_context;
121
122
123 /***************************************************************************
124  * Prototypes.
125  */
126 static void free_lockward (lockward_context *ctx);
127
128
129 /***************************************************************************
130  * Global variables.
131  */
132 static lockward_context *g_ctx = NULL;
133 static Bool             g_blink_p = True;
134 static int              g_blades = NBLADES;
135 static int              g_rotateidle_min,
136                         g_rotateidle_max;
137 static int              g_blinkidle_min,
138                         g_blinkidle_max;
139 static int              g_blinkdwell_min,
140                         g_blinkdwell_max;
141
142 #define DEF_BLINK               "True"
143 #define DEF_ROTATEIDLEMIN       "1000"
144 #define DEF_ROTATEIDLEMAX       "6000"
145 #define DEF_BLINKIDLEMIN        "1000"
146 #define DEF_BLINKIDLEMAX        "9000"
147 #define DEF_BLINKDWELLMIN       "100"
148 #define DEF_BLINKDWELLMAX       "600"
149
150
151 static XrmOptionDescRec opts[] = {
152         { "-blink",     ".blink",  XrmoptionNoArg, "on" },
153         { "+blink",     ".blink",  XrmoptionNoArg, "off" },
154         { "-rotateidle-min",    ".rotateidlemin",       XrmoptionSepArg,        0 },
155         { "-rotateidle-max",    ".rotateidlemax",       XrmoptionSepArg,        0 },
156         { "-blinkidle-min",     ".blinkidlemin",        XrmoptionSepArg,        0 },
157         { "-blinkidle-max",     ".blinkidlemax",        XrmoptionSepArg,        0 },
158         { "-blinkdwell-min",    ".blinkdwellmin",       XrmoptionSepArg,        0 },
159         { "-blinkdwell-max",    ".blinkdwellmax",       XrmoptionSepArg,        0 },
160 };
161
162 static argtype vars[] = {
163         { &g_blink_p, "blink",  "Blink", DEF_BLINK, t_Bool      },
164         { &g_rotateidle_min, "rotateidlemin",   "Rotateidlemin", DEF_ROTATEIDLEMIN, t_Int       },
165         { &g_rotateidle_max, "rotateidlemax",   "Rotateidlemax", DEF_ROTATEIDLEMAX, t_Int       },
166         { &g_blinkidle_min, "blinkidlemin", "Blinkidlemin", DEF_BLINKIDLEMIN, t_Int },
167         { &g_blinkidle_max, "blinkidlemax", "Blinkidlemax", DEF_BLINKIDLEMAX, t_Int },
168         { &g_blinkdwell_min, "blinkdwellmin", "Blinkdwellmin", DEF_BLINKDWELLMIN, t_Int },
169         { &g_blinkdwell_max, "blinkdwellmax", "Blinkdwellmax", DEF_BLINKDWELLMAX, t_Int },
170 };
171
172 static OptionStruct desc[] = {
173         { "-/+blink", "Turn on/off blinking effects." },
174         { "-rotateidle-min", "Minimum idle time for rotators, in milliseconds." },
175         { "-rotateidle-max", "Maximum idle time for rotators, in milliseconds." },
176         { "-blinkidle-min", "Minimum idle time between blink effects, in milliseconds." },
177         { "-blinkidle-max", "Maximum idle time between blink effects, in milliseconds." },
178         { "-blinkdwell-min", "Minimum dwell time for blink effects, in milliseconds." },
179         { "-blinkdwell-max", "Maximum dwell time for blink effects, in milliseconds." },
180 };
181
182 ENTRYPOINT ModeSpecOpt lockward_opts = {
183         NUMOF(opts), opts, NUMOF(vars), vars, desc
184 };
185
186
187 /***************************************************************************
188  * Window management.
189  */
190 ENTRYPOINT void
191 reshape_lockward (ModeInfo *mi, int width, int height)
192 {
193         GLfloat h = (GLfloat) height / (GLfloat) width;
194
195         glViewport (0, 0, (GLint) width, (GLint) height);
196
197         glMatrixMode (GL_PROJECTION);
198         glLoadIdentity ();
199         if (height > width)
200                 gluOrtho2D (-8.0, 8.0, -8.0 * h, 8.0 * h);
201         else
202                 gluOrtho2D (-8.0 / h, 8.0 / h, -8.0, 8.0);
203
204         glMatrixMode (GL_MODELVIEW);
205 }
206
207 ENTRYPOINT Bool
208 lockward_handle_event (ModeInfo *mi, XEvent *event)
209 {
210         lockward_context *ctx = &g_ctx[MI_SCREEN (mi)];
211
212         if (event->xany.type == KeyPress) {
213                 KeySym  keysym;
214                 char    c = 0;
215
216                 XLookupString (&event->xkey, &c, 1, &keysym, 0);
217                 if (c == 'b') {
218                         ctx->blendmode ^= 1;
219                         return True;
220                 }
221         }
222
223         return False;
224 }
225
226
227 /***************************************************************************
228  * "Blade" routines.
229  */
230 static void
231 random_blade_rot (lockward_context *ctx, struct spinnerstate *ss)
232 {
233         /*
234          * The circle is divided up in to g_blades divisions.  The idea here
235          * is to rotate to an exact division point.
236          *
237          * The target rotation is computed via random numbers.
238          *
239          * The time it takes to get there is a maximum of six seconds per
240          * division, and a minimum of one second (no matter how far away it
241          * is), and is selected by random numbers.
242          *
243          * The time value is converted into frames, and a per-frame rotation
244          * is computed.
245          *
246          * During rendering, we approach the target rotation by subtracting
247          * from it the per-frame rotation times the number of outstanding
248          * ticks.  Doing it this way means we'll hit the target rotation
249          * exactly, without low-order errors creeping in to the values (at
250          * least not nearly as quickly).
251          */
252         GLfloat d;
253         int     dist;
254
255         dist = random() % g_blades + 1;
256
257         ss->rotcount = random() % (6 * dist * ctx->fps - ctx->fps)
258                      + ctx->fps;
259
260         if (random() & 4)
261                 dist = -dist;
262         d = dist * 360.0 / (GLfloat) g_blades;
263         ss->rot += d;
264         ss->rotinc = d / (GLfloat) ss->rotcount;
265 }
266
267
268 /*
269  * A "blade" is pie-wedge shaped flat thing that is rotated around where the
270  * apex is/would be.  Initially envisioned as 1/12th of a circle, but that
271  * could be configurable.  The inner and outer edges are rounded off using
272  * six subdivisions so that, when multiple blades are assembled, it looks
273  * more like a circle and less like a polygon.
274  *
275  * The blade is assembled as a tri-fan.  It is oriented centered at 3
276  * o'clock.  The blade is composed of two display lists -- arcs, essentially
277  * -- the outer and the inner one.  The outer one *must* be called before
278  * the inner one, or the blade clockwise-ness will be wrong, and become
279  * invisible.  Arcs of various radii are compiled.
280  */
281 #define SUBDIV          6
282
283 static void
284 gen_blade_arcs (lockward_context *ctx)
285 {
286         GLfloat here, there, step;
287         int     i, n;
288
289         here = 0;
290         there = M_PI * 2.0 / g_blades;
291         step = there / SUBDIV;
292         here -= SUBDIV * step / 2.0;
293
294         /*
295          * Build outer blade arcs.
296          * Start at left side of outer radius.  Strike all its vertices.
297          */
298         for (n = 0;  n < NRADII;  ++n) {
299                 glNewList (ctx->blades_outer + n, GL_COMPILE);
300                 for (i = SUBDIV;  i >= 0;  --i)
301                         glVertex3f (cos (here + step * i) * (n + 1.0),
302                                     sin (here + step * i) * (n + 1.0), 0);
303                 glEndList ();
304         }
305
306         /*
307          * Build inner blade arcs.
308          * Move to inner radius, strike all vertices in opposite order.
309          */
310         for (n = 0;  n < NRADII;  ++n) {
311                 glNewList (ctx->blades_inner + n, GL_COMPILE);
312                 for (i = 0;  i <= SUBDIV;  ++i)
313                         glVertex3f (cos (here + step * i) * (n + 1.0),
314                                     sin (here + step * i) * (n + 1.0), 0);
315                 glEndList ();
316         }
317 }
318
319 static void
320 gen_rings (lockward_context *ctx)
321 {
322         GLfloat step;
323         int     i, n;
324
325         step = M_PI * 2.0 / (g_blades * SUBDIV);
326
327         for (n = 0;  n < NRADII - 1;  ++n) {
328                 glNewList (ctx->rings + n, GL_COMPILE);
329                 glBegin (GL_TRIANGLE_STRIP);
330                 for (i = g_blades * SUBDIV;  i >= 0;  --i) {
331                         glVertex3f (cos (step * i) * (n + 1.0),
332                                     sin (step * i) * (n + 1.0), 0);
333                         glVertex3f (cos (step * i) * (n + 2.0),
334                                     sin (step * i) * (n + 2.0), 0);
335                 }
336                 glEnd();
337                 glEndList ();
338         }
339 }
340
341
342 /***************************************************************************
343  * "Blink" routines.
344  */
345 static int
346 calc_interval_frames (lockward_context *ctx, int min, int max)
347 {
348         /*
349          * Compute random interval between min and max milliseconds.
350          * Returned value is in frames.
351          */
352         register int i;
353
354         i = min;
355         if (max > min)
356                 i += random() % (max - min);
357
358         return i * ctx->fps / 1000;
359 }
360
361 static void
362 set_alpha_by_dwell (struct blinkstate *bs)
363 {
364         if (bs->dwell > 0)
365                 bs->color[3] = (GLfloat) bs->dwellcnt / (GLfloat) bs->dwell;
366         else
367                 bs->color[3] = bs->dwellcnt > (-bs->dwell >> 2)  ?  1.0  : 0.0;
368 }
369
370 static void
371 draw_blink_blade (lockward_context *ctx, int inner, int outer)
372 {
373         glBegin (GL_TRIANGLE_FAN);
374         glCallList (ctx->blades_outer + outer);
375         glCallList (ctx->blades_inner + inner);
376         glEnd ();
377 }
378
379 static int
380 draw_blink_radial_random (lockward_context *ctx, struct blinkstate *bs)
381 {
382         int i;
383
384         /*
385          * There is no sense of direction in a random sweep, so re-use the
386          * 'direction' field to hold the current blade we're messing with.
387          */
388         if (bs->dwellcnt < 0) {
389                 if (bs->counter <= 0) {
390                         bs->drawfunc = NULL;
391                         return 0;
392                 }
393
394                 /*
395                  * Find available blade.  Potentially very slow, depending on
396                  * how unlucky we are.
397                  */
398                 do {
399                         i = random() % g_blades;
400                 } while (bs->val & (1 << i));
401                 bs->val |= (1 << i);    /*  Mark as used.  */
402                 bs->direction = i;
403                 if ((bs->dwellcnt = bs->dwell) < 0)
404                         bs->dwellcnt = -bs->dwellcnt;
405                 
406                 if (    bs->type == BTYPE_SEGMENT_SINGLE
407                     ||  bs->type == BTYPE_SEGMENT_RANDOM)
408                         bs->radius = random() % (NRADII - 1);
409
410                 --bs->counter;
411         }
412
413         set_alpha_by_dwell (bs);
414         glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
415         glColor4fv (bs->color);
416         glRotatef (bs->direction * 360.0 / (GLfloat) g_blades, 0, 0, 1);
417         if (bs->radius >= 0)
418                 draw_blink_blade (ctx, bs->radius, bs->radius + 1);
419         else
420                 draw_blink_blade (ctx, 0, NRADII - 1);
421
422         --bs->dwellcnt;
423
424         return SUBDIV + SUBDIV;
425 }
426
427 static int
428 draw_blink_radial_sequential (lockward_context *ctx, struct blinkstate *bs)
429 {
430         if (bs->dwellcnt < 0) {
431                 if (bs->counter <= 0) {
432                         bs->drawfunc = NULL;
433                         return 0;
434                 }
435                 if ((bs->dwellcnt = bs->dwell) < 0)
436                         bs->dwellcnt = -bs->dwellcnt;
437                 --bs->counter;
438         }
439
440         set_alpha_by_dwell (bs);
441         glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
442         glColor4fv (bs->color);
443         glRotatef ((bs->counter * bs->direction + (int) bs->val)
444                     * 360.0 / (GLfloat) g_blades,
445                    0, 0, 1);
446         draw_blink_blade (ctx, 0, NRADII - 1);
447
448         --bs->dwellcnt;
449
450         return SUBDIV + SUBDIV;
451 }
452
453 static int
454 draw_blink_radial_doubleseq (lockward_context *ctx, struct blinkstate *bs)
455 {
456         int polys;
457
458         if (bs->dwellcnt < 0) {
459                 if (bs->counter <= 0) {
460                         bs->drawfunc = NULL;
461                         return 0;
462                 }
463                 if ((bs->dwellcnt = bs->dwell) < 0)
464                         bs->dwellcnt = -bs->dwellcnt;
465                 --bs->counter;
466         }
467
468         set_alpha_by_dwell (bs);
469         glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
470         glColor4fv (bs->color);
471
472         glPushMatrix ();
473         glRotatef (((int) bs->val + bs->counter) * 360.0 / (GLfloat) g_blades,
474                    0, 0, 1);
475         draw_blink_blade (ctx, 0, NRADII - 1);
476         glPopMatrix ();
477         polys = SUBDIV + SUBDIV;
478
479         if (bs->counter  &&  bs->counter < g_blades / 2) {
480                 glRotatef (((int) bs->val - bs->counter)
481                             * 360.0 / (GLfloat) g_blades,
482                            0, 0, 1);
483                 draw_blink_blade (ctx, 0, NRADII - 1);
484                 polys += SUBDIV + SUBDIV;
485         }
486
487         --bs->dwellcnt;
488
489         return polys;
490 }
491
492 static int
493 draw_blink_concentric_random (lockward_context *ctx, struct blinkstate *bs)
494 {
495         int i;
496
497         if (bs->dwellcnt < 0) {
498                 if (bs->counter <= 0) {
499                         bs->drawfunc = NULL;
500                         return 0;
501                 }
502
503                 do {
504                         i = random() % (NRADII - 1);
505                 } while (bs->val & (1 << i));
506                 bs->val |= (1 << i);
507                 bs->direction = i;
508                 if ((bs->dwellcnt = bs->dwell) < 0)
509                         bs->dwellcnt = -bs->dwellcnt;
510
511                 --bs->counter;
512         }
513
514         set_alpha_by_dwell (bs);
515         glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
516         glColor4fv (bs->color);
517         glCallList (ctx->rings + bs->direction);
518
519         --bs->dwellcnt;
520
521         return g_blades * SUBDIV * 2;
522 }
523
524 static int
525 draw_blink_concentric_sequential (lockward_context *ctx, struct blinkstate *bs)
526 {
527         if (bs->dwellcnt < 0) {
528                 if (bs->counter <= 0) {
529                         bs->drawfunc = NULL;
530                         return 0;
531                 }
532                 if ((bs->dwellcnt = bs->dwell) < 0)
533                         bs->dwellcnt = -bs->dwellcnt;
534                 --bs->counter;
535         }
536
537         set_alpha_by_dwell (bs);
538         glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
539         glColor4fv (bs->color);
540         if (bs->direction > 0)
541                 glCallList (ctx->rings + (NRADII - 2) - bs->counter);
542         else
543                 glCallList (ctx->rings + bs->counter);
544
545         --bs->dwellcnt;
546
547         return g_blades * SUBDIV * 2;
548 }
549
550 static int
551 draw_blink_segment_scatter (lockward_context *ctx, struct blinkstate *bs)
552 {
553         int i, polys = 0;
554
555         if (bs->dwellcnt < 0) {
556                 if (bs->counter <= 0) {
557                         bs->drawfunc = NULL;
558                         return 0;
559                 }
560
561                 /*
562                  * Init random noise array.  On average, 1/4 of the bits will
563                  * be set, which should look nice.  (1/2 looks too busy.)
564                  */
565                 for (i = g_blades;  --i >= 0; )
566                         bs->noise[i] = random() & random()
567                                      & ((1 << (NRADII - 1)) - 1);
568
569                 if ((bs->dwellcnt = bs->dwell) < 0)
570                         bs->dwellcnt = -bs->dwellcnt;
571                 --bs->counter;
572         }
573
574         set_alpha_by_dwell (bs);
575         glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
576         glColor4fv (bs->color);
577
578         for (i = g_blades;  --i >= 0; ) {
579                 register uint32_t       bits;
580                 int                     inner, outer;
581
582                 /*
583                  * Find consecutive runs of 1 bits.  Keep going until we run
584                  * out of them.
585                  */
586                 for (bits = bs->noise[i];  bits; ) {
587                         inner = ffs (bits) - 1;
588                         bits = ~bits & ~((1 << inner) - 1);
589                         outer = ffs (bits) - 1;
590                         bits = ~bits & ~((1 << outer) - 1);
591
592                         glPushMatrix ();
593                         glRotatef (i * 360.0 / (GLfloat) g_blades, 0, 0, 1);
594                         draw_blink_blade (ctx, inner, outer);
595                         glPopMatrix ();
596
597                         polys += SUBDIV + SUBDIV;
598                 }
599         }
600
601         --bs->dwellcnt;
602
603         return polys;
604 }
605
606 static void
607 random_blink (lockward_context *ctx, struct blinkstate *bs)
608 {
609         bs->color[0]    =
610         bs->color[1]    =
611         bs->color[2]    =
612         bs->color[3]    = 1.0;
613         bs->dwellcnt    = -1;
614         bs->radius      = -1;
615         bs->dwell       = calc_interval_frames
616                           (ctx, g_blinkdwell_min, g_blinkdwell_max);
617         if (random() & 2)
618                 bs->dwell = -bs->dwell;
619
620         bs->type = random() % MAX_BTYPE;
621
622         switch (bs->type) {
623         case BTYPE_RADIAL_SINGLE:
624         case BTYPE_SEGMENT_SINGLE:
625                 bs->drawfunc = draw_blink_radial_random;
626                 bs->val = 0;
627                 bs->counter = 1;
628                 break;
629         case BTYPE_RADIAL_RANDOM:
630         case BTYPE_SEGMENT_RANDOM:
631                 bs->drawfunc = draw_blink_radial_random;
632                 bs->val = 0;
633                 bs->counter = g_blades;
634                 break;
635         case BTYPE_RADIAL_SEQ:
636                 bs->drawfunc = draw_blink_radial_sequential;
637                 bs->val = random() % g_blades;  /*  Initial offset  */
638                 bs->direction = random() & 8  ?  1 :  -1;
639                 bs->counter = g_blades;
640                 break;
641         case BTYPE_RADIAL_DOUBLESEQ:
642                 bs->drawfunc = draw_blink_radial_doubleseq;
643                 bs->val = random() % g_blades;  /*  Initial offset  */
644                 bs->counter = g_blades / 2 + 1;
645                 break;
646         case BTYPE_CONCENTRIC_SINGLE:
647                 bs->drawfunc = draw_blink_concentric_random;
648                 bs->val = 0;
649                 bs->counter = 1;
650                 break;
651         case BTYPE_CONCENTRIC_RANDOM:
652                 bs->drawfunc = draw_blink_concentric_random;
653                 bs->val = 0;
654                 bs->counter = NRADII - 1;
655                 break;
656         case BTYPE_CONCENTRIC_SEQ:
657                 bs->drawfunc = draw_blink_concentric_sequential;
658                 bs->direction = random() & 8  ?  1 :  -1;
659                 bs->counter = NRADII - 1;
660                 break;
661         case BTYPE_SEGMENT_SCATTER:
662                 bs->drawfunc = draw_blink_segment_scatter;
663                 bs->counter = random() % (g_blades / 2) + (g_blades / 2) + 1;
664                 break;
665         }
666 }
667
668
669 /***************************************************************************
670  * Main rendering routine.
671  */
672 ENTRYPOINT void
673 draw_lockward (ModeInfo *mi)
674 {
675         lockward_context        *ctx = &g_ctx[MI_SCREEN (mi)];
676         spinnerstate    *ss;
677         Display         *dpy = MI_DISPLAY(mi);
678         Window          window = MI_WINDOW(mi);
679         int             i, n;
680
681         GLfloat scolor[4] = {0.0, 0.0, 0.0, 0.5};
682
683         if (!ctx->glx_context)
684                 return;
685
686         glXMakeCurrent (MI_DISPLAY (mi), MI_WINDOW (mi), *(ctx->glx_context));
687
688
689         glClear (GL_COLOR_BUFFER_BIT);
690
691         if (ctx->blendmode)
692                 glBlendFunc (GL_ONE, GL_ONE);
693         else
694                 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
695
696         glPushMatrix ();
697         glLoadIdentity ();
698
699         mi->polygon_count = 0;
700
701         for (n = NSPINNERS;  --n >= 0; ) {
702                 ss = &ctx->spinners[n];
703
704                 /*  Set color.  */
705                 i = ss->ccolor >> COLORIDX_SHF;
706                 scolor[0] = ss->colors[i].red   / 65535.0;
707                 scolor[1] = ss->colors[i].green / 65535.0;
708                 scolor[2] = ss->colors[i].blue  / 65535.0;
709                 glColor4fv (scolor);
710
711                 glPushMatrix ();
712                 glRotatef (ss->rot - ss->rotcount * ss->rotinc, 0, 0, 1);
713                 for (i = ss->nblades;  --i >= 0; ) {
714                         glPushMatrix ();
715                         glRotatef (360.0 * i / ss->nblades, 0, 0, 1);
716
717                         glBegin (GL_TRIANGLE_FAN);
718                         glCallList (ctx->blades_outer + ss->bladeidx[i].outer);
719                         glCallList (ctx->blades_inner + ss->bladeidx[i].inner);
720                         glEnd ();
721
722                         glPopMatrix ();
723                         mi->polygon_count += SUBDIV + SUBDIV;
724                 }
725                 glPopMatrix ();
726
727                 /*  Advance rotation.  */
728                 if (ss->rotcount) {
729                         if (ss->rotcount > 0)
730                                 --ss->rotcount;
731                 } else {
732                         if (ss->rotinc == 0.0)
733                                 random_blade_rot (ctx, ss);
734                         else {
735                                 /*  Compute # of ticks to sit idle.  */
736                                 ss->rotinc = 0.0;
737                                 ss->rotcount =
738                                  calc_interval_frames (ctx,
739                                                        g_rotateidle_min,
740                                                        g_rotateidle_max);
741                         }
742                 }
743
744                 /*  Advance colors.  */
745                 if ((ss->ccolor += ss->colorinc) >= ss->ncolors)
746                         ss->ccolor -= ss->ncolors;
747                 else if (ss->ccolor < 0)
748                         ss->ccolor += ss->ncolors;
749         }
750
751         if (g_blink_p) {
752                 if (ctx->blink.drawfunc) {
753                         mi->polygon_count +=
754                          ctx->blink.drawfunc (ctx, &ctx->blink);
755                 } else {
756                         if (ctx->nextblink > 0)
757                                 --ctx->nextblink;
758                         else {
759                                 /* Compute # of frames for blink idle time. */
760                                 ctx->nextblink =
761                                  calc_interval_frames (ctx,
762                                                        g_blinkidle_min,
763                                                        g_blinkidle_max);
764                                 random_blink (ctx, &ctx->blink);
765                         }
766                 }
767         }
768         glPopMatrix ();
769
770         if (MI_IS_FPS (mi)) do_fps (mi);
771         glFinish();
772
773         glXSwapBuffers (dpy, window);
774 }
775
776
777 /***************************************************************************
778  * Initialization/teardown.
779  */
780 ENTRYPOINT void 
781 init_lockward (ModeInfo *mi)
782 {
783         lockward_context        *ctx;
784         int             i, n;
785
786         if (!g_ctx) {
787                 g_ctx = (lockward_context *) calloc (MI_NUM_SCREENS (mi),
788                                                    sizeof (lockward_context));
789                 if (!g_ctx) {
790                         fprintf (stderr, "%s: can't allocate context.\n",
791                                  progname);
792                         exit (1);
793                 }
794         }
795         ctx = &g_ctx[MI_SCREEN (mi)];
796
797         ctx->glx_context = init_GL (mi);
798
799         reshape_lockward (mi, MI_WIDTH (mi), MI_HEIGHT (mi));
800
801         glEnable (GL_CULL_FACE);
802         glEnable (GL_BLEND);
803         glDisable (GL_DEPTH_TEST);
804
805         glShadeModel (GL_FLAT);
806         glFrontFace (GL_CW);
807
808
809         glClearColor (0.0, 0.0, 0.0, 1.0);
810
811         ctx->blades_outer       = glGenLists (NRADII);
812         ctx->blades_inner       = glGenLists (NRADII);
813         ctx->rings              = glGenLists (NRADII - 1);
814         ctx->blendmode          = 0;
815         ctx->fps                = 1000000 / MI_DELAY (mi);
816         ctx->nextblink          = calc_interval_frames
817                                    (ctx, g_blinkidle_min, g_blinkidle_max);
818         ctx->blink.drawfunc     = NULL;
819         ctx->blink.noise        = malloc (sizeof (uint32_t) * g_blades);
820         if (!ctx->blink.noise) {
821                 fprintf (stderr, "Can't allocate noise array.\n");
822                 exit (1);
823         }
824
825         gen_blade_arcs (ctx);
826         gen_rings (ctx);
827
828         for (i = NSPINNERS;  --i >= 0; ) {
829                 spinnerstate *ss = &ctx->spinners[i];
830
831                 ss->rot         = 0.0;
832                 ss->rotcount    = -1;
833
834                 /*  Establish rotation  */
835                 random_blade_rot (ctx, ss);
836
837                 /*
838                  * Establish color cycling path and rate.  Rate avoids zero.
839                  */
840                 ss->ncolors = 128;
841                 ss->colorinc = (random() & ((2 << COLORIDX_SHF) - 1))
842                              - (1 << COLORIDX_SHF);
843                 if (ss->colorinc >= 0)
844                         ++ss->colorinc;
845
846                 ss->colors = (XColor *) calloc (ss->ncolors, sizeof (XColor));
847                 if (!ss->colors) {
848                         fprintf (stderr,
849                                  "Can't allocate XColors for spinner %d.\n",
850                                  i);
851                         exit (1);
852                 }
853                 make_smooth_colormap (0, 0, 0,
854                                       ss->colors, &ss->ncolors,
855                                       False, 0, False);
856                 ss->ncolors <<= COLORIDX_SHF;
857
858                 /*
859                  * Create blades.
860                  */
861                 ss->nblades     = g_blades;
862                 ss->bladeidx    = malloc (sizeof (bladestate) * g_blades);
863                 if (!ss->bladeidx) {
864                         fprintf (stderr, "Can't allocate blades.\n");
865                         exit (1);
866                 }
867                 for (n = g_blades;  --n >= 0; ) {
868                         /*
869                          * Establish blade radii.  Can't be equal.  Ensure
870                          * outer > inner.
871                          */
872                         do {
873                                 ss->bladeidx[n].outer = random() & 7;
874                                 ss->bladeidx[n].inner = random() & 7;
875                         } while (ss->bladeidx[n].outer ==
876                                  ss->bladeidx[n].inner);
877
878                         if (ss->bladeidx[n].outer < ss->bladeidx[n].inner) {
879                                 uint8_t tmp;
880
881                                 tmp = ss->bladeidx[n].outer;
882                                 ss->bladeidx[n].outer = ss->bladeidx[n].inner;
883                                 ss->bladeidx[n].inner = tmp;
884                         }
885                 }
886         }
887 }
888
889 static void
890 free_lockward (lockward_context *ctx)
891 {
892         int i;
893
894         if (ctx->blink.noise)
895                 free (ctx->blink.noise);
896         if (glIsList (ctx->rings))
897                 glDeleteLists (ctx->rings, NRADII - 1);
898         if (glIsList (ctx->blades_outer))
899                 glDeleteLists (ctx->blades_outer, NRADII);
900         if (glIsList (ctx->blades_inner))
901                 glDeleteLists (ctx->blades_inner, NRADII);
902
903         for (i = NSPINNERS;  --i >= 0; ) {
904                 spinnerstate *ss = &ctx->spinners[i];
905
906                 if (ss->colors)
907                         free (ss->colors);
908                 if (ss->bladeidx)
909                         free (ss->bladeidx);
910         }
911 }
912
913 ENTRYPOINT void
914 release_lockward (ModeInfo *mi)
915 {
916         int i;
917
918         if (!g_ctx)
919                 return;
920
921         for (i = MI_NUM_SCREENS (mi);  --i >= 0; ) {
922                 if (g_ctx[i].glx_context)
923                         glXMakeCurrent (MI_DISPLAY (mi), MI_WINDOW (mi),
924                                         *(g_ctx[i].glx_context));
925                 free_lockward (&g_ctx[i]);
926         }
927
928         FreeAllGL (mi);
929         free (g_ctx);  g_ctx = NULL;
930 }
931
932
933 XSCREENSAVER_MODULE ("Lockward", lockward)
934
935 #endif /* USE_GL */
936
937 /*  vim:se ts=8 sts=8 sw=8:  */