RigsofRods
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 "GfxScene.h"
28 #include "Terrain.h"
29 #include "Water.h"
30 
31 using namespace Ogre;
32 using namespace RoR;
33 
34 #ifndef _WIN32
35 // This definitions is needed because the variable is declared but not defined in DustPool
36  const int DustPool::MAX_DUSTS;
37 #endif // !_WIN32
38 
39 DustPool::DustPool(Ogre::SceneManager* sm, const char* dname, int dsize):
40  allocated(0),
41  size(std::min(dsize, static_cast<int>(MAX_DUSTS))),
42  m_is_discarded(false)
43 {
44  parent_snode = sm->getRootSceneNode()->createChildSceneNode(fmt::format("DustPools/{}", dname));
45 
46  for (int i = 0; i < size; i++)
47  {
48  char dename[256];
49  sprintf(dename, "Dust %s %i", dname, i);
50  sns[i] = parent_snode->createChildSceneNode();
51  pss[i] = sm->createParticleSystem(dename, dname);
52  if (pss[i])
53  {
54  sns[i]->attachObject(pss[i]);
55  pss[i]->setCastShadows(false);
56  pss[i]->setVisibilityFlags(RoR::DEPTHMAP_DISABLED);
57  if (pss[i]->getNumEmitters() > 0)
58  {
59  pss[i]->getEmitter(0)->setEnabled(false);
60  }
61  }
62  }
63 }
64 
66 {
68 }
69 
70 void DustPool::Discard(Ogre::SceneManager* sm)
71 {
72  for (int i = 0; i < size; i++)
73  {
74  sns[i]->removeAndDestroyAllChildren();
75  sm->destroySceneNode(sns[i]);
76  sns[i] = nullptr;
77 
78  if (pss[i])
79  {
80  sm->destroyParticleSystem(pss[i]);
81  pss[i] = nullptr;
82  }
83  }
84  sm->destroySceneNode(parent_snode);
85  m_is_discarded = true;
86 }
87 
89 {
90  for (int i = 0; i < size; i++)
91  {
92  pss[i]->setVisible(s);
93  }
94 }
95 
96 //Dust
97 void DustPool::malloc(Vector3 pos, Vector3 vel, ColourValue col)
98 {
99  if (allocated < size)
100  {
101  positions[allocated] = pos;
102  velocities[allocated] = vel;
103  colours[allocated] = col;
105  allocated++;
106  }
107 }
108 
109 //Clumps
110 void DustPool::allocClump(Vector3 pos, Vector3 vel, ColourValue col)
111 {
112  if (allocated < size)
113  {
114  positions[allocated] = pos;
115  velocities[allocated] = vel;
116  colours[allocated] = col;
118  allocated++;
119  }
120 }
121 
122 //Rubber smoke
123 void DustPool::allocSmoke(Vector3 pos, Vector3 vel)
124 {
125  if (allocated < size)
126  {
127  positions[allocated] = pos;
128  velocities[allocated] = vel;
130  allocated++;
131  }
132 }
133 
134 //
135 void DustPool::allocSparks(Vector3 pos, Vector3 vel)
136 {
137  if (vel.length() < 0.1)
138  return; // try to prevent emitting sparks while standing
139  if (allocated < size)
140  {
141  positions[allocated] = pos;
142  velocities[allocated] = vel;
144  allocated++;
145  }
146 }
147 
148 //Water vapour
149 void DustPool::allocVapour(Vector3 pos, Vector3 vel, float time)
150 {
151  if (allocated < size)
152  {
153  positions[allocated] = pos;
154  velocities[allocated] = vel;
156  rates[allocated] = 5.0 - time;
157  allocated++;
158  }
159 }
160 
161 void DustPool::allocDrip(Vector3 pos, Vector3 vel, float time)
162 {
163  if (allocated < size)
164  {
165  positions[allocated] = pos;
166  velocities[allocated] = vel;
168  rates[allocated] = 5.0 - time;
169  allocated++;
170  }
171 }
172 
173 void DustPool::allocSplash(Vector3 pos, Vector3 vel)
174 {
175  if (allocated < size)
176  {
177  positions[allocated] = pos;
178  velocities[allocated] = vel;
180  allocated++;
181  }
182 }
183 
184 void DustPool::allocRipple(Vector3 pos, Vector3 vel)
185 {
186  if (allocated < size)
187  {
188  positions[allocated] = pos;
189  velocities[allocated] = vel;
191  allocated++;
192  }
193 }
194 
196 {
197  for (int i = 0; i < allocated; i++)
198  {
200  ParticleEmitter* emit = pss[i]->getEmitter(0);
201  Vector3 ndir = velocities[i];
202  Real vel = ndir.length();
203  ColourValue col = colours[i];
204 
205  if (vel == 0)
206  vel += 0.0001;
207  ndir = ndir / vel;
208 
209  emit->setEnabled(true);
210 
211  if (types[i] != DUST_RIPPLE)
212  {
213  emit->setDirection(ndir);
214  emit->setParticleVelocity(vel);
215  sns[i]->setPosition(positions[i]);
216  }
217 
218  if (types[i] == DUST_NORMAL)
219  {
220  ndir.y = 0;
221  ndir = ndir / 2.0;
222 
223  col.a = vel * 0.05;
224  emit->setTimeToLive(vel * 0.05 / 0.1);
225  }
226  else if (types[i] == DUST_CLUMP)
227  {
228  ndir = ndir / 2.0;
229  if (ndir.y < 0)
230  ndir.y = -ndir.y;
231 
232  col.a = 1.0;
233  }
234  else if (types[i] == DUST_RUBBER)
235  {
236  ndir.y = 0;
237  ndir = ndir / 4.0;
238 
239  col.a = sqrt(vel) * 0.1;
240  col.b = 0.9;
241  col.g = 0.9;
242  col.r = 0.9;
243 
244  emit->setTimeToLive(vel * 0.025 / 0.1);
245  }
246  else if (types[i] == DUST_SPARKS)
247  {
248  //ugh
249  }
250  else if (types[i] == DUST_VAPOUR)
251  {
252  emit->setParticleVelocity(vel / 2.0);
253 
254  col.a = rates[i] * 0.03;
255  col.b = 0.9;
256  col.g = 0.9;
257  col.r = 0.9;
258 
259  emit->setTimeToLive(rates[i] * 0.03 / 0.1);
260  }
261  else if (types[i] == DUST_DRIP)
262  {
263  emit->setEmissionRate(rates[i]);
264  }
265  else if (types[i] == DUST_SPLASH)
266  {
267  if (ndir.y < 0)
268  ndir.y = -ndir.y / 2.0;
269 
270  emit->setDirection(ndir);
271 
272  col.a = sqrt(vel) * 0.04;
273  col.b = 0.9;
274  col.g = 0.9;
275  col.r = 0.9;
276 
277  emit->setTimeToLive(vel * 0.025 / 0.1);
278  }
279  else if (types[i] == DUST_RIPPLE)
280  {
282  sns[i]->setPosition(positions[i]);
283 
284  col.a = vel * 0.04;
285  col.b = 0.9;
286  col.g = 0.9;
287  col.r = 0.9;
288 
289  emit->setTimeToLive(vel * 0.04 / 0.1);
290  }
291 
292  emit->setColour(col);
293  }
294  for (int i = allocated; i < size; i++)
295  {
296  pss[i]->getEmitter(0)->setEnabled(false);
297  }
298  allocated = 0;
299 }
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:123
RoR::DustPool::allocSparks
void allocSparks(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition: DustPool.cpp:135
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:195
RoR::DustPool::allocDrip
void allocDrip(Ogre::Vector3 pos, Ogre::Vector3 vel, float time)
Definition: DustPool.cpp:161
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:110
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:184
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:97
RoR::DustPool::~DustPool
~DustPool()
Definition: DustPool.cpp:65
GfxScene.h
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:283
RoR::DustPool::velocities
Ogre::Vector3 velocities[MAX_DUSTS]
Velocity in wall time, ignoring the time scale.
Definition: DustPool.h:83
RoR::DustPool::setVisible
void setVisible(bool s)
Definition: DustPool.cpp:88
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:173
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:149
RoR::DEPTHMAP_DISABLED
@ DEPTHMAP_DISABLED
Definition: Application.h:302
RoR::DustPool::DUST_SPARKS
@ DUST_SPARKS
Definition: DustPool.h:74
RoR::GfxScene::AdjustParticleSystemTimeFactor
void AdjustParticleSystemTimeFactor(Ogre::ParticleSystem *psys)
Definition: GfxScene.cpp:428
RoR::DustPool::DUST_NORMAL
@ DUST_NORMAL
Definition: DustPool.h:68
RoR
Definition: AppContext.h:36
Water.h
RoR::App::GetGfxScene
GfxScene * GetGfxScene()
Definition: Application.cpp:279
RoR::Terrain::getWater
IWater * getWater()
Definition: Terrain.h:83
RoR::DustPool::Discard
void Discard(Ogre::SceneManager *sm)
Definition: DustPool.cpp:70
RoR::DustPool::sns
Ogre::SceneNode * sns[MAX_DUSTS]
Definition: DustPool.h:80
RoR::GameContext::GetTerrain
const TerrainPtr & GetTerrain()
Definition: GameContext.h:117