518b43c979153df645dcc4cc221cc6f410ce0cd4
[xscreensaver] / hacks / glx / swim.c
1 /* atlantis --- Shows moving 3D sea animals */
2
3 #if !defined( lint ) && !defined( SABER )
4 static const char sccsid[] = "@(#)whale.c       1.1 98/05/13 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  */
38
39 /**
40  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
41  * ALL RIGHTS RESERVED
42  * Permission to use, copy, modify, and distribute this software for
43  * any purpose and without fee is hereby granted, provided that the above
44  * copyright notice appear in all copies and that both the copyright notice
45  * and this permission notice appear in supporting documentation, and that
46  * the name of Silicon Graphics, Inc. not be used in advertising
47  * or publicity pertaining to distribution of the software without specific,
48  * written prior permission.
49  *
50  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
51  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
52  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
53  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
54  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
55  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
56  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
57  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
58  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
59  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
60  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
61  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
62  *
63  * US Government Users Restricted Rights
64  * Use, duplication, or disclosure by the Government is subject to
65  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
66  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
67  * clause at DFARS 252.227-7013 and/or in similar or successor
68  * clauses in the FAR or the DOD or NASA FAR Supplement.
69  * Unpublished-- rights reserved under the copyright laws of the
70  * United States.  Contractor/manufacturer is Silicon Graphics,
71  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
72  *
73  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
74  */
75
76 #include <math.h>
77
78 #ifdef STANDALONE
79 # include "xlockmoreI.h"        /* from the xscreensaver distribution */
80 #else /* !STANDALONE */
81 # include "xlock.h"            /* from the xlockmore distribution */
82 #endif /* !STANDALONE */
83
84 #ifdef USE_GL
85
86 #include "atlantis.h"
87 #include <stdlib.h>
88
89 void
90 FishTransform(fishRec * fish)
91 {
92
93     glTranslatef(fish->y, fish->z, -fish->x);
94     glRotatef(-fish->psi, 0.0, 1.0, 0.0);
95     glRotatef(fish->theta, 1.0, 0.0, 0.0);
96     glRotatef(-fish->phi, 0.0, 0.0, 1.0);
97 }
98
99 void
100 WhalePilot(fishRec * fish)
101 {
102
103     fish->phi = -20.0;
104     fish->theta = 0.0;
105     fish->psi -= 0.5;
106
107     fish->x += WHALESPEED * fish->v * cos(fish->psi / RAD) * cos(fish->theta / RAD);
108     fish->y += WHALESPEED * fish->v * sin(fish->psi / RAD) * cos(fish->theta / RAD);
109     fish->z += WHALESPEED * fish->v * sin(fish->theta / RAD);
110 }
111
112 void
113 SharkPilot(fishRec * fish)
114 {
115     static int sign = 1;
116     float X, Y, Z, tpsi, ttheta, thetal;
117
118     fish->xt = 60000.0;
119     fish->yt = 0.0;
120     fish->zt = 0.0;
121
122     X = fish->xt - fish->x;
123     Y = fish->yt - fish->y;
124     Z = fish->zt - fish->z;
125
126     thetal = fish->theta;
127
128     ttheta = RAD * atan(Z / (sqrt(X * X + Y * Y)));
129
130     if (ttheta > fish->theta + 0.25) {
131         fish->theta += 0.5;
132     } else if (ttheta < fish->theta - 0.25) {
133         fish->theta -= 0.5;
134     }
135     if (fish->theta > 90.0) {
136         fish->theta = 90.0;
137     }
138     if (fish->theta < -90.0) {
139         fish->theta = -90.0;
140     }
141     fish->dtheta = fish->theta - thetal;
142
143     tpsi = RAD * atan2(Y, X);
144
145     fish->attack = 0;
146
147     if (fabs(tpsi - fish->psi) < 10.0) {
148         fish->attack = 1;
149     } else if (fabs(tpsi - fish->psi) < 45.0) {
150         if (fish->psi > tpsi) {
151             fish->psi -= 0.5;
152             if (fish->psi < -180.0) {
153                 fish->psi += 360.0;
154             }
155         } else if (fish->psi < tpsi) {
156             fish->psi += 0.5;
157             if (fish->psi > 180.0) {
158                 fish->psi -= 360.0;
159             }
160         }
161     } else {
162         if (NRAND(100) > 98) {
163             sign = 1 - sign;
164         }
165         fish->psi += sign;
166         if (fish->psi > 180.0) {
167             fish->psi -= 360.0;
168         }
169         if (fish->psi < -180.0) {
170             fish->psi += 360.0;
171         }
172     }
173
174     if (fish->attack) {
175         if (fish->v < 1.1) {
176             fish->spurt = 1;
177         }
178         if (fish->spurt) {
179             fish->v += 0.2;
180         }
181         if (fish->v > 5.0) {
182             fish->spurt = 0;
183         }
184         if ((fish->v > 1.0) && (!fish->spurt)) {
185             fish->v -= 0.2;
186         }
187     } else {
188         if (!(NRAND(400)) && (!fish->spurt)) {
189             fish->spurt = 1;
190         }
191         if (fish->spurt) {
192             fish->v += 0.05;
193         }
194         if (fish->v > 3.0) {
195             fish->spurt = 0;
196         }
197         if ((fish->v > 1.0) && (!fish->spurt)) {
198             fish->v -= 0.05;
199         }
200     }
201
202     fish->x += SHARKSPEED * fish->v * cos(fish->psi / RAD) * cos(fish->theta / RAD);
203     fish->y += SHARKSPEED * fish->v * sin(fish->psi / RAD) * cos(fish->theta / RAD);
204     fish->z += SHARKSPEED * fish->v * sin(fish->theta / RAD);
205 }
206
207 void
208 SharkMiss(atlantisstruct *ap, int i)
209 {
210     int j;
211     float avoid, thetal;
212     float X, Y, Z, R;
213
214     for (j = 0; j < NUM_SHARKS; j++) {
215         if (j != i) {
216             X = ap->sharks[j].x - ap->sharks[i].x;
217             Y = ap->sharks[j].y - ap->sharks[i].y;
218             Z = ap->sharks[j].z - ap->sharks[i].z;
219
220             R = sqrt(X * X + Y * Y + Z * Z);
221
222             avoid = 1.0;
223             thetal = ap->sharks[i].theta;
224
225             if (R < SHARKSIZE) {
226                 if (Z > 0.0) {
227                     ap->sharks[i].theta -= avoid;
228                 } else {
229                     ap->sharks[i].theta += avoid;
230                 }
231             }
232             ap->sharks[i].dtheta += (ap->sharks[i].theta - thetal);
233         }
234     }
235 }
236 #endif