1 /* atlantis --- Shows moving 3D sea animals */
3 #if !defined( lint ) && !defined( SABER )
4 static const char sccsid[] = "@(#)atlantis.c 1.1 98/05/13 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)
37 * TODO : - use def_xxx options to initialize local variables (number of
39 * - test standalone mode
40 * - better colormap handling (problems somewhere ?)
44 /* Copyright (c) Mark J. Kilgard, 1994. */
47 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
49 * Permission to use, copy, modify, and distribute this software for
50 * any purpose and without fee is hereby granted, provided that the above
51 * copyright notice appear in all copies and that both the copyright notice
52 * and this permission notice appear in supporting documentation, and that
53 * the name of Silicon Graphics, Inc. not be used in advertising
54 * or publicity pertaining to distribution of the software without specific,
55 * written prior permission.
57 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
58 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
59 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
60 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
61 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
62 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
63 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
64 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
65 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
66 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
67 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
68 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
70 * US Government Users Restricted Rights
71 * Use, duplication, or disclosure by the Government is subject to
72 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
73 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
74 * clause at DFARS 252.227-7013 and/or in similar or successor
75 * clauses in the FAR or the DOD or NASA FAR Supplement.
76 * Unpublished-- rights reserved under the copyright laws of the
77 * United States. Contractor/manufacturer is Silicon Graphics,
78 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
80 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
84 # define PROGCLASS "Atlantis"
85 # define HACK_INIT init_atlantis
86 # define HACK_DRAW draw_atlantis
87 # define atlantis_opts xlockmore_opts
88 # define DEFAULTS "*delay: 1000 \n"
89 # include "xlockmore.h" /* from the xscreensaver distribution */
90 #else /* !STANDALONE */
91 # include "xlock.h" /* from the xlockmore distribution */
93 #endif /* !STANDALONE */
101 #include "atlantis.h"
105 ModeSpecOpt atlantis_opts =
106 {0, NULL, 0, NULL, NULL};
109 ModStruct atlantis_description =
110 {"atlantis", "init_atlantis", "draw_atlantis", "release_atlantis",
111 "refresh_atlantis", "change_atlantis", NULL, &atlantis_opts,
112 1000, 2, 5, 500, 64, 1.0, "",
113 "Shows moving sharks/whales/dolphin", 0, NULL};
117 static atlantisstruct *atlantis = NULL;
120 InitFishs(atlantisstruct *ap)
124 for (i = 0; i < NUM_SHARKS; i++) {
125 ap->sharks[i].x = 70000.0 + NRAND(6000);
126 ap->sharks[i].y = NRAND(6000);
127 ap->sharks[i].z = NRAND(6000);
128 ap->sharks[i].psi = NRAND(360) - 180.0;
129 ap->sharks[i].v = 1.0;
132 /* i = LRAND() & 1; */ /* Works the first time but they do not return */
133 ap->dolph.x = 30000.0;
135 ap->dolph.z = 6000.0;
136 ap->dolph.psi = /* (i) ? 90.0 : - */ 90.0;
137 ap->dolph.theta = 0.0;
140 ap->momWhale.x = 70000.0;
141 ap->momWhale.y = 0.0;
142 ap->momWhale.z = 0.0;
143 ap->momWhale.psi = /* (i) ? 90.0 : - */ 90.0;
144 ap->momWhale.theta = 0.0;
145 ap->momWhale.v = 3.0;
147 ap->babyWhale.x = 60000.0;
148 ap->babyWhale.y = -2000.0;
149 ap->babyWhale.z = -2000.0;
150 ap->babyWhale.psi = /* (i) ? 90.0 : - */ 90.0;
151 ap->babyWhale.theta = 0.0;
152 ap->babyWhale.v = 3.0;
156 Init(atlantisstruct *ap)
158 static float ambient[] =
159 {0.1, 0.1, 0.1, 1.0};
160 static float diffuse[] =
161 {1.0, 1.0, 1.0, 1.0};
162 static float position[] =
163 {0.0, 1.0, 0.0, 0.0};
164 static float mat_shininess[] =
166 static float mat_specular[] =
167 {0.8, 0.8, 0.8, 1.0};
168 static float mat_diffuse[] =
169 {0.46, 0.66, 0.795, 1.0};
170 static float mat_ambient[] =
171 {0.0, 0.1, 0.2, 1.0};
172 static float lmodel_ambient[] =
173 {0.4, 0.4, 0.4, 1.0};
174 static float lmodel_localviewer[] =
178 {0.0, 0.0, 0.0, 0.0};
180 {0.0, 0.0, 0.0, 0.0};
185 glDepthFunc(GL_LEQUAL);
186 glEnable(GL_DEPTH_TEST);
188 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
189 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
190 glLightfv(GL_LIGHT0, GL_POSITION, position);
191 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
192 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
193 glEnable(GL_LIGHTING);
196 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
197 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
198 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
199 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
203 glClearColor(0.0, 0.5, 0.9, 0.0);
207 Reshape(ModeInfo *mi,int width, int height)
209 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
211 glViewport(0, 0, ap->WinW= (GLint) width, ap->WinH= (GLint) height);
213 glMatrixMode(GL_PROJECTION);
215 gluPerspective(400.0, (GLdouble)width/(GLdouble)height, 1.0, 2000000.0);
216 glMatrixMode(GL_MODELVIEW);
220 Animate(atlantisstruct *ap)
224 for (i = 0; i < NUM_SHARKS; i++) {
225 SharkPilot(&(ap->sharks[i]));
228 WhalePilot(&(ap->dolph));
230 WhalePilot(&(ap->momWhale));
232 WhalePilot(&(ap->babyWhale));
237 AllDisplay(atlantisstruct *ap)
241 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
243 for (i = 0; i < NUM_SHARKS; i++) {
245 FishTransform(&(ap->sharks[i]));
246 DrawShark(&(ap->sharks[i]));
251 FishTransform(&(ap->dolph));
252 DrawDolphin(&(ap->dolph));
256 FishTransform(&(ap->momWhale));
257 DrawWhale(&(ap->momWhale));
261 FishTransform(&(ap->babyWhale));
262 glScalef(0.45, 0.45, 0.3);
263 DrawWhale(&(ap->babyWhale));
268 *-----------------------------------------------------------------------------
269 *-----------------------------------------------------------------------------
271 *-----------------------------------------------------------------------------
272 *-----------------------------------------------------------------------------
276 *-----------------------------------------------------------------------------
277 * Initialize atlantis. Called each time the window changes.
278 *-----------------------------------------------------------------------------
282 init_atlantis(ModeInfo * mi)
284 int screen = MI_SCREEN(mi);
286 Display *display = MI_DISPLAY(mi);
287 Window window = MI_WINDOW(mi);
289 if (atlantis == NULL) {
290 if ((atlantis = (atlantisstruct *) calloc(MI_NUM_SCREENS(mi),
291 sizeof (atlantisstruct))) == NULL)
294 ap = &atlantis[screen];
296 if ((ap->glx_context = init_GL(mi)) != NULL) {
298 Reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
299 glDrawBuffer(GL_BACK);
302 glXSwapBuffers(display, window);
310 *-----------------------------------------------------------------------------
311 * Called by the mainline code periodically to update the display.
312 *-----------------------------------------------------------------------------
315 draw_atlantis(ModeInfo * mi)
317 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
319 Display *display = MI_DISPLAY(mi);
320 Window window = MI_WINDOW(mi);
322 if (!ap->glx_context)
325 glXMakeCurrent(display, window, *(ap->glx_context));
327 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
334 glXSwapBuffers(display, window);
339 *-----------------------------------------------------------------------------
340 * The display is being taken away from us. Free up malloc'ed
341 * memory and X resources that we've alloc'ed. Only called
342 * once, we must zap everything for every screen.
343 *-----------------------------------------------------------------------------
347 release_atlantis(ModeInfo * mi)
349 if (atlantis != NULL) {
350 (void) free((void *) atlantis);
357 *-----------------------------------------------------------------------------
358 * Called when the mainline xlock code notices possible window
359 * damage. This hook should take steps to repaint the entire
360 * window (no specific damage area information is provided).
361 *-----------------------------------------------------------------------------
365 refresh_atlantis(ModeInfo * mi)
368 Reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
372 change_atlantis(ModeInfo * mi)
374 atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
376 if (!ap->glx_context)
379 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ap->glx_context));