1 /* atlantis --- Shows moving 3D sea animals */
3 #if !defined( lint ) && !defined( SABER )
4 static const char sccsid[] = "@(#)atlantis.c 1.3 98/06/18 xlockmore";
8 /* Copyright (c) E. Lassauge, 1998. */
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.
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.
23 * The original code for this mode was written by Mark J. Kilgard
24 * as a demo for openGL programming.
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.
30 * Thanks goes also to Brian Paul for making it possible and inexpensive
31 * to use OpenGL at home.
33 * My e-mail address is lassauge@sagem.fr
35 * Eric Lassauge (May-13-1998)
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.
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
52 * E.Lassauge - 98/06/16: Use the following global options to initialize
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!)
61 * - add a sort of background image or random bg color
62 * - better handling of sizes and speeds
63 * - test standalone and module modes
67 /* Copyright (c) Mark J. Kilgard, 1994. */
70 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
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.
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.
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.
103 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
106 #define DEF_TEXTURE "True"
107 #define DEF_GRADIENT "False"
108 #define DEF_WHALESPEED "250"
111 # define PROGCLASS "Atlantis"
112 # define HACK_INIT init_atlantis
113 # define HACK_DRAW draw_atlantis
114 # define HACK_RESHAPE reshape_atlantis
115 # define atlantis_opts xlockmore_opts
116 # define DEFAULTS "*delay: 25000 \n" \
118 "*showFPS: False \n" \
121 "*wireframe: False \n" \
122 "*texture: " DEF_TEXTURE " \n" \
123 "*gradient: " DEF_GRADIENT " \n" \
124 "*whalespeed: " DEF_WHALESPEED " \n"
125 # include "xlockmore.h" /* from the xscreensaver distribution */
126 #else /* !STANDALONE */
127 # include "xlock.h" /* from the xlockmore distribution */
129 #endif /* !STANDALONE */
133 #include "atlantis.h"
137 static int whalespeed;
138 static int do_texture;
139 static int do_gradient;
140 static XrmOptionDescRec opts[] =
142 {"-whalespeed", ".atlantis.whalespeed", XrmoptionSepArg, (caddr_t) NULL},
143 {"-texture", ".atlantis.texture", XrmoptionNoArg, (caddr_t)"true"},
144 {"+texture", ".atlantis.texture", XrmoptionNoArg, (caddr_t)"false"},
145 {"-gradient", ".atlantis.gradient", XrmoptionNoArg, (caddr_t)"true"},
146 {"+gradient", ".atlantis.gradient", XrmoptionNoArg, (caddr_t)"false"},
149 static argtype vars[] =
151 {(caddr_t *) & whalespeed, "whalespeed", "WhaleSpeed", DEF_WHALESPEED, t_Int},
152 {(caddr_t *) &do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
153 {(caddr_t *) &do_gradient, "gradient", "Gradient", DEF_GRADIENT, t_Bool},
156 static OptionStruct desc[] =
158 {"-whalespeed num", "speed of whales and the dolphin"},
159 {"-texture num", "whether to introduce water-like distortion"}
162 ModeSpecOpt atlantis_opts =
163 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
166 ModStruct atlantis_description =
167 {"atlantis", "init_atlantis", "draw_atlantis", "release_atlantis",
168 "refresh_atlantis", "change_atlantis", NULL, &atlantis_opts,
169 1000, NUM_SHARKS, SHARKSPEED, SHARKSIZE, 64, 1.0, "",
170 "Shows moving sharks/whales/dolphin", 0, NULL};
174 static atlantisstruct *atlantis = NULL;
176 #include "xpm-ximage.h"
177 #include "../images/sea-texture.xpm"
180 parse_image_data(ModeInfo *mi)
182 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
183 ap->texture = xpm_to_ximage (mi->dpy,
190 InitFishs(atlantisstruct * ap)
194 for (i = 0; i < ap->num_sharks; i++) {
195 ap->sharks[i].x = 70000.0 + NRAND(ap->sharksize);
196 ap->sharks[i].y = NRAND(ap->sharksize);
197 ap->sharks[i].z = NRAND(ap->sharksize);
198 ap->sharks[i].psi = NRAND(360) - 180.0;
199 ap->sharks[i].v = 1.0;
202 /* Random whae direction */
203 ap->whaledir = LRAND() & 1;
205 ap->dolph.x = 30000.0;
207 ap->dolph.z = (float) (ap->sharksize);
208 ap->dolph.psi = (ap->whaledir) ? 90.0 : -90.0;
209 ap->dolph.theta = 0.0;
212 ap->momWhale.x = 70000.0;
213 ap->momWhale.y = 0.0;
214 ap->momWhale.z = 0.0;
215 ap->momWhale.psi = (ap->whaledir) ? 90.0 : -90.0;
216 ap->momWhale.theta = 0.0;
217 ap->momWhale.v = 3.0;
219 ap->babyWhale.x = 60000.0;
220 ap->babyWhale.y = -2000.0;
221 ap->babyWhale.z = -2000.0;
222 ap->babyWhale.psi = (ap->whaledir) ? 90.0 : -90.0;
223 ap->babyWhale.theta = 0.0;
224 ap->babyWhale.v = 3.0;
230 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
232 static float ambient[] =
233 {0.1, 0.1, 0.1, 1.0};
234 static float diffuse[] =
235 {1.0, 1.0, 1.0, 1.0};
236 static float position[] =
237 {0.0, 1.0, 0.0, 0.0};
238 static float mat_shininess[] =
240 static float mat_specular[] =
241 {0.8, 0.8, 0.8, 1.0};
242 static float mat_diffuse[] =
243 {0.46, 0.66, 0.795, 1.0};
244 static float mat_ambient[] =
245 {0.0, 0.1, 0.2, 1.0};
246 static float lmodel_ambient[] =
247 {0.4, 0.4, 0.4, 1.0};
248 static float lmodel_localviewer[] =
255 glDisable(GL_DEPTH_TEST);
256 glDisable(GL_CULL_FACE);
257 glDisable(GL_LIGHTING);
258 glDisable(GL_NORMALIZE);
262 glDepthFunc(GL_LEQUAL);
263 glEnable(GL_DEPTH_TEST);
264 glEnable(GL_CULL_FACE);
265 glEnable(GL_NORMALIZE);
266 glShadeModel(GL_SMOOTH);
268 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
269 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
270 glLightfv(GL_LIGHT0, GL_POSITION, position);
271 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
272 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
273 glEnable(GL_LIGHTING);
276 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
277 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
278 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
279 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
282 if (ap->wire || !do_texture)
284 glDisable(GL_TEXTURE_2D);
288 GLfloat s_plane[] = { 1, 0, 0, 0 };
289 GLfloat t_plane[] = { 0, 0, 1, 0 };
290 GLfloat scale = 0.0005;
293 parse_image_data (mi);
296 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
297 ap->texture->width, ap->texture->height, 0,
298 GL_RGBA, GL_UNSIGNED_BYTE,
300 check_gl_error("texture");
302 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
303 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
304 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
305 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
307 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
309 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
310 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
311 glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
312 glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
314 glEnable(GL_TEXTURE_GEN_S);
315 glEnable(GL_TEXTURE_GEN_T);
316 glEnable(GL_TEXTURE_2D);
318 glMatrixMode(GL_TEXTURE);
320 glScalef(scale, scale, 1);
321 glMatrixMode(GL_MODELVIEW);
326 glClearColor(0.0, 0.39, 0.7, 0.0);
330 reshape_atlantis(ModeInfo * mi, int width, int height)
332 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
334 glViewport(0, 0, ap->WinW = (GLint) width, ap->WinH = (GLint) height);
336 glMatrixMode(GL_PROJECTION);
338 gluPerspective(400.0, (GLdouble) width / (GLdouble) height, 1.0, 2000000.0);
339 glMatrixMode(GL_MODELVIEW);
344 clear_tank (atlantisstruct * ap)
346 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
348 if (do_gradient && !ap->wire)
350 static GLint gradient_list = -1;
351 static GLint gradient_tex = -1;
353 if (gradient_list == -1)
355 unsigned char *pixels = 0;
357 int end = start + 128;
358 int size = 4 * (end - start);
361 pixels = (unsigned char *) malloc (size);
366 pixels[i++] = (start + (i>>2)) * 0.56;
367 pixels[i++] = (start + (i>>2));
373 glGenTextures(1, &gradient_tex);
374 glBindTexture(GL_TEXTURE_1D, gradient_tex);
376 glTexImage1D(GL_TEXTURE_1D, 0, 4,
378 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
379 check_gl_error ("gradient texture");
380 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
381 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
384 gradient_list = glGenLists(1);
385 glNewList(gradient_list, GL_COMPILE);
388 glDisable(GL_DEPTH_TEST);
389 glDisable(GL_LIGHTING);
390 glDisable(GL_TEXTURE_2D);
391 glEnable(GL_TEXTURE_1D);
392 glBindTexture(GL_TEXTURE_1D, gradient_tex);
393 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
394 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
396 glMatrixMode(GL_PROJECTION);
399 glRotatef(90, 0, 0, 1);
400 glTranslatef(-1, -1, 0);
404 glTexCoord1i(1); glVertex3i(1, 0, 0);
405 glTexCoord1i(0); glVertex3i(0, 0, 0);
406 glTexCoord1i(0); glVertex3i(0, 1, 0);
407 glTexCoord1i(1); glVertex3i(1, 1, 0);
412 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
413 glDisable(GL_TEXTURE_1D);
416 glEnable(GL_DEPTH_TEST);
417 glEnable(GL_CULL_FACE);
418 glEnable(GL_LIGHTING);
420 glEnable(GL_TEXTURE_2D);
423 check_gl_error ("gradient list");
426 glCallList(gradient_list);
432 Animate(atlantisstruct * ap)
436 for (i = 0; i < ap->num_sharks; i++) {
437 SharkPilot(&(ap->sharks[i]), ap->sharkspeed);
440 WhalePilot(&(ap->dolph), ap->whalespeed, ap->whaledir);
442 WhalePilot(&(ap->momWhale), ap->whalespeed, ap->whaledir);
444 WhalePilot(&(ap->babyWhale), ap->whalespeed, ap->whaledir);
449 AllDisplay(atlantisstruct * ap)
455 for (i = 0; i < ap->num_sharks; i++) {
457 FishTransform(&(ap->sharks[i]));
458 DrawShark(&(ap->sharks[i]), ap->wire);
463 FishTransform(&(ap->dolph));
464 DrawDolphin(&(ap->dolph), ap->wire);
468 FishTransform(&(ap->momWhale));
469 DrawWhale(&(ap->momWhale), ap->wire);
473 FishTransform(&(ap->babyWhale));
474 glScalef(0.45, 0.45, 0.3);
475 DrawWhale(&(ap->babyWhale), ap->wire);
480 *-----------------------------------------------------------------------------
481 *-----------------------------------------------------------------------------
483 *-----------------------------------------------------------------------------
484 *-----------------------------------------------------------------------------
488 *-----------------------------------------------------------------------------
489 * Initialize atlantis. Called each time the window changes.
490 *-----------------------------------------------------------------------------
494 init_atlantis(ModeInfo * mi)
496 int screen = MI_SCREEN(mi);
498 Display *display = MI_DISPLAY(mi);
499 Window window = MI_WINDOW(mi);
501 if (atlantis == NULL) {
502 if ((atlantis = (atlantisstruct *) calloc(MI_NUM_SCREENS(mi),
503 sizeof (atlantisstruct))) == NULL)
506 ap = &atlantis[screen];
507 ap->num_sharks = MI_COUNT(mi);
508 if (ap->sharks == NULL) {
509 if ((ap->sharks = (fishRec *) calloc(ap->num_sharks,
510 sizeof (fishRec))) == NULL) {
511 /* free everything up to now */
512 (void) free((void *) atlantis);
517 ap->sharkspeed = MI_CYCLES(mi); /* has influence on the "width"
519 ap->sharksize = MI_SIZE(mi); /* has influence on the "distance"
521 ap->whalespeed = whalespeed;
522 ap->wire = MI_IS_WIREFRAME(mi);
524 if (MI_IS_DEBUG(mi)) {
525 (void) fprintf(stderr,
526 "%s:\n\tnum_sharks=%d\n\tsharkspeed=%.1f\n\tsharksize=%d\n\twhalespeed=%.1f\n\twireframe=%s\n",
532 ap->wire ? "yes" : "no"
535 if ((ap->glx_context = init_GL(mi)) != NULL) {
537 reshape_atlantis(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
538 glDrawBuffer(GL_BACK);
541 glXSwapBuffers(display, window);
549 *-----------------------------------------------------------------------------
550 * Called by the mainline code periodically to update the display.
551 *-----------------------------------------------------------------------------
554 draw_atlantis(ModeInfo * mi)
556 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
558 Display *display = MI_DISPLAY(mi);
559 Window window = MI_WINDOW(mi);
561 MI_IS_DRAWN(mi) = True;
563 if (!ap->glx_context)
566 glXMakeCurrent(display, window, *(ap->glx_context));
568 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
573 if (mi->fps_p) do_fps (mi);
574 glXSwapBuffers(display, window);
579 *-----------------------------------------------------------------------------
580 * The display is being taken away from us. Free up malloc'ed
581 * memory and X resources that we've alloc'ed. Only called
582 * once, we must zap everything for every screen.
583 *-----------------------------------------------------------------------------
587 release_atlantis(ModeInfo * mi)
591 if (atlantis != NULL) {
592 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
593 atlantisstruct *ap = &atlantis[screen];
596 (void) free((void *) ap->sharks);
598 (void) free((void *) atlantis);
605 refresh_atlantis(ModeInfo * mi)
610 change_atlantis(ModeInfo * mi)
612 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
614 if (!ap->glx_context)
617 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ap->glx_context));