http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[xscreensaver] / hacks / glx / noof.c
index 534acc6882eff274c2b3f7e7d35ae647d32fdb7f..e273082058dfc9503fd305d1d6852a3060162c97 100644 (file)
@@ -1,4 +1,4 @@
-/* noof, Copyright (c) 2004 Mark Kilgard <mjk@nvidia.com>
+/* noof, Copyright (c) 2004 Bill Torzewski <billt@worksitez.com>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
  * ported to raw GL and xscreensaver by jwz, 12-Feb-2004.
  */
 
-#include <X11/Intrinsic.h>
-
-extern XtAppContext app;
-
-#define PROGCLASS      "Noof"
-#define HACK_INIT      init_noof
-#define HACK_DRAW      draw_noof
-#define HACK_RESHAPE   reshape_noof
-#define noof_opts      xlockmore_opts
-
 #define DEFAULTS       "*delay:        10000       \n" \
                        "*showFPS:      False       \n" \
                        "*fpsSolid:     True        \n" \
 
+# define refresh_noof 0
+# define release_noof 0
+# define noof_handle_event 0
 #include "xlockmore.h"
 
 #ifdef USE_GL /* whole file */
 
+#define N_SHAPES 7
+
+ENTRYPOINT ModeSpecOpt noof_opts = {0, NULL, 0, NULL, NULL};
+
 typedef struct {
   GLXContext *glx_context;
+
+  float pos[N_SHAPES * 3];
+  float dir[N_SHAPES * 3];
+  float acc[N_SHAPES * 3];
+  float col[N_SHAPES * 3];
+  float hsv[N_SHAPES * 3];
+  float hpr[N_SHAPES * 3];
+  float ang[N_SHAPES];
+  float spn[N_SHAPES];
+  float sca[N_SHAPES];
+  float geep[N_SHAPES];
+  float peep[N_SHAPES];
+  float speedsq[N_SHAPES];
+  int blad[N_SHAPES];
+
+  float ht, wd;
+
+  int tko;
+
 } noof_configuration;
 
 static noof_configuration *bps = NULL;
-ModeSpecOpt noof_opts = {0, NULL, 0, NULL, NULL};
 
 
-/* --- shape parameters def'n --- */
-#define N_SHAPES 7
-static float pos[N_SHAPES * 3];
-static float dir[N_SHAPES * 3];
-static float acc[N_SHAPES * 3];
-static float col[N_SHAPES * 3];
-static float hsv[N_SHAPES * 3];
-static float hpr[N_SHAPES * 3];
-static float ang[N_SHAPES];
-static float spn[N_SHAPES];
-static float sca[N_SHAPES];
-static float geep[N_SHAPES];
-static float peep[N_SHAPES];
-static float speedsq[N_SHAPES];
-static int blad[N_SHAPES];
-
-static float ht, wd;
-
 static void
-initshapes(int i)
+initshapes(noof_configuration *bp, int i)
 {
   int k;
   float f;
@@ -66,53 +63,51 @@ initshapes(int i)
   /* random init of pos, dir, color */
   for (k = i * 3; k <= i * 3 + 2; k++) {
     f = random() / (double) RAND_MAX;
-    pos[k] = f;
+    bp->pos[k] = f;
     f = random() / (double) RAND_MAX;
     f = (f - 0.5) * 0.05;
-    dir[k] = f;
+    bp->dir[k] = f;
     f = random() / (double) RAND_MAX;
     f = (f - 0.5) * 0.0002;
-    acc[k] = f;
+    bp->acc[k] = f;
     f = random() / (double) RAND_MAX;
-    col[k] = f;
+    bp->col[k] = f;
   }
 
-  speedsq[i] = dir[i * 3] * dir[i * 3] + dir[i * 3 + 1] * dir[i * 3 + 1];
+  bp->speedsq[i] = bp->dir[i * 3] * bp->dir[i * 3] + bp->dir[i * 3 + 1] * bp->dir[i * 3 + 1];
   f = random() / (double) RAND_MAX;
-  blad[i] = 2 + (int) (f * 17.0);
+  bp->blad[i] = 2 + (int) (f * 17.0);
   f = random() / (double) RAND_MAX;
-  ang[i] = f;
+  bp->ang[i] = f;
   f = random() / (double) RAND_MAX;
-  spn[i] = (f - 0.5) * 40.0 / (10 + blad[i]);
+  bp->spn[i] = (f - 0.5) * 40.0 / (10 + bp->blad[i]);
   f = random() / (double) RAND_MAX;
-  sca[i] = (f * 0.1 + 0.08);
-  dir[i * 3] *= sca[i];
-  dir[i * 3 + 1] *= sca[i];
+  bp->sca[i] = (f * 0.1 + 0.08);
+  bp->dir[i * 3] *= bp->sca[i];
+  bp->dir[i * 3 + 1] *= bp->sca[i];
 
   f = random() / (double) RAND_MAX;
-  hsv[i * 3] = f * 360.0;
+  bp->hsv[i * 3] = f * 360.0;
 
   f = random() / (double) RAND_MAX;
-  hsv[i * 3 + 1] = f * 0.6 + 0.4;
+  bp->hsv[i * 3 + 1] = f * 0.6 + 0.4;
 
   f = random() / (double) RAND_MAX;
-  hsv[i * 3 + 2] = f * 0.7 + 0.3;
+  bp->hsv[i * 3 + 2] = f * 0.7 + 0.3;
 
   f = random() / (double) RAND_MAX;
-  hpr[i * 3] = f * 0.005 * 360.0;
+  bp->hpr[i * 3] = f * 0.005 * 360.0;
   f = random() / (double) RAND_MAX;
-  hpr[i * 3 + 1] = f * 0.03;
+  bp->hpr[i * 3 + 1] = f * 0.03;
   f = random() / (double) RAND_MAX;
-  hpr[i * 3 + 2] = f * 0.02;
+  bp->hpr[i * 3 + 2] = f * 0.02;
 
-  geep[i] = 0;
+  bp->geep[i] = 0;
   f = random() / (double) RAND_MAX;
-  peep[i] = 0.01 + f * 0.2;
+  bp->peep[i] = 0.01 + f * 0.2;
 }
 
-static int tko = 0;
-
-static float bladeratio[] =
+static const float bladeratio[] =
 {
   /* nblades = 2..7 */
   0.0, 0.0, 3.00000, 1.73205, 1.00000, 0.72654, 0.57735, 0.48157,
@@ -123,29 +118,29 @@ static float bladeratio[] =
 };
 
 static void
-drawleaf(int l)
+drawleaf(noof_configuration *bp, int l)
 {
 
   int b, blades;
   float x, y;
   float wobble;
 
-  blades = blad[l];
+  blades = bp->blad[l];
 
-  y = 0.10 * sin(geep[l] * M_PI / 180.0) + 0.099 * sin(geep[l] * 5.12 * M_PI / 180.0);
+  y = 0.10 * sin(bp->geep[l] * M_PI / 180.0) + 0.099 * sin(bp->geep[l] * 5.12 * M_PI / 180.0);
   if (y < 0)
     y = -y;
-  x = 0.15 * cos(geep[l] * M_PI / 180.0) + 0.149 * cos(geep[l] * 5.12 * M_PI / 180.0);
+  x = 0.15 * cos(bp->geep[l] * M_PI / 180.0) + 0.149 * cos(bp->geep[l] * 5.12 * M_PI / 180.0);
   if (x < 0.0)
     x = 0.0 - x;
-  if (y < 0.001 && x > 0.000002 && ((tko & 0x1) == 0)) {
-    initshapes(l);      /* let it become reborn as something
+  if (y < 0.001 && x > 0.000002 && ((bp->tko & 0x1) == 0)) {
+    initshapes(bp, l);      /* let it become reborn as something
                            else */
-    tko++;
+    bp->tko++;
     return;
   } {
-    float w1 = sin(geep[l] * 15.3 * M_PI / 180.0);
-    wobble = 3.0 + 2.00 * sin(geep[l] * 0.4 * M_PI / 180.0) + 3.94261 * w1;
+    float w1 = sin(bp->geep[l] * 15.3 * M_PI / 180.0);
+    wobble = 3.0 + 2.00 * sin(bp->geep[l] * 0.4 * M_PI / 180.0) + 3.94261 * w1;
   }
 
   /**
@@ -162,9 +157,9 @@ drawleaf(int l)
 
   for (b = 0; b < blades; b++) {
     glPushMatrix();
-    glTranslatef(pos[l * 3], pos[l * 3 + 1], pos[l * 3 + 2]);
-    glRotatef(ang[l] + b * (360.0 / blades), 0.0, 0.0, 1.0);
-    glScalef(wobble * sca[l], wobble * sca[l], wobble * sca[l]);
+    glTranslatef(bp->pos[l * 3], bp->pos[l * 3 + 1], bp->pos[l * 3 + 2]);
+    glRotatef(bp->ang[l] + b * (360.0 / blades), 0.0, 0.0, 1.0);
+    glScalef(wobble * bp->sca[l], wobble * bp->sca[l], wobble * bp->sca[l]);
     /**
     if(tko & 0x40000) glColor3f(col[l*3], col[l*3+1], col[l*3+2]); 
     else
@@ -175,7 +170,7 @@ drawleaf(int l)
     glEnable(GL_BLEND);
 
     glBegin(GL_TRIANGLE_STRIP);
-    glVertex2f(x * sca[l], 0.0);
+    glVertex2f(x * bp->sca[l], 0.0);
     glVertex2f(x, y);
     glVertex2f(x, -y);  /* C */
     glVertex2f(0.3, 0.0);  /* D */
@@ -185,9 +180,9 @@ drawleaf(int l)
     if(tko++ & 0x40000) glColor3f(0,0,0);
     else
     */
-    glColor3f(col[l * 3], col[l * 3 + 1], col[l * 3 + 2]);
+    glColor3f(bp->col[l * 3], bp->col[l * 3 + 1], bp->col[l * 3 + 2]);
     glBegin(GL_LINE_LOOP);
-    glVertex2f(x * sca[l], 0.0);
+    glVertex2f(x * bp->sca[l], 0.0);
     glVertex2f(x, y);
     glVertex2f(0.3, 0.0);  /* D */
     glVertex2f(x, -y);  /* C */
@@ -199,67 +194,67 @@ drawleaf(int l)
 }
 
 static void
-motionUpdate(int t)
+motionUpdate(noof_configuration *bp, int t)
 {
-  if (pos[t * 3] < -sca[t] * wd && dir[t * 3] < 0.0) {
-    dir[t * 3] = -dir[t * 3];
+  if (bp->pos[t * 3] < -bp->sca[t] * bp->wd && bp->dir[t * 3] < 0.0) {
+    bp->dir[t * 3] = -bp->dir[t * 3];
   /**
   acc[t*3+1] += 0.8*acc[t*3];
   acc[t*3] = -0.8*acc[t*3];
   */
-  } else if (pos[t * 3] > (1 + sca[t]) * wd && dir[t * 3] > 0.0) {
-    dir[t * 3] = -dir[t * 3];
+  } else if (bp->pos[t * 3] > (1 + bp->sca[t]) * bp->wd && bp->dir[t * 3] > 0.0) {
+    bp->dir[t * 3] = -bp->dir[t * 3];
     /**
     acc[t*3+1] += 0.8*acc[t*3];
     acc[t*3] = -0.8*acc[t*3];
     */
-  } else if (pos[t * 3 + 1] < -sca[t] * ht && dir[t * 3 + 1] < 0.0) {
-    dir[t * 3 + 1] = -dir[t * 3 + 1];
+  } else if (bp->pos[t * 3 + 1] < -bp->sca[t] * bp->ht && bp->dir[t * 3 + 1] < 0.0) {
+    bp->dir[t * 3 + 1] = -bp->dir[t * 3 + 1];
     /**
     acc[t*3] += 0.8*acc[t*3+1];
     acc[t*3+1] = -0.8*acc[t*3+1];
     */
-  } else if (pos[t * 3 + 1] > (1 + sca[t]) * ht && dir[t * 3 + 1] > 0.0) {
-    dir[t * 3 + 1] = -dir[t * 3 + 1];
+  } else if (bp->pos[t * 3 + 1] > (1 + bp->sca[t]) * bp->ht && bp->dir[t * 3 + 1] > 0.0) {
+    bp->dir[t * 3 + 1] = -bp->dir[t * 3 + 1];
     /**
     acc[t*3] += 0.8*acc[t*3+1];
     acc[t*3+1] = -0.8*acc[t*3+1];
     */
   }
 
-  pos[t * 3] += dir[t * 3];
-  pos[t * 3 + 1] += dir[t * 3 + 1];
+  bp->pos[t * 3] += bp->dir[t * 3];
+  bp->pos[t * 3 + 1] += bp->dir[t * 3 + 1];
   /**
   dir[t*3]   += acc[t*3];
   dir[t*3+1] += acc[t*3+1];
   */
-  ang[t] += spn[t];
-  geep[t] += peep[t];
-  if (geep[t] > 360 * 5.0)
-    geep[t] -= 360 * 5.0;
-  if (ang[t] < 0.0) {
-    ang[t] += 360.0;
+  bp->ang[t] += bp->spn[t];
+  bp->geep[t] += bp->peep[t];
+  if (bp->geep[t] > 360 * 5.0)
+    bp->geep[t] -= 360 * 5.0;
+  if (bp->ang[t] < 0.0) {
+    bp->ang[t] += 360.0;
   }
-  if (ang[t] > 360.0) {
-    ang[t] -= 360.0;
+  if (bp->ang[t] > 360.0) {
+    bp->ang[t] -= 360.0;
   }
 }
 
 static void
-colorUpdate(int i)
+colorUpdate(noof_configuration *bp, int i)
 {
-  if (hsv[i * 3 + 1] <= 0.5 && hpr[i * 3 + 1] < 0.0)
-    hpr[i * 3 + 1] = -hpr[i * 3 + 1];  /* adjust s */
-  if (hsv[i * 3 + 1] >= 1.0 && hpr[i * 3 + 1] > 0.0)
-    hpr[i * 3 + 1] = -hpr[i * 3 + 1];  /* adjust s */
-  if (hsv[i * 3 + 2] <= 0.4 && hpr[i * 3 + 2] < 0.0)
-    hpr[i * 3 + 2] = -hpr[i * 3 + 2];  /* adjust s */
-  if (hsv[i * 3 + 2] >= 1.0 && hpr[i * 3 + 2] > 0.0)
-    hpr[i * 3 + 2] = -hpr[i * 3 + 2];  /* adjust s */
-
-  hsv[i * 3] += hpr[i * 3];
-  hsv[i * 3 + 1] += hpr[i * 3 + 1];
-  hsv[i * 3 + 2] += hpr[i * 3 + 2];
+  if (bp->hsv[i * 3 + 1] <= 0.5 && bp->hpr[i * 3 + 1] < 0.0)
+    bp->hpr[i * 3 + 1] = -bp->hpr[i * 3 + 1];  /* adjust s */
+  if (bp->hsv[i * 3 + 1] >= 1.0 && bp->hpr[i * 3 + 1] > 0.0)
+    bp->hpr[i * 3 + 1] = -bp->hpr[i * 3 + 1];  /* adjust s */
+  if (bp->hsv[i * 3 + 2] <= 0.4 && bp->hpr[i * 3 + 2] < 0.0)
+    bp->hpr[i * 3 + 2] = -bp->hpr[i * 3 + 2];  /* adjust s */
+  if (bp->hsv[i * 3 + 2] >= 1.0 && bp->hpr[i * 3 + 2] > 0.0)
+    bp->hpr[i * 3 + 2] = -bp->hpr[i * 3 + 2];  /* adjust s */
+
+  bp->hsv[i * 3] += bp->hpr[i * 3];
+  bp->hsv[i * 3 + 1] += bp->hpr[i * 3 + 1];
+  bp->hsv[i * 3 + 2] += bp->hpr[i * 3 + 2];
 
   /* --- hsv -> rgb --- */
 #define H(hhh) hhh[i*3  ]
@@ -270,66 +265,66 @@ colorUpdate(int i)
 #define G(hhh) hhh[i*3+1]
 #define B(hhh) hhh[i*3+2]
 
-  if (V(hsv) < 0.0)
-    V(hsv) = 0.0;
-  if (V(hsv) > 1.0)
-    V(hsv) = 1.0;
-  if (S(hsv) <= 0.0) {
-    R(col) = V(hsv);
-    G(col) = V(hsv);
-    B(col) = V(hsv);
+  if (V(bp->hsv) < 0.0)
+    V(bp->hsv) = 0.0;
+  if (V(bp->hsv) > 1.0)
+    V(bp->hsv) = 1.0;
+  if (S(bp->hsv) <= 0.0) {
+    R(bp->col) = V(bp->hsv);
+    G(bp->col) = V(bp->hsv);
+    B(bp->col) = V(bp->hsv);
   } else {
     float f, h, p, q, t, v;
     int hi;
 
-    while (H(hsv) < 0.0)
-      H(hsv) += 360.0;
-    while (H(hsv) >= 360.0)
-      H(hsv) -= 360.0;
+    while (H(bp->hsv) < 0.0)
+      H(bp->hsv) += 360.0;
+    while (H(bp->hsv) >= 360.0)
+      H(bp->hsv) -= 360.0;
 
-    if (S(hsv) < 0.0)
-      S(hsv) = 0.0;
-    if (S(hsv) > 1.0)
-      S(hsv) = 1.0;
+    if (S(bp->hsv) < 0.0)
+      S(bp->hsv) = 0.0;
+    if (S(bp->hsv) > 1.0)
+      S(bp->hsv) = 1.0;
 
-    h = H(hsv) / 60.0;
+    h = H(bp->hsv) / 60.0;
     hi = (int) (h);
     f = h - hi;
-    v = V(hsv);
-    p = V(hsv) * (1 - S(hsv));
-    q = V(hsv) * (1 - S(hsv) * f);
-    t = V(hsv) * (1 - S(hsv) * (1 - f));
+    v = V(bp->hsv);
+    p = V(bp->hsv) * (1 - S(bp->hsv));
+    q = V(bp->hsv) * (1 - S(bp->hsv) * f);
+    t = V(bp->hsv) * (1 - S(bp->hsv) * (1 - f));
 
     if (hi <= 0) {
-      R(col) = v;
-      G(col) = t;
-      B(col) = p;
+      R(bp->col) = v;
+      G(bp->col) = t;
+      B(bp->col) = p;
     } else if (hi == 1) {
-      R(col) = q;
-      G(col) = v;
-      B(col) = p;
+      R(bp->col) = q;
+      G(bp->col) = v;
+      B(bp->col) = p;
     } else if (hi == 2) {
-      R(col) = p;
-      G(col) = v;
-      B(col) = t;
+      R(bp->col) = p;
+      G(bp->col) = v;
+      B(bp->col) = t;
     } else if (hi == 3) {
-      R(col) = p;
-      G(col) = q;
-      B(col) = v;
+      R(bp->col) = p;
+      G(bp->col) = q;
+      B(bp->col) = v;
     } else if (hi == 4) {
-      R(col) = t;
-      G(col) = p;
-      B(col) = v;
+      R(bp->col) = t;
+      G(bp->col) = p;
+      B(bp->col) = v;
     } else {
-      R(col) = v;
-      G(col) = p;
-      B(col) = q;
+      R(bp->col) = v;
+      G(bp->col) = p;
+      B(bp->col) = q;
     }
   }
 }
 
 static void
-gravity(float fx)
+gravity(noof_configuration *bp, float fx)
 {
   int a, b;
 
@@ -337,24 +332,24 @@ gravity(float fx)
     for (b = 0; b < a; b++) {
       float t, d2;
 
-      t = pos[b * 3] - pos[a * 3];
+      t = bp->pos[b * 3] - bp->pos[a * 3];
       d2 = t * t;
-      t = pos[b * 3 + 1] - pos[a * 3 + 1];
+      t = bp->pos[b * 3 + 1] - bp->pos[a * 3 + 1];
       d2 += t * t;
       if (d2 < 0.000001)
         d2 = 0.00001;
       if (d2 < 0.1) {
 
         float v0, v1, z;
-        v0 = pos[b * 3] - pos[a * 3];
-        v1 = pos[b * 3 + 1] - pos[a * 3 + 1];
+        v0 = bp->pos[b * 3] - bp->pos[a * 3];
+        v1 = bp->pos[b * 3 + 1] - bp->pos[a * 3 + 1];
 
         z = 0.00000001 * fx / (d2);
 
-        dir[a * 3] += v0 * z * sca[b];
-        dir[b * 3] += -v0 * z * sca[a];
-        dir[a * 3 + 1] += v1 * z * sca[b];
-        dir[b * 3 + 1] += -v1 * z * sca[a];
+        bp->dir[a * 3] += v0 * z * bp->sca[b];
+        bp->dir[b * 3] += -v0 * z * bp->sca[a];
+        bp->dir[a * 3 + 1] += v1 * z * bp->sca[b];
+        bp->dir[b * 3 + 1] += -v1 * z * bp->sca[a];
 
       }
     }
@@ -368,10 +363,15 @@ gravity(float fx)
   }
 }
 
-void
+ENTRYPOINT void
 draw_noof (ModeInfo *mi)
 {
   int i;
+  noof_configuration *bp = &bps[MI_SCREEN(mi)];
+
+  if (!bp->glx_context)
+    return;
+  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
 
   /**
   if((random() & 0xff) == 0x34){
@@ -389,11 +389,11 @@ draw_noof (ModeInfo *mi)
   }
   */
 
-  gravity(-2.0);
+  gravity(bp, -2.0);
   for (i = 0; i < N_SHAPES; i++) {
-    motionUpdate(i);
-    colorUpdate(i);
-    drawleaf(i);
+    motionUpdate(bp, i);
+    colorUpdate(bp, i);
+    drawleaf(bp, i);
   }
 
   if (mi->fps_p) do_fps (mi);
@@ -403,21 +403,22 @@ draw_noof (ModeInfo *mi)
 }
 
 
-void
+ENTRYPOINT void
 reshape_noof(ModeInfo *mi, int w, int h)
 {
+  noof_configuration *bp = &bps[MI_SCREEN(mi)];
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (w <= h) {
-    wd = 1.0;
-    ht = (GLfloat) h / (GLfloat) w;
+    bp->wd = 1.0;
+    bp->ht = (GLfloat) h / (GLfloat) w;
     glOrtho(0.0, 1.0,
       0.0, 1.0 * (GLfloat) h / (GLfloat) w,
       -16.0, 4.0);
   } else {
-    wd = (GLfloat) w / (GLfloat) h;
-    ht = 1.0;
+    bp->wd = (GLfloat) w / (GLfloat) h;
+    bp->ht = 1.0;
     glOrtho(0.0, 1.0 * (GLfloat) w / (GLfloat) h,
       0.0, 1.0,
       -16.0, 4.0);
@@ -428,7 +429,7 @@ reshape_noof(ModeInfo *mi, int w, int h)
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
-void 
+ENTRYPOINT void 
 init_noof (ModeInfo *mi)
 {
   int i;
@@ -453,9 +454,11 @@ init_noof (ModeInfo *mi)
   glShadeModel(GL_FLAT);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   for (i = 0; i < N_SHAPES; i++)
-    initshapes(i);
+    initshapes(bp, i);
   reshape_noof (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
 }
 
 
+XSCREENSAVER_MODULE ("Noof", noof)
+
 #endif /* USE_GL */