-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleSystem.cpp
96 lines (87 loc) · 2.72 KB
/
ParticleSystem.cpp
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
#include <apollo/Graphics/ParticleSystem.h>
#include <apollo/Utilities/GameTime.h>
#include <apollo/Graphics/ImageLoader.h>
#include <apollo/Utilities/RNG.h>
#include <apollo/Graphics/Shaders.h>
#include <map>
#include <assert.h>
namespace Graphics
{
std::map<std::string, GLuint> particles;
GLuint GetParticleTexture ( const std::string& name )
{
std::map<std::string, GLuint>::iterator iter = particles.find(name);
if (iter == particles.end())
{
SDL_Surface* sfc = ImageLoader::LoadImage("Particles/" + name + ".png");
assert(sfc);
GLuint texture = ImageLoader::CreateTexture(sfc, true, false);
particles.insert(std::make_pair(name, texture));
return texture;
}
else
{
return iter->second;
}
}
static RNG particleRNG (17);
ParticleSystem::ParticleSystem ( const std::string& name, unsigned long pcount, float sf, vec2 centre, vec2 averageVelocity, vec2 velocityVariance, vec2 accel, float lifetime )
: count(pcount), acceleration(accel), sizeFactor(sf)
{
size = vec2(6.0f, 6.0f);
baseTime = GameTime();
lastTime = baseTime;
expireTime = baseTime + lifetime;
velocity = new vec2[pcount];
position = new vec2[pcount];
for (unsigned long i = 0; i < pcount; i++)
{
position[i] = centre;
velocity[i].X() = averageVelocity.X() + particleRNG.RandomFloat(-velocityVariance.X(), velocityVariance.X());
velocity[i].Y() = averageVelocity.Y() + particleRNG.RandomFloat(-velocityVariance.Y(), velocityVariance.Y());
}
texID = GetParticleTexture(name);
}
void ParticleSystem::Draw ()
{
float delta = (lastTime - baseTime) / (expireTime - baseTime);
vec2 targetSize = size * sizeFactor;
vec2 halfParticleSize = ((targetSize*delta) + (size*(1.0f-delta))) * 0.25f;
vec2 halfParticleYI = vec2(halfParticleSize.X(), -halfParticleSize.Y());
glBindTexture(GL_TEXTURE_2D, texID);
assert(texID);
vec2 particles[count*4];
vec2 texCoords[count*4];
for (unsigned long i = 0; i < count; i++)
{
vec2 pos = position[i];
particles[(i*4)+0] = pos - halfParticleSize;
particles[(i*4)+1] = pos - halfParticleYI;
particles[(i*4)+2] = pos + halfParticleSize;
particles[(i*4)+3] = pos + halfParticleYI;
texCoords[(i*4)+0] = vec2(0.0f, 0.0f);
texCoords[(i*4)+1] = vec2(0.0f, 1.0f);
texCoords[(i*4)+2] = vec2(1.0f, 1.0f);
texCoords[(i*4)+3] = vec2(1.0f, 0.0f);
}
glFlush();
glUniform1f(Shaders::UniformLocation("Alpha"), 1.0f-delta);
glVertexPointer(2, GL_FLOAT, 0, particles);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_QUADS, 0, count*4);
}
bool ParticleSystem::Update ()
{
float time = GameTime();
float dt = time - baseTime;
if (time > expireTime)
return true;
for (unsigned long i = 0; i < count; i++)
{
velocity[i] += acceleration * dt;
position[i] += velocity[i] * dt;
}
lastTime = time;
return false;
}
}