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