http://svn.poeml.de/viewvc/ppc/src-unpacked/xscreensaver/xscreensaver-4.12.tar.bz2...
[xscreensaver] / hacks / glx / flurry.h
1 /*
2
3 Copyright (c) 2002, Calum Robinson
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9 * Redistributions of source code must retain the above copyright notice, this
10   list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above copyright notice,
13   this list of conditions and the following disclaimer in the documentation
14   and/or other materials provided with the distribution.
15
16 * Neither the name of the author nor the names of its contributors may be used
17   to endorse or promote products derived from this software without specific
18   prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 */
32
33 /* -*- Mode: C; tab-width: 4 c-basic-offset: 4 indent-tabs-mode: t -*- */
34 /* flurry */
35 #ifndef __GLCODE__
36 #define __GLCODE__
37
38 #include <GL/glu.h>
39 #include <GL/gl.h>
40 #include <GL/glx.h>
41
42 #include <stdlib.h>
43 #include <math.h>
44
45 #include "yarandom.h"
46 #include "rotator.h"
47 #include "gltrackball.h"
48
49 typedef struct _global_info_t global_info_t;
50 typedef struct _flurry_info_t flurry_info_t;
51
52 #define sqr(X)     ((X) * (X))
53 #define PI         3.14159265358979323846f
54 #define DEG2RAD(X) (PI*(X)/180.0)
55 #define RAD2DEG(X) ((X)*180.0/PI)
56 #define rnd()      (frand(1.0))
57
58 /* fabs: Absolute function. */
59 /* #undef abs */
60 /* #define abs(a)     ( (a) > 0 ? (a) : -(a) ) */
61
62 /* Force sign clamping to (-1;0;1) */
63 #define sgn(a)      ((a)<0?-1:((a)?1:0))
64
65 /* used to compute the min and max of two expresions */
66 #define MIN_(a, b)  (((a) < (b)) ? (a) : (b)) 
67 #define MAX_(a, b)  (((a) > (b)) ? (a) : (b)) 
68
69 typedef union {
70     float               f[4];
71 #if __VEC__
72     vector float        v;
73 #endif
74 } floatToVector;
75
76 typedef union {
77     unsigned int        i[4];
78 #if __VEC__
79     vector unsigned int v;
80 #endif
81 } intToVector;
82
83 typedef struct SmokeParticleV  
84 {
85         floatToVector color[4];
86         floatToVector position[3];
87         floatToVector oldposition[3];
88         floatToVector delta[3];
89         intToVector dead;
90         floatToVector time;
91         intToVector animFrame;
92 } SmokeParticleV;
93
94 #define NUMSMOKEPARTICLES 3600
95
96 typedef struct SmokeV  
97 {
98         SmokeParticleV p[NUMSMOKEPARTICLES/4];
99         int nextParticle;
100         int nextSubParticle;
101         float lastParticleTime;
102         int firstTime;
103         long frame;
104         float old[3];
105         floatToVector seraphimVertices[NUMSMOKEPARTICLES*2+1];
106         floatToVector seraphimColors[NUMSMOKEPARTICLES*4+1];
107         float seraphimTextures[NUMSMOKEPARTICLES*2*4];
108 } SmokeV;
109
110 void InitSmoke(SmokeV *s);
111
112 void UpdateSmoke_ScalarBase(global_info_t *global, flurry_info_t *flurry, SmokeV *s);
113 #ifdef __ppc__
114 void UpdateSmoke_ScalarFrsqrte(global_info_t *global, flurry_info_t *flurry, SmokeV *s);
115 #endif
116 #ifdef __VEC__
117 void UpdateSmoke_VectorBase(global_info_t *global, flurry_info_t *flurry, SmokeV *s);
118 void UpdateSmoke_VectorUnrolled(global_info_t *global, flurry_info_t *flurry, SmokeV *s);
119 #endif
120
121 void DrawSmoke_Scalar(global_info_t *global, flurry_info_t *flurry, SmokeV *s, float);
122 void DrawSmoke_Vector(global_info_t *global, flurry_info_t *flurry, SmokeV *s, float);
123
124 typedef struct Star  
125 {
126         float position[3];
127         float mystery;
128         float rotSpeed;
129         int ate;
130 } Star;
131
132 void UpdateStar(global_info_t *global, flurry_info_t *flurry, Star *s);
133 void InitStar(Star *s);
134
135 typedef struct Spark  
136 {
137     float position[3];
138     int mystery;
139     float delta[3];
140     float color[4];    
141 } Spark;
142
143 void UpdateSparkColour(global_info_t *info, flurry_info_t *flurry, Spark *s);
144 void InitSpark(Spark *s);
145 void UpdateSpark(global_info_t *info, flurry_info_t *flurry, Spark *s);
146 void DrawSpark(global_info_t *info, flurry_info_t *flurry, Spark *s);
147
148 /* #define FastDistance2D(x,y) hypot(x,y) */
149
150 /* UInt8  sys_glBPP=32; */
151 /* int SSMODE = FALSE; */
152 /* int currentVideoMode = 0; */
153 /* int cohesiveness = 7; */
154 /* int fieldStrength; */
155 /* int colorCoherence = 7; */
156 /* int fieldIncoherence = 0; */
157 /* int ifieldSpeed = 120; */
158
159 static inline float FastDistance2D(float x, float y)
160 {
161         /* this function computes the distance from 0,0 to x,y with ~3.5% error */
162         float mn;
163         /* first compute the absolute value of x,y */
164         x = (x < 0.0f) ? -x : x;
165         y = (y < 0.0f) ? -y : y;
166         
167         /* compute the minimum of x,y */
168         mn = x<y?x:y;
169         
170         /* return the distance */
171         return(x+y-(mn*0.5f)-(mn*0.25f)+(mn*0.0625f));
172 }
173
174 #ifdef __VEC__
175
176 static vector float FastDistance2DV(vector float x, vector float y) {
177     vector float mn, temp;
178     
179     x = vec_abs(x);
180     y = vec_abs(y);
181     mn = vec_min(x,y);
182     temp = vec_add(x,y);
183     temp = vec_madd(mn, (vector float)(-0.6875), temp);
184     return temp;
185 }
186
187 #endif
188
189 #define RandFlt(min, max) ((min) + frand((max) - (min)))
190
191 #define RandBell(scale) ((scale) * (-(frand(.5) + frand(.5) + frand(.5))))
192
193 extern GLuint theTexture;
194
195 void MakeTexture(void);
196
197 #define OPT_MODE_SCALAR_BASE            0x0
198
199 #ifdef __ppc__
200 #define OPT_MODE_SCALAR_FRSQRTE         0x1
201 #endif
202
203 #ifdef __VEC__
204 #define OPT_MODE_VECTOR_SIMPLE          0x2
205 #define OPT_MODE_VECTOR_UNROLLED        0x3
206 #endif
207
208 typedef enum _ColorModes
209 {
210         redColorMode = 0,
211         magentaColorMode,
212         blueColorMode,
213         cyanColorMode,
214         greenColorMode,
215         yellowColorMode,
216         slowCyclicColorMode,
217         cyclicColorMode,
218         tiedyeColorMode,
219         rainbowColorMode,
220         whiteColorMode,
221         multiColorMode,
222         darkColorMode
223 } ColorModes;
224
225 #define gravity 1500000.0f
226
227 #define incohesion 0.07f
228 #define colorIncoherence 0.15f
229 #define streamSpeed 450.0
230 #define fieldCoherence 0
231 #define fieldSpeed 12.0f
232 #define numParticles 250
233 #define starSpeed 50
234 #define seraphDistance 2000.0f
235 #define streamSize 25000.0f
236 #define fieldRange 1000.0f
237 #define streamBias 7.0f
238
239 #define MAX_SPARKS 64
240
241 struct _flurry_info_t {
242         flurry_info_t *next;
243         ColorModes currentColorMode;
244         SmokeV *s;
245         Star *star;
246         Spark *spark[MAX_SPARKS];
247         float streamExpansion;
248         int numStreams;
249         double flurryRandomSeed;
250         double fTime;
251         double fOldTime;
252         double fDeltaTime;
253         double briteFactor;
254         float drag;
255         int dframe;
256 };
257
258 struct _global_info_t {
259   /* system values */
260         GLXContext *glx_context;
261         Window window;
262         int optMode;
263
264         float sys_glWidth;
265         float sys_glHeight;
266
267         flurry_info_t *flurry;
268 };
269
270 #define kNumSpectrumEntries 512
271
272 void OTSetup(void);
273 double TimeInSecondsSinceStart(void);
274
275 #endif /* Include/Define */