1 /*
2  * Compiz cube atlantis plugin
3  *
4  * atlantis.c
5  *
6  * This plugin renders a fish tank inside of the transparent cube,
7  * replete with fish, crabs, sand, bubbles, and coral.
8  *
9  * Copyright : (C) 2007-2008 by David Mikos
10  * Email     : infiniteloopcounter@gmail.com
11  *
12  * Copyright : (C) 2007 by Dennis Kasprzyk
13  * E-mail    : onestone@opencompositing.org
14  *
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  */
27 
28 /* atlantis --- Shows moving 3D sea animals */
29 
30 /* Copyright (c) E. Lassauge, 1998. */
31 
32 /*
33  * Permission to use, copy, modify, and distribute this software and its
34  * documentation for any purpose and without fee is hereby granted,
35  * provided that the above copyright notice appear in all copies and that
36  * both that copyright notice and this permission notice appear in
37  * supporting documentation.
38  *
39  * This file is provided AS IS with no warranties of any kind.  The author
40  * shall have no liability with respect to the infringement of copyrights,
41  * trade secrets or any patents by this file or any part thereof.  In no
42  * event will the author be liable for any lost revenue or profits or
43  * other special, indirect and consequential damages.
44  *
45  * The original code for this mode was written by Mark J. Kilgard
46  * as a demo for openGL programming.
47  *
48  * Porting it to xlock  was possible by comparing the original Mesa's morph3d
49  * demo with it's ported version to xlock, so thanks for Marcelo F. Vianna
50  * (look at morph3d.c) for his indirect help.
51  *
52  * Thanks goes also to Brian Paul for making it possible and inexpensive
53  * to use OpenGL at home.
54  *
55  * My e-mail address is lassauge@users.sourceforge.net
56  *
57  * Eric Lassauge  (May-13-1998)
58  *
59  */
60 
61 /**
62  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
63  * ALL RIGHTS RESERVED
64  * Permission to use, copy, modify, and distribute this software for
65  * any purpose and without fee is hereby granted, provided that the above
66  * copyright notice appear in all copies and that both the copyright notice
67  * and this permission notice appear in supporting documentation, and that
68  * the name of Silicon Graphics, Inc. not be used in advertising
69  * or publicity pertaining to distribution of the software without specific,
70  * written prior permission.
71  *
72  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
73  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
74  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
75  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
76  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
77  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
78  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
79  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
80  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
81  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
82  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
83  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
84  *
85  * US Government Users Restricted Rights
86  * Use, duplication, or disclosure by the Government is subject to
87  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
88  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
89  * clause at DFARS 252.227-7013 and/or in similar or successor
90  * clauses in the FAR or the DOD or NASA FAR Supplement.
91  * Unpublished-- rights reserved under the copyright laws of the
92  * United States.  Contractor/manufacturer is Silicon Graphics,
93  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
94  *
95  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
96  */
97 
98 #ifndef _ATLANTIS_INTERNAL_H
99 #define _ATLANTIS_INTERNAL_H
100 
101 #define LRAND()                 ((long) (random() & 0x7fffffff))
102 #define NRAND(n)                ((int) (LRAND() % (n)))
103 #define MAXRAND                 (2147483648.0) /* unsigned 1<<31 as a float */
104 
105 #include <math.h>
106 #include <float.h>
107 
108 /* some constants */
109 #define PI     M_PI
110 #define PIdiv2 M_PI_2
111 #define toDegrees (180.0f * M_1_PI)
112 #define toRadians (M_PI / 180.0f)
113 
114 /* for original atlantis screensaver models */
115 #define RAD toDegrees
116 #define RRAD toRadians
117 
118 /* return random number in range [0,x) */
119 #define randf(x) ((float) (rand()/(((double)RAND_MAX + 1)/(x))))
120 
121 
122 #include <compiz-core.h>
123 #include <compiz-cube.h>
124 
125 extern int atlantisDisplayPrivateIndex;
126 extern int cubeDisplayPrivateIndex;
127 
128 #define GET_ATLANTIS_DISPLAY(d) \
129     ((AtlantisDisplay *) (d)->base.privates[atlantisDisplayPrivateIndex].ptr)
130 #define ATLANTIS_DISPLAY(d) \
131     AtlantisDisplay *ad = GET_ATLANTIS_DISPLAY(d);
132 
133 #define GET_ATLANTIS_SCREEN(s, ad) \
134     ((AtlantisScreen *) (s)->base.privates[(ad)->screenPrivateIndex].ptr)
135 #define ATLANTIS_SCREEN(s) \
136     AtlantisScreen *as = GET_ATLANTIS_SCREEN(s, GET_ATLANTIS_DISPLAY(s->display))
137 
138 
139 /* matching value from atlantis.xml.in */
140 #define BUTTERFLYFISH	0
141 #define CHROMIS		1
142 #define CHROMIS2	2
143 #define CHROMIS3	3
144 #define FISH		4
145 #define FISH2		5
146 #define DOLPHIN		6
147 #define SHARK		7
148 #define WHALE		8
149 #define CRAB		9
150 
151 
152 /*
153  * groups to separate fish for calculating boidsAngles
154  * at each time slice only one of the groups will have the boidsAngles updated
155 */
156 #define NUM_GROUPS 6
157 
158 
159 /* matching values from cubeaddon plugin */
160 #define DeformationNone		0
161 #define DeformationCylinder	1
162 #define DeformationSphere	2
163 
164 
165 typedef struct _fishRec
166 {
167     float x, y, z, theta, psi, v;
168     float htail, vtail;
169     float dtheta;
170     int spurt, attack;
171     int size;
172     float speed;
173     int type;
174     float color[4];
175     int group;
176     int boidsCounter; /* so that boidsAngles aren't computed every time step */
177     float boidsPsi;
178     float boidsTheta;
179     int smoothTurnCounter;
180     float smoothTurnAmount; /* psi direction */
181     float smoothTurnTh;     /* theta direction */
182     float prevRandPsi;
183     float prevRandTh;
184 }
185 fishRec;
186 
187 typedef struct _crabRec
188 {
189     float x, y, z, theta, psi;
190     int size;
191     float speed;
192     float color[4];
193     int scuttleAmount;
194     float scuttlePsi;
195     float scuttleTheta;
196     Bool isFalling;
197 }
198 crabRec;
199 
200 typedef struct _coralRec
201 {
202     float x, y, z, psi;
203     int size;
204     int type;
205     float color[4];
206 }
207 coralRec;
208 
209 typedef struct _Bubble
210 {
211     float x, y, z;
212     float size;
213     float speed;
214     float counter;
215     float offset;
216 }
217 Bubble;
218 
219 typedef struct _aeratorRec
220 {
221     float x, y, z;
222     int size;
223     int type;
224     float color[4];
225     Bubble *bubbles;
226 
227     int numBubbles;
228 }
229 aeratorRec;
230 
231 typedef struct _Vertex
232 {
233     float v[3];
234     float n[3];
235 }
236 Vertex;
237 
238 typedef struct _Water
239 {
240     int size;
241     float distance;
242     int sDiv;
243 
244     float bh;
245     float wa;
246     float swa;
247     float wf;
248     float swf;
249 
250     Vertex *vertices;
251     unsigned int *indices;
252 
253     Vertex *vertices2; /* for extra side wall detail in sphere deformation */
254     unsigned int *indices2;
255 
256     int *rippleFactor;
257     int rippleTimer;
258 
259     unsigned int nVertices;
260     unsigned int nIndices;
261 
262     unsigned int nSVer;
263     unsigned int nSIdx;
264     unsigned int nWVer;
265     unsigned int nWIdx;
266     unsigned int nBIdx;
267 
268     unsigned int nWVer2;
269     unsigned int nWIdx2;
270     unsigned int nBIdx2;
271 
272     float wave1;
273     float wave2;
274 }
275 Water;
276 
277 typedef struct _AtlantisDisplay
278 {
279     int screenPrivateIndex;
280 }
281 AtlantisDisplay;
282 
283 typedef struct _AtlantisScreen
284 {
285     DonePaintScreenProc donePaintScreen;
286     PreparePaintScreenProc preparePaintScreen;
287 
288     CubeClearTargetOutputProc clearTargetOutput;
289     CubePaintInsideProc paintInside;
290 
291     Bool damage;
292 
293     int numFish;
294     int numCrabs;
295     int numCorals;
296     int numAerators;
297 
298     fishRec *fish;
299     crabRec *crab;
300     coralRec *coral;
301     aeratorRec *aerator;
302 
303     Water *water;
304     Water *ground;
305 
306     float waterHeight;  /* water surface height */
307 
308     int hsize;
309     float sideDistance; /* perpendicular distance to side wall from centre */
310     float topDistance;  /* perpendicular distance to top wall from centre */
311     float radius;       /* radius on which the hSize points lie */
312     float arcAngle;   	/* 360 degrees / horizontal size */
313     float ratio;        /* screen width to height */
314 
315     float speedFactor;  /* multiply fish/crab speeds by this value */
316 
317     float oldProgress;
318 
319     GLuint crabDisplayList;
320     GLuint coralDisplayList;
321     GLuint coral2DisplayList;
322     GLuint bubbleDisplayList;
323 }
324 AtlantisScreen;
325 
326 void
327 updateWater(CompScreen *s, float time);
328 
329 void
330 updateGround(CompScreen *s, float time);
331 
332 void
333 updateDeformation (CompScreen *s, int currentDeformation);
334 
335 void
336 updateHeight(Water *w, Water *w2, Bool, int currentDeformation);
337 
338 void
339 freeWater(Water *w);
340 
341 void
342 drawWater(Water *w, Bool full, Bool wire, int currentDeformation);
343 
344 void
345 drawGround(Water *w, Water *g, int currentDeformation);
346 
347 void
348 drawBottomGround(Water *w, float distance, float bottom, int currentDeformation);
349 
350 void
351 drawBottomWater(Water *w, float distance, float bottom, int currentDeformation);
352 
353 void
354 setWaterMaterial (unsigned short *);
355 
356 void
357 setGroundMaterial (unsigned short *);
358 
359 Bool
360 isInside(CompScreen *s, float x, float y, float z);
361 
362 float
363 getHeight(Water *w, float x, float z);
364 
365 float
366 getGroundHeight (CompScreen *s, float x, float z);
367 
368 void
369 FishTransform(fishRec *);
370 
371 void
372 FishPilot(CompScreen *, int);
373 
374 void
375 CrabTransform(crabRec *);
376 
377 void
378 CrabPilot(CompScreen *, int);
379 
380 void
381 BubbleTransform(Bubble *);
382 
383 void
384 BubblePilot(CompScreen *, int, int);
385 
386 void
387 BoidsAngle(CompScreen *, int);
388 
389 void
390 RenderWater(int, float, Bool, Bool);
391 
392 void
393 DrawWhale(fishRec *, int);
394 
395 void
396 DrawShark(fishRec *, int);
397 
398 void
399 DrawDolphin(fishRec *, int);
400 
401 void
402 AnimateBFish(float);
403 
404 void
405 DrawAnimatedBFish(void);
406 
407 void
408 initDrawBFish(float *);
409 
410 void
411 finDrawBFish(void);
412 
413 void
414 AnimateChromis(float);
415 
416 void
417 DrawAnimatedChromis(void);
418 
419 void
420 initDrawChromis(float *);
421 
422 void
423 initDrawChromis2(float *);
424 
425 void
426 initDrawChromis3(float *);
427 
428 void
429 finDrawChromis(void);
430 
431 void
432 DrawAnimatedFish(void);
433 
434 void
435 AnimateFish(float);
436 
437 void
438 initDrawFish(float *);
439 
440 void
441 finDrawFish(void);
442 
443 void
444 DrawAnimatedFish2(void);
445 
446 void
447 AnimateFish2(float);
448 
449 void
450 initDrawFish2(float *);
451 
452 void
453 finDrawFish2(void);
454 
455 void
456 DrawCrab(int);
457 
458 void
459 initDrawCrab(void);
460 
461 void
462 finDrawCrab(void);
463 
464 void
465 DrawCoral(int);
466 
467 void
468 DrawCoralLow(int);
469 
470 void
471 initDrawCoral(void);
472 
473 void
474 finDrawCoral(void);
475 
476 void
477 DrawCoral2(int);
478 
479 void
480 DrawCoral2Low(int);
481 
482 void
483 initDrawCoral2(void);
484 
485 void
486 finDrawCoral2(void);
487 
488 void
489 DrawBubble(int, int);
490 
491 
492 /* utility methods */
493 int
494 getCurrentDeformation(CompScreen *s);
495 
496 int
497 getDeformationMode(CompScreen *s);
498 
499 float
500 symmDistr(void); /* symmetric distribution */
501 
502 void
503 setColor(float *, float, float, float, float, float, float);
504 
505 void
506 setSimilarColor(float *, float*, float, float);
507 
508 void
509 setSimilarColor4us(float *, unsigned short *, float, float);
510 
511 void
512 setSpecifiedColor(float *, int);
513 
514 void
515 setRandomLocation(CompScreen *, float *, float *, float);
516 
517 void
518 setMaterialAmbientDiffuse (float *, float, float);
519 
520 void
521 setMaterialAmbientDiffuse4us (unsigned short *, float, float);
522 
523 void
524 copyColor (float *, float *, float);
525 
526 void
527 convert4usTof (unsigned short *, float *);
528 
529 
530 #endif
531