From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[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 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif /* HAVE_CONFIG_H */
41
42 #ifndef HAVE_JWXYZ
43 # include <GL/gl.h>
44 # include <GL/glu.h>
45 # include <GL/glx.h>
46 #endif
47
48 #ifdef HAVE_JWZGLES
49 # include "jwzgles.h"
50 #endif /* HAVE_JWZGLES */
51
52
53 #include <stdlib.h>
54 #include <math.h>
55
56 #include "yarandom.h"
57 #include "rotator.h"
58 #include "gltrackball.h"
59
60 typedef struct _global_info_t global_info_t;
61 typedef struct _flurry_info_t flurry_info_t;
62
63 #define sqr(X)     ((X) * (X))
64 #define PI         3.14159265358979323846f
65 #define DEG2RAD(X) (PI*(X)/180.0)
66 #define RAD2DEG(X) ((X)*180.0/PI)
67 #define rnd()      (frand(1.0))
68
69 /* fabs: Absolute function. */
70 /* #undef abs */
71 /* #define abs(a)     ( (a) > 0 ? (a) : -(a) ) */
72
73 /* Force sign clamping to (-1;0;1) */
74 #define sgn(a)      ((a)<0?-1:((a)?1:0))
75
76 /* used to compute the min and max of two expresions */
77 #define MIN_(a, b)  (((a) < (b)) ? (a) : (b)) 
78 #define MAX_(a, b)  (((a) > (b)) ? (a) : (b)) 
79
80 typedef union {
81     float               f[4];
82 #if 0
83 #if __VEC__
84     vector float        v;
85 #endif
86 #endif /* 0 */
87 } floatToVector;
88
89 typedef union {
90     unsigned int        i[4];
91 #if 0
92 #if __VEC__
93     vector unsigned int v;
94 #endif
95 #endif /* 0 */
96 } intToVector;
97
98 typedef struct SmokeParticleV  
99 {
100         floatToVector color[4];
101         floatToVector position[3];
102         floatToVector oldposition[3];
103         floatToVector delta[3];
104         intToVector dead;
105         floatToVector time;
106         intToVector animFrame;
107 } SmokeParticleV;
108
109 #define NUMSMOKEPARTICLES 3600
110
111 typedef struct SmokeV  
112 {
113         SmokeParticleV p[NUMSMOKEPARTICLES/4];
114         int nextParticle;
115         int nextSubParticle;
116         float lastParticleTime;
117         int firstTime;
118         long frame;
119         float old[3];
120         floatToVector seraphimVertices[NUMSMOKEPARTICLES*2+1];
121         floatToVector seraphimColors[NUMSMOKEPARTICLES*4+1];
122         float seraphimTextures[NUMSMOKEPARTICLES*2*4];
123 } SmokeV;
124
125 void InitSmoke(SmokeV *s);
126
127 void UpdateSmoke_ScalarBase(global_info_t *global, flurry_info_t *flurry, SmokeV *s);
128 #if 0
129 #ifdef __ppc__
130 void UpdateSmoke_ScalarFrsqrte(global_info_t *global, flurry_info_t *flurry, SmokeV *s);
131 #endif
132 #ifdef __VEC__
133 void UpdateSmoke_VectorBase(global_info_t *global, flurry_info_t *flurry, SmokeV *s);
134 void UpdateSmoke_VectorUnrolled(global_info_t *global, flurry_info_t *flurry, SmokeV *s);
135 #endif
136 #endif /* 0 */
137
138 void DrawSmoke_Scalar(global_info_t *global, flurry_info_t *flurry, SmokeV *s, float);
139 void DrawSmoke_Vector(global_info_t *global, flurry_info_t *flurry, SmokeV *s, float);
140
141 typedef struct Star  
142 {
143         float position[3];
144         float mystery;
145         float rotSpeed;
146         int ate;
147 } Star;
148
149 void UpdateStar(global_info_t *global, flurry_info_t *flurry, Star *s);
150 void InitStar(Star *s);
151
152 typedef struct Spark  
153 {
154     float position[3];
155     int mystery;
156     float delta[3];
157     float color[4];    
158 } Spark;
159
160 void UpdateSparkColour(global_info_t *info, flurry_info_t *flurry, Spark *s);
161 void InitSpark(Spark *s);
162 void UpdateSpark(global_info_t *info, flurry_info_t *flurry, Spark *s);
163 void DrawSpark(global_info_t *info, flurry_info_t *flurry, Spark *s);
164
165 /* #define FastDistance2D(x,y) hypot(x,y) */
166
167 /* UInt8  sys_glBPP=32; */
168 /* int SSMODE = FALSE; */
169 /* int currentVideoMode = 0; */
170 /* int cohesiveness = 7; */
171 /* int fieldStrength; */
172 /* int colorCoherence = 7; */
173 /* int fieldIncoherence = 0; */
174 /* int ifieldSpeed = 120; */
175
176 static inline float FastDistance2D(float x, float y)
177 {
178         /* this function computes the distance from 0,0 to x,y with ~3.5% error */
179         float mn;
180         /* first compute the absolute value of x,y */
181         x = (x < 0.0f) ? -x : x;
182         y = (y < 0.0f) ? -y : y;
183         
184         /* compute the minimum of x,y */
185         mn = x<y?x:y;
186         
187         /* return the distance */
188         return(x+y-(mn*0.5f)-(mn*0.25f)+(mn*0.0625f));
189 }
190
191 #if 0
192 #ifdef __VEC__
193
194 static vector float FastDistance2DV(vector float x, vector float y) {
195     vector float mn, temp;
196     
197     x = vec_abs(x);
198     y = vec_abs(y);
199     mn = vec_min(x,y);
200     temp = vec_add(x,y);
201     temp = vec_madd(mn, (vector float)(-0.6875), temp);
202     return temp;
203 }
204
205 #endif
206 #endif /* 0 */
207
208 #define RandFlt(min, max) ((min) + frand((max) - (min)))
209
210 #define RandBell(scale) ((scale) * (-(frand(.5) + frand(.5) + frand(.5))))
211
212 extern GLuint theTexture;
213
214 void MakeTexture(void);
215
216 #define OPT_MODE_SCALAR_BASE            0x0
217
218 #if 0
219 #ifdef __ppc__
220 #define OPT_MODE_SCALAR_FRSQRTE         0x1
221 #endif
222
223 #ifdef __VEC__
224 #define OPT_MODE_VECTOR_SIMPLE          0x2
225 #define OPT_MODE_VECTOR_UNROLLED        0x3
226 #endif
227 #endif /* 0 */
228
229 typedef enum _ColorModes
230 {
231         redColorMode = 0,
232         magentaColorMode,
233         blueColorMode,
234         cyanColorMode,
235         greenColorMode,
236         yellowColorMode,
237         slowCyclicColorMode,
238         cyclicColorMode,
239         tiedyeColorMode,
240         rainbowColorMode,
241         whiteColorMode,
242         multiColorMode,
243         darkColorMode
244 } ColorModes;
245
246 #define gravity 1500000.0f
247
248 #define incohesion 0.07f
249 #define colorIncoherence 0.15f
250 #define streamSpeed 450.0
251 #define fieldCoherence 0
252 #define fieldSpeed 12.0f
253 #define numParticles 250
254 #define starSpeed 50
255 #define seraphDistance 2000.0f
256 #define streamSize 25000.0f
257 #define fieldRange 1000.0f
258 #define streamBias 7.0f
259
260 #define MAX_SPARKS 64
261
262 struct _flurry_info_t {
263         flurry_info_t *next;
264         ColorModes currentColorMode;
265         SmokeV *s;
266         Star *star;
267         Spark *spark[MAX_SPARKS];
268         float streamExpansion;
269         int numStreams;
270         double flurryRandomSeed;
271         double fTime;
272         double fOldTime;
273         double fDeltaTime;
274         double briteFactor;
275         float drag;
276         int dframe;
277 };
278
279 struct _global_info_t {
280   /* system values */
281         GLXContext *glx_context;
282         Window window;
283         int optMode;
284
285         float sys_glWidth;
286         float sys_glHeight;
287
288         double gTimeCounter;
289         int first;
290         double oldFrameTime;
291
292         flurry_info_t *flurry;
293 };
294
295 #define kNumSpectrumEntries 512
296
297 double TimeInSecondsSinceStart(const global_info_t *global);
298
299 #endif /* Include/Define */