Zen 0.3.0
Loading...
Searching...
No Matches
ZEN_ParticleTestLayer.cpp
1
2#include <glm/gtc/random.hpp>
3#include <glm/gtc/type_ptr.hpp>
4#include <include/imgui/imgui.h>
5#include <zen/particles/ZEN_ParticleTestLayer.h>
6#include <zen/renderer/ZEN_RenderCommand.h>
7#include <zen/renderer/ZEN_Renderer.h>
8
9namespace Zen {
10 void ParticleTestLayer::onAttach() {
11 glEnable(GL_BLEND);
12 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
13
14 m_camera.setPosition({0.0f, 0.0f, 0.0f});
15 m_camera.setOrthographic(-10.0f, 10.0f, -5.625f, 5.625f);
16
17 m_ps = std::make_unique<ParticleSystem>(2000);
18 m_particle.position = {0.0f, 0.0f};
19 m_particle.velocity = {(float)(((rand() % 400) / 100.0) - 2.0),
20 (float)(((rand() % 300) / 100.0) + 2.0)};
21 m_particle.lifeTime = 3.0f;
22 m_particle.sizeBegin = 1.0f;
23 m_particle.sizeEnd = 0.0f;
24 m_particle.colourBegin = {1.0f, 0.7f, 0.3f, 1.0f};
25 m_particle.colourEnd = {1.0f, 0.2f, 0.0f, 1.0f};
26 }
27
28 void ParticleTestLayer::onUpdate(DeltaTime deltaTime) {
29 m_cameraController.onUpdate(deltaTime);
30 RenderCommand::setClearColour({0.1f, 0.1f, 0.1f, 1.0f});
31 RenderCommand::clear();
32
33 float rate = m_spawnRate;
34 float period = 1.0f / glm::max(rate, 0.001f);
35
36 m_emitAccumulator += deltaTime.seconds();
37 while (m_emitAccumulator > period) {
38 m_particle.velocity = {(float)(((rand() % 400) / 100.0) - 2.0),
39 (float)(((rand() % 300) / 100.0) + 2.0)};
40 m_ps->emit(m_particle);
41 m_emitAccumulator -= period;
42 }
43
44 m_ps->update(deltaTime);
45 m_ps->upload();
46
47 Renderer::beginScene(m_camera);
48 Renderer::submit(m_ps->shader(), m_ps->vao());
49 Renderer::endScene();
50 }
51
52 bool ParticleTestLayer::onEvent(const ZenEvent &event) {
53 // m_cameraController.onEvent(event);
54 return false;
55 }
56
57 void ParticleTestLayer::onGUIRender() {
58 ImGui::Begin("Particle Settings");
59 ImGui::ColorEdit4("Birth Color", glm::value_ptr(m_particle.colourBegin));
60 ImGui::ColorEdit4("Death Color", glm::value_ptr(m_particle.colourEnd));
61 if (ImGui::DragFloat("Lifetime", &m_particle.lifeTime, 0.1f, 0.01f, 1000.0f)) {
62 m_ps->clear();
63 }
64 ImGui::DragFloat("Size (beginning)", &m_particle.sizeBegin, 0.1f, 0.1f, 100.0f);
65 ImGui::DragFloat("Size (end)", &m_particle.sizeEnd, 0.1f, 0.0f, 100.0f);
66 ImGui::DragFloat("Spawn Rate (p/s)", &m_spawnRate, 1.0f, 1.0f, 1000.0f);
67 ImGui::End();
68 }
69} // namespace Zen