SuperTuxKart
stk_particle.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2017 SuperTuxKart-Team
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 3
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 #ifndef SERVER_ONLY
19 
20 #ifndef HEADER_STK_PARTICLE_HPP
21 #define HEADER_STK_PARTICLE_HPP
22 
23 #include "graphics/gl_headers.hpp"
24 #include "../lib/irrlicht/source/Irrlicht/CParticleSystemSceneNode.h"
25 #include <cassert>
26 #include <vector>
27 
28 using namespace irr;
29 
30 struct CPUParticle;
31 
32 inline float glslSmoothstep(float edge0, float edge1, float x)
33 {
34  float t = core::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
35  return t * t * (3.0f - 2.0f * t);
36 }
37 
38 class STKParticle : public scene::CParticleSystemSceneNode
39 {
40 private:
41  // ------------------------------------------------------------------------
43  {
44  const std::vector<std::vector<float> > m_array;
45  const float m_x;
46  const float m_z;
47  const float m_x_len;
48  const float m_z_len;
49  // --------------------------------------------------------------------
50  HeightMapData(std::vector<std::vector<float> >& array,
51  float track_x, float track_z, float track_x_len,
52  float track_z_len)
53  : m_array(std::move(array)), m_x(track_x), m_z(track_z),
54  m_x_len(track_x_len), m_z_len(track_z_len) {}
55  };
56  // ------------------------------------------------------------------------
57  struct ParticleData
58  {
59  core::vector3df m_position;
60  float m_lifetime;
61  core::vector3df m_direction;
62  float m_size;
63  };
64  // ------------------------------------------------------------------------
65  HeightMapData* m_hm;
66 
67  std::vector<ParticleData> m_particles_generating, m_initial_particles;
68 
69  core::vector3df m_color_from, m_color_to;
70 
71  float m_size_increase_factor;
72 
73  bool m_first_execution, m_randomize_initial_y, m_flips, m_pre_generating;
74 
76  core::matrix4 m_previous_frame_matrix;
77 
79  unsigned m_max_count;
80 
81  static std::vector<float> m_flips_data;
82 
83  static GLuint m_flips_buffer;
84 
85  // ------------------------------------------------------------------------
86  void generateParticlesFromPointEmitter(scene::IParticlePointEmitter*);
87  // ------------------------------------------------------------------------
88  void generateParticlesFromBoxEmitter(scene::IParticleBoxEmitter*);
89  // ------------------------------------------------------------------------
90  void generateParticlesFromSphereEmitter(scene::IParticleSphereEmitter*);
91  // ------------------------------------------------------------------------
92  void stimulateHeightMap(float, unsigned int, std::vector<CPUParticle>*);
93  // ------------------------------------------------------------------------
94  void stimulateNormal(float, unsigned int, std::vector<CPUParticle>*);
95 
96 public:
97  // ------------------------------------------------------------------------
98  STKParticle(bool randomize_initial_y = false,
99  ISceneNode* parent = 0, s32 id = -1,
100  const core::vector3df& position = core::vector3df(0, 0, 0),
101  const core::vector3df& rotation = core::vector3df(0, 0, 0),
102  const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f));
103  // ------------------------------------------------------------------------
104  ~STKParticle()
105  {
106  delete m_hm;
107  }
108  // ------------------------------------------------------------------------
109  void setColorFrom(float r, float g, float b)
110  {
111  m_color_from.X = r;
112  m_color_from.Y = g;
113  m_color_from.Z = b;
114  }
115  // ------------------------------------------------------------------------
116  void setColorTo(float r, float g, float b)
117  {
118  m_color_to.X = r;
119  m_color_to.Y = g;
120  m_color_to.Z = b;
121  }
122  // ------------------------------------------------------------------------
123  virtual void setEmitter(scene::IParticleEmitter* emitter);
124  // ------------------------------------------------------------------------
125  virtual void OnRegisterSceneNode();
126  // ------------------------------------------------------------------------
127  void setIncreaseFactor(float val) { m_size_increase_factor = val; }
128  // ------------------------------------------------------------------------
129  void setHeightmap(std::vector<std::vector<float> >& array, float track_x,
130  float track_z, float track_x_len, float track_z_len)
131  {
132  m_hm = new HeightMapData(array, track_x, track_z, track_x_len,
133  track_z_len);
134  }
135  // ------------------------------------------------------------------------
136  void generate(std::vector<CPUParticle>* out);
137  // ------------------------------------------------------------------------
138  void setFlips() { m_flips = true; }
139  // ------------------------------------------------------------------------
140  virtual bool getFlips() const { return m_flips; }
141  // ------------------------------------------------------------------------
142  unsigned getMaxCount() const { return m_max_count; }
143  // ------------------------------------------------------------------------
144  void setPreGenerating(bool val) { m_pre_generating = val; }
145  // ------------------------------------------------------------------------
146  static void updateFlips(unsigned maximum_particle_count);
147  // ------------------------------------------------------------------------
148  static void destroyFlipsBuffer()
149  {
150  if (m_flips_buffer != 0)
151  {
152  glDeleteBuffers(1, &m_flips_buffer);
153  m_flips_buffer = 0;
154  }
155  m_flips_data.clear();
156  }
157  // ------------------------------------------------------------------------
158  static GLuint getFlipsBuffer()
159  {
160  assert(m_flips_buffer != 0);
161  return m_flips_buffer;
162  }
163  // ------------------------------------------------------------------------
164  virtual bool isSkyParticle() const { return m_hm != NULL; }
165 };
166 
167 #endif
168 
169 #endif // !SERVER_ONLY
Definition: stk_particle.hpp:39
unsigned m_max_count
Maximum count of particles.
Definition: stk_particle.hpp:79
core::matrix4 m_previous_frame_matrix
Previous frame particles emitter source matrix.
Definition: stk_particle.hpp:76
Definition: cpu_particle_manager.hpp:44
Definition: stk_particle.hpp:43
Definition: stk_particle.hpp:58