From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / atlantis.c
index 140295c1fe22d86cfe3cdb95dc334b3cd1d6f99b..c310a69a770a803be4660df2e9ce0062e9131251 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,12 +29,17 @@ 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)
  *
  * REVISION HISTORY:
  * 
+ * Jamie Zawinski, 2-Apr-01:  - The fishies were inside out!  The back faces
+ *                              were being drawn, not the front faces.
+ *                            - Added a texture to simulate light from the
+ *                              surface, like in the SGI version.
+ *
  * David A. Bagley - 98/06/17 : Add whalespeed option. Global options to
  *                              initialize local variables are now:
  *                              XLock.atlantis.cycles: 100      ! SharkSpeed
@@ -53,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 (!)
@@ -98,51 +101,62 @@ static const char sccsid[] = "@(#)atlantis.c       1.3 98/06/18 xlockmore";
  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  */
 
+#define DEF_TEXTURE "True"
+#define DEF_GRADIENT "True"
+#define DEF_WHALESPEED  "250"
+
 #ifdef STANDALONE
-# define PROGCLASS     "Atlantis"
-# define HACK_INIT     init_atlantis
-# define HACK_DRAW     draw_atlantis
-# define atlantis_opts xlockmore_opts
-# define DEFAULTS      "*delay:        1000 \n" \
+# define DEFAULTS      "*delay:       25000 \n" \
                         "*count:          4 \n" \
+                        "*showFPS:    False \n" \
                         "*cycles:       100 \n" \
                         "*size:        6000 \n" \
-                        "*whalespeed:   250 \n"
+                        "*wireframe:  False \n"
+# define release_atlantis 0
+# define atlantis_handle_event 0
 # include "xlockmore.h"                /* from the xscreensaver distribution */
 #else  /* !STANDALONE */
 # include "xlock.h"            /* from the xlockmore distribution */
-#include "vis.h"
+# include "vis.h"
 #endif /* !STANDALONE */
 
 #ifdef USE_GL
 
 #include "atlantis.h"
-#include <GL/glu.h>
 
 
-#define DEF_WHALESPEED  "250"
 static int  whalespeed;
+static int do_texture;
+static int do_gradient;
 static XrmOptionDescRec opts[] =
 {
-     {"-whalespeed", ".atlantis.whalespeed", XrmoptionSepArg, (caddr_t) NULL}
+     {"-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}
+ {&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"}
+       {"-whalespeed num", "speed of whales and the dolphin"},
+       {"-texture",        "whether to introduce water-like distortion"},
+       {"-gradient",       "whether to introduce gradient-filled background"},
 };
 
-ModeSpecOpt atlantis_opts =
+ENTRYPOINT ModeSpecOpt atlantis_opts =
 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
 
 #ifdef USE_MODULES
 ModStruct   atlantis_description =
-{"atlantis", "init_atlantis", "draw_atlantis", "release_atlantis",
+{"atlantis", "init_atlantis", "draw_atlantis", NULL,
  "refresh_atlantis", "change_atlantis", NULL, &atlantis_opts,
  1000, NUM_SHARKS, SHARKSPEED, SHARKSIZE, 64, 1.0, "",
  "Shows moving sharks/whales/dolphin", 0, NULL};
@@ -151,6 +165,21 @@ ModStruct   atlantis_description =
 
 static atlantisstruct *atlantis = NULL;
 
+#include "xpm-ximage.h"
+
+#include "../images/sea-texture.xpm"
+
+
+static void
+parse_image_data(ModeInfo *mi)
+{
+  atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
+  ap->texture = xpm_to_ximage (mi->dpy,
+                              mi->xgwa.visual,
+                              mi->xgwa.colormap,
+                              sea_texture);
+}
+
 static void
 InitFishs(atlantisstruct * ap)
 {
@@ -164,7 +193,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;
@@ -190,56 +219,108 @@ InitFishs(atlantisstruct * ap)
 }
 
 static void
-Init(atlantisstruct * ap)
+Init(ModeInfo *mi)
 {
-       static float ambient[] =
-       {0.1, 0.1, 0.1, 1.0};
-       static float diffuse[] =
-       {1.0, 1.0, 1.0, 1.0};
-       static float position[] =
-       {0.0, 1.0, 0.0, 0.0};
-       static float mat_shininess[] =
-       {90.0};
-       static float mat_specular[] =
-       {0.8, 0.8, 0.8, 1.0};
-       static float mat_diffuse[] =
-       {0.46, 0.66, 0.795, 1.0};
-       static float mat_ambient[] =
-       {0.0, 0.1, 0.2, 1.0};
-       static float lmodel_ambient[] =
-       {0.4, 0.4, 0.4, 1.0};
-       static float lmodel_localviewer[] =
-       {0.0};
-       float       fblue = 0.0, fgreen;
-
-       glFrontFace(GL_CW);
-
-       glDepthFunc(GL_LEQUAL);
-       glEnable(GL_DEPTH_TEST);
-
-       glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
-       glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
-       glLightfv(GL_LIGHT0, GL_POSITION, position);
-       glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
-       glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
-       glEnable(GL_LIGHTING);
-       glEnable(GL_LIGHT0);
-
-       glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
-       glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
-       glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
-       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
+       atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
+
+       static const float ambient[]        = {0.1, 0.1, 0.1, 1.0};
+       static const float diffuse[]        = {1.0, 1.0, 1.0, 1.0};
+       static const float position[]       = {0.0, 1.0, 0.0, 0.0};
+       static const float mat_shininess[]  = {90.0};
+       static const float mat_specular[]   = {0.8, 0.8, 0.8, 1.0};
+       static const float mat_diffuse[]    = {0.46, 0.66, 0.795, 1.0};
+       static const float mat_ambient[]    = {0.0, 0.1, 0.2, 1.0};
+       static const float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
+       static const float lmodel_localviewer[] = {0.0};
+
+       float        fblue = 0.0, fgreen;
+
+       glFrontFace(GL_CCW);
+
+        if (ap->wire)
+          {
+            glDisable(GL_DEPTH_TEST);
+            glDisable(GL_CULL_FACE);
+            glDisable(GL_LIGHTING);
+            glDisable(GL_NORMALIZE);
+          }
+        else
+          {
+            glDepthFunc(GL_LEQUAL);
+            glEnable(GL_DEPTH_TEST);
+            glEnable(GL_CULL_FACE);
+            glEnable(GL_NORMALIZE);
+            glShadeModel(GL_SMOOTH);
+
+            glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
+            glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
+            glLightfv(GL_LIGHT0, GL_POSITION, position);
+            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
+            glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
+            glEnable(GL_LIGHTING);
+            glEnable(GL_LIGHT0);
+
+            glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
+            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
+            glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
+            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
+          }
+
+        if (ap->wire || !do_texture)
+          {
+            glDisable(GL_TEXTURE_2D);
+          }
+        else
+          {
+            GLfloat scale = 0.0005;
+
+            if (!ap->texture)
+              parse_image_data (mi);
+
+            clear_gl_error();
+            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
+                         ap->texture->width, ap->texture->height, 0,
+                         GL_RGBA, GL_UNSIGNED_BYTE,
+                         ap->texture->data);
+            check_gl_error("texture");
+
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+
+            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+
+# ifndef HAVE_JWZGLES
+            {
+              GLfloat s_plane[] = { 1, 0, 0, 0 };
+              GLfloat t_plane[] = { 0, 0, 1, 0 };
+              glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
+              glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
+              glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
+              glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
+              glEnable(GL_TEXTURE_GEN_S);
+              glEnable(GL_TEXTURE_GEN_T);
+            }
+# endif
+            glEnable(GL_TEXTURE_2D);
+
+            glMatrixMode(GL_TEXTURE);
+            glLoadIdentity();
+            glScalef(scale, scale, 1);
+            glMatrixMode(GL_MODELVIEW);
+          }
 
        InitFishs(ap);
 
        /* Add a little randomness */
-       fblue = ((float) (NRAND(50)) / 100.0) + 0.50;
+       fblue = ((float) (NRAND(30)) / 100.0) + 0.70;
        fgreen = fblue * 0.56;
-       glClearColor(0.0, fgreen, fblue, 0.0);
+       glClearColor(0.0, fgreen, fblue, 1.0);
 }
 
-static void
-Reshape(ModeInfo * mi, int width, int height)
+ENTRYPOINT void
+reshape_atlantis(ModeInfo * mi, int width, int height)
 {
        atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
 
@@ -251,6 +332,53 @@ Reshape(ModeInfo * mi, int width, int height)
        glMatrixMode(GL_MODELVIEW);
 }
 
+
+/* 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)
+{
+  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+  if (do_gradient && !ap->wire)
+    {
+      GLfloat top[4] = { 0.00, 0.40, 0.70, };
+      GLfloat bot[4] = { 0.00, 0.05, 0.18, };
+
+      glMatrixMode(GL_PROJECTION);
+      glPushMatrix();
+      {
+        glLoadIdentity();
+        glMatrixMode(GL_MODELVIEW);
+        glPushMatrix();
+        {
+          glLoadIdentity();
+          /* glRotatef(current_device_rotation(), 0, 0, 1); */
+
+# ifndef HAVE_JWZGLES
+          glShadeModel (GL_SMOOTH);
+# endif
+          glDisable (GL_LIGHTING);
+          glBegin (GL_QUADS);
+          glColor3fv (bot);
+          glVertex3f (-1, -1, 1); glVertex3f ( 1, -1, 1);
+          glColor3fv (top);
+          glVertex3f ( 1,  1, 1); glVertex3f (-1,  1, 1);
+          glEnd();
+          glEnable (GL_LIGHTING);
+        }
+        glPopMatrix();
+      }
+      glMatrixMode(GL_PROJECTION);
+      glPopMatrix();
+
+      glMatrixMode(GL_MODELVIEW);
+    }
+}
+
+
 static void
 Animate(atlantisstruct * ap)
 {
@@ -273,7 +401,7 @@ AllDisplay(atlantisstruct * ap)
 {
        int         i;
 
-       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+        clear_tank(ap);
 
        for (i = 0; i < ap->num_sharks; i++) {
                glPushMatrix();
@@ -307,13 +435,15 @@ AllDisplay(atlantisstruct * ap)
  *-----------------------------------------------------------------------------
  */
 
+static void free_atlantis(ModeInfo * mi);
+
 /*
  *-----------------------------------------------------------------------------
  *    Initialize atlantis.  Called each time the window changes.
  *-----------------------------------------------------------------------------
  */
 
-void
+ENTRYPOINT void
 init_atlantis(ModeInfo * mi)
 {
        int         screen = MI_SCREEN(mi);
@@ -321,11 +451,7 @@ init_atlantis(ModeInfo * mi)
        Display    *display = MI_DISPLAY(mi);
        Window      window = MI_WINDOW(mi);
 
-       if (atlantis == NULL) {
-               if ((atlantis = (atlantisstruct *) calloc(MI_NUM_SCREENS(mi),
-                                          sizeof (atlantisstruct))) == NULL)
-                       return;
-       }
+       MI_INIT(mi, atlantis, free_atlantis);
        ap = &atlantis[screen];
        ap->num_sharks = MI_COUNT(mi);
        if (ap->sharks == NULL) {
@@ -357,9 +483,9 @@ init_atlantis(ModeInfo * mi)
        }
        if ((ap->glx_context = init_GL(mi)) != NULL) {
 
-               Reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+               reshape_atlantis(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
                glDrawBuffer(GL_BACK);
-               Init(ap);
+               Init(mi);
                AllDisplay(ap);
                glXSwapBuffers(display, window);
 
@@ -373,7 +499,7 @@ init_atlantis(ModeInfo * mi)
  *    Called by the mainline code periodically to update the display.
  *-----------------------------------------------------------------------------
  */
-void
+ENTRYPOINT void
 draw_atlantis(ModeInfo * mi)
 {
        atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
@@ -390,11 +516,13 @@ draw_atlantis(ModeInfo * mi)
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-       glPushMatrix();
-
-       AllDisplay(ap);
-       Animate(ap);
+        glPushMatrix();
+        glRotatef(current_device_rotation(), 0, 0, 1);
+        AllDisplay(ap);
+        Animate(ap);
+        glPopMatrix();
 
+        if (mi->fps_p) do_fps (mi);
        glXSwapBuffers(display, window);
 }
 
@@ -402,35 +530,28 @@ draw_atlantis(ModeInfo * mi)
 /*
  *-----------------------------------------------------------------------------
  *    The display is being taken away from us.  Free up malloc'ed 
- *      memory and X resources that we've alloc'ed.  Only called
- *      once, we must zap everything for every screen.
+ *      memory and X resources that we've alloc'ed.
  *-----------------------------------------------------------------------------
  */
 
-void
-release_atlantis(ModeInfo * mi)
+static void
+free_atlantis(ModeInfo * mi)
 {
-       int         screen;
+#if 0
+       atlantisstruct *ap = &atlantis[screen];
 
-       if (atlantis != NULL) {
-               for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
-                       atlantisstruct *ap = &atlantis[screen];
-
-                       if (ap->sharks)
-                               (void) free((void *) ap->sharks);
-               }
-               (void) free((void *) atlantis);
-               atlantis = NULL;
-       }
-       FreeAllGL(mi);
+       if (ap->sharks)
+               (void) free((void *) ap->sharks);
+#endif
 }
 
-void
+ENTRYPOINT void
 refresh_atlantis(ModeInfo * mi)
 {
 }
 
-void
+#ifndef STANDALONE
+ENTRYPOINT void
 change_atlantis(ModeInfo * mi)
 {
        atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
@@ -439,7 +560,10 @@ change_atlantis(ModeInfo * mi)
                return;
 
        glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ap->glx_context));
-       Init(ap);
+       Init(mi);
 }
+#endif /* !STANDALONE */
+
+XSCREENSAVER_MODULE ("Atlantis", atlantis)
 
 #endif /* USE_GL */