TDME2  1.9.200
ParticleSystemGroup.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 #include <vector>
5 
6 #include <tdme/tdme.h>
8 #include <tdme/engine/Engine.h>
10 #include <tdme/engine/Object.h>
13 #include <tdme/engine/Partition.h>
15 #include <tdme/engine/Transform.h>
16 
17 using std::string;
18 using std::vector;
19 
30 
31 ParticleSystemGroup::ParticleSystemGroup(const string& id, bool autoEmit, bool contributesShadows, bool receivesShadows, const vector<ParticleSystem*>& particleSystems) :
32  id(id), autoEmit(autoEmit), contributesShadows(contributesShadows), receivesShadows(receivesShadows), particleSystems(particleSystems)
33 {
34  this->enabled = true;
35  this->pickable = false;
36  this->effectColorMul.set(1.0f, 1.0f, 1.0f, 1.0f);
37  this->effectColorAdd.set(0.0f, 0.0f, 0.0f, 0.0f);
38  this->contributesShadows = false;
39  this->receivesShadows = false;
41  // TODO: put parent entity into a interface
42  for (auto particleSystem: particleSystems) {
43  auto ops = dynamic_cast<ObjectParticleSystem*>(particleSystem);
44  if (ops != nullptr) ops->setParentEntity(this);
45  auto pps = dynamic_cast<PointsParticleSystem*>(particleSystem);
46  if (pps != nullptr) pps->setParentEntity(this);
47  auto fps = dynamic_cast<FogParticleSystem*>(particleSystem);
48  if (fps != nullptr) fps->setParentEntity(this);
49  }
50 }
51 
53  for (auto particleSystem: particleSystems) delete particleSystem;
54 }
55 
57 {
58  for (auto particleSystem: particleSystems) dynamic_cast<Entity*>(particleSystem)->initialize();
59 }
60 
62 {
63  Transform::setTransform(transform);
64  //
65  auto entityTransform = parentTransform * (*this);
66  entityTransformMatrix = entityTransform.getTransformMatrix();
67  //
68  for (auto particleSystem: particleSystems) dynamic_cast<Entity*>(particleSystem)->setTransform(*this);
69  // update world bounding box
71  // update object
72  if (parentEntity == nullptr && frustumCulling == true && engine != nullptr && enabled == true) engine->partition->updateEntity(this);
73 }
74 
76 {
78  //
79  for (auto particleSystem: particleSystems) dynamic_cast<Entity*>(particleSystem)->setTransform(*this);
80  // update world bounding box
82  // update object
83  if (parentEntity == nullptr && frustumCulling == true && engine != nullptr && enabled == true) engine->partition->updateEntity(this);
84 }
85 
87 {
88  // return if enable state has not changed
89  if (this->enabled == enabled) return;
90 
91  // frustum if root entity
92  if (parentEntity == nullptr) {
93  // frustum culling enabled?
94  if (frustumCulling == true) {
95  // yeo, add or remove from partition
96  if (enabled == true) {
97  if (engine != nullptr) engine->partition->addEntity(this);
98  } else {
99  if (engine != nullptr) engine->partition->removeEntity(this);
100  }
101  }
102  }
103 
104  //
105  this->enabled = enabled;
106 }
107 
109 {
110  // skip if no ps attached
111  if (particleSystems.size() == 0) return;
112 
113  // update particles
114  for (auto particleSystem: particleSystems) particleSystem->updateParticles();
115 
116  //
117  if (parentEntity == nullptr && frustumCulling == true && engine != nullptr && enabled == true) engine->partition->updateEntity(this);
118 }
119 
120 void ParticleSystemGroup::setFrustumCulling(bool frustumCulling) {
121  // check if enabled and engine attached
122  if (enabled == true && engine != nullptr) {
123  // had frustum culling
124  if (this->frustumCulling == true) {
125  // yep, remove if set to false now
126  if (frustumCulling == false) engine->partition->removeEntity(this);
127  } else {
128  // yep, add if set to true now
129  if (frustumCulling == true) engine->partition->addEntity(this);
130  }
131  }
132  this->frustumCulling = frustumCulling;
133  // delegate change to engine
134  if (engine != nullptr) engine->updateEntityRegistration(this);
135 }
136 
137 void ParticleSystemGroup::setAutoEmit(bool autoEmit) {
138  // delegate to sub particle systems
139  for (auto particleSystem: particleSystems) particleSystem->setAutoEmit(autoEmit);
140  this->autoEmit = autoEmit;
141  // delegate change to engine
142  if (engine != nullptr) engine->updateEntityRegistration(this);
143 }
144 
146 {
147  for (auto particleSystem: particleSystems) dynamic_cast<Entity*>(particleSystem)->dispose();
148 }
149 
151 {
152  if (this->engine != nullptr) this->engine->deregisterEntity(this);
153  this->engine = engine;
154  if (engine != nullptr) engine->registerEntity(this);
155  for (auto particleSystem: particleSystems) dynamic_cast<Entity*>(particleSystem)->setEngine(engine);
156 }
157 
159 {
160  for (auto particleSystem: particleSystems) dynamic_cast<Entity*>(particleSystem)->setRenderer(renderer);
161 }
162 
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
unique_ptr< Partition > partition
Definition: Engine.h:289
void updateEntityRegistration(Entity *entity)
Updates registration of engine by performing deregisterEntity() and registerEntity()
Definition: Engine.h:1489
void deregisterEntity(Entity *entity)
Removes a entity from internal lists, those entities can also be sub entities from entity hierarchy o...
Definition: Engine.cpp:382
void registerEntity(Entity *entity)
Adds a entity to internal lists, those entities can also be sub entities from entity hierarchy or par...
Definition: Engine.cpp:437
Engine entity.
Definition: Entity.h:30
Entity * parentEntity
Definition: Entity.h:39
void setParentEntity(Entity *entity)
Set parent entity, needs to be called before adding to engine.
Definition: Entity.h:45
Fog particle system entity to be used with engine class.
Object particle system entity to be used with engine class.
Object to be used with engine class.
Definition: Object.h:60
Particle system group, which combines several particle systems into a group, to be used with engine c...
BoundingBox * getBoundingBox() override
void updateParticles() override
Updates the particle entity.
void dispose() override
Dispose this entity.
vector< ParticleSystem * > particleSystems
void initialize() override
Initiates this entity.
void update() override
Computes transform matrix.
void setTransform(const Transform &transform) override
Set transform.
void setFrustumCulling(bool frustumCulling) override
Set frustum culling.
void setAutoEmit(bool autoEmit) override
Set auto emit.
void setEngine(Engine *engine) override
Set up engine.
void setEnabled(bool enabled) override
Enable/disable rendering.
void setRenderer(Renderer *renderer) override
Set up renderer.
Point particle system entity to be used with engine class.
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
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
void fromBoundingVolumeWithTransformMatrix(BoundingBox *original, const Matrix4x4 &transformMatrix)
Create bounding volume from given original(of same type) with applied transform matrix.
Definition: BoundingBox.cpp:79
Matrix4x4 & identity()
Creates identity matrix.
Definition: Matrix4x4.h:158
Particle system entity interface.
Partition interface.
Definition: Partition.h:18