-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticleSystem.h
263 lines (221 loc) · 5.73 KB
/
ParticleSystem.h
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
#ifndef PARTICLESYSTEM_H
#define PARTICLESYSTEM_H
#include "Point.h"
#include "Vector.h"
#include "Color.h"
#include "Debug.h"
#include <cstdlib>
#include <vector>
#include <algorithm>
const float PARTICLE_OFFSET = 0.03;
const float GRAVITY = 0.013;
const int NUM_PARTICLES = 1000;
const int FOUNTAIN_MAX_LIFETIME = 60;
const float FOUNTAIN_X_VAR = 0.04;
const float FOUNTAIN_Y_MAX = 0.45;
const float FOUNTAIN_Y_MIN = 0.35;
const float FOUNTAIN_Z_VAR = 0.04;
const Color3f FOUNTAIN_COLOR(0.0, 0.8, 1.0);
const int FIREWORKS_MAX_LIFETIME = 20;
const int FIREWORKS_ROCKET_LIFETIME = 35;
const float FIREWORKS_ROCKET_X = 0.01;
const float FIREWORKS_ROCKET_Y = 0.3;
const float FIREWORKS_ROCKET_Z = 0.0;
const float FIREWORKS_SPEED = 0.07;
const float FIREWORKS_GRAVITY = 0.008;
const float FIREWORKS_RADIUS = 0.05;
const Color3f FIREWORKS_COLOR(1.0, 0.0, 0.0);
const float SNOW_MAX_VELOCITY = 0.012;
const float SNOW_MIN_VELOCITY = 0.004;
const float SNOW_PARTICLE_OFFSET = 0.01;
const Color3f SNOW_COLOR(1.0, 1.0, 1.0);
struct Particle
{
Point3f pos;
Vector3f velocity;
unsigned int lifetime, time_alive;
};
class ParticleSystem
{
protected:
float particle_offset, gravity;
Particle *particles;
int num_particles;
Color3f color;
public:
ParticleSystem(int num = NUM_PARTICLES, const Color3f &col = RED, float _gravity = GRAVITY, float offset = PARTICLE_OFFSET) :
particle_offset(offset), gravity(_gravity), num_particles(num), color(col)
{
particles = new Particle[num_particles];
}
virtual ~ParticleSystem()
{
delete[] particles;
}
virtual void update() = 0;
virtual void render() const;
float getParticleOffset() const
{
return particle_offset;
}
void setParticleOffset(float offset)
{
ASSERT(offset > 0);
particle_offset = offset;
}
float getGravity() const
{
return gravity;
}
void setGravity(float g)
{
ASSERT(g > 0);
gravity = g;
}
int getNumParticles() const
{
return num_particles;
}
};
class Fountain : public ParticleSystem
{
private:
int max_lifetime;
float x_var, y_max, y_min, z_var;
void init(Particle &);
public:
Fountain(int num = NUM_PARTICLES, int lifetime = FOUNTAIN_MAX_LIFETIME, float _x_var = FOUNTAIN_X_VAR, float _y_max = FOUNTAIN_Y_MAX,
float _y_min = FOUNTAIN_Y_MIN, float _z_var = FOUNTAIN_Z_VAR, const Color3f &col = FOUNTAIN_COLOR) : ParticleSystem(num, col),
max_lifetime(lifetime), x_var(_x_var), y_max(_y_max), y_min(_y_min), z_var(_z_var)
{
for (int i = 0; i < num_particles; ++i)
init(particles[i]);
}
void update();
int getMaxLifetime() const
{
return max_lifetime;
}
void setMaxLifetime(int max)
{
ASSERT(max > 0);
max_lifetime = max;
}
float getXVar() const
{
return x_var;
}
void setXVar(float var)
{
ASSERT(var > 0);
x_var = var;
}
float getZVar() const
{
return z_var;
}
void setZVar(float var)
{
ASSERT(var > 0);
z_var = var;
}
float getYMin() const
{
return y_min;
}
void setYMin(float min)
{
y_min = min;
}
float getYMax() const
{
return y_max;
}
void setYMax(float max)
{
y_max = max;
}
Color3f getColor() const
{
return color;
}
void setColor(const Color3f col)
{
color = col;
}
};
class Snow : public ParticleSystem
{
private:
float max_v, min_v;
void initFirst(Particle &);
void init(Particle &);
public:
Snow(int num = NUM_PARTICLES, const Color3f &col = SNOW_COLOR, float offset = SNOW_PARTICLE_OFFSET,
float grav = GRAVITY, float max_velocity = SNOW_MAX_VELOCITY, float min_velocity = SNOW_MIN_VELOCITY) :
ParticleSystem(num, col, grav, offset), max_v(SNOW_MAX_VELOCITY), min_v(min_velocity)
{
for (int i = 0; i < num_particles; ++i)
initFirst(particles[i]);
}
void update();
};
class Fireworks : public ParticleSystem
{
private:
int max_lifetime, rocket_lifetime;
Vector3f rocket_velocity;
Point3f rocket_pos;
bool exploded, _dead;
float radius, speed;
public:
Fireworks(int num = NUM_PARTICLES, int max_life = FIREWORKS_MAX_LIFETIME, int rocket_life = FIREWORKS_ROCKET_LIFETIME, Vector3f rocket_v =
Vector3f(FIREWORKS_ROCKET_X, FIREWORKS_ROCKET_Y, FIREWORKS_ROCKET_Z), const Color3f &col = FIREWORKS_COLOR,
float grav = FIREWORKS_GRAVITY, float _speed = FIREWORKS_SPEED, float rad = FIREWORKS_RADIUS) : ParticleSystem(num, col, grav),
max_lifetime(max_life), rocket_lifetime(rocket_life), rocket_velocity(rocket_v),
exploded(false), radius(rad), speed(_speed)
{
init();
}
void init();
void update();
void render() const;
int getMaxLifetime() const
{
return max_lifetime;
}
void setMaxLifetime(int life)
{
ASSERT(life > 0);
max_lifetime = life;
}
bool dead() const
{
return _dead;
}
};
inline float frand()
{
return (float)rand() / RAND_MAX;
}
inline float randInRange(float range)
{
return frand() * range * 2 - range;
}
inline float absRandInRange(float range)
{
return frand() * range;
}
inline Vector3f randVec(const Vector3f &vec)
{
return Vector3f(randInRange(vec.getX()), randInRange(vec.getY()), randInRange(vec.getZ()));
}
inline Vector3f absRandVec(const Vector3f &vec)
{
return Vector3f(absRandInRange(vec.getX()), absRandInRange(vec.getY()), absRandInRange(vec.getZ()));
}
inline bool back(Particle *first, Particle *second)
{
return first->pos.getZ() < second->pos.getZ();
}
#endif