SuzanneIsland: An island of Real-time rendering effects!
particlesystem.h
1 #pragma once
2 
3 #include <vector>
4 #include <memory>
5 #include <algorithm>
6 
7 #include <glm/gtc/type_ptr.hpp>
8 #include <glm/gtx/norm.hpp>
9 
10 #include "../sceneobject.hpp"
11 #include "../shader.h"
12 #include "../texture.hpp"
13 
14 
15 struct Particle
16 {
17  glm::vec3 pos, velocity;
18  GLfloat viewDepth = 0.0f; // for sorting
19  GLfloat timeToLive = 1000.0f; // seconds
20 
21  // for sorting in back to front drawing order.
22  // this is needed for alpha blending since zbuffer test rejects fragments that lie behind,
23  // but their color data is needed for blending.
24  bool operator<(Particle& other)
25  {
26  return this->viewDepth > other.viewDepth;
27  }
28 };
29 
30 
32 {
33 public:
34  ParticleSystem(const glm::mat4 &matrix_, const std::string &texturePath, int maxParticleCount_, float spawnRate_, float timeToLive_, float gravity_);
35  ~ParticleSystem();
36 
38  void update(
39  float timeDelta,
40  const glm::mat4 &viewMat
41  );
42 
44  void draw(const glm::mat4 &viewMat, const glm::mat4 &projMat, const glm::vec3 &color);
45 
47  void respawn(glm::vec3 location);
48 
49 private:
50  GLuint vao;
51  GLuint particleQuadVBO;
52  GLuint particleInstanceDataVBO;
53 
54  Shader *particleShader = nullptr;
55  Texture *particleTexture = nullptr;
56 
57  unsigned int maxParticleCount = 1000; // maximum total particle count
58  bool spawningPaused = true;
59  float spawnRate = 200.0f; // how many particles to spawn per second
60  float secondsSinceLastSpawn = -1.0f; // time since last spawned particle, in seconds
61  float timeToLive = 10.0f; // time in seconds until particle disappears
62  float gravity = 0.1f; // factor for gravitational acceleration
63 
64  std::vector<std::shared_ptr<Particle>> particles;
65 
68  float randomFloat();
69 };
70 
Texture class.
Definition: texture.hpp:13
Shader class.
Definition: shader.h:15
Definition: particlesystem.h:15
Definition: particlesystem.h:31
A base class for all scene objects.
Definition: sceneobject.hpp:15