summaryrefslogtreecommitdiffstats
path: root/kscreensaver/kdesavers/firesaverparticle.cpp
blob: 421115a77fc39cda1789618c313e29b891335805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//     This file is part of KFireSaver3D.

//     KFireSaver3D is free software; you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation; either version 2 of the License, or
//     (at your option) any later version.

//     KFireSaver3D is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.

//     You should have received a copy of the GNU General Public License
//     along with KFireSaver3D; if not, write to the Free Software
//     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


//     Author: Enrico Ros, based on the great work of David Sansome (kfiresaver)
//     Email:  asy@libero.it

#include <math.h>
#include <stdlib.h>

#include "firesaverparticle.h"
#include "firesaver.h"

//current color scheme :		red  orng grn  blue w  m  c
static const GLfloat c_red_min[7] =   { 0.4, 0.9,   0,   0, 1, 0, 0 };
static const GLfloat c_red_max[7] =   {   1,   1,   0,   0, 1, 1, 0 };
static const GLfloat c_green_min[7] = {   0, 0.4, 0.4,   0, 1, 0, 0 };
static const GLfloat c_green_max[7] = {   0, 0.5,   1, 0.5, 1, 0, 1 };
static const GLfloat c_blue_min[7] =  {   0, 0.1,   0, 0.5, 1, 0, 0 };
static const GLfloat c_blue_max[7] =  {   0, 0.2,   0,   1, 1, 1, 1 };


//default initialization (good for Leader / Fire)
Particle :: Particle( ParticleType pT )
    : particleType( pT ), explosionsDepth( 0 ), texture( 0 ),
    xpos( 0.0 ), ypos( 0.0 ), zpos( -9.9 ),
    xspeed( 0.0 ), yspeed( 0.0 ), zspeed( 0.0 ), zacc( -9.807 ),
    life( 0.0 ), startLife( 0.0 ),
    pixelSize( 5.0 ), useLife( true ), flicker( 0 )
{
	colour[0] = 0;
	colour[1] = 0;
	colour[2] = 0;
	colour[3] = 1;
}


void Particle :: initializeValues (
	int cs,
	Particle* debrisParent,
	GLfloat powermin, GLfloat powermax,
	bool flickers,
	GLfloat *displace )
//note:	this function is called when a particle needs a set of default parameters.
//	these depends on the ParticleType.
{
	switch (particleType)
	{
//	-- FireParticle (params: [cs])
//		born in a rectangle centered in { 0, 0, -9.9 }
//		speed 3-6 [m/s] blue/cyan colored
//		this is only done at the beginning or when a particle dies
	    case FireParticle:
		xpos = DRAND * FIELDWIDTH - FIELDW_2;
		ypos = DRAND - 2.0;

		xspeed = DRAND * 4.0 - 2.0;
		yspeed = DRAND * 2.0;
		zspeed = DRAND * 3.0 + 3.0;

		useLife = false;
		pixelSize = 2.0 + DRAND * 2.0;
		break;

//	-- FireWorkLeaderParticle (params: none)
//		they start in a rectangle at the same height but
//		with different power and the same 'orange' colour
	    case FireWorkLeaderParticle:
		xpos = DRAND * 14.0 - 7.0;
		ypos = DRAND * 2.0 - 1.0;

		xspeed = DRAND * 8.0 - 4.0 - (xpos / 2.0)*DRAND;
		yspeed = DRAND * 8.0 - 4.0;
		zspeed = DRAND * 6.5 + 18.0;

		colour[0] = 0.6;
		colour[1] = DRAND * 0.4;
		colour[2] = 0.0;

		useLife = false;
		break;

//	-- LogoParticle (params: none)
//		they start in the middle of the screen.
//		little g-force, constant life, weavy-y
	    case LogoParticle:
		yspeed = 3*(DRAND - DRAND);
		zacc = -9.807f / 5.0f;

		startLife = 0.7f;
		life = 1.7f;
		break;

//	-- StarParticle (params: none)
//		spherically distributed. xpos and ypos are the
//		transformed screen positions of the star.
	    case StarParticle:
		colour[0] = DRAND * 0.2 + 0.5;
		colour[1] = DRAND * 0.2 + 0.5;
		colour[2] = DRAND * 0.2 + 0.5;

		{bool accepted = false;
		while (!accepted) {
			float module = 30,
			      theta = DRAND * M_PI * 2.0,
			      u = DRAND * 2.0 - 1.0,
			      root = sqrt( 1 - u*u );
			xpos = module * root * cos(theta);
			ypos = fabs(module * root * sin(theta)) - 10.0;
			zpos = fabs(module * u);

			float sfactor = 256.0 / (256.0 + PERSP_MAG_FACTOR*ypos);
			xpos *= sfactor;
			ypos = sfactor * zpos - FIELDW_2;

			pixelSize = sfactor * (2.0 + 3.0*DRAND);

			accepted = xpos > -FIELDW_2 && xpos < FIELDW_2 &&
				   ypos > -FIELDW_2 && ypos < FIELDW_2;
		}}
		break;

//	-- FireWorkDebrisParticle (params: cs, parent, [powerm], [powerM], [flickers], [displace])
//		parameters are randomized for a 'spherical' explosion.
//		power{min,max}, flickers and displace applies only for that
//		kind of ParticleType.
	    case FireWorkDebrisParticle:

		//same origin of the dead leader
		xpos = debrisParent->xpos;
		ypos = debrisParent->ypos;
		zpos = debrisParent->zpos;

		//true spherical randomization
		float module = powermin + DRAND * (powermax - powermin),
		      theta = DRAND * M_PI * 2.0,
		      u = DRAND * 2.0 - 1.0,
		      root = sqrt( 1 - u*u );
		xspeed = debrisParent->xspeed + module * root * cos(theta) * (1.0 + DRAND/3.0);
		yspeed = debrisParent->yspeed + module * root * sin(theta) * (1.0 + DRAND/3.0);
		zspeed = module * u * (1.0 + DRAND/3.0);	//was 0.9 + DRAND/3

		//if set add a displace to speed
		if ( displace ) {
			xspeed += displace[0];
			yspeed += displace[1];
			zspeed += displace[2];
		}

		//randomize the color choosing on current palette
		colour[0] = c_red_min[cs] + (c_red_max[cs]-c_red_min[cs]) * DRAND;
		colour[1] = c_green_min[cs] + (c_green_max[cs]-c_green_min[cs]) * DRAND;
		colour[2] = c_blue_min[cs] + (c_blue_max[cs]-c_blue_min[cs]) * DRAND;

		pixelSize = DRAND * 2.0 + 2.0;
		zacc = -9.807 / (6.0 - pixelSize);

		life = startLife = pixelSize / 2.0;

		//if flickers is set the current visible delay is randomized
		if ( flickers )
			flicker = (int) ((DRAND * 2.0 - 1.0) * (float)FLICKER_FRAMES_DELAY);
		break;
	}
}


void Particle :: updateParameters( float dT )
//note:	this procedure uses a reduced set of parameters
//	x and y axis acceleration is no more used
//	the only external iterations are:
//	- the g force
//	- a sort of air friction that limits speed in x,y and
//	  acceleration on z
{
	xpos += xspeed * dT;
	ypos += yspeed * dT;
	zpos += (zspeed + zacc*dT/2) * dT;

	zspeed += zacc * dT;

	xspeed *= 0.998;
	yspeed *= 0.998;
	zspeed *= 0.999;

	if (useLife)
	    life -= dT;
}


// BEGIN TurningParticle class 

TurningParticle :: TurningParticle( ParticleType pT )
	: Particle( pT ) {}

void TurningParticle :: initializeValues (
	int cs,
	Particle* leader,
	GLfloat powermin,
	GLfloat powermax,
	bool /*flickers*/,
	GLfloat * /*displace*/ )
{
	//same origin of the parent
	xpos = leader->xpos;
	ypos = leader->ypos;
	zpos = leader->zpos;

	//velocity : true spherical randomization
	float   module = powermin + (powermax - powermin) * DRAND * 0.6,
		theta = DRAND * M_PI * 2.0,
		u = DRAND * 2.0 - 1.0,
		root = sqrt( 1 - u*u );
	xspeed = -module * root * cos(theta);
	yspeed = -module * root * sin(theta);
	zspeed = module * u;

	//spin axis : in quadrature with velocity
	module = (1 + DRAND) / 40;
	u = DRAND * 2.0 - 1.0;
	root = sqrt( 1 - u*u );
	// axis to spin around
	wx = module * root * cos(theta + M_PI_2);
	wy = module * root * sin(theta + M_PI_2);
	wz = module * u;

	//randomize the color choosing on current palette
	colour[0] = c_red_min[cs] + (c_red_max[cs]-c_red_min[cs]) * DRAND;
	colour[1] = c_green_min[cs] + (c_green_max[cs]-c_green_min[cs]) * DRAND;
	colour[2] = c_blue_min[cs] + (c_blue_max[cs]-c_blue_min[cs]) * DRAND;

        pixelSize = DRAND * 2.0 + 2.0;
	zacc = -9.807 / 5.0;
	life = startLife = pixelSize / 2.0;
}


void TurningParticle :: updateParameters ( float dT )
{
	//update position
	xpos += xspeed * dT;
	ypos += yspeed * dT;
	zpos += zspeed * dT;

	//tan vector = velocity vector (vect producted by) spin axis
	float vx = yspeed * wz - zspeed * wy,
	      vy = zspeed * wx - xspeed * wz,
	      vz = xspeed * wy - yspeed * wx;

	//update velocity adding a tangential component (aka infinitesimally
	//rotating the vector)
	xspeed += vx;
	yspeed += vy;
	zspeed += vz + zacc * dT;

	if (useLife)
		life -= dT;
}

//END TurningParticle