From http://www.jwz.org/xscreensaver/xscreensaver-5.33.tar.gz
[xscreensaver] / hacks / glx / noof.c
1 /* noof, Copyright (c) 2004 Bill Torzewski <billt@worksitez.com>
2  *
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 
9  * implied warranty.
10  *
11  * Originally a demo included with GLUT;
12  * (Apparently this was called "diatoms" on Irix.)
13  * ported to raw GL and xscreensaver by jwz, 12-Feb-2004.
14  */
15
16 #define DEFAULTS        "*delay:        10000       \n" \
17                         "*showFPS:      False       \n" \
18                         "*fpsSolid:     True        \n" \
19                         "*doubleBuffer: False       \n" \
20
21 # define refresh_noof 0
22 # define release_noof 0
23 # define noof_handle_event 0
24 #include "xlockmore.h"
25
26 #ifdef USE_GL /* whole file */
27
28 #define N_SHAPES 7
29
30 static int dbuf_p = 1;  /* always */
31
32 ENTRYPOINT ModeSpecOpt noof_opts = {0, NULL, 0, NULL, NULL};
33
34 typedef struct {
35   GLXContext *glx_context;
36
37   float pos[N_SHAPES * 3];
38   float dir[N_SHAPES * 3];
39   float acc[N_SHAPES * 3];
40   float col[N_SHAPES * 3];
41   float hsv[N_SHAPES * 3];
42   float hpr[N_SHAPES * 3];
43   float ang[N_SHAPES];
44   float spn[N_SHAPES];
45   float sca[N_SHAPES];
46   float geep[N_SHAPES];
47   float peep[N_SHAPES];
48   float speedsq[N_SHAPES];
49   int blad[N_SHAPES];
50
51   float ht, wd;
52
53   int tko;
54
55 } noof_configuration;
56
57 static noof_configuration *bps = NULL;
58
59
60 static void
61 initshapes(noof_configuration *bp, int i)
62 {
63   int k;
64   float f;
65
66   /* random init of pos, dir, color */
67   for (k = i * 3; k <= i * 3 + 2; k++) {
68     f = random() / (double) RAND_MAX;
69     bp->pos[k] = f;
70     f = random() / (double) RAND_MAX;
71     f = (f - 0.5) * 0.05;
72     bp->dir[k] = f;
73     f = random() / (double) RAND_MAX;
74     f = (f - 0.5) * 0.0002;
75     bp->acc[k] = f;
76     f = random() / (double) RAND_MAX;
77     bp->col[k] = f;
78   }
79
80   bp->speedsq[i] = bp->dir[i * 3] * bp->dir[i * 3] + bp->dir[i * 3 + 1] * bp->dir[i * 3 + 1];
81   f = random() / (double) RAND_MAX;
82   bp->blad[i] = 2 + (int) (f * 17.0);
83   f = random() / (double) RAND_MAX;
84   bp->ang[i] = f;
85   f = random() / (double) RAND_MAX;
86   bp->spn[i] = (f - 0.5) * 40.0 / (10 + bp->blad[i]);
87   f = random() / (double) RAND_MAX;
88   bp->sca[i] = (f * 0.1 + 0.08);
89   bp->dir[i * 3] *= bp->sca[i];
90   bp->dir[i * 3 + 1] *= bp->sca[i];
91
92   f = random() / (double) RAND_MAX;
93   bp->hsv[i * 3] = f * 360.0;
94
95   f = random() / (double) RAND_MAX;
96   bp->hsv[i * 3 + 1] = f * 0.6 + 0.4;
97
98   f = random() / (double) RAND_MAX;
99   bp->hsv[i * 3 + 2] = f * 0.7 + 0.3;
100
101   f = random() / (double) RAND_MAX;
102   bp->hpr[i * 3] = f * 0.005 * 360.0;
103   f = random() / (double) RAND_MAX;
104   bp->hpr[i * 3 + 1] = f * 0.03;
105   f = random() / (double) RAND_MAX;
106   bp->hpr[i * 3 + 2] = f * 0.02;
107
108   bp->geep[i] = 0;
109   f = random() / (double) RAND_MAX;
110   bp->peep[i] = 0.01 + f * 0.2;
111 }
112
113 static const float bladeratio[] =
114 {
115   /* nblades = 2..7 */
116   0.0, 0.0, 3.00000, 1.73205, 1.00000, 0.72654, 0.57735, 0.48157,
117   /* 8..13 */
118   0.41421, 0.36397, 0.19076, 0.29363, 0.26795, 0.24648,
119   /* 14..19 */
120   0.22824, 0.21256, 0.19891, 0.18693, 0.17633, 0.16687,
121 };
122
123 static int
124 drawleaf(noof_configuration *bp, int l)
125 {
126   int polys = 0;
127   int b, blades;
128   float x, y;
129   float wobble;
130
131   blades = bp->blad[l];
132
133   y = 0.10 * sin(bp->geep[l] * M_PI / 180.0) + 0.099 * sin(bp->geep[l] * 5.12 * M_PI / 180.0);
134   if (y < 0)
135     y = -y;
136   x = 0.15 * cos(bp->geep[l] * M_PI / 180.0) + 0.149 * cos(bp->geep[l] * 5.12 * M_PI / 180.0);
137   if (x < 0.0)
138     x = 0.0 - x;
139   if (y < 0.001 && x > 0.000002 && ((bp->tko & 0x1) == 0)) {
140     initshapes(bp, l);      /* let it become reborn as something
141                            else */
142     bp->tko++;
143     return polys;
144   } {
145     float w1 = sin(bp->geep[l] * 15.3 * M_PI / 180.0);
146     wobble = 3.0 + 2.00 * sin(bp->geep[l] * 0.4 * M_PI / 180.0) + 3.94261 * w1;
147   }
148
149   /**
150   if(blades == 2) if (y > 3.000*x) y = x*3.000;
151   if(blades == 3) if (y > 1.732*x) y = x*1.732;
152   if(blades == 4) if (y >       x) y = x;
153   if(blades == 5) if (y > 0.726*x) y = x*0.726;
154   if(blades == 6) if (y > 0.577*x) y = x*0.577;
155   if(blades == 7) if (y > 0.481*x) y = x*0.481;
156   if(blades == 8) if (y > 0.414*x) y = x*0.414;
157   */
158   if (y > x * bladeratio[blades])
159     y = x * bladeratio[blades];
160
161   for (b = 0; b < blades; b++) {
162     glPushMatrix();
163     glTranslatef(bp->pos[l * 3], bp->pos[l * 3 + 1], bp->pos[l * 3 + 2]);
164     glRotatef(bp->ang[l] + b * (360.0 / blades), 0.0, 0.0, 1.0);
165     glScalef(wobble * bp->sca[l], wobble * bp->sca[l], wobble * bp->sca[l]);
166     /**
167     if(tko & 0x40000) glColor3f(col[l*3], col[l*3+1], col[l*3+2]); 
168     else
169     */
170     glColor4ub(0, 0, 0, 0x60);
171
172     /* constrain geep cooridinates here XXX */
173     glEnable(GL_BLEND);
174
175     glBegin(GL_TRIANGLE_STRIP);
176     glVertex2f(x * bp->sca[l], 0.0);
177     glVertex2f(x, y);
178     glVertex2f(x, -y);  /* C */
179     glVertex2f(0.3, 0.0);  /* D */
180     polys += 2;
181     glEnd();
182
183     /**
184     if(tko++ & 0x40000) glColor3f(0,0,0);
185     else
186     */
187     glColor3f(bp->col[l * 3], bp->col[l * 3 + 1], bp->col[l * 3 + 2]);
188     glBegin(GL_LINE_LOOP);
189     glVertex2f(x * bp->sca[l], 0.0);
190     glVertex2f(x, y);
191     glVertex2f(0.3, 0.0);  /* D */
192     glVertex2f(x, -y);  /* C */
193     polys += 3;
194     glEnd();
195     glDisable(GL_BLEND);
196
197     glPopMatrix();
198   }
199   return polys;
200 }
201
202 static void
203 motionUpdate(noof_configuration *bp, int t)
204 {
205   if (bp->pos[t * 3] < -bp->sca[t] * bp->wd && bp->dir[t * 3] < 0.0) {
206     bp->dir[t * 3] = -bp->dir[t * 3];
207   /**
208   acc[t*3+1] += 0.8*acc[t*3];
209   acc[t*3] = -0.8*acc[t*3];
210   */
211   } else if (bp->pos[t * 3] > (1 + bp->sca[t]) * bp->wd && bp->dir[t * 3] > 0.0) {
212     bp->dir[t * 3] = -bp->dir[t * 3];
213     /**
214     acc[t*3+1] += 0.8*acc[t*3];
215     acc[t*3] = -0.8*acc[t*3];
216     */
217   } else if (bp->pos[t * 3 + 1] < -bp->sca[t] * bp->ht && bp->dir[t * 3 + 1] < 0.0) {
218     bp->dir[t * 3 + 1] = -bp->dir[t * 3 + 1];
219     /**
220     acc[t*3] += 0.8*acc[t*3+1];
221     acc[t*3+1] = -0.8*acc[t*3+1];
222     */
223   } else if (bp->pos[t * 3 + 1] > (1 + bp->sca[t]) * bp->ht && bp->dir[t * 3 + 1] > 0.0) {
224     bp->dir[t * 3 + 1] = -bp->dir[t * 3 + 1];
225     /**
226     acc[t*3] += 0.8*acc[t*3+1];
227     acc[t*3+1] = -0.8*acc[t*3+1];
228     */
229   }
230
231   bp->pos[t * 3] += bp->dir[t * 3];
232   bp->pos[t * 3 + 1] += bp->dir[t * 3 + 1];
233   /**
234   dir[t*3]   += acc[t*3];
235   dir[t*3+1] += acc[t*3+1];
236   */
237   bp->ang[t] += bp->spn[t];
238   bp->geep[t] += bp->peep[t];
239   if (bp->geep[t] > 360 * 5.0)
240     bp->geep[t] -= 360 * 5.0;
241   if (bp->ang[t] < 0.0) {
242     bp->ang[t] += 360.0;
243   }
244   if (bp->ang[t] > 360.0) {
245     bp->ang[t] -= 360.0;
246   }
247 }
248
249 static void
250 colorUpdate(noof_configuration *bp, int i)
251 {
252   if (bp->hsv[i * 3 + 1] <= 0.5 && bp->hpr[i * 3 + 1] < 0.0)
253     bp->hpr[i * 3 + 1] = -bp->hpr[i * 3 + 1];  /* adjust s */
254   if (bp->hsv[i * 3 + 1] >= 1.0 && bp->hpr[i * 3 + 1] > 0.0)
255     bp->hpr[i * 3 + 1] = -bp->hpr[i * 3 + 1];  /* adjust s */
256   if (bp->hsv[i * 3 + 2] <= 0.4 && bp->hpr[i * 3 + 2] < 0.0)
257     bp->hpr[i * 3 + 2] = -bp->hpr[i * 3 + 2];  /* adjust s */
258   if (bp->hsv[i * 3 + 2] >= 1.0 && bp->hpr[i * 3 + 2] > 0.0)
259     bp->hpr[i * 3 + 2] = -bp->hpr[i * 3 + 2];  /* adjust s */
260
261   bp->hsv[i * 3] += bp->hpr[i * 3];
262   bp->hsv[i * 3 + 1] += bp->hpr[i * 3 + 1];
263   bp->hsv[i * 3 + 2] += bp->hpr[i * 3 + 2];
264
265   /* --- hsv -> rgb --- */
266 #define H(hhh) hhh[i*3  ]
267 #define S(hhh) hhh[i*3+1]
268 #define V(hhh) hhh[i*3+2]
269
270 #define R(hhh) hhh[i*3  ]
271 #define G(hhh) hhh[i*3+1]
272 #define B(hhh) hhh[i*3+2]
273
274   if (V(bp->hsv) < 0.0)
275     V(bp->hsv) = 0.0;
276   if (V(bp->hsv) > 1.0)
277     V(bp->hsv) = 1.0;
278   if (S(bp->hsv) <= 0.0) {
279     R(bp->col) = V(bp->hsv);
280     G(bp->col) = V(bp->hsv);
281     B(bp->col) = V(bp->hsv);
282   } else {
283     float f, h, p, q, t, v;
284     int hi;
285
286     while (H(bp->hsv) < 0.0)
287       H(bp->hsv) += 360.0;
288     while (H(bp->hsv) >= 360.0)
289       H(bp->hsv) -= 360.0;
290
291     if (S(bp->hsv) < 0.0)
292       S(bp->hsv) = 0.0;
293     if (S(bp->hsv) > 1.0)
294       S(bp->hsv) = 1.0;
295
296     h = H(bp->hsv) / 60.0;
297     hi = (int) (h);
298     f = h - hi;
299     v = V(bp->hsv);
300     p = V(bp->hsv) * (1 - S(bp->hsv));
301     q = V(bp->hsv) * (1 - S(bp->hsv) * f);
302     t = V(bp->hsv) * (1 - S(bp->hsv) * (1 - f));
303
304     if (hi <= 0) {
305       R(bp->col) = v;
306       G(bp->col) = t;
307       B(bp->col) = p;
308     } else if (hi == 1) {
309       R(bp->col) = q;
310       G(bp->col) = v;
311       B(bp->col) = p;
312     } else if (hi == 2) {
313       R(bp->col) = p;
314       G(bp->col) = v;
315       B(bp->col) = t;
316     } else if (hi == 3) {
317       R(bp->col) = p;
318       G(bp->col) = q;
319       B(bp->col) = v;
320     } else if (hi == 4) {
321       R(bp->col) = t;
322       G(bp->col) = p;
323       B(bp->col) = v;
324     } else {
325       R(bp->col) = v;
326       G(bp->col) = p;
327       B(bp->col) = q;
328     }
329   }
330 }
331
332 static void
333 gravity(noof_configuration *bp, float fx)
334 {
335   int a, b;
336
337   for (a = 0; a < N_SHAPES; a++) {
338     for (b = 0; b < a; b++) {
339       float t, d2;
340
341       t = bp->pos[b * 3] - bp->pos[a * 3];
342       d2 = t * t;
343       t = bp->pos[b * 3 + 1] - bp->pos[a * 3 + 1];
344       d2 += t * t;
345       if (d2 < 0.000001)
346         d2 = 0.00001;
347       if (d2 < 0.1) {
348
349         float v0, v1, z;
350         v0 = bp->pos[b * 3] - bp->pos[a * 3];
351         v1 = bp->pos[b * 3 + 1] - bp->pos[a * 3 + 1];
352
353         z = 0.00000001 * fx / (d2);
354
355         bp->dir[a * 3] += v0 * z * bp->sca[b];
356         bp->dir[b * 3] += -v0 * z * bp->sca[a];
357         bp->dir[a * 3 + 1] += v1 * z * bp->sca[b];
358         bp->dir[b * 3 + 1] += -v1 * z * bp->sca[a];
359
360       }
361     }
362     /** apply brakes
363     if(dir[a*3]*dir[a*3] + dir[a*3+1]*dir[a*3+1]
364       > 0.0001) {
365       dir[a*3] *= 0.9;
366       dir[a*3+1] *= 0.9;
367     }
368     */
369   }
370 }
371
372 ENTRYPOINT void
373 draw_noof (ModeInfo *mi)
374 {
375   int i;
376   noof_configuration *bp = &bps[MI_SCREEN(mi)];
377
378   if (!bp->glx_context)
379     return;
380   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
381   mi->polygon_count = 0;
382
383   /**
384   if((random() & 0xff) == 0x34){
385     glClear(GL_COLOR_BUFFER_BIT);
386   }
387
388   if((tko & 0x1f) == 0x1f){
389     glEnable(GL_BLEND);
390     glColor4f(0.0, 0.0, 0.0, 0.09);
391     glRectf(0.0, 0.0, wd, ht);
392     glDisable(GL_BLEND);
393 #ifdef __sgi
394     sginap(0);
395 #endif
396   }
397   */
398
399   gravity(bp, -2.0);
400   for (i = 0; i < N_SHAPES; i++) {
401     motionUpdate(bp, i);
402     colorUpdate(bp, i);
403       mi->polygon_count += drawleaf(bp, i);
404   }
405
406   if (mi->fps_p) do_fps (mi);
407   glFinish();
408
409   if (dbuf_p)
410     glXSwapBuffers(MI_DISPLAY(mi), MI_WINDOW(mi));
411 }
412
413
414 ENTRYPOINT void
415 reshape_noof(ModeInfo *mi, int w, int h)
416 {
417   noof_configuration *bp = &bps[MI_SCREEN(mi)];
418   glViewport(0, 0, w, h);
419   glMatrixMode(GL_PROJECTION);
420   glLoadIdentity();
421   if (w <= h) {
422     bp->wd = 1.0;
423     bp->ht = (GLfloat) h / (GLfloat) w;
424     glOrtho(0.0, 1.0,
425       0.0, 1.0 * (GLfloat) h / (GLfloat) w,
426       -16.0, 4.0);
427   } else {
428     bp->wd = (GLfloat) w / (GLfloat) h;
429     bp->ht = 1.0;
430     glOrtho(0.0, 1.0 * (GLfloat) w / (GLfloat) h,
431       0.0, 1.0,
432       -16.0, 4.0);
433   }
434   glMatrixMode(GL_MODELVIEW);
435   glLoadIdentity();
436
437   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
438 }
439
440 ENTRYPOINT void 
441 init_noof (ModeInfo *mi)
442 {
443   int i;
444   noof_configuration *bp;
445
446   if (!bps) {
447     bps = (noof_configuration *)
448       calloc (MI_NUM_SCREENS(mi), sizeof (noof_configuration));
449     if (!bps) {
450       fprintf(stderr, "%s: out of memory\n", progname);
451       exit(1);
452     }
453   }
454
455   bp = &bps[MI_SCREEN(mi)];
456
457   bp->glx_context = init_GL(mi);
458
459   glDrawBuffer(dbuf_p ? GL_BACK : GL_FRONT);
460   glEnable(GL_LINE_SMOOTH);
461   glShadeModel(GL_FLAT);
462   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
463   for (i = 0; i < N_SHAPES; i++)
464     initshapes(bp, i);
465   reshape_noof (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
466 }
467
468
469 XSCREENSAVER_MODULE ("Noof", noof)
470
471 #endif /* USE_GL */