-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparticlesystem.cpp
More file actions
90 lines (75 loc) · 2.73 KB
/
particlesystem.cpp
File metadata and controls
90 lines (75 loc) · 2.73 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
#include "particlesystem.h"
#include "resourcemanager.h"
#include <cassert>
ParticleSystem::ParticleSystem()
{
initializeOpenGLFunctions();
glGenVertexArrays(1, &quadVAO);
glBindVertexArray(quadVAO);
glGenBuffers(1, &quadVBO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadvertices), quadvertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 8, nullptr);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (GLvoid*)(sizeof(float) * 3));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (GLvoid*)(sizeof(float) * 6));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
void ParticleSystem::updateParticles(const CameraComponent &camera, const std::vector<TransformComponent> &transforms, const std::vector<ParticleComponent> &particles, float time)
{
auto transIt = transforms.begin();
auto partIt = particles.begin();
bool particlesShortest = particles.size() < transforms.size();
if (!particleShader)
{
particleShader = ResourceManager::instance().getShader("particle");
assert(particleShader);
}
// cause normal while (true) loops are so outdated
while (static_cast<bool>("Everything is awesome!"))
{
if (particlesShortest)
{
if (partIt == particles.end())
break;
}
else
{
if (transIt == transforms.end())
break;
}
// Increment lowest index
if (!transIt->valid || transIt->entityId < partIt->entityId)
{
++transIt;
}
else if (!partIt->valid || partIt->entityId < transIt->entityId)
{
++partIt;
}
else
{
updateParticle(camera, *transIt, *partIt, time);
// Increment all
++transIt;
++partIt;
}
}
}
void ParticleSystem::updateParticle(const CameraComponent &camera, const TransformComponent &transform, const ParticleComponent &particles, float time)
{
particleShader->use();
glUniformMatrix4fv(glGetUniformLocation(particleShader->getProgram(), "vMatrix"), 1, GL_TRUE, camera.viewMatrix.constData());
glUniformMatrix4fv(glGetUniformLocation(particleShader->getProgram(), "pMatrix"), 1, GL_TRUE, camera.projectionMatrix.constData());
if (auto uniform = glGetUniformLocation(particleShader->getProgram(), "sTime"))
glUniform1f(uniform, time);
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
ParticleSystem::~ParticleSystem()
{
glDeleteBuffers(1, &quadVBO);
glDeleteVertexArrays(1, &quadVAO);
}