began for maemo
[xscreensaver] / 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         float       X, Y, Z, tpsi, ttheta, thetal;
113
114         fish->xt = 60000.0;
115         fish->yt = 0.0;
116         fish->zt = 0.0;
117
118         X = fish->xt - fish->x;
119         Y = fish->yt - fish->y;
120         Z = fish->zt - fish->z;
121
122         thetal = fish->theta;
123
124         ttheta = RAD * atan(Z / (sqrt(X * X + Y * Y)));
125
126         if (ttheta > fish->theta + 0.25) {
127                 fish->theta += 0.5;
128         } else if (ttheta < fish->theta - 0.25) {
129                 fish->theta -= 0.5;
130         }
131         if (fish->theta > 90.0) {
132                 fish->theta = 90.0;
133         }
134         if (fish->theta < -90.0) {
135                 fish->theta = -90.0;
136         }
137         fish->dtheta = fish->theta - thetal;
138
139         tpsi = RAD * atan2(Y, X);
140
141         fish->attack = 0;
142
143         if (fabs(tpsi - fish->psi) < 10.0) {
144                 fish->attack = 1;
145         } else if (fabs(tpsi - fish->psi) < 45.0) {
146                 if (fish->psi > tpsi) {
147                         fish->psi -= 0.5;
148                         if (fish->psi < -180.0) {
149                                 fish->psi += 360.0;
150                         }
151                 } else if (fish->psi < tpsi) {
152                         fish->psi += 0.5;
153                         if (fish->psi > 180.0) {
154                                 fish->psi -= 360.0;
155                         }
156                 }
157         } else {
158                 if (NRAND(100) > 98) {
159                         fish->sign = (fish->sign < 0 ? 1 : -1);
160                 }
161                 fish->psi += (fish->sign ? 1 : -1);
162                 if (fish->psi > 180.0) {
163                         fish->psi -= 360.0;
164                 }
165                 if (fish->psi < -180.0) {
166                         fish->psi += 360.0;
167                 }
168         }
169
170         if (fish->attack) {
171                 if (fish->v < 1.1) {
172                         fish->spurt = 1;
173                 }
174                 if (fish->spurt) {
175                         fish->v += 0.2;
176                 }
177                 if (fish->v > 5.0) {
178                         fish->spurt = 0;
179                 }
180                 if ((fish->v > 1.0) && (!fish->spurt)) {
181                         fish->v -= 0.2;
182                 }
183         } else {
184                 if (!(NRAND(400)) && (!fish->spurt)) {
185                         fish->spurt = 1;
186                 }
187                 if (fish->spurt) {
188                         fish->v += 0.05;
189                 }
190                 if (fish->v > 3.0) {
191                         fish->spurt = 0;
192                 }
193                 if ((fish->v > 1.0) && (!fish->spurt)) {
194                         fish->v -= 0.05;
195                 }
196         }
197
198         fish->x += sharkspeed * fish->v * cos(fish->psi / RAD) * cos(fish->theta / RAD);
199         fish->y += sharkspeed * fish->v * sin(fish->psi / RAD) * cos(fish->theta / RAD);
200         fish->z += sharkspeed * fish->v * sin(fish->theta / RAD);
201 }
202
203 void
204 SharkMiss(atlantisstruct * ap, int i)
205 {
206         int         j;
207         float       avoid, thetal;
208         float       X, Y, Z, R;
209
210         for (j = 0; j < ap->num_sharks; j++) {
211                 if (j != i) {
212                         X = ap->sharks[j].x - ap->sharks[i].x;
213                         Y = ap->sharks[j].y - ap->sharks[i].y;
214                         Z = ap->sharks[j].z - ap->sharks[i].z;
215
216                         R = sqrt(X * X + Y * Y + Z * Z);
217
218                         avoid = 1.0;
219                         thetal = ap->sharks[i].theta;
220
221                         if (R < ap->sharksize) {
222                                 if (Z > 0.0) {
223                                         ap->sharks[i].theta -= avoid;
224                                 } else {
225                                         ap->sharks[i].theta += avoid;
226                                 }
227                         }
228                         ap->sharks[i].dtheta += (ap->sharks[i].theta - thetal);
229                 }
230         }
231 }
232 #endif