RigsofRods
Soft-body Physics Simulation
DustPool.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5 
6  For more information, see http://www.rigsofrods.org/
7 
8  Rigs of Rods is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License version 3, as
10  published by the Free Software Foundation.
11 
12  Rigs of Rods is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "DustPool.h"
22 
23 #include <Ogre.h>
24 
25 #include "Application.h"
26 #include "GameContext.h"
27 #include "Terrain.h"
28 #include "Water.h"
29 
30 using namespace Ogre;
31 using namespace RoR;
32 
33 #ifndef _WIN32
34 // This definitions is needed because the variable is declared but not defined in DustPool
35  const int DustPool::MAX_DUSTS;
36 #endif // !_WIN32
37 
38 DustPool::DustPool(Ogre::SceneManager* sm, const char* dname, int dsize):
39  allocated(0),
40  size(std::min(dsize, static_cast<int>(MAX_DUSTS))),
41  m_is_discarded(false)
42 {
43  parent_snode = sm->getRootSceneNode()->createChildSceneNode(fmt::format("DustPools/{}", dname));
44 
45  for (int i = 0; i < size; i++)
46  {
47  char dename[256];
48  sprintf(dename, "Dust %s %i", dname, i);
49  sns[i] = parent_snode->createChildSceneNode();
50  pss[i] = sm->createParticleSystem(dename, dname);
51  if (pss[i])
52  {
53  sns[i]->attachObject(pss[i]);
54  pss[i]->setCastShadows(false);
55  pss[i]->setVisibilityFlags(RoR::DEPTHMAP_DISABLED);
56  if (pss[i]->getNumEmitters() > 0)
57  {
58  pss[i]->getEmitter(0)->setEnabled(false);
59  }
60  }
61  }
62 }
63 
65 {
67 }
68 
69 void DustPool::Discard(Ogre::SceneManager* sm)
70 {
71  for (int i = 0; i < size; i++)
72  {
73  sns[i]->removeAndDestroyAllChildren();
74  sm->destroySceneNode(sns[i]);
75  sns[i] = nullptr;
76 
77  if (pss[i])
78  {
79  sm->destroyParticleSystem(pss[i]);
80  pss[i] = nullptr;
81  }
82  }
83  sm->destroySceneNode(parent_snode);
84  m_is_discarded = true;
85 }
86 
88 {
89  for (int i = 0; i < size; i++)
90  {
91  pss[i]->setVisible(s);
92  }
93 }
94 
95 //Dust
96 void DustPool::malloc(Vector3 pos, Vector3 vel, ColourValue col)
97 {
98  if (allocated < size)
99  {
100  positions[allocated] = pos;
101  velocities[allocated] = vel;
102  colours[allocated] = col;
104  allocated++;
105  }
106 }
107 
108 //Clumps
109 void DustPool::allocClump(Vector3 pos, Vector3 vel, ColourValue col)
110 {
111  if (allocated < size)
112  {
113  positions[allocated] = pos;
114  velocities[allocated] = vel;
115  colours[allocated] = col;
117  allocated++;
118  }
119 }
120 
121 //Rubber smoke
122 void DustPool::allocSmoke(Vector3 pos, Vector3 vel)
123 {
124  if (allocated < size)
125  {
126  positions[allocated] = pos;
127  velocities[allocated] = vel;
129  allocated++;
130  }
131 }
132 
133 //
134 void DustPool::allocSparks(Vector3 pos, Vector3 vel)
135 {
136  if (vel.length() < 0.1)
137  return; // try to prevent emitting sparks while standing
138  if (allocated < size)
139  {
140  positions[allocated] = pos;
141  velocities[allocated] = vel;
143  allocated++;
144  }
145 }
146 
147 //Water vapour
148 void DustPool::allocVapour(Vector3 pos, Vector3 vel, float time)
149 {
150  if (allocated < size)
151  {
152  positions[allocated] = pos;
153  velocities[allocated] = vel;
155  rates[allocated] = 5.0 - time;
156  allocated++;
157  }
158 }
159 
160 void DustPool::allocDrip(Vector3 pos, Vector3 vel, float time)
161 {
162  if (allocated < size)
163  {
164  positions[allocated] = pos;
165  velocities[allocated] = vel;
167  rates[allocated] = 5.0 - time;
168  allocated++;
169  }
170 }
171 
172 void DustPool::allocSplash(Vector3 pos, Vector3 vel)
173 {
174  if (allocated < size)
175  {
176  positions[allocated] = pos;
177  velocities[allocated] = vel;
179  allocated++;
180  }
181 }
182 
183 void DustPool::allocRipple(Vector3 pos, Vector3 vel)
184 {
185  if (allocated < size)
186  {
187  positions[allocated] = pos;
188  velocities[allocated] = vel;
190  allocated++;
191  }
192 }
193 
195 {
196  for (int i = 0; i < allocated; i++)
197  {
198  ParticleEmitter* emit = pss[i]->getEmitter(0);
199  Vector3 ndir = velocities[i];
200  Real vel = ndir.length();
201  ColourValue col = colours[i];
202 
203  if (vel == 0)
204  vel += 0.0001;
205  ndir = ndir / vel;
206 
207  emit->setEnabled(true);
208 
209  if (types[i] != DUST_RIPPLE)
210  {
211  emit->setDirection(ndir);
212  emit->setParticleVelocity(vel);
213  sns[i]->setPosition(positions[i]);
214  }
215 
216  if (types[i] == DUST_NORMAL)
217  {
218  ndir.y = 0;
219  ndir = ndir / 2.0;
220 
221  col.a = vel * 0.05;
222  emit->setTimeToLive(vel * 0.05 / 0.1);
223  }
224  else if (types[i] == DUST_CLUMP)
225  {
226  ndir = ndir / 2.0;
227  if (ndir.y < 0)
228  ndir.y = -ndir.y;
229 
230  col.a = 1.0;
231  }
232  else if (types[i] == DUST_RUBBER)
233  {
234  ndir.y = 0;
235  ndir = ndir / 4.0;
236 
237  col.a = sqrt(vel) * 0.1;
238  col.b = 0.9;
239  col.g = 0.9;
240  col.r = 0.9;
241 
242  emit->setTimeToLive(vel * 0.025 / 0.1);
243  }
244  else if (types[i] == DUST_SPARKS)
245  {
246  //ugh
247  }
248  else if (types[i] == DUST_VAPOUR)
249  {
250  emit->setParticleVelocity(vel / 2.0);
251 
252  col.a = rates[i] * 0.03;
253  col.b = 0.9;
254  col.g = 0.9;
255  col.r = 0.9;
256 
257  emit->setTimeToLive(rates[i] * 0.03 / 0.1);
258  }
259  else if (types[i] == DUST_DRIP)
260  {
261  emit->setEmissionRate(rates[i]);
262  }
263  else if (types[i] == DUST_SPLASH)
264  {
265  if (ndir.y < 0)
266  ndir.y = -ndir.y / 2.0;
267 
268  emit->setDirection(ndir);
269 
270  col.a = sqrt(vel) * 0.04;
271  col.b = 0.9;
272  col.g = 0.9;
273  col.r = 0.9;
274 
275  emit->setTimeToLive(vel * 0.025 / 0.1);
276  }
277  else if (types[i] == DUST_RIPPLE)
278  {
280  sns[i]->setPosition(positions[i]);
281 
282  col.a = vel * 0.04;
283  col.b = 0.9;
284  col.g = 0.9;
285  col.r = 0.9;
286 
287  emit->setTimeToLive(vel * 0.04 / 0.1);
288  }
289 
290  emit->setColour(col);
291  }
292  for (int i = allocated; i < size; i++)
293  {
294  pss[i]->getEmitter(0)->setEnabled(false);
295  }
296  allocated = 0;
297 }
ROR_ASSERT
#define ROR_ASSERT(_EXPR)
Definition: Application.h:40
GameContext.h
Game state manager and message-queue provider.
RoR::IWater::GetStaticWaterHeight
virtual float GetStaticWaterHeight()=0
Returns static water level configured in 'terrn2'.
RoR::DustPool::allocSmoke
void allocSmoke(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition: DustPool.cpp:122
RoR::DustPool::allocSparks
void allocSparks(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition: DustPool.cpp:134
RoR::DustPool::DUST_CLUMP
@ DUST_CLUMP
Definition: DustPool.h:75
RoR::DustPool::size
int size
Definition: DustPool.h:86
RoR::DustPool::colours
Ogre::ColourValue colours[MAX_DUSTS]
Definition: DustPool.h:78
RoR::DustPool::DUST_RUBBER
@ DUST_RUBBER
Definition: DustPool.h:69
format
Truck file format(technical spec)
RoR::DustPool::update
void update()
Definition: DustPool.cpp:194
RoR::DustPool::allocDrip
void allocDrip(Ogre::Vector3 pos, Ogre::Vector3 vel, float time)
Definition: DustPool.cpp:160
RoR::DustPool::positions
Ogre::Vector3 positions[MAX_DUSTS]
Definition: DustPool.h:82
RoR::DustPool::allocClump
void allocClump(Ogre::Vector3 pos, Ogre::Vector3 vel, Ogre::ColourValue col=Ogre::ColourValue(0.83, 0.71, 0.64, 1.0))
Definition: DustPool.cpp:109
RoR::DustPool::DUST_RIPPLE
@ DUST_RIPPLE
Definition: DustPool.h:73
RoR::DustPool::rates
float rates[MAX_DUSTS]
Definition: DustPool.h:84
RoR::DustPool::allocRipple
void allocRipple(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition: DustPool.cpp:183
RoR::DustPool::pss
Ogre::ParticleSystem * pss[MAX_DUSTS]
Definition: DustPool.h:79
RoR::DustPool::DUST_DRIP
@ DUST_DRIP
Definition: DustPool.h:70
RoR::DustPool::parent_snode
Ogre::SceneNode * parent_snode
Definition: DustPool.h:81
RoR::DustPool::malloc
void malloc(Ogre::Vector3 pos, Ogre::Vector3 vel, Ogre::ColourValue col=Ogre::ColourValue(0.83, 0.71, 0.64, 1.0))
Definition: DustPool.cpp:96
RoR::DustPool::~DustPool
~DustPool()
Definition: DustPool.cpp:64
Application.h
Central state/object manager and communications hub.
RoR::DustPool::DUST_VAPOUR
@ DUST_VAPOUR
Definition: DustPool.h:71
RoR::App::GetGameContext
GameContext * GetGameContext()
Definition: Application.cpp:280
RoR::DustPool::velocities
Ogre::Vector3 velocities[MAX_DUSTS]
Definition: DustPool.h:83
RoR::DustPool::setVisible
void setVisible(bool s)
Definition: DustPool.cpp:87
DustPool.h
RoR::DustPool::allocated
int allocated
Definition: DustPool.h:85
RoR::DustPool::allocSplash
void allocSplash(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition: DustPool.cpp:172
RoR::DustPool::m_is_discarded
bool m_is_discarded
Definition: DustPool.h:88
Terrain.h
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::DustPool::types
int types[MAX_DUSTS]
Definition: DustPool.h:87
RoR::DustPool::DUST_SPLASH
@ DUST_SPLASH
Definition: DustPool.h:72
RoR::DustPool::allocVapour
void allocVapour(Ogre::Vector3 pos, Ogre::Vector3 vel, float time)
Definition: DustPool.cpp:148
RoR::DEPTHMAP_DISABLED
@ DEPTHMAP_DISABLED
Definition: Application.h:285
RoR::DustPool::DUST_SPARKS
@ DUST_SPARKS
Definition: DustPool.h:74
RoR::DustPool::DUST_NORMAL
@ DUST_NORMAL
Definition: DustPool.h:68
RoR
Definition: AppContext.h:36
Water.h
RoR::Terrain::getWater
IWater * getWater()
Definition: Terrain.h:84
RoR::DustPool::Discard
void Discard(Ogre::SceneManager *sm)
Definition: DustPool.cpp:69
RoR::DustPool::sns
Ogre::SceneNode * sns[MAX_DUSTS]
Definition: DustPool.h:80
RoR::GameContext::GetTerrain
const TerrainPtr & GetTerrain()
Definition: GameContext.h:117