ftp://ftp.linux.ncsu.edu/mirror/ftp.redhat.com/pub/redhat/linux/enterprise/4/en/os...
[xscreensaver] / hacks / glx / atlantis.c
index 9f07ddbd17b9a5c0df25b241417a2e106cdee280..b7afd8cbbe022e8ac4518d89324d1b1b783b412f 100644 (file)
@@ -1,8 +1,7 @@
 /* atlantis --- Shows moving 3D sea animals */
 
-#if !defined( lint ) && !defined( SABER )
-static const char sccsid[] = "@(#)atlantis.c   1.3 98/06/18 xlockmore";
-
+#if 0
+static const char sccsid[] = "@(#)atlantis.c   5.08 2003/04/09 xlockmore";
 #endif
 
 /* Copyright (c) E. Lassauge, 1998. */
@@ -30,7 +29,7 @@ static const char sccsid[] = "@(#)atlantis.c  1.3 98/06/18 xlockmore";
  * Thanks goes also to Brian Paul for making it possible and inexpensive
  * to use OpenGL at home.
  *
- * My e-mail address is lassauge@sagem.fr
+ * My e-mail address is lassauge@users.sourceforge.net
  *
  * Eric Lassauge  (May-13-1998)
  *
@@ -58,7 +57,6 @@ static const char sccsid[] = "@(#)atlantis.c  1.3 98/06/18 xlockmore";
  *                        Add support for -/+ wireframe (t'was so easy to do!)
  *
  * TODO : 
- *        - add a sort of background image or random bg color
  *        - better handling of sizes and speeds
  *        - test standalone and module modes
  *        - purify it (!)
@@ -139,24 +137,25 @@ static int do_texture;
 static int do_gradient;
 static XrmOptionDescRec opts[] =
 {
-     {"-whalespeed", ".atlantis.whalespeed", XrmoptionSepArg, (caddr_t) NULL},
-     {"-texture",    ".atlantis.texture",    XrmoptionNoArg, (caddr_t)"true"},
-     {"+texture",    ".atlantis.texture",    XrmoptionNoArg, (caddr_t)"false"},
-     {"-gradient",   ".atlantis.gradient",   XrmoptionNoArg, (caddr_t)"true"},
-     {"+gradient",   ".atlantis.gradient",   XrmoptionNoArg, (caddr_t)"false"},
+     {"-whalespeed", ".atlantis.whalespeed", XrmoptionSepArg, 0},
+     {"-texture",    ".atlantis.texture",    XrmoptionNoArg, "true"},
+     {"+texture",    ".atlantis.texture",    XrmoptionNoArg, "false"},
+     {"-gradient",   ".atlantis.gradient",   XrmoptionNoArg, "true"},
+     {"+gradient",   ".atlantis.gradient",   XrmoptionNoArg, "false"},
 };
 
 static argtype vars[] =
 {
- {(caddr_t *) & whalespeed, "whalespeed", "WhaleSpeed", DEF_WHALESPEED, t_Int},
- {(caddr_t *) &do_texture,  "texture",    "Texture",    DEF_TEXTURE,   t_Bool},
- {(caddr_t *) &do_gradient, "gradient",   "Gradient",   DEF_GRADIENT,  t_Bool},
+ {&whalespeed, "whalespeed", "WhaleSpeed", DEF_WHALESPEED, t_Int},
+ {&do_texture,  "texture",    "Texture",    DEF_TEXTURE,   t_Bool},
+ {&do_gradient, "gradient",   "Gradient",   DEF_GRADIENT,  t_Bool},
 };
 
 static OptionStruct desc[] =
 {
        {"-whalespeed num", "speed of whales and the dolphin"},
-       {"-texture num",    "whether to introduce water-like distortion"}
+       {"-texture",        "whether to introduce water-like distortion"},
+       {"-gradient",       "whether to introduce gradient-filled background"},
 };
 
 ModeSpecOpt atlantis_opts =
@@ -174,8 +173,10 @@ ModStruct   atlantis_description =
 static atlantisstruct *atlantis = NULL;
 
 #include "xpm-ximage.h"
+
 #include "../images/sea-texture.xpm"
 
+
 static void
 parse_image_data(ModeInfo *mi)
 {
@@ -199,7 +200,7 @@ InitFishs(atlantisstruct * ap)
                ap->sharks[i].v = 1.0;
        }
 
-       /* Random whae direction */
+       /* Random whale direction */
        ap->whaledir = LRAND() & 1;
 
        ap->dolph.x = 30000.0;
@@ -247,6 +248,7 @@ Init(ModeInfo *mi)
        {0.4, 0.4, 0.4, 1.0};
        static float lmodel_localviewer[] =
        {0.0};
+       float        fblue = 0.0, fgreen;
 
        glFrontFace(GL_CCW);
 
@@ -323,7 +325,10 @@ Init(ModeInfo *mi)
 
        InitFishs(ap);
 
-       glClearColor(0.0, 0.39, 0.7, 0.0);
+       /* Add a little randomness */
+       fblue = ((float) (NRAND(30)) / 100.0) + 0.70;
+       fgreen = fblue * 0.56;
+       glClearColor(0.0, fgreen, fblue, 0.0);
 }
 
 void
@@ -340,6 +345,10 @@ reshape_atlantis(ModeInfo * mi, int width, int height)
 }
 
 
+/* Fill the background with a gradient -- thanks to 
+   Phil Carrig <pod@internode.on.net> for figuring out
+   how to do this more efficiently!
+ */
 static void
 clear_tank (atlantisstruct * ap)
 {
@@ -347,83 +356,44 @@ clear_tank (atlantisstruct * ap)
 
   if (do_gradient && !ap->wire)
     {
-      static GLint gradient_list = -1;
-      static GLint gradient_tex = -1;
-
-      if (gradient_list == -1)
+      GLfloat top[3] = { 0.00, 0.40, 0.70 };
+      GLfloat bot[3] = { 0.00, 0.05, 0.18 };
+
+      glMatrixMode(GL_PROJECTION);
+      glPushMatrix();
+      {
+        glLoadIdentity();
+        glMatrixMode(GL_MODELVIEW);
+        glPushMatrix();
         {
-          unsigned char *pixels = 0;
-          int start = 64;
-          int end = start + 128;
-          int size = 4 * (end - start);
-          int i;
-
-          pixels = (unsigned char *) malloc (size);
-          i = 0;
-          while (i < size)
-            {
-              pixels[i++] = 0;
-              pixels[i++] = (start + (i>>2)) * 0.56;
-              pixels[i++] = (start + (i>>2));
-              pixels[i++] = 255;
-            }
-
-          clear_gl_error();
-
-          glGenTextures(1, &gradient_tex);
-          glBindTexture(GL_TEXTURE_1D, gradient_tex);
-
-          glTexImage1D(GL_TEXTURE_1D, 0, 4,
-                       (end - start), 0,
-                       GL_RGBA, GL_UNSIGNED_BYTE, pixels);
-          check_gl_error ("gradient texture");
-          glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-          glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-
-
-          gradient_list = glGenLists(1);
-          glNewList(gradient_list, GL_COMPILE);
-
-          glDepthMask (False);
-          glDisable(GL_DEPTH_TEST);
-          glDisable(GL_LIGHTING);
-          glDisable(GL_TEXTURE_2D);
-          glEnable(GL_TEXTURE_1D);
-          glBindTexture(GL_TEXTURE_1D, gradient_tex);
-          glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
-          glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
-
-          glMatrixMode(GL_PROJECTION);
-          glPushMatrix();
           glLoadIdentity();
-          glRotatef(90, 0, 0, 1);
-          glTranslatef(-1, -1, 0);
-          glScalef(2, 2, 1);
-
-          glBegin(GL_QUADS);
-          glTexCoord1i(1); glVertex3i(1, 0, 0);
-          glTexCoord1i(0); glVertex3i(0, 0, 0);
-          glTexCoord1i(0); glVertex3i(0, 1, 0);
-          glTexCoord1i(1); glVertex3i(1, 1, 0);
-          glEnd();
-
-          glPopMatrix();
-
-          glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
-          glDisable(GL_TEXTURE_1D);
-
-          glDepthMask (True);
-          glEnable(GL_DEPTH_TEST);
-          glEnable(GL_CULL_FACE);
-          glEnable(GL_LIGHTING);
-          if (do_texture)
-            glEnable(GL_TEXTURE_2D);
 
-          glEndList();
-          check_gl_error ("gradient list");
+          /* save GL_COLOR_MATERIAL, GL_COLOR_MATERIAL_FACE, etc.
+             This stalls the pipeline, so it would be better to do this
+             with explicit enable/disable calls, but I can't figure
+             out how to undo the glEnable() and glColor() calls below!
+             Simply calling glDisable(GL_COLOR_MATERIAL) is insufficient!
+           */
+          glPushAttrib (GL_LIGHTING_BIT);
+          {
+            glEnable (GL_COLOR_MATERIAL);
+
+            glShadeModel(GL_SMOOTH);
+            glBegin(GL_QUADS);
+            glColor3f (bot[0], bot[1], bot[2]); glVertex3f (-1, -1, 1);
+            glColor3f (bot[0], bot[1], bot[2]); glVertex3f ( 1, -1, 1);
+            glColor3f (top[0], top[1], top[2]); glVertex3f ( 1,  1, 1);
+            glColor3f (top[0], top[1], top[2]); glVertex3f (-1,  1, 1);
+            glEnd();
+          }
+          glPopAttrib();
         }
+        glPopMatrix();
+      }
+      glMatrixMode(GL_PROJECTION);
+      glPopMatrix();
 
-      glCallList(gradient_list);
+      glMatrixMode(GL_MODELVIEW);
     }
 }