1 /* noof, Copyright (c) 2004 Bill Torzewski <billt@worksitez.com>
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 * 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.
16 #define DEFAULTS "*delay: 10000 \n" \
17 "*showFPS: False \n" \
18 "*fpsSolid: True \n" \
19 "*doubleBuffer: False \n" \
20 "*suppressRotationAnimation: True\n" \
22 # define refresh_noof 0
23 # define release_noof 0
24 # define noof_handle_event 0
25 #include "xlockmore.h"
27 #ifdef USE_GL /* whole file */
31 static int dbuf_p = 1; /* always */
33 ENTRYPOINT ModeSpecOpt noof_opts = {0, NULL, 0, NULL, NULL};
36 GLXContext *glx_context;
38 float pos[N_SHAPES * 3];
39 float dir[N_SHAPES * 3];
40 float acc[N_SHAPES * 3];
41 float col[N_SHAPES * 3];
42 float hsv[N_SHAPES * 3];
43 float hpr[N_SHAPES * 3];
49 float speedsq[N_SHAPES];
58 static noof_configuration *bps = NULL;
62 initshapes(noof_configuration *bp, int i)
67 /* random init of pos, dir, color */
68 for (k = i * 3; k <= i * 3 + 2; k++) {
69 f = random() / (double) RAND_MAX;
71 f = random() / (double) RAND_MAX;
74 f = random() / (double) RAND_MAX;
75 f = (f - 0.5) * 0.0002;
77 f = random() / (double) RAND_MAX;
81 bp->speedsq[i] = bp->dir[i * 3] * bp->dir[i * 3] + bp->dir[i * 3 + 1] * bp->dir[i * 3 + 1];
82 f = random() / (double) RAND_MAX;
83 bp->blad[i] = 2 + (int) (f * 17.0);
84 f = random() / (double) RAND_MAX;
86 f = random() / (double) RAND_MAX;
87 bp->spn[i] = (f - 0.5) * 40.0 / (10 + bp->blad[i]);
88 f = random() / (double) RAND_MAX;
89 bp->sca[i] = (f * 0.1 + 0.08);
90 bp->dir[i * 3] *= bp->sca[i];
91 bp->dir[i * 3 + 1] *= bp->sca[i];
93 f = random() / (double) RAND_MAX;
94 bp->hsv[i * 3] = f * 360.0;
96 f = random() / (double) RAND_MAX;
97 bp->hsv[i * 3 + 1] = f * 0.6 + 0.4;
99 f = random() / (double) RAND_MAX;
100 bp->hsv[i * 3 + 2] = f * 0.7 + 0.3;
102 f = random() / (double) RAND_MAX;
103 bp->hpr[i * 3] = f * 0.005 * 360.0;
104 f = random() / (double) RAND_MAX;
105 bp->hpr[i * 3 + 1] = f * 0.03;
106 f = random() / (double) RAND_MAX;
107 bp->hpr[i * 3 + 2] = f * 0.02;
110 f = random() / (double) RAND_MAX;
111 bp->peep[i] = 0.01 + f * 0.2;
114 static const float bladeratio[] =
117 0.0, 0.0, 3.00000, 1.73205, 1.00000, 0.72654, 0.57735, 0.48157,
119 0.41421, 0.36397, 0.19076, 0.29363, 0.26795, 0.24648,
121 0.22824, 0.21256, 0.19891, 0.18693, 0.17633, 0.16687,
125 drawleaf(noof_configuration *bp, int l)
132 blades = bp->blad[l];
134 y = 0.10 * sin(bp->geep[l] * M_PI / 180.0) + 0.099 * sin(bp->geep[l] * 5.12 * M_PI / 180.0);
137 x = 0.15 * cos(bp->geep[l] * M_PI / 180.0) + 0.149 * cos(bp->geep[l] * 5.12 * M_PI / 180.0);
140 if (y < 0.001 && x > 0.000002 && ((bp->tko & 0x1) == 0)) {
141 initshapes(bp, l); /* let it become reborn as something
146 float w1 = sin(bp->geep[l] * 15.3 * M_PI / 180.0);
147 wobble = 3.0 + 2.00 * sin(bp->geep[l] * 0.4 * M_PI / 180.0) + 3.94261 * w1;
151 if(blades == 2) if (y > 3.000*x) y = x*3.000;
152 if(blades == 3) if (y > 1.732*x) y = x*1.732;
153 if(blades == 4) if (y > x) y = x;
154 if(blades == 5) if (y > 0.726*x) y = x*0.726;
155 if(blades == 6) if (y > 0.577*x) y = x*0.577;
156 if(blades == 7) if (y > 0.481*x) y = x*0.481;
157 if(blades == 8) if (y > 0.414*x) y = x*0.414;
159 if (y > x * bladeratio[blades])
160 y = x * bladeratio[blades];
162 for (b = 0; b < blades; b++) {
164 glTranslatef(bp->pos[l * 3], bp->pos[l * 3 + 1], bp->pos[l * 3 + 2]);
165 glRotatef(bp->ang[l] + b * (360.0 / blades), 0.0, 0.0, 1.0);
166 glScalef(wobble * bp->sca[l], wobble * bp->sca[l], wobble * bp->sca[l]);
168 if(tko & 0x40000) glColor3f(col[l*3], col[l*3+1], col[l*3+2]);
171 glColor4ub(0, 0, 0, 0x60);
173 /* constrain geep cooridinates here XXX */
176 glBegin(GL_TRIANGLE_STRIP);
177 glVertex2f(x * bp->sca[l], 0.0);
179 glVertex2f(x, -y); /* C */
180 glVertex2f(0.3, 0.0); /* D */
185 if(tko++ & 0x40000) glColor3f(0,0,0);
188 glColor3f(bp->col[l * 3], bp->col[l * 3 + 1], bp->col[l * 3 + 2]);
189 glBegin(GL_LINE_LOOP);
190 glVertex2f(x * bp->sca[l], 0.0);
192 glVertex2f(0.3, 0.0); /* D */
193 glVertex2f(x, -y); /* C */
204 motionUpdate(noof_configuration *bp, int t)
206 if (bp->pos[t * 3] < -bp->sca[t] * bp->wd && bp->dir[t * 3] < 0.0) {
207 bp->dir[t * 3] = -bp->dir[t * 3];
209 acc[t*3+1] += 0.8*acc[t*3];
210 acc[t*3] = -0.8*acc[t*3];
212 } else if (bp->pos[t * 3] > (1 + bp->sca[t]) * bp->wd && bp->dir[t * 3] > 0.0) {
213 bp->dir[t * 3] = -bp->dir[t * 3];
215 acc[t*3+1] += 0.8*acc[t*3];
216 acc[t*3] = -0.8*acc[t*3];
218 } else if (bp->pos[t * 3 + 1] < -bp->sca[t] * bp->ht && bp->dir[t * 3 + 1] < 0.0) {
219 bp->dir[t * 3 + 1] = -bp->dir[t * 3 + 1];
221 acc[t*3] += 0.8*acc[t*3+1];
222 acc[t*3+1] = -0.8*acc[t*3+1];
224 } else if (bp->pos[t * 3 + 1] > (1 + bp->sca[t]) * bp->ht && bp->dir[t * 3 + 1] > 0.0) {
225 bp->dir[t * 3 + 1] = -bp->dir[t * 3 + 1];
227 acc[t*3] += 0.8*acc[t*3+1];
228 acc[t*3+1] = -0.8*acc[t*3+1];
232 bp->pos[t * 3] += bp->dir[t * 3];
233 bp->pos[t * 3 + 1] += bp->dir[t * 3 + 1];
235 dir[t*3] += acc[t*3];
236 dir[t*3+1] += acc[t*3+1];
238 bp->ang[t] += bp->spn[t];
239 bp->geep[t] += bp->peep[t];
240 if (bp->geep[t] > 360 * 5.0)
241 bp->geep[t] -= 360 * 5.0;
242 if (bp->ang[t] < 0.0) {
245 if (bp->ang[t] > 360.0) {
251 colorUpdate(noof_configuration *bp, int i)
253 if (bp->hsv[i * 3 + 1] <= 0.5 && bp->hpr[i * 3 + 1] < 0.0)
254 bp->hpr[i * 3 + 1] = -bp->hpr[i * 3 + 1]; /* adjust s */
255 if (bp->hsv[i * 3 + 1] >= 1.0 && bp->hpr[i * 3 + 1] > 0.0)
256 bp->hpr[i * 3 + 1] = -bp->hpr[i * 3 + 1]; /* adjust s */
257 if (bp->hsv[i * 3 + 2] <= 0.4 && bp->hpr[i * 3 + 2] < 0.0)
258 bp->hpr[i * 3 + 2] = -bp->hpr[i * 3 + 2]; /* adjust s */
259 if (bp->hsv[i * 3 + 2] >= 1.0 && bp->hpr[i * 3 + 2] > 0.0)
260 bp->hpr[i * 3 + 2] = -bp->hpr[i * 3 + 2]; /* adjust s */
262 bp->hsv[i * 3] += bp->hpr[i * 3];
263 bp->hsv[i * 3 + 1] += bp->hpr[i * 3 + 1];
264 bp->hsv[i * 3 + 2] += bp->hpr[i * 3 + 2];
266 /* --- hsv -> rgb --- */
267 #define H(hhh) hhh[i*3 ]
268 #define S(hhh) hhh[i*3+1]
269 #define V(hhh) hhh[i*3+2]
271 #define R(hhh) hhh[i*3 ]
272 #define G(hhh) hhh[i*3+1]
273 #define B(hhh) hhh[i*3+2]
275 if (V(bp->hsv) < 0.0)
277 if (V(bp->hsv) > 1.0)
279 if (S(bp->hsv) <= 0.0) {
280 R(bp->col) = V(bp->hsv);
281 G(bp->col) = V(bp->hsv);
282 B(bp->col) = V(bp->hsv);
284 float f, h, p, q, t, v;
287 while (H(bp->hsv) < 0.0)
289 while (H(bp->hsv) >= 360.0)
292 if (S(bp->hsv) < 0.0)
294 if (S(bp->hsv) > 1.0)
297 h = H(bp->hsv) / 60.0;
301 p = V(bp->hsv) * (1 - S(bp->hsv));
302 q = V(bp->hsv) * (1 - S(bp->hsv) * f);
303 t = V(bp->hsv) * (1 - S(bp->hsv) * (1 - f));
309 } else if (hi == 1) {
313 } else if (hi == 2) {
317 } else if (hi == 3) {
321 } else if (hi == 4) {
334 gravity(noof_configuration *bp, float fx)
338 for (a = 0; a < N_SHAPES; a++) {
339 for (b = 0; b < a; b++) {
342 t = bp->pos[b * 3] - bp->pos[a * 3];
344 t = bp->pos[b * 3 + 1] - bp->pos[a * 3 + 1];
351 v0 = bp->pos[b * 3] - bp->pos[a * 3];
352 v1 = bp->pos[b * 3 + 1] - bp->pos[a * 3 + 1];
354 z = 0.00000001 * fx / (d2);
356 bp->dir[a * 3] += v0 * z * bp->sca[b];
357 bp->dir[b * 3] += -v0 * z * bp->sca[a];
358 bp->dir[a * 3 + 1] += v1 * z * bp->sca[b];
359 bp->dir[b * 3 + 1] += -v1 * z * bp->sca[a];
364 if(dir[a*3]*dir[a*3] + dir[a*3+1]*dir[a*3+1]
374 draw_noof (ModeInfo *mi)
377 noof_configuration *bp = &bps[MI_SCREEN(mi)];
379 if (!bp->glx_context)
381 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
382 mi->polygon_count = 0;
385 if((random() & 0xff) == 0x34){
386 glClear(GL_COLOR_BUFFER_BIT);
389 if((tko & 0x1f) == 0x1f){
391 glColor4f(0.0, 0.0, 0.0, 0.09);
392 glRectf(0.0, 0.0, wd, ht);
401 for (i = 0; i < N_SHAPES; i++) {
404 mi->polygon_count += drawleaf(bp, i);
407 if (mi->fps_p) do_fps (mi);
411 glXSwapBuffers(MI_DISPLAY(mi), MI_WINDOW(mi));
416 reshape_noof(ModeInfo *mi, int w, int h)
418 noof_configuration *bp = &bps[MI_SCREEN(mi)];
419 glViewport(0, 0, w, h);
420 glMatrixMode(GL_PROJECTION);
424 bp->ht = (GLfloat) h / (GLfloat) w;
426 0.0, 1.0 * (GLfloat) h / (GLfloat) w,
429 bp->wd = (GLfloat) w / (GLfloat) h;
431 glOrtho(0.0, 1.0 * (GLfloat) w / (GLfloat) h,
435 glMatrixMode(GL_MODELVIEW);
438 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
442 init_noof (ModeInfo *mi)
445 noof_configuration *bp;
447 MI_INIT (mi, bps, NULL);
449 bp = &bps[MI_SCREEN(mi)];
451 bp->glx_context = init_GL(mi);
453 glDrawBuffer(dbuf_p ? GL_BACK : GL_FRONT);
454 glEnable(GL_LINE_SMOOTH);
455 glShadeModel(GL_FLAT);
456 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
457 for (i = 0; i < N_SHAPES; i++)
459 reshape_noof (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
463 XSCREENSAVER_MODULE ("Noof", noof)