-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSystem.cpp
More file actions
98 lines (85 loc) · 2.38 KB
/
ParticleSystem.cpp
File metadata and controls
98 lines (85 loc) · 2.38 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
#include "ParticleSystem.h"
#include "Window.h"
ParticleSystem::ParticleSystem()
{
init();
}
ParticleSystem::~ParticleSystem()
{
}
void ParticleSystem::init()
{
GLfloat 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, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(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(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
// Create this->amount default particle instances
for (GLuint i = 0; i < max; ++i) {
particles.push_back(Particle());
}
}
void ParticleSystem::draw(GLuint shader)
{
glUseProgram(shader);
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);
}
}
// Don't forget to reset to default blending mode
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void ParticleSystem::update()
{
}
GLuint lastUsedParticle = 0;
GLuint ParticleSystem::firstUnusedParticle()
{
// First search from last used particle, this will usually return almost instantly
for (GLuint i = lastUsedParticle; i < max; ++i) {
if (this->particles[i].Life <= 0.0f) {
lastUsedParticle = i;
return i;
}
}
// Otherwise, do a linear search
for (GLuint i = 0; i < lastUsedParticle; ++i) {
if (this->particles[i].Life <= 0.0f) {
lastUsedParticle = i;
return i;
}
}
// All particles are taken, override the first one (note that if it repeatedly hits this case, more particles should be reserved)
lastUsedParticle = 0;
return 0;
}
void ParticleSystem::respawnParticle(Particle & particle, glm::vec3 position, glm::vec3 velocity, glm::vec3 offset)
{
GLfloat random = ((rand() % 100) - 50) / 10.0f;
GLfloat rColor = 0.5 + ((rand() % 100) / 100.0f);
particle.Position = position + random + offset;
particle.Color = glm::vec4(rColor, rColor, rColor, 1.0f);
particle.Life = 1.0f;
particle.Velocity = velocity * 0.1f;
}