1 /* atlantis --- Shows moving 3D sea animals */
4 static const char sccsid[] = "@(#)atlantis.c 5.08 2003/04/09 xlockmore";
7 /* Copyright (c) E. Lassauge, 1998. */
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
22 * The original code for this mode was written by Mark J. Kilgard
23 * as a demo for openGL programming.
25 * Porting it to xlock was possible by comparing the original Mesa's morph3d
26 * demo with it's ported version to xlock, so thanks for Marcelo F. Vianna
27 * (look at morph3d.c) for his indirect help.
29 * Thanks goes also to Brian Paul for making it possible and inexpensive
30 * to use OpenGL at home.
32 * My e-mail address is lassauge@users.sourceforge.net
34 * Eric Lassauge (May-13-1998)
38 * Jamie Zawinski, 2-Apr-01: - The fishies were inside out! The back faces
39 * were being drawn, not the front faces.
40 * - Added a texture to simulate light from the
41 * surface, like in the SGI version.
43 * David A. Bagley - 98/06/17 : Add whalespeed option. Global options to
44 * initialize local variables are now:
45 * XLock.atlantis.cycles: 100 ! SharkSpeed
46 * XLock.atlantis.batchcount: 4 ! SharkNum
47 * XLock.atlantis.whalespeed: 250 ! WhaleSpeed
48 * XLock.atlantis.size: 6000 ! SharkSize
49 * Add random direction for whales/dolphins
51 * E.Lassauge - 98/06/16: Use the following global options to initialize
53 * XLock.atlantis.delay: 100 ! SharkSpeed
54 * XLock.atlantis.batchcount: 4 ! SharkNum
55 * XLock.atlantis.cycles: 250 ! WhaleSpeed
56 * XLock.atlantis.size: 6000 ! SharkSize
57 * Add support for -/+ wireframe (t'was so easy to do!)
60 * - better handling of sizes and speeds
61 * - test standalone and module modes
65 /* Copyright (c) Mark J. Kilgard, 1994. */
68 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
70 * Permission to use, copy, modify, and distribute this software for
71 * any purpose and without fee is hereby granted, provided that the above
72 * copyright notice appear in all copies and that both the copyright notice
73 * and this permission notice appear in supporting documentation, and that
74 * the name of Silicon Graphics, Inc. not be used in advertising
75 * or publicity pertaining to distribution of the software without specific,
76 * written prior permission.
78 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
79 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
80 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
81 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
82 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
83 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
84 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
85 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
86 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
87 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
88 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
89 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
91 * US Government Users Restricted Rights
92 * Use, duplication, or disclosure by the Government is subject to
93 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
94 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
95 * clause at DFARS 252.227-7013 and/or in similar or successor
96 * clauses in the FAR or the DOD or NASA FAR Supplement.
97 * Unpublished-- rights reserved under the copyright laws of the
98 * United States. Contractor/manufacturer is Silicon Graphics,
99 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
101 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
104 #define DEF_TEXTURE "True"
105 #define DEF_GRADIENT "False"
106 #define DEF_WHALESPEED "250"
109 # define PROGCLASS "Atlantis"
110 # define HACK_INIT init_atlantis
111 # define HACK_DRAW draw_atlantis
112 # define HACK_RESHAPE reshape_atlantis
113 # define atlantis_opts xlockmore_opts
114 # define DEFAULTS "*delay: 25000 \n" \
116 "*showFPS: False \n" \
119 "*wireframe: False \n" \
121 # include "xlockmore.h" /* from the xscreensaver distribution */
122 #else /* !STANDALONE */
123 # include "xlock.h" /* from the xlockmore distribution */
125 #endif /* !STANDALONE */
129 #include "atlantis.h"
133 static int whalespeed;
134 static int do_texture;
135 static int do_gradient;
136 static XrmOptionDescRec opts[] =
138 {"-whalespeed", ".atlantis.whalespeed", XrmoptionSepArg, 0},
139 {"-texture", ".atlantis.texture", XrmoptionNoArg, "true"},
140 {"+texture", ".atlantis.texture", XrmoptionNoArg, "false"},
141 {"-gradient", ".atlantis.gradient", XrmoptionNoArg, "true"},
142 {"+gradient", ".atlantis.gradient", XrmoptionNoArg, "false"},
145 static argtype vars[] =
147 {&whalespeed, "whalespeed", "WhaleSpeed", DEF_WHALESPEED, t_Int},
148 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
149 {&do_gradient, "gradient", "Gradient", DEF_GRADIENT, t_Bool},
152 static OptionStruct desc[] =
154 {"-whalespeed num", "speed of whales and the dolphin"},
155 {"-texture", "whether to introduce water-like distortion"},
156 {"-gradient", "whether to introduce gradient-filled background"},
159 ModeSpecOpt atlantis_opts =
160 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
163 ModStruct atlantis_description =
164 {"atlantis", "init_atlantis", "draw_atlantis", "release_atlantis",
165 "refresh_atlantis", "change_atlantis", NULL, &atlantis_opts,
166 1000, NUM_SHARKS, SHARKSPEED, SHARKSIZE, 64, 1.0, "",
167 "Shows moving sharks/whales/dolphin", 0, NULL};
171 static atlantisstruct *atlantis = NULL;
173 #include "xpm-ximage.h"
175 #include "../images/sea-texture.xpm"
179 parse_image_data(ModeInfo *mi)
181 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
182 ap->texture = xpm_to_ximage (mi->dpy,
189 InitFishs(atlantisstruct * ap)
193 for (i = 0; i < ap->num_sharks; i++) {
194 ap->sharks[i].x = 70000.0 + NRAND(ap->sharksize);
195 ap->sharks[i].y = NRAND(ap->sharksize);
196 ap->sharks[i].z = NRAND(ap->sharksize);
197 ap->sharks[i].psi = NRAND(360) - 180.0;
198 ap->sharks[i].v = 1.0;
201 /* Random whale direction */
202 ap->whaledir = LRAND() & 1;
204 ap->dolph.x = 30000.0;
206 ap->dolph.z = (float) (ap->sharksize);
207 ap->dolph.psi = (ap->whaledir) ? 90.0 : -90.0;
208 ap->dolph.theta = 0.0;
211 ap->momWhale.x = 70000.0;
212 ap->momWhale.y = 0.0;
213 ap->momWhale.z = 0.0;
214 ap->momWhale.psi = (ap->whaledir) ? 90.0 : -90.0;
215 ap->momWhale.theta = 0.0;
216 ap->momWhale.v = 3.0;
218 ap->babyWhale.x = 60000.0;
219 ap->babyWhale.y = -2000.0;
220 ap->babyWhale.z = -2000.0;
221 ap->babyWhale.psi = (ap->whaledir) ? 90.0 : -90.0;
222 ap->babyWhale.theta = 0.0;
223 ap->babyWhale.v = 3.0;
229 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
231 static float ambient[] =
232 {0.1, 0.1, 0.1, 1.0};
233 static float diffuse[] =
234 {1.0, 1.0, 1.0, 1.0};
235 static float position[] =
236 {0.0, 1.0, 0.0, 0.0};
237 static float mat_shininess[] =
239 static float mat_specular[] =
240 {0.8, 0.8, 0.8, 1.0};
241 static float mat_diffuse[] =
242 {0.46, 0.66, 0.795, 1.0};
243 static float mat_ambient[] =
244 {0.0, 0.1, 0.2, 1.0};
245 static float lmodel_ambient[] =
246 {0.4, 0.4, 0.4, 1.0};
247 static float lmodel_localviewer[] =
249 float fblue = 0.0, fgreen;
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 /* Add a little randomness */
327 fblue = ((float) (NRAND(30)) / 100.0) + 0.70;
328 fgreen = fblue * 0.56;
329 glClearColor(0.0, fgreen, fblue, 0.0);
333 reshape_atlantis(ModeInfo * mi, int width, int height)
335 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
337 glViewport(0, 0, ap->WinW = (GLint) width, ap->WinH = (GLint) height);
339 glMatrixMode(GL_PROJECTION);
341 gluPerspective(400.0, (GLdouble) width / (GLdouble) height, 1.0, 2000000.0);
342 glMatrixMode(GL_MODELVIEW);
346 /* Fill the background with a gradient -- thanks to
347 Phil Carrig <pod@internode.on.net> for figuring out
348 how to do this more efficiently!
351 clear_tank (atlantisstruct * ap)
353 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
355 if (do_gradient && !ap->wire)
357 GLfloat top[3] = { 0.00, 0.40, 0.70 };
358 GLfloat bot[3] = { 0.00, 0.05, 0.18 };
360 glMatrixMode(GL_PROJECTION);
364 glMatrixMode(GL_MODELVIEW);
369 /* save GL_COLOR_MATERIAL, GL_COLOR_MATERIAL_FACE, etc.
370 This stalls the pipeline, so it would be better to do this
371 with explicit enable/disable calls, but I can't figure
372 out how to undo the glEnable() and glColor() calls below!
373 Simply calling glDisable(GL_COLOR_MATERIAL) is insufficient!
375 glPushAttrib (GL_LIGHTING_BIT);
377 glEnable (GL_COLOR_MATERIAL);
379 glShadeModel(GL_SMOOTH);
381 glColor3f (bot[0], bot[1], bot[2]); glVertex3f (-1, -1, 1);
382 glColor3f (bot[0], bot[1], bot[2]); glVertex3f ( 1, -1, 1);
383 glColor3f (top[0], top[1], top[2]); glVertex3f ( 1, 1, 1);
384 glColor3f (top[0], top[1], top[2]); glVertex3f (-1, 1, 1);
391 glMatrixMode(GL_PROJECTION);
394 glMatrixMode(GL_MODELVIEW);
400 Animate(atlantisstruct * ap)
404 for (i = 0; i < ap->num_sharks; i++) {
405 SharkPilot(&(ap->sharks[i]), ap->sharkspeed);
408 WhalePilot(&(ap->dolph), ap->whalespeed, ap->whaledir);
410 WhalePilot(&(ap->momWhale), ap->whalespeed, ap->whaledir);
412 WhalePilot(&(ap->babyWhale), ap->whalespeed, ap->whaledir);
417 AllDisplay(atlantisstruct * ap)
423 for (i = 0; i < ap->num_sharks; i++) {
425 FishTransform(&(ap->sharks[i]));
426 DrawShark(&(ap->sharks[i]), ap->wire);
431 FishTransform(&(ap->dolph));
432 DrawDolphin(&(ap->dolph), ap->wire);
436 FishTransform(&(ap->momWhale));
437 DrawWhale(&(ap->momWhale), ap->wire);
441 FishTransform(&(ap->babyWhale));
442 glScalef(0.45, 0.45, 0.3);
443 DrawWhale(&(ap->babyWhale), ap->wire);
448 *-----------------------------------------------------------------------------
449 *-----------------------------------------------------------------------------
451 *-----------------------------------------------------------------------------
452 *-----------------------------------------------------------------------------
456 *-----------------------------------------------------------------------------
457 * Initialize atlantis. Called each time the window changes.
458 *-----------------------------------------------------------------------------
462 init_atlantis(ModeInfo * mi)
464 int screen = MI_SCREEN(mi);
466 Display *display = MI_DISPLAY(mi);
467 Window window = MI_WINDOW(mi);
469 if (atlantis == NULL) {
470 if ((atlantis = (atlantisstruct *) calloc(MI_NUM_SCREENS(mi),
471 sizeof (atlantisstruct))) == NULL)
474 ap = &atlantis[screen];
475 ap->num_sharks = MI_COUNT(mi);
476 if (ap->sharks == NULL) {
477 if ((ap->sharks = (fishRec *) calloc(ap->num_sharks,
478 sizeof (fishRec))) == NULL) {
479 /* free everything up to now */
480 (void) free((void *) atlantis);
485 ap->sharkspeed = MI_CYCLES(mi); /* has influence on the "width"
487 ap->sharksize = MI_SIZE(mi); /* has influence on the "distance"
489 ap->whalespeed = whalespeed;
490 ap->wire = MI_IS_WIREFRAME(mi);
492 if (MI_IS_DEBUG(mi)) {
493 (void) fprintf(stderr,
494 "%s:\n\tnum_sharks=%d\n\tsharkspeed=%.1f\n\tsharksize=%d\n\twhalespeed=%.1f\n\twireframe=%s\n",
500 ap->wire ? "yes" : "no"
503 if ((ap->glx_context = init_GL(mi)) != NULL) {
505 reshape_atlantis(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
506 glDrawBuffer(GL_BACK);
509 glXSwapBuffers(display, window);
517 *-----------------------------------------------------------------------------
518 * Called by the mainline code periodically to update the display.
519 *-----------------------------------------------------------------------------
522 draw_atlantis(ModeInfo * mi)
524 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
526 Display *display = MI_DISPLAY(mi);
527 Window window = MI_WINDOW(mi);
529 MI_IS_DRAWN(mi) = True;
531 if (!ap->glx_context)
534 glXMakeCurrent(display, window, *(ap->glx_context));
536 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
541 if (mi->fps_p) do_fps (mi);
542 glXSwapBuffers(display, window);
547 *-----------------------------------------------------------------------------
548 * The display is being taken away from us. Free up malloc'ed
549 * memory and X resources that we've alloc'ed. Only called
550 * once, we must zap everything for every screen.
551 *-----------------------------------------------------------------------------
555 release_atlantis(ModeInfo * mi)
559 if (atlantis != NULL) {
560 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
561 atlantisstruct *ap = &atlantis[screen];
564 (void) free((void *) ap->sharks);
566 (void) free((void *) atlantis);
573 refresh_atlantis(ModeInfo * mi)
578 change_atlantis(ModeInfo * mi)
580 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
582 if (!ap->glx_context)
585 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ap->glx_context));