http://packetstormsecurity.org/UNIX/admin/xscreensaver-3.32.tar.gz
[xscreensaver] / hacks / glx / atlantis.c
1 /* atlantis --- Shows moving 3D sea animals */
2
3 #if !defined( lint ) && !defined( SABER )
4 static const char sccsid[] = "@(#)atlantis.c    1.3 98/06/18 xlockmore";
5
6 #endif
7
8 /* Copyright (c) E. Lassauge, 1998. */
9
10 /*
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * The original code for this mode was written by Mark J. Kilgard
24  * as a demo for openGL programming.
25  * 
26  * Porting it to xlock  was possible by comparing the original Mesa's morph3d 
27  * demo with it's ported version to xlock, so thanks for Marcelo F. Vianna 
28  * (look at morph3d.c) for his indirect help.
29  *
30  * Thanks goes also to Brian Paul for making it possible and inexpensive
31  * to use OpenGL at home.
32  *
33  * My e-mail address is lassauge@sagem.fr
34  *
35  * Eric Lassauge  (May-13-1998)
36  *
37  * REVISION HISTORY:
38  * 
39  * Jamie Zawinski, 2-Apr-01:  - The fishies were inside out!  The back faces
40  *                              were being drawn, not the front faces.
41  *                            - Added a texture to simulate light from the
42  *                              surface, like in the SGI version.
43  *
44  * David A. Bagley - 98/06/17 : Add whalespeed option. Global options to
45  *                              initialize local variables are now:
46  *                              XLock.atlantis.cycles: 100      ! SharkSpeed
47  *                              XLock.atlantis.batchcount: 4    ! SharkNum
48  *                              XLock.atlantis.whalespeed: 250  ! WhaleSpeed
49  *                              XLock.atlantis.size: 6000       ! SharkSize
50  *                              Add random direction for whales/dolphins
51  * 
52  * E.Lassauge - 98/06/16: Use the following global options to initialize
53  *                        local variables :
54  *                              XLock.atlantis.delay: 100       ! SharkSpeed
55  *                              XLock.atlantis.batchcount: 4    ! SharkNum
56  *                              XLock.atlantis.cycles: 250      ! WhaleSpeed
57  *                              XLock.atlantis.size: 6000       ! SharkSize
58  *                        Add support for -/+ wireframe (t'was so easy to do!)
59  *
60  * TODO : 
61  *        - add a sort of background image or random bg color
62  *        - better handling of sizes and speeds
63  *        - test standalone and module modes
64  *        - purify it (!)
65  */
66
67 /* Copyright (c) Mark J. Kilgard, 1994. */
68
69 /**
70  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
71  * ALL RIGHTS RESERVED
72  * Permission to use, copy, modify, and distribute this software for
73  * any purpose and without fee is hereby granted, provided that the above
74  * copyright notice appear in all copies and that both the copyright notice
75  * and this permission notice appear in supporting documentation, and that
76  * the name of Silicon Graphics, Inc. not be used in advertising
77  * or publicity pertaining to distribution of the software without specific,
78  * written prior permission.
79  *
80  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
81  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
82  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
83  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
84  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
85  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
86  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
87  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
88  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
89  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
90  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
91  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
92  *
93  * US Government Users Restricted Rights
94  * Use, duplication, or disclosure by the Government is subject to
95  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
96  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
97  * clause at DFARS 252.227-7013 and/or in similar or successor
98  * clauses in the FAR or the DOD or NASA FAR Supplement.
99  * Unpublished-- rights reserved under the copyright laws of the
100  * United States.  Contractor/manufacturer is Silicon Graphics,
101  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
102  *
103  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
104  */
105
106 #ifdef STANDALONE
107 # define PROGCLASS      "Atlantis"
108 # define HACK_INIT      init_atlantis
109 # define HACK_DRAW      draw_atlantis
110 # define HACK_RESHAPE   reshape_atlantis
111 # define atlantis_opts  xlockmore_opts
112 # define DEFAULTS       "*delay:       40000 \n" \
113                          "*count:          4 \n" \
114                          "*showFPS:    False \n" \
115                          "*cycles:       100 \n" \
116                          "*size:        6000 \n" \
117                          "*wireframe:  False \n" \
118                          "*whalespeed:   250 \n"
119 # include "xlockmore.h"         /* from the xscreensaver distribution */
120 #else  /* !STANDALONE */
121 # include "xlock.h"             /* from the xlockmore distribution */
122 #include "vis.h"
123 #endif /* !STANDALONE */
124
125 #ifdef USE_GL
126
127 #include "atlantis.h"
128 #include <GL/glu.h>
129
130
131 #define DEF_TEXTURE "True"
132
133 #define DEF_WHALESPEED  "250"
134 static int  whalespeed;
135 static int do_texture;
136 static XrmOptionDescRec opts[] =
137 {
138      {"-whalespeed", ".atlantis.whalespeed", XrmoptionSepArg, (caddr_t) NULL},
139      {"-texture",    ".atlantis.texture",    XrmoptionNoArg, (caddr_t)"true"},
140      {"+texture",    ".atlantis.texture",    XrmoptionNoArg, (caddr_t)"false"},
141 };
142
143 static argtype vars[] =
144 {
145  {(caddr_t *) & whalespeed, "whalespeed", "WhaleSpeed", DEF_WHALESPEED, t_Int},
146  {(caddr_t *) &do_texture,  "texture",    "Texture",    DEF_TEXTURE,   t_Bool},
147 };
148
149 static OptionStruct desc[] =
150 {
151         {"-whalespeed num", "speed of whales and the dolphin"},
152         {"-texture num",    "whether to introduce water-like distortion"}
153 };
154
155 ModeSpecOpt atlantis_opts =
156 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
157
158 #ifdef USE_MODULES
159 ModStruct   atlantis_description =
160 {"atlantis", "init_atlantis", "draw_atlantis", "release_atlantis",
161  "refresh_atlantis", "change_atlantis", NULL, &atlantis_opts,
162  1000, NUM_SHARKS, SHARKSPEED, SHARKSIZE, 64, 1.0, "",
163  "Shows moving sharks/whales/dolphin", 0, NULL};
164
165 #endif
166
167 static atlantisstruct *atlantis = NULL;
168
169 #include "xpm-ximage.h"
170 #include "../images/sea-texture.xpm"
171
172 static void
173 parse_image_data(ModeInfo *mi)
174 {
175   atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
176   ap->texture = xpm_to_ximage (mi->dpy,
177                                mi->xgwa.visual,
178                                mi->xgwa.colormap,
179                                sea_texture);
180 }
181
182 static void
183 InitFishs(atlantisstruct * ap)
184 {
185         int         i;
186
187         for (i = 0; i < ap->num_sharks; i++) {
188                 ap->sharks[i].x = 70000.0 + NRAND(ap->sharksize);
189                 ap->sharks[i].y = NRAND(ap->sharksize);
190                 ap->sharks[i].z = NRAND(ap->sharksize);
191                 ap->sharks[i].psi = NRAND(360) - 180.0;
192                 ap->sharks[i].v = 1.0;
193         }
194
195         /* Random whae direction */
196         ap->whaledir = LRAND() & 1;
197
198         ap->dolph.x = 30000.0;
199         ap->dolph.y = 0.0;
200         ap->dolph.z = (float) (ap->sharksize);
201         ap->dolph.psi = (ap->whaledir) ? 90.0 : -90.0;
202         ap->dolph.theta = 0.0;
203         ap->dolph.v = 6.0;
204
205         ap->momWhale.x = 70000.0;
206         ap->momWhale.y = 0.0;
207         ap->momWhale.z = 0.0;
208         ap->momWhale.psi = (ap->whaledir) ? 90.0 : -90.0;
209         ap->momWhale.theta = 0.0;
210         ap->momWhale.v = 3.0;
211
212         ap->babyWhale.x = 60000.0;
213         ap->babyWhale.y = -2000.0;
214         ap->babyWhale.z = -2000.0;
215         ap->babyWhale.psi = (ap->whaledir) ? 90.0 : -90.0;
216         ap->babyWhale.theta = 0.0;
217         ap->babyWhale.v = 3.0;
218 }
219
220 static void
221 Init(ModeInfo *mi)
222 {
223         atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
224
225         static float ambient[] =
226         {0.1, 0.1, 0.1, 1.0};
227         static float diffuse[] =
228         {1.0, 1.0, 1.0, 1.0};
229         static float position[] =
230         {0.0, 1.0, 0.0, 0.0};
231         static float mat_shininess[] =
232         {90.0};
233         static float mat_specular[] =
234         {0.8, 0.8, 0.8, 1.0};
235         static float mat_diffuse[] =
236         {0.46, 0.66, 0.795, 1.0};
237         static float mat_ambient[] =
238         {0.0, 0.1, 0.2, 1.0};
239         static float lmodel_ambient[] =
240         {0.4, 0.4, 0.4, 1.0};
241         static float lmodel_localviewer[] =
242         {0.0};
243         float       fblue = 0.0, fgreen;
244
245         glFrontFace(GL_CCW);
246
247         if (ap->wire)
248           {
249             glDisable(GL_DEPTH_TEST);
250             glDisable(GL_CULL_FACE);
251             glDisable(GL_LIGHTING);
252             glDisable(GL_NORMALIZE);
253           }
254         else
255           {
256             glDepthFunc(GL_LEQUAL);
257             glEnable(GL_DEPTH_TEST);
258             glEnable(GL_CULL_FACE);
259             glEnable(GL_NORMALIZE);
260             glShadeModel(GL_SMOOTH);
261
262             glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
263             glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
264             glLightfv(GL_LIGHT0, GL_POSITION, position);
265             glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
266             glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
267             glEnable(GL_LIGHTING);
268             glEnable(GL_LIGHT0);
269
270             glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
271             glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
272             glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
273             glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
274           }
275
276         if (ap->wire || !do_texture)
277           {
278             glDisable(GL_TEXTURE_2D);
279           }
280         else
281           {
282             GLfloat s_plane[] = { 1, 0, 0, 0 };
283             GLfloat t_plane[] = { 0, 0, 1, 0 };
284             GLfloat scale = 0.0005;
285
286             if (!ap->texture)
287               parse_image_data (mi);
288
289             clear_gl_error();
290             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
291                          ap->texture->width, ap->texture->height, 0,
292                          GL_RGBA, GL_UNSIGNED_BYTE,
293                          ap->texture->data);
294             check_gl_error("texture");
295
296             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
297             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
298             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
299             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
300
301             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
302
303             glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
304             glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
305             glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
306             glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
307
308             glEnable(GL_TEXTURE_GEN_S);
309             glEnable(GL_TEXTURE_GEN_T);
310             glEnable(GL_TEXTURE_2D);
311
312             glMatrixMode(GL_TEXTURE);
313             glLoadIdentity();
314             glScalef(scale, scale, 1);
315             glMatrixMode(GL_MODELVIEW);
316           }
317
318         InitFishs(ap);
319
320         /* Add a little randomness */
321         fblue = ((float) (NRAND(50)) / 100.0) + 0.50;
322         fgreen = fblue * 0.56;
323         glClearColor(0.0, fgreen, fblue, 0.0);
324 }
325
326 void
327 reshape_atlantis(ModeInfo * mi, int width, int height)
328 {
329         atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
330
331         glViewport(0, 0, ap->WinW = (GLint) width, ap->WinH = (GLint) height);
332
333         glMatrixMode(GL_PROJECTION);
334         glLoadIdentity();
335         gluPerspective(400.0, (GLdouble) width / (GLdouble) height, 1.0, 2000000.0);
336         glMatrixMode(GL_MODELVIEW);
337 }
338
339 static void
340 Animate(atlantisstruct * ap)
341 {
342         int         i;
343
344         for (i = 0; i < ap->num_sharks; i++) {
345                 SharkPilot(&(ap->sharks[i]), ap->sharkspeed);
346                 SharkMiss(ap, i);
347         }
348         WhalePilot(&(ap->dolph), ap->whalespeed, ap->whaledir);
349         ap->dolph.phi++;
350         WhalePilot(&(ap->momWhale), ap->whalespeed, ap->whaledir);
351         ap->momWhale.phi++;
352         WhalePilot(&(ap->babyWhale), ap->whalespeed, ap->whaledir);
353         ap->babyWhale.phi++;
354 }
355
356 static void
357 AllDisplay(atlantisstruct * ap)
358 {
359         int         i;
360
361         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
362
363         for (i = 0; i < ap->num_sharks; i++) {
364                 glPushMatrix();
365                 FishTransform(&(ap->sharks[i]));
366                 DrawShark(&(ap->sharks[i]), ap->wire);
367                 glPopMatrix();
368         }
369
370         glPushMatrix();
371         FishTransform(&(ap->dolph));
372         DrawDolphin(&(ap->dolph), ap->wire);
373         glPopMatrix();
374
375         glPushMatrix();
376         FishTransform(&(ap->momWhale));
377         DrawWhale(&(ap->momWhale), ap->wire);
378         glPopMatrix();
379
380         glPushMatrix();
381         FishTransform(&(ap->babyWhale));
382         glScalef(0.45, 0.45, 0.3);
383         DrawWhale(&(ap->babyWhale), ap->wire);
384         glPopMatrix();
385 }
386
387 /*
388  *-----------------------------------------------------------------------------
389  *-----------------------------------------------------------------------------
390  *    Xlock hooks.
391  *-----------------------------------------------------------------------------
392  *-----------------------------------------------------------------------------
393  */
394
395 /*
396  *-----------------------------------------------------------------------------
397  *    Initialize atlantis.  Called each time the window changes.
398  *-----------------------------------------------------------------------------
399  */
400
401 void
402 init_atlantis(ModeInfo * mi)
403 {
404         int         screen = MI_SCREEN(mi);
405         atlantisstruct *ap;
406         Display    *display = MI_DISPLAY(mi);
407         Window      window = MI_WINDOW(mi);
408
409         if (atlantis == NULL) {
410                 if ((atlantis = (atlantisstruct *) calloc(MI_NUM_SCREENS(mi),
411                                            sizeof (atlantisstruct))) == NULL)
412                         return;
413         }
414         ap = &atlantis[screen];
415         ap->num_sharks = MI_COUNT(mi);
416         if (ap->sharks == NULL) {
417                 if ((ap->sharks = (fishRec *) calloc(ap->num_sharks,
418                                                 sizeof (fishRec))) == NULL) {
419                         /* free everything up to now */
420                         (void) free((void *) atlantis);
421                         atlantis = NULL;
422                         return;
423                 }
424         }
425         ap->sharkspeed = MI_CYCLES(mi);         /* has influence on the "width"
426                                                    of the movement */
427         ap->sharksize = MI_SIZE(mi);    /* has influence on the "distance"
428                                            of the sharks */
429         ap->whalespeed = whalespeed;
430         ap->wire = MI_IS_WIREFRAME(mi);
431
432         if (MI_IS_DEBUG(mi)) {
433                 (void) fprintf(stderr,
434                                "%s:\n\tnum_sharks=%d\n\tsharkspeed=%.1f\n\tsharksize=%d\n\twhalespeed=%.1f\n\twireframe=%s\n",
435                                MI_NAME(mi),
436                                ap->num_sharks,
437                                ap->sharkspeed,
438                                ap->sharksize,
439                                ap->whalespeed,
440                                ap->wire ? "yes" : "no"
441                         );
442         }
443         if ((ap->glx_context = init_GL(mi)) != NULL) {
444
445                 reshape_atlantis(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
446                 glDrawBuffer(GL_BACK);
447                 Init(mi);
448                 AllDisplay(ap);
449                 glXSwapBuffers(display, window);
450
451         } else {
452                 MI_CLEARWINDOW(mi);
453         }
454 }
455
456 /*
457  *-----------------------------------------------------------------------------
458  *    Called by the mainline code periodically to update the display.
459  *-----------------------------------------------------------------------------
460  */
461 void
462 draw_atlantis(ModeInfo * mi)
463 {
464         atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
465
466         Display    *display = MI_DISPLAY(mi);
467         Window      window = MI_WINDOW(mi);
468
469         MI_IS_DRAWN(mi) = True;
470
471         if (!ap->glx_context)
472                 return;
473
474         glXMakeCurrent(display, window, *(ap->glx_context));
475
476         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
477
478         AllDisplay(ap);
479         Animate(ap);
480
481         if (mi->fps_p) do_fps (mi);
482         glXSwapBuffers(display, window);
483 }
484
485
486 /*
487  *-----------------------------------------------------------------------------
488  *    The display is being taken away from us.  Free up malloc'ed 
489  *      memory and X resources that we've alloc'ed.  Only called
490  *      once, we must zap everything for every screen.
491  *-----------------------------------------------------------------------------
492  */
493
494 void
495 release_atlantis(ModeInfo * mi)
496 {
497         int         screen;
498
499         if (atlantis != NULL) {
500                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
501                         atlantisstruct *ap = &atlantis[screen];
502
503                         if (ap->sharks)
504                                 (void) free((void *) ap->sharks);
505                 }
506                 (void) free((void *) atlantis);
507                 atlantis = NULL;
508         }
509         FreeAllGL(mi);
510 }
511
512 void
513 refresh_atlantis(ModeInfo * mi)
514 {
515 }
516
517 void
518 change_atlantis(ModeInfo * mi)
519 {
520         atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
521
522         if (!ap->glx_context)
523                 return;
524
525         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ap->glx_context));
526         Init(mi);
527 }
528
529 #endif /* USE_GL */