3#define GLM_ENABLE_EXPERIMENTAL
4#include <glm/gtc/random.hpp>
5#include <glm/gtx/rotate_vector.hpp>
6#include <zen/renderer/ZEN_Shader.h>
7#include <zen/renderer/ZEN_VertexArray.h>
8#include <zen/time/ZEN_DeltaTime.h>
9#include <zen/zen_pch.h>
13 glm::vec2 position{0};
14 glm::vec2 velocity{0};
16 float sizeBegin{8.0f};
18 glm::vec4 colourBegin{1.0f, 1.0f, 1.0f, 1.0f};
19 glm::vec4 colourEnd{1.0f, 1.0f, 1.0f, 0};
25 float lifeRemaining{0};
27 float sizeBegin{8.0f};
29 glm::vec4 colourBegin{1.0f};
30 glm::vec4 colourEnd{0, 0, 0, 0};
41 float speedMinMul = 0.5f;
42 float speedMaxMul = 1.5f;
43 float noiseSigma = 1.0f;
46 inline glm::vec2 sampleVelocityFromBase(glm::vec2 baseVelocity,
const VelocityRandomizer &vRand) {
47 float baseSpeed = glm::length(baseVelocity);
48 glm::vec2 direction = (baseSpeed > 0) ? baseVelocity / baseSpeed : glm::vec2{0, 1};
50 float theta = glm::radians(glm::linearRand(-vRand.coneDeg, vRand.coneDeg));
51 direction = glm::rotate(direction, theta);
53 float k = glm::linearRand(vRand.speedMinMul, vRand.speedMaxMul);
54 float speed = baseSpeed * k;
56 glm::vec2 noise{glm::linearRand(-vRand.noiseSigma, vRand.noiseSigma),
57 glm::linearRand(-vRand.noiseSigma, vRand.noiseSigma)};
58 return direction * speed + noise;
61 struct ParticleEmitter {
63 glm::vec2 size{1.0f, 1.0f};
64 glm::vec4 colour{1.0f, 1.0f, 1.0f, 1.0f};
68 float spawnRate = 30.0f;
69 float emitAccumulator = 0;
71 ParticleEmitter(
const glm::vec2 &position,
const glm::vec2 &sz,
const glm::vec4 &col)
72 : pos(position), size(sz), colour(col) {
73 props.colourBegin = col;
74 props.position = position;
75 props.sizeBegin = sz.x;
79 class ParticleSystem {
81 explicit ParticleSystem(
size_t maxParticles = 1000);
90 const std::shared_ptr<VertexArray> &vao()
const {
return m_vao; }
91 std::shared_ptr<Shader> &shader() {
return m_shader; }
93 size_t capacity()
const {
return m_max; }
96 void updateEmitters(std::vector<ParticleEmitter> &emitters,
DeltaTime deltaTime);
101 std::vector<Particle> m_pool;
102 size_t m_poolIndex{0};
104 std::vector<QuadVertex> m_cpuQuad;
106 std::shared_ptr<VertexArray> m_vao;
107 std::shared_ptr<VertexBuffer> m_vbo;
108 std::shared_ptr<IndexBuffer> m_ibo;
109 std::shared_ptr<Shader> m_shader;