TDME2  1.9.200
FogParticleSystemInternal.cpp
Go to the documentation of this file.
2 
3 #include <memory>
4 #include <string>
5 #include <vector>
6 
7 #include <tdme/tdme.h>
8 #include <tdme/engine/Texture.h>
10 #include <tdme/engine/Color4.h>
18 #include <tdme/engine/Engine.h>
19 #include <tdme/engine/Timing.h>
20 #include <tdme/engine/Transform.h>
21 #include <tdme/math/Math.h>
22 #include <tdme/math/Matrix4x4.h>
23 #include <tdme/math/Vector3.h>
24 
25 using std::make_unique;
26 using std::string;
27 using std::unique_ptr;
28 using std::vector;
29 
44 using tdme::math::Math;
47 
48 FogParticleSystemInternal::FogParticleSystemInternal(const string& id, ParticleEmitter* emitter, int32_t maxPoints, float pointSize, Texture* texture, int32_t textureHorizontalSprites, int32_t textureVerticalSprites, float fps)
49 {
50  this->id = id;
51  this->enabled = true;
52  // will be activated on emit and auto unactivated if no more active particles
53  this->active = false;
54  this->emitter = unique_ptr<ParticleEmitter>(emitter);
55  particles.resize(maxPoints);
56  this->maxPoints = maxPoints;
57  this->effectColorMul.set(1.0f, 1.0f, 1.0f, 1.0f);
58  this->effectColorAdd.set(0.0f, 0.0f, 0.0f, 0.0f);
59  this->pickable = false;
60  this->pointSize = pointSize;
61  this->pointSizeScale = 1.0f;
62  this->pointsRenderPool = nullptr;
63  this->textureHorizontalSprites = textureHorizontalSprites;
64  this->textureVerticalSprites = textureVerticalSprites;
65  this->fps = fps;
66  this->texture = texture != nullptr?texture:TextureReader::read("resources/engine/textures", "point.png");
68 }
69 
71  if (texture != nullptr) texture->releaseReference();
72 }
73 
75  this->pointsRenderPool = make_unique<TransparentRenderPointsPool>(maxPoints);
76 
77  //
78  Vector3 center;
79  const auto& localTransformMatrix = localTransform.getTransformMatrix();
80  localTransformMatrix.getTranslation(center);
81  center.add(emitter->getCenter());
82 
83  //
84  Vector3 point;
85 
86  // enable particle system
87  active = true;
88  // emit particles
89  for (auto i = 0; i < particles.size(); i++) {
90  // emit particle
91  emitter->emit(&particles[i]);
92  }
93 
94  //
95  auto first = false;
96  // compute distance from camera
97  float distanceFromCamera;
98  // process particles
99  pointsRenderPool->reset();
100  //
101  auto activeParticles = 0;
102  for (auto i = 0; i < particles.size(); i++) {
103  auto& particle = particles[i];
104  if (particle.active == false) continue;
105 
106  //
107  activeParticles++;
108 
109  // color
110  int64_t timeRnd = (int64_t)(Math::random() * (float)particle.lifeTimeMax);
111  auto& color = particle.color;
112  const auto& colorAdd = particle.colorAdd;
113  color[0] += colorAdd[0] * static_cast<float>(timeRnd);
114  color[1] += colorAdd[1] * static_cast<float>(timeRnd);
115  color[2] += colorAdd[2] * static_cast<float>(timeRnd);
116  color[3] += colorAdd[3] * static_cast<float>(timeRnd);
117 
118  // set up bounding box
119  point = localTransformMatrix.multiply(particle.position);
120  point.add(center);
121 
122  // set up bounding box
123  if (first == false) {
124  boundingBox.getMin().set(point);
125  boundingBox.getMax().set(point);
126  first = true;
127  } else {
128  boundingBox.extend(point);
129  }
130  }
131 
132  // auto disable particle system if no more active particles
133  if (activeParticles == 0) {
134  active = false;
135  return;
136  }
137  // scale a bit up to make picking work better
138  // compute bounding boxes
144 }
145 
147 {
148  Transform::setTransform(transform);
149  //
150  auto entityTransform = parentTransform * (*this);
151  entityTransformMatrix = entityTransform.getTransformMatrix();
152  //
153  updateInternal();
154 }
155 
157 {
159  //
160  auto entityTransform = parentTransform * (*this);
161  entityTransformMatrix = entityTransform.getTransformMatrix();
162  //
163  updateInternal();
164 }
165 
167 {
168  if (enabled == false || active == false) return;
169 
170  //
171  Vector3 center;
172  const auto& localTransformMatrix = localTransform.getTransformMatrix();
173  localTransformMatrix.getTranslation(center);
174  center.add(emitter->getCenter());
175  //
176  Vector3 point;
177  //
178  auto first = false;
179  // process particles
180  pointsRenderPool->reset();
181  auto activeParticles = 0;
182  auto timeDelta = engine->getTiming()->getDeltaTime();
183  for (auto i = 0; i < particles.size(); i++) {
184  auto& particle = particles[i];
185  if (particle.active == false) continue;
186  // sprite index
187  particle.spriteIndex+= (static_cast<float>(timeDelta) / 1000.0f) * fps;
188  //
189  activeParticles++;
190  // set up bounding box
191  point = localTransformMatrix.multiply(particle.position);
192  point.add(center);
193  //
194  // set up bounding box
195  if (first == false) {
196  boundingBox.getMin().set(point);
197  boundingBox.getMax().set(point);
198  first = true;
199  } else {
200  boundingBox.extend(point);
201  }
202  // transform particle according to its transform
203  point = entityTransformMatrix.multiply(point);
204  // add to render points pool
205  pointsRenderPool->addPoint(point, static_cast<uint16_t>(particle.spriteIndex) % (textureHorizontalSprites * textureVerticalSprites), particle.color, 1, this);
206  }
207  // auto disable particle system if no more active particles
208  if (activeParticles == 0) {
209  active = false;
210  return;
211  }
212  // scale a bit up to make picking work better
218 }
219 
221 {
222  pointsRenderPool = nullptr;
223 }
224 
Color 4 definition class.
Definition: Color4.h:18
void set(float r, float g, float b, float a)
Sets this color by its components.
Definition: Color4.h:66
Engine main class.
Definition: Engine.h:131
Timing * getTiming()
Definition: Engine.h:1064
Texture entity.
Definition: Texture.h:24
Timing class.
Definition: Timing.h:16
int64_t getDeltaTime()
Gets the time passed between last and current frame.
Definition: Timing.h:85
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
virtual void setTransform(const Transform &transform)
Set transform.
Definition: Transform.h:177
virtual void update()
Computes transform matrix.
Definition: Transform.cpp:33
const Matrix4x4 & getTransformMatrix() const
Definition: Transform.h:169
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
void extend(BoundingBox *boundingBox)
Extend bounding box with given bounding box.
Definition: BoundingBox.h:158
void fromBoundingVolumeWithTransformMatrix(BoundingBox *original, const Matrix4x4 &transformMatrix)
Create bounding volume from given original(of same type) with applied transform matrix.
Definition: BoundingBox.cpp:79
void update()
Updates this bounding box.
void setTransform(const Transform &transform) override
Set transform.
Standard math functions.
Definition: Math.h:19
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Matrix4x4 & identity()
Creates identity matrix.
Definition: Matrix4x4.h:158
Vector3 multiply(const Vector3 &vector3) const
Multiplies this matrix with vector3.
Definition: Matrix4x4.h:225
void getTranslation(Vector3 &translation) const
Get translation.
Definition: Matrix4x4.h:433
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector3 & add(float scalar)
Adds a scalar.
Definition: Vector3.h:153
Vector3 & sub(float scalar)
Subtracts a scalar.
Definition: Vector3.h:177
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
Definition: Reference.h:38