-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSystem.h
More file actions
105 lines (79 loc) · 2.36 KB
/
ParticleSystem.h
File metadata and controls
105 lines (79 loc) · 2.36 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
/*
* File: ParticleSystem.h
* Author: Evan
*
* Created on November 28, 2015, 1:32 PM
*/
#ifndef PARTICLESYSTEM_H
#define PARTICLESYSTEM_H
#include "Particle.h"
#include "SmartGeode.h"
#include "ParticleSource.h"
#include <omp.h>
class ParticleSystem : public SmartGeode {
public:
std::vector<Particle> parts;
const bool isInfinite;
const ParticleSource* source;
bool isFinished;
ParticleSystem(int size, ParticleSource* src, bool refresh = true, Vector3 pos = Vector3(0, 0, 0)) : SmartGeode(pos, 1, 1), parts(0), isInfinite(refresh) {
isFinished = false;
source = src;
transform.identify();
reset(size);
}
ParticleSystem(const ParticleSystem& orig) : SmartGeode(orig), isInfinite(orig.isInfinite) {
}
virtual ~ParticleSystem() {
}
void reset(int size) {
parts.clear();
parts.resize(size);
for (auto& par : parts) {
reset(par);
}
}
virtual void render() override {
if (isInfinite || !isFinished) {
// glEnable(GL_COLOR_MATERIAL);
// glEnable(GL_POINT_SMOOTH);
glPointSize(source->size);
for (auto& part : parts) {
part.draw();
}
glPointSize(1);
glEnd();
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Color::black().ptr());
// glDisable(GL_COLOR_MATERIAL);
}
}
void update2(Particle& part) {
if (part.isAlive)
isFinished = false;
source->update(part);
if (!part.update(source->survivalRate) && isInfinite) {
reset(part);
}
}
virtual void update(int tick) override {
if (isInfinite || !isFinished) {
__gnu_parallel::for_each(parts.begin(), parts.end(), [this](Particle & p) {
this->update2(p);
});
if (!isInfinite)
parts.erase(std::remove_if(parts.begin(), parts.end(), toDelete), parts.end());
}
}
static bool toDelete(Particle& p) {
return !p.isAlive;
}
private:
void reset(Particle& part) {
part.point = source->randOffset();
part.speed = source->speedGenerator(part.point);
part.color = source->colorGenerator();
part.isAlive = true;
isFinished = false;
}
};
#endif /* PARTICLESYSTEM_H */