7
7
8
8
ParticleSystem::ParticleSystem ()
9
9
{
10
+ ParticleMaterial* material = new ParticleMaterial;
11
+ setMaterial (material);
10
12
mesh_ = new Mesh;
11
13
}
12
14
@@ -20,10 +22,14 @@ void ParticleSystem::renderGeometry(DrawContext &context)
20
22
{
21
23
update (context.deltaTime );
22
24
25
+ if (particles_.size () < 1 ) {
26
+ return ;
27
+ }
28
+
23
29
VertexBufferDesc desc;
24
30
desc.bufferData = particles_.data ();
25
31
desc.bufferSize = particles_.size () * sizeof (Particle);
26
- desc.vertexElementSizes = {3 , 1 , 1 };
32
+ desc.vertexElementSizes = {3 , 1 , 1 , 3 , 1 };
27
33
desc.stride = sizeof (Particle);
28
34
29
35
mesh_->setVertexBuffer (desc, PrimitiveType::Points);
@@ -33,16 +39,73 @@ void ParticleSystem::renderGeometry(DrawContext &context)
33
39
34
40
void ParticleSystem::setParticleTexture (Texture *texture)
35
41
{
36
- ParticleMaterial* material = new ParticleMaterial;
42
+ ParticleMaterial* material = static_cast < ParticleMaterial *>(material_) ;
37
43
material->setTexture (texture);
38
- setMaterial (material);
39
44
}
40
45
41
46
void ParticleSystem::update (float dt)
42
47
{
43
- for (Particle& particle : particles_) {
44
- particle.position += {randf () * 10 * dt, randf () * 10 * dt, randf () * 10 * dt};
45
- particle.size += 0.5 * dt;
46
- particle.rotation += 90 * dt;
48
+ timeElapsed_ += dt;
49
+
50
+ auto it = begin (particles_);
51
+ while (it != end (particles_)) {
52
+ Particle& particle = *it;
53
+ particle.life += dt;
54
+ if (particle.life >= particle.maxLife ) {
55
+ particles_.erase (it);
56
+ }
57
+ else {
58
+ this ->updateParticle (particle, dt);
59
+ it++;
60
+ }
61
+ }
62
+
63
+ float numParticles = emissionRate_ * dt;
64
+ numParticlesToSpawn_ += numParticles;
65
+
66
+ uint32_t count = 0 ;
67
+ if (particles_.size () < maxParticleCount_) {
68
+ count = min ((int )(maxParticleCount_ - particles_.size ()), (int )numParticlesToSpawn_);
69
+ }
70
+
71
+ numParticlesToSpawn_ -= count;
72
+
73
+ for (uint32_t i = 0 ; i < count; i++) {
74
+ Particle particle;
75
+ this ->spawnParticle (&particle);
76
+ particles_.push_back (particle);
47
77
}
48
78
}
79
+
80
+ void ParticleSystem::spawnParticle (Particle *particle)
81
+ {
82
+
83
+ }
84
+
85
+ void ParticleSystem::updateParticle (Particle &particle, float dt)
86
+ {
87
+
88
+ }
89
+
90
+ uint32_t ParticleSystem::emissionRate () const
91
+ {
92
+ return emissionRate_;
93
+ }
94
+
95
+ void ParticleSystem::setEmissionRate (int emissionRate)
96
+ {
97
+ emissionRate_ = emissionRate;
98
+ }
99
+
100
+ uint32_t ParticleSystem::maxParticleCount () const
101
+ {
102
+ return maxParticleCount_;
103
+ }
104
+
105
+ void ParticleSystem::setMaxParticleCount (int maxParticleCount)
106
+ {
107
+ maxParticleCount_ = maxParticleCount;
108
+ }
109
+
110
+
111
+
0 commit comments