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