-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.h
67 lines (55 loc) · 1.87 KB
/
particles.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
#ifndef PARTICLES_H
#define PARTICLES_H
#include <vector>
#include <glm/glm.hpp>
#include "object.h"
#include "shader.h"
struct Particle {
glm::vec3 direction;
glm::vec3 position;
glm::mat4 model;
float life;
};
const float PARTICLE_SPEED = 300.0f;
const float PARTICLE_LIFE = 5.0f;
class Particles {
public:
Particles(Shader *particleShader, Object *particleObject){
this->particleShader = particleShader;
this->particleObject = particleObject;
}
void addNew(glm::vec3 direction, glm::vec3 position, glm::mat4 model){
Particle * part = new Particle();
part->direction = direction;
part->position = position;
part->model = model;
part->life = PARTICLE_LIFE;
particles.push_back(*part);
}
void update(float deltaTime){
//remove every dead particles
while(!particles.empty() && particles.front().life <= deltaTime)
particles.erase(particles.begin());
//update particles life and position
for(auto partIt = particles.begin(); partIt != particles.end(); ++ partIt){
partIt->life -= deltaTime;
partIt->position += (deltaTime * PARTICLE_SPEED) * partIt->direction;
}
}
void draw(){
for(auto partIt = particles.begin(); partIt != particles.end(); ++ partIt){
glm::mat4 modelParticle = glm::mat4(1.0f);
modelParticle = glm::scale(modelParticle, glm::vec3(0.4f, 0.04f, 0.04f));
glm::mat4 modelTransform = partIt->model;
modelTransform[3] = glm::vec4(partIt->position,1);
modelParticle = modelTransform * modelParticle;
particleShader->setMatrix4("M", modelParticle);
particleObject->draw();
}
}
private:
std::vector<Particle> particles;
Shader *particleShader;
Object *particleObject;
};
#endif