File: ParticleEmitterType.cpp

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (595 lines) | stat: -rw-r--r-- 18,714 bytes parent folder | download | duplicates (2)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* Copyright (C) 2022 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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.
 *
 * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "ParticleEmitterType.h"

#include "graphics/Color.h"
#include "graphics/ParticleEmitter.h"
#include "graphics/ParticleManager.h"
#include "graphics/TextureManager.h"
#include "maths/MathUtil.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/XML/Xeromyces.h"
#include "renderer/Renderer.h"

#include <boost/random/uniform_real_distribution.hpp>


/**
 * Interface for particle state variables, which get evaluated for each newly
 * constructed particle.
 */
class IParticleVar
{
public:
	IParticleVar() : m_LastValue(0) { }
	virtual ~IParticleVar() {}

	/// Computes and returns a new value.
	float Evaluate(CParticleEmitter& emitter)
	{
		m_LastValue = Compute(*emitter.m_Type, emitter);
		return m_LastValue;
	}

	/**
	 * Returns the last value that Evaluate returned.
	 * This is used for variables that depend on other variables (which is kind
	 * of fragile since it's very order-dependent), so they don't get re-randomised
	 * and don't have a danger of infinite recursion.
	 */
	float LastValue() { return m_LastValue; }

	/**
	 * Returns the minimum value that Evaluate might ever return,
	 * for computing bounds.
	 */
	virtual float Min(CParticleEmitterType& type) = 0;

	/**
	 * Returns the maximum value that Evaluate might ever return,
	 * for computing bounds.
	 */
	virtual float Max(CParticleEmitterType& type) = 0;

protected:
	virtual float Compute(CParticleEmitterType& type, CParticleEmitter& emitter) = 0;

private:
	float m_LastValue;
};

/**
 * Particle variable that returns a constant value.
 */
class CParticleVarConstant : public IParticleVar
{
public:
	CParticleVarConstant(float val) :
		m_Value(val)
	{
	}

	virtual float Compute(CParticleEmitterType& UNUSED(type), CParticleEmitter& UNUSED(emitter))
	{
		return m_Value;
	}

	virtual float Min(CParticleEmitterType& UNUSED(type))
	{
		return m_Value;
	}

	virtual float Max(CParticleEmitterType& UNUSED(type))
	{
		return m_Value;
	}

private:
	float m_Value;
};

/**
 * Particle variable that returns a uniformly-distributed random value.
 */
class CParticleVarUniform : public IParticleVar
{
public:
	CParticleVarUniform(float min, float max) :
		m_Min(min), m_Max(max)
	{
	}

	virtual float Compute(CParticleEmitterType& type, CParticleEmitter& UNUSED(emitter))
	{
		return boost::random::uniform_real_distribution<float>(m_Min, m_Max)(type.m_Manager.m_RNG);
	}

	virtual float Min(CParticleEmitterType& UNUSED(type))
	{
		return m_Min;
	}

	virtual float Max(CParticleEmitterType& UNUSED(type))
	{
		return m_Max;
	}

private:
	float m_Min;
	float m_Max;
};

/**
 * Particle variable that returns the same value as some other variable
 * (assuming that variable was evaluated before this one).
 */
class CParticleVarCopy : public IParticleVar
{
public:
	CParticleVarCopy(int from) :
		m_From(from)
	{
	}

	virtual float Compute(CParticleEmitterType& type, CParticleEmitter& UNUSED(emitter))
	{
		return type.m_Variables[m_From]->LastValue();
	}

	virtual float Min(CParticleEmitterType& type)
	{
		return type.m_Variables[m_From]->Min(type);
	}

	virtual float Max(CParticleEmitterType& type)
	{
		return type.m_Variables[m_From]->Max(type);
	}

private:
	int m_From;
};

/**
 * A terrible ad-hoc attempt at handling some particular variable calculation,
 * which really needs to be cleaned up and generalised.
 */
class CParticleVarExpr : public IParticleVar
{
public:
	CParticleVarExpr(const CStr& from, float mul, float max) :
		m_From(from), m_Mul(mul), m_Max(max)
	{
	}

	virtual float Compute(CParticleEmitterType& UNUSED(type), CParticleEmitter& emitter)
	{
		return std::min(m_Max, emitter.m_EntityVariables[m_From] * m_Mul);
	}

	virtual float Min(CParticleEmitterType& UNUSED(type))
	{
		return 0.f;
	}

	virtual float Max(CParticleEmitterType& UNUSED(type))
	{
		return m_Max;
	}

private:
	CStr m_From;
	float m_Mul;
	float m_Max;
};



/**
 * Interface for particle effectors, which get evaluated every frame to
 * update particles.
 */
class IParticleEffector
{
public:
	IParticleEffector() { }
	virtual ~IParticleEffector() {}

	/// Updates all particles.
	virtual void Evaluate(std::vector<SParticle>& particles, float dt) = 0;

	/// Returns maximum acceleration caused by this effector.
	virtual CVector3D Max() = 0;

};

/**
 * Particle effector that applies a constant acceleration.
 */
class CParticleEffectorForce : public IParticleEffector
{
public:
	CParticleEffectorForce(float x, float y, float z) :
		m_Accel(x, y, z)
	{
	}

	virtual void Evaluate(std::vector<SParticle>& particles, float dt)
	{
		CVector3D dv = m_Accel * dt;

		for (size_t i = 0; i < particles.size(); ++i)
			particles[i].velocity += dv;
	}

	virtual CVector3D Max()
	{
		return m_Accel;
	}

private:
	CVector3D m_Accel;
};




CParticleEmitterType::CParticleEmitterType(const VfsPath& path, CParticleManager& manager) :
	m_Manager(manager)
{
	LoadXML(path);
	// TODO: handle load failure

	// Upper bound on number of particles depends on maximum rate and lifetime
	m_MaxLifetime = m_Variables[VAR_LIFETIME]->Max(*this);
	m_MaxParticles = ceil(m_Variables[VAR_EMISSIONRATE]->Max(*this) * m_MaxLifetime);


	// Compute the worst-case bounds of all possible particles,
	// based on the min/max values of positions and velocities and accelerations
	// and sizes. (This isn't a guaranteed bound but it should be sufficient for
	// culling.)

	// Assuming constant acceleration,
	//        p = p0 + v0*t + 1/2 a*t^2
	// => dp/dt = v0 + a*t
	//          = 0 at t = -v0/a
	// max(p) is at t=0, or t=tmax, or t=-v0/a if that's between 0 and tmax
	// => max(p) = max(p0, p0 + v0*tmax + 1/2 a*tmax, p0 - 1/2 v0^2/a)

	// Compute combined acceleration (assume constant)
	CVector3D accel;
	for (size_t i = 0; i < m_Effectors.size(); ++i)
		accel += m_Effectors[i]->Max();

	CVector3D vmin(m_Variables[VAR_VELOCITY_X]->Min(*this), m_Variables[VAR_VELOCITY_Y]->Min(*this), m_Variables[VAR_VELOCITY_Z]->Min(*this));
	CVector3D vmax(m_Variables[VAR_VELOCITY_X]->Max(*this), m_Variables[VAR_VELOCITY_Y]->Max(*this), m_Variables[VAR_VELOCITY_Z]->Max(*this));

	// Start by assuming p0 = 0; compute each XYZ component individually
	m_MaxBounds.SetEmpty();
	// Lower/upper bounds at t=0, t=tmax
	m_MaxBounds[0].X = std::min(0.f, vmin.X*m_MaxLifetime + 0.5f*accel.X*m_MaxLifetime*m_MaxLifetime);
	m_MaxBounds[0].Y = std::min(0.f, vmin.Y*m_MaxLifetime + 0.5f*accel.Y*m_MaxLifetime*m_MaxLifetime);
	m_MaxBounds[0].Z = std::min(0.f, vmin.Z*m_MaxLifetime + 0.5f*accel.Z*m_MaxLifetime*m_MaxLifetime);
	m_MaxBounds[1].X = std::max(0.f, vmax.X*m_MaxLifetime + 0.5f*accel.X*m_MaxLifetime*m_MaxLifetime);
	m_MaxBounds[1].Y = std::max(0.f, vmax.Y*m_MaxLifetime + 0.5f*accel.Y*m_MaxLifetime*m_MaxLifetime);
	m_MaxBounds[1].Z = std::max(0.f, vmax.Z*m_MaxLifetime + 0.5f*accel.Z*m_MaxLifetime*m_MaxLifetime);
	// Extend bounds to include position at t where dp/dt=0, if 0 < t < tmax
	if (accel.X && 0 < -vmin.X/accel.X && -vmin.X/accel.X < m_MaxLifetime)
		m_MaxBounds[0].X = std::min(m_MaxBounds[0].X, -0.5f*vmin.X*vmin.X / accel.X);
	if (accel.Y && 0 < -vmin.Y/accel.Y && -vmin.Y/accel.Y < m_MaxLifetime)
		m_MaxBounds[0].Y = std::min(m_MaxBounds[0].Y, -0.5f*vmin.Y*vmin.Y / accel.Y);
	if (accel.Z && 0 < -vmin.Z/accel.Z && -vmin.Z/accel.Z < m_MaxLifetime)
		m_MaxBounds[0].Z = std::min(m_MaxBounds[0].Z, -0.5f*vmin.Z*vmin.Z / accel.Z);
	if (accel.X && 0 < -vmax.X/accel.X && -vmax.X/accel.X < m_MaxLifetime)
		m_MaxBounds[1].X = std::max(m_MaxBounds[1].X, -0.5f*vmax.X*vmax.X / accel.X);
	if (accel.Y && 0 < -vmax.Y/accel.Y && -vmax.Y/accel.Y < m_MaxLifetime)
		m_MaxBounds[1].Y = std::max(m_MaxBounds[1].Y, -0.5f*vmax.Y*vmax.Y / accel.Y);
	if (accel.Z && 0 < -vmax.Z/accel.Z && -vmax.Z/accel.Z < m_MaxLifetime)
		m_MaxBounds[1].Z = std::max(m_MaxBounds[1].Z, -0.5f*vmax.Z*vmax.Z / accel.Z);

	// Offset by the initial positions
	m_MaxBounds[0] += CVector3D(m_Variables[VAR_POSITION_X]->Min(*this), m_Variables[VAR_POSITION_Y]->Min(*this), m_Variables[VAR_POSITION_Z]->Min(*this));
	m_MaxBounds[1] += CVector3D(m_Variables[VAR_POSITION_X]->Max(*this), m_Variables[VAR_POSITION_Y]->Max(*this), m_Variables[VAR_POSITION_Z]->Max(*this));
}

int CParticleEmitterType::GetVariableID(const std::string& name)
{
	if (name == "emissionrate")		return VAR_EMISSIONRATE;
	if (name == "lifetime")			return VAR_LIFETIME;
	if (name == "position.x")		return VAR_POSITION_X;
	if (name == "position.y")		return VAR_POSITION_Y;
	if (name == "position.z")		return VAR_POSITION_Z;
	if (name == "angle")			return VAR_ANGLE;
	if (name == "velocity.x")		return VAR_VELOCITY_X;
	if (name == "velocity.y")		return VAR_VELOCITY_Y;
	if (name == "velocity.z")		return VAR_VELOCITY_Z;
	if (name == "velocity.angle")	return VAR_VELOCITY_ANGLE;
	if (name == "size")				return VAR_SIZE;
	if (name == "size.growthRate")	return VAR_SIZE_GROWTHRATE;
	if (name == "color.r")			return VAR_COLOR_R;
	if (name == "color.g")			return VAR_COLOR_G;
	if (name == "color.b")			return VAR_COLOR_B;
	LOGWARNING("Particle sets unknown variable '%s'", name.c_str());
	return -1;
}

bool CParticleEmitterType::LoadXML(const VfsPath& path)
{
	// Initialise with sane defaults
	m_Variables.clear();
	m_Variables.resize(VAR__MAX);
	m_Variables[VAR_EMISSIONRATE] = IParticleVarPtr(new CParticleVarConstant(10.f));
	m_Variables[VAR_LIFETIME] = IParticleVarPtr(new CParticleVarConstant(3.f));
	m_Variables[VAR_POSITION_X] = IParticleVarPtr(new CParticleVarConstant(0.f));
	m_Variables[VAR_POSITION_Y] = IParticleVarPtr(new CParticleVarConstant(0.f));
	m_Variables[VAR_POSITION_Z] = IParticleVarPtr(new CParticleVarConstant(0.f));
	m_Variables[VAR_ANGLE] = IParticleVarPtr(new CParticleVarConstant(0.f));
	m_Variables[VAR_VELOCITY_X] = IParticleVarPtr(new CParticleVarConstant(0.f));
	m_Variables[VAR_VELOCITY_Y] = IParticleVarPtr(new CParticleVarConstant(1.f));
	m_Variables[VAR_VELOCITY_Z] = IParticleVarPtr(new CParticleVarConstant(0.f));
	m_Variables[VAR_VELOCITY_ANGLE] = IParticleVarPtr(new CParticleVarConstant(0.f));
	m_Variables[VAR_SIZE] = IParticleVarPtr(new CParticleVarConstant(1.f));
	m_Variables[VAR_SIZE_GROWTHRATE] = IParticleVarPtr(new CParticleVarConstant(0.f));
	m_Variables[VAR_COLOR_R] = IParticleVarPtr(new CParticleVarConstant(1.f));
	m_Variables[VAR_COLOR_G] = IParticleVarPtr(new CParticleVarConstant(1.f));
	m_Variables[VAR_COLOR_B] = IParticleVarPtr(new CParticleVarConstant(1.f));
	m_BlendMode = BlendMode::ADD;
	m_StartFull = false;
	m_UseRelativeVelocity = false;
	m_Texture = g_Renderer.GetTextureManager().GetErrorTexture();

	CXeromyces XeroFile;
	PSRETURN ret = XeroFile.Load(g_VFS, path, "particle");
	if (ret != PSRETURN_OK)
		return false;

	// Define all the elements and attributes used in the XML file
#define EL(x) int el_##x = XeroFile.GetElementID(#x)
#define AT(x) int at_##x = XeroFile.GetAttributeID(#x)
	EL(texture);
	EL(blend);
	EL(start_full);
	EL(use_relative_velocity);
	EL(constant);
	EL(uniform);
	EL(copy);
	EL(expr);
	EL(force);
	AT(mode);
	AT(name);
	AT(value);
	AT(min);
	AT(max);
	AT(mul);
	AT(from);
	AT(x);
	AT(y);
	AT(z);
#undef AT
#undef EL

	XMBElement Root = XeroFile.GetRoot();

	XERO_ITER_EL(Root, Child)
	{
		if (Child.GetNodeName() == el_texture)
		{
			CTextureProperties textureProps(Child.GetText().FromUTF8());
			textureProps.SetAddressMode(
				Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE);
			m_Texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
		}
		else if (Child.GetNodeName() == el_blend)
		{
			const CStr mode = Child.GetAttributes().GetNamedItem(at_mode);
			if (mode == "add")
				m_BlendMode = BlendMode::ADD;
			else if (mode == "subtract")
				m_BlendMode = BlendMode::SUBTRACT;
			else if (mode == "over")
				m_BlendMode = BlendMode::OVERLAY;
			else if (mode == "multiply")
				m_BlendMode = BlendMode::MULTIPLY;
		}
		else if (Child.GetNodeName() == el_start_full)
		{
			m_StartFull = true;
		}
		else if (Child.GetNodeName() == el_use_relative_velocity)
		{
			m_UseRelativeVelocity = true;
		}
		else if (Child.GetNodeName() == el_constant)
		{
			int id = GetVariableID(Child.GetAttributes().GetNamedItem(at_name));
			if (id != -1)
			{
				m_Variables[id] = IParticleVarPtr(new CParticleVarConstant(
					Child.GetAttributes().GetNamedItem(at_value).ToFloat()
				));
			}
		}
		else if (Child.GetNodeName() == el_uniform)
		{
			int id = GetVariableID(Child.GetAttributes().GetNamedItem(at_name));
			if (id != -1)
			{
				float min = Child.GetAttributes().GetNamedItem(at_min).ToFloat();
				float max = Child.GetAttributes().GetNamedItem(at_max).ToFloat();
				// To avoid hangs in the RNG, only use it if [min, max) is non-empty
				if (min < max)
					m_Variables[id] = IParticleVarPtr(new CParticleVarUniform(min, max));
				else
					m_Variables[id] = IParticleVarPtr(new CParticleVarConstant(min));
			}
		}
		else if (Child.GetNodeName() == el_copy)
		{
			int id = GetVariableID(Child.GetAttributes().GetNamedItem(at_name));
			int from = GetVariableID(Child.GetAttributes().GetNamedItem(at_from));
			if (id != -1 && from != -1)
				m_Variables[id] = IParticleVarPtr(new CParticleVarCopy(from));
		}
		else if (Child.GetNodeName() == el_expr)
		{
			int id = GetVariableID(Child.GetAttributes().GetNamedItem(at_name));
			CStr from = Child.GetAttributes().GetNamedItem(at_from);
			float mul = Child.GetAttributes().GetNamedItem(at_mul).ToFloat();
			float max = Child.GetAttributes().GetNamedItem(at_max).ToFloat();
			if (id != -1)
				m_Variables[id] = IParticleVarPtr(new CParticleVarExpr(from, mul, max));
		}
		else if (Child.GetNodeName() == el_force)
		{
			float x = Child.GetAttributes().GetNamedItem(at_x).ToFloat();
			float y = Child.GetAttributes().GetNamedItem(at_y).ToFloat();
			float z = Child.GetAttributes().GetNamedItem(at_z).ToFloat();
			m_Effectors.push_back(IParticleEffectorPtr(new CParticleEffectorForce(x, y, z)));
		}
	}

	return true;
}

void CParticleEmitterType::UpdateEmitter(CParticleEmitter& emitter, float dt)
{
	// If dt is very large, we should do the update in multiple small
	// steps to prevent all the particles getting clumped together at
	// low framerates

	const float maxStepLength = 0.2f;

	// Avoid wasting time by computing periods longer than the lifetime
	// period of the particles
	dt = std::min(dt, m_MaxLifetime);

	while (dt > maxStepLength)
	{
		UpdateEmitterStep(emitter, maxStepLength);
		dt -= maxStepLength;
	}

	UpdateEmitterStep(emitter, dt);
}

void CParticleEmitterType::UpdateEmitterStep(CParticleEmitter& emitter, float dt)
{
	ENSURE(emitter.m_Type.get() == this);

	if (emitter.m_Active)
	{
		float emissionRate = m_Variables[VAR_EMISSIONRATE]->Evaluate(emitter);

		// Find how many new particles to spawn, and accumulate any rounding errors
		// (to maintain a constant emission rate even if dt is very small)
		int newParticles = floor(emitter.m_EmissionRoundingError + dt*emissionRate);
		emitter.m_EmissionRoundingError += dt*emissionRate - newParticles;

		// If dt was very large, there's no point spawning new particles that
		// we'll immediately overwrite, so clamp it
		newParticles = std::min(newParticles, (int)m_MaxParticles);

		for (int i = 0; i < newParticles; ++i)
		{
			// Compute new particle state based on variables
			SParticle particle;

			particle.pos.X = m_Variables[VAR_POSITION_X]->Evaluate(emitter);
			particle.pos.Y = m_Variables[VAR_POSITION_Y]->Evaluate(emitter);
			particle.pos.Z = m_Variables[VAR_POSITION_Z]->Evaluate(emitter);
			particle.pos += emitter.m_Pos;

			if (m_UseRelativeVelocity)
			{
				float xVel = m_Variables[VAR_VELOCITY_X]->Evaluate(emitter);
				float yVel = m_Variables[VAR_VELOCITY_Y]->Evaluate(emitter);
				float zVel = m_Variables[VAR_VELOCITY_Z]->Evaluate(emitter);
				CVector3D EmitterAngle = emitter.GetRotation().ToMatrix().Transform(CVector3D(xVel,yVel,zVel));
				particle.velocity.X = EmitterAngle.X;
				particle.velocity.Y = EmitterAngle.Y;
				particle.velocity.Z = EmitterAngle.Z;
			} else {
				particle.velocity.X = m_Variables[VAR_VELOCITY_X]->Evaluate(emitter);
				particle.velocity.Y = m_Variables[VAR_VELOCITY_Y]->Evaluate(emitter);
				particle.velocity.Z = m_Variables[VAR_VELOCITY_Z]->Evaluate(emitter);
			}
			particle.angle = m_Variables[VAR_ANGLE]->Evaluate(emitter);
			particle.angleSpeed = m_Variables[VAR_VELOCITY_ANGLE]->Evaluate(emitter);

			particle.size = m_Variables[VAR_SIZE]->Evaluate(emitter);
			particle.sizeGrowthRate = m_Variables[VAR_SIZE_GROWTHRATE]->Evaluate(emitter);

			RGBColor color;
			color.X = m_Variables[VAR_COLOR_R]->Evaluate(emitter);
			color.Y = m_Variables[VAR_COLOR_G]->Evaluate(emitter);
			color.Z = m_Variables[VAR_COLOR_B]->Evaluate(emitter);
			particle.color = ConvertRGBColorTo4ub(color);

			particle.age = 0.f;
			particle.maxAge = m_Variables[VAR_LIFETIME]->Evaluate(emitter);

			emitter.AddParticle(particle);
		}
	}

	// Update particle states
	for (size_t i = 0; i < emitter.m_Particles.size(); ++i)
	{
		SParticle& p = emitter.m_Particles[i];

		// Don't bother updating particles already at the end of their life
		if (p.age > p.maxAge)
			continue;

		p.pos += p.velocity * dt;
		p.angle += p.angleSpeed * dt;
		p.age += dt;
		p.size += p.sizeGrowthRate * dt;

		// Make alpha fade in/out nicely
		// TODO: this should probably be done as a variable or something,
		// instead of hardcoding
		float ageFrac = p.age / p.maxAge;
		float a = std::min(1.f - ageFrac, 5.f * ageFrac);
		p.color.A = Clamp(static_cast<int>(a * 255.f), 0, 255);
	}

	for (size_t i = 0; i < m_Effectors.size(); ++i)
	{
		m_Effectors[i]->Evaluate(emitter.m_Particles, dt);
	}
}

CBoundingBoxAligned CParticleEmitterType::CalculateBounds(CVector3D emitterPos, CBoundingBoxAligned emittedBounds)
{
	CBoundingBoxAligned bounds = m_MaxBounds;
	bounds[0] += emitterPos;
	bounds[1] += emitterPos;

	bounds += emittedBounds;

	// The current bounds is for the particles' centers, so expand by
	// sqrt(2) * max_size/2 to ensure any rotated billboards fit in
	bounds.Expand(m_Variables[VAR_SIZE]->Max(*this)/2.f * sqrt(2.f));

	return bounds;
}