-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleGenerator.cpp
More file actions
110 lines (101 loc) · 2.97 KB
/
Copy pathParticleGenerator.cpp
File metadata and controls
110 lines (101 loc) · 2.97 KB
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
#include "ParticleGenerator.h"
ParticleGenerator::ParticleGenerator(Shader shader, Texture texture, unsigned int amount)
: shader(shader), texture(texture), amount(amount)
{
this->init();
}
void ParticleGenerator::Update(float dt, GameObj& obj, unsigned int newParticles, glm::vec2 offset)
{
// Add new particles
for (unsigned int i = 0; i < newParticles; ++i)
{
int unusedParticle = this->firstUnusedParticle();
this->respawnParticle(this->particles[unusedParticle], obj, offset);
}
// Update all exisiting particles
for (unsigned int i = 0; i < this->amount; ++i)
{
Particle& p = this->particles[i];
p.Life -= dt; // Reduce life of particle
// If the life of the particle is still alive
if (p.Life > 0.0f)
{
p.Position -= p.Velocity * dt; // Reduce velocity
p.Color.a -= dt * 2.5; // Reduce alpha
}
}
}
void ParticleGenerator::Draw()
{
// Use additive blending to give a glow effect
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
this->shader.Use();
for (Particle particle : this->particles)
{
if (particle.Life > 0.0f)
{
this->shader.SetVector2f("offset", particle.Position);
this->shader.SetVector4f("color", particle.Color);
this->texture.Bind();
glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
}
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void ParticleGenerator::init()
{
unsigned int VBO;
float particle_quad[] = {
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f
};
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(this->VAO);
// Fill mesh buffer
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(particle_quad), particle_quad, GL_STATIC_DRAW);
// Set mesh attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glBindVertexArray(0);
// Create this amount default particle instances
for (unsigned int i = 0; i < this->amount; ++i)
this->particles.push_back(Particle());
}
unsigned int lastUsedParticle = 0;
unsigned int ParticleGenerator::firstUnusedParticle()
{
// Search from last used particle.
for (unsigned int i = lastUsedParticle; i < this->amount; ++i) {
if (particles[i].Life <= 0.0f) {
lastUsedParticle = i;
return i;
}
}
// Else linear search
for (unsigned int i = 0; i < lastUsedParticle; ++i) {
if (particles[i].Life <= 0.0f) {
lastUsedParticle = i;
return i;
}
}
// Else nothing else worked, we just use the first particle again
lastUsedParticle = 0;
return 0;
}
void ParticleGenerator::respawnParticle(Particle& particle, GameObj& object, glm::vec2 offset)
{
float random = ((rand() & 100) - 50) / 10.0f;
float rColor = 0.5f + ((rand() % 100) / 100.0f);
particle.Position = object.position + random + offset;
particle.Color = glm::vec4(rColor, rColor, rColor, 1.0f);
particle.Life = 1.0f;
particle.Velocity = object.velocity * 0.1f;
}