ftp://ftp.smr.ru/pub/0/FreeBSD/releases/distfiles/xscreensaver-3.16.tar.gz
[xscreensaver] / hacks / glx / swim.c
1 /* atlantis --- Shows moving 3D sea animals */
2
3 #if !defined( lint ) && !defined( SABER )
4 static const char sccsid[] = "@(#)swim.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  */
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 #ifdef STANDALONE
77 # include <math.h>
78 # include "xlockmoreI.h"        /* from the xscreensaver distribution */
79 #else /* !STANDALONE */
80 # include "xlock.h"            /* from the xlockmore distribution */
81 #endif /* !STANDALONE */
82
83 #ifdef USE_GL
84
85 #include "atlantis.h"
86
87 void
88 FishTransform(fishRec * fish)
89 {
90
91         glTranslatef(fish->y, fish->z, -fish->x);
92         glRotatef(-fish->psi, 0.0, 1.0, 0.0);
93         glRotatef(fish->theta, 1.0, 0.0, 0.0);
94         glRotatef(-fish->phi, 0.0, 0.0, 1.0);
95 }
96
97 void
98 WhalePilot(fishRec * fish, float whalespeed, Bool whaledir)
99 {
100
101         fish->phi = -20.0;
102         fish->theta = 0.0;
103         fish->psi += ((whaledir) ? -0.5 : 0.5);
104
105         fish->x += whalespeed * fish->v * cos(fish->psi / RAD) * cos(fish->theta / RAD);
106         fish->y += whalespeed * fish->v * sin(fish->psi / RAD) * cos(fish->theta / RAD);
107         fish->z += whalespeed * fish->v * sin(fish->theta / RAD);
108 }
109
110 void
111 SharkPilot(fishRec * fish, float sharkspeed)
112 {
113         static int  sign = 1;
114         float       X, Y, Z, tpsi, ttheta, thetal;
115
116         fish->xt = 60000.0;
117         fish->yt = 0.0;
118         fish->zt = 0.0;
119
120         X = fish->xt - fish->x;
121         Y = fish->yt - fish->y;
122         Z = fish->zt - fish->z;
123
124         thetal = fish->theta;
125
126         ttheta = RAD * atan(Z / (sqrt(X * X + Y * Y)));
127
128         if (ttheta > fish->theta + 0.25) {
129                 fish->theta += 0.5;
130         } else if (ttheta < fish->theta - 0.25) {
131                 fish->theta -= 0.5;
132         }
133         if (fish->theta > 90.0) {
134                 fish->theta = 90.0;
135         }
136         if (fish->theta < -90.0) {
137                 fish->theta = -90.0;
138         }
139         fish->dtheta = fish->theta - thetal;
140
141         tpsi = RAD * atan2(Y, X);
142
143         fish->attack = 0;
144
145         if (fabs(tpsi - fish->psi) < 10.0) {
146                 fish->attack = 1;
147         } else if (fabs(tpsi - fish->psi) < 45.0) {
148                 if (fish->psi > tpsi) {
149                         fish->psi -= 0.5;
150                         if (fish->psi < -180.0) {
151                                 fish->psi += 360.0;
152                         }
153                 } else if (fish->psi < tpsi) {
154                         fish->psi += 0.5;
155                         if (fish->psi > 180.0) {
156                                 fish->psi -= 360.0;
157                         }
158                 }
159         } else {
160                 if (NRAND(100) > 98) {
161                         sign = 1 - sign;
162                 }
163                 fish->psi += sign;
164                 if (fish->psi > 180.0) {
165                         fish->psi -= 360.0;
166                 }
167                 if (fish->psi < -180.0) {
168                         fish->psi += 360.0;
169                 }
170         }
171
172         if (fish->attack) {
173                 if (fish->v < 1.1) {
174                         fish->spurt = 1;
175                 }
176                 if (fish->spurt) {
177                         fish->v += 0.2;
178                 }
179                 if (fish->v > 5.0) {
180                         fish->spurt = 0;
181                 }
182                 if ((fish->v > 1.0) && (!fish->spurt)) {
183                         fish->v -= 0.2;
184                 }
185         } else {
186                 if (!(NRAND(400)) && (!fish->spurt)) {
187                         fish->spurt = 1;
188                 }
189                 if (fish->spurt) {
190                         fish->v += 0.05;
191                 }
192                 if (fish->v > 3.0) {
193                         fish->spurt = 0;
194                 }
195                 if ((fish->v > 1.0) && (!fish->spurt)) {
196                         fish->v -= 0.05;
197                 }
198         }
199
200         fish->x += sharkspeed * fish->v * cos(fish->psi / RAD) * cos(fish->theta / RAD);
201         fish->y += sharkspeed * fish->v * sin(fish->psi / RAD) * cos(fish->theta / RAD);
202         fish->z += sharkspeed * fish->v * sin(fish->theta / RAD);
203 }
204
205 void
206 SharkMiss(atlantisstruct * ap, int i)
207 {
208         int         j;
209         float       avoid, thetal;
210         float       X, Y, Z, R;
211
212         for (j = 0; j < ap->num_sharks; j++) {
213                 if (j != i) {
214                         X = ap->sharks[j].x - ap->sharks[i].x;
215                         Y = ap->sharks[j].y - ap->sharks[i].y;
216                         Z = ap->sharks[j].z - ap->sharks[i].z;
217
218                         R = sqrt(X * X + Y * Y + Z * Z);
219
220                         avoid = 1.0;
221                         thetal = ap->sharks[i].theta;
222
223                         if (R < ap->sharksize) {
224                                 if (Z > 0.0) {
225                                         ap->sharks[i].theta -= avoid;
226                                 } else {
227                                         ap->sharks[i].theta += avoid;
228                                 }
229                         }
230                         ap->sharks[i].dtheta += (ap->sharks[i].theta - thetal);
231                 }
232         }
233 }
234 #endif