Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
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 "GfxWater.h"
30
31using namespace Ogre;
32using 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
39DustPool::DustPool(Ogre::SceneManager* sm, const char* dname, int dsize, const std::string& custom_name /* = "" */):
40 allocated(0),
41 size(std::min(dsize, static_cast<int>(MAX_DUSTS))),
42 m_is_discarded(false)
43{
44 std::string poolname = fmt::format("DustPools/{}", custom_name.empty() ? dname : custom_name);
45 parent_snode = sm->getRootSceneNode()->createChildSceneNode(poolname);
46
47 for (int i = 0; i < size; i++)
48 {
49 sns[i] = parent_snode->createChildSceneNode();
50 pss[i] = sm->createParticleSystem(fmt::format("{}_{}", poolname, i), 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
68
69void 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
96void 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
109void 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
122void 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//
134void 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
148void 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
160void 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
172void 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
183void 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
194void DustPool::AdjustDustPoolSpeedFactor(GfxActor* gfx_actor /* = nullptr */)
195{
196 // This must be done over all particle system, even those not allocated at the moment
197 for (int i = 0; i < size; i++)
198 {
200 }
201}
202
204{
205 for (int i = 0; i < allocated; i++)
206 {
207 ParticleEmitter* emit = pss[i]->getEmitter(0);
208 Vector3 ndir = velocities[i];
209 Real vel = ndir.length();
210 ColourValue col = colours[i];
211
212 if (vel == 0)
213 vel += 0.0001;
214 ndir = ndir / vel;
215
216 emit->setEnabled(true);
217
218 if (types[i] != DUST_RIPPLE)
219 {
220 emit->setDirection(ndir);
221 emit->setParticleVelocity(vel);
222 sns[i]->setPosition(positions[i]);
223 }
224
225 if (types[i] == DUST_NORMAL)
226 {
227 ndir.y = 0;
228 ndir = ndir / 2.0;
229
230 col.a = vel * 0.05;
231 emit->setTimeToLive(vel * 0.05 / 0.1);
232 }
233 else if (types[i] == DUST_CLUMP)
234 {
235 ndir = ndir / 2.0;
236 if (ndir.y < 0)
237 ndir.y = -ndir.y;
238
239 col.a = 1.0;
240 }
241 else if (types[i] == DUST_RUBBER)
242 {
243 ndir.y = 0;
244 ndir = ndir / 4.0;
245
246 col.a = sqrt(vel) * 0.1;
247 col.b = 0.9;
248 col.g = 0.9;
249 col.r = 0.9;
250
251 emit->setTimeToLive(vel * 0.025 / 0.1);
252 }
253 else if (types[i] == DUST_SPARKS)
254 {
255 //ugh
256 }
257 else if (types[i] == DUST_VAPOUR)
258 {
259 emit->setParticleVelocity(vel / 2.0);
260
261 col.a = rates[i] * 0.03;
262 col.b = 0.9;
263 col.g = 0.9;
264 col.r = 0.9;
265
266 emit->setTimeToLive(rates[i] * 0.03 / 0.1);
267 }
268 else if (types[i] == DUST_DRIP)
269 {
270 emit->setEmissionRate(rates[i]);
271 }
272 else if (types[i] == DUST_SPLASH)
273 {
274 if (ndir.y < 0)
275 ndir.y = -ndir.y / 2.0;
276
277 emit->setDirection(ndir);
278
279 col.a = sqrt(vel) * 0.04;
280 col.b = 0.9;
281 col.g = 0.9;
282 col.r = 0.9;
283
284 emit->setTimeToLive(vel * 0.025 / 0.1);
285 }
286 else if (types[i] == DUST_RIPPLE)
287 {
289 sns[i]->setPosition(positions[i]);
290
291 col.a = vel * 0.04;
292 col.b = 0.9;
293 col.g = 0.9;
294 col.r = 0.9;
295
296 emit->setTimeToLive(vel * 0.04 / 0.1);
297 }
298
299 emit->setColour(col);
300 }
301 for (int i = allocated; i < size; i++)
302 {
303 pss[i]->getEmitter(0)->setEnabled(false);
304 }
305 allocated = 0;
306}
Central state/object manager and communications hub.
#define ROR_ASSERT(_EXPR)
Definition Application.h:40
Game state manager and message-queue provider.
Ogre::SceneNode * sns[MAX_DUSTS]
Definition DustPool.h:82
void Discard(Ogre::SceneManager *sm)
Definition DustPool.cpp:69
void allocRipple(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition DustPool.cpp:183
Ogre::Vector3 positions[MAX_DUSTS]
Definition DustPool.h:84
Ogre::Vector3 velocities[MAX_DUSTS]
Velocity in wall time, ignoring the time scale.
Definition DustPool.h:85
int types[MAX_DUSTS]
Definition DustPool.h:89
void allocVapour(Ogre::Vector3 pos, Ogre::Vector3 vel, float time)
Definition DustPool.cpp:148
Ogre::ParticleSystem * pss[MAX_DUSTS]
Definition DustPool.h:81
Ogre::SceneNode * parent_snode
Definition DustPool.h:83
Ogre::ColourValue colours[MAX_DUSTS]
Definition DustPool.h:80
DustPool(Ogre::SceneManager *sm, const char *dname, int dsize, const std::string &custom_name="")
Definition DustPool.cpp:39
void AdjustDustPoolSpeedFactor(GfxActor *gfx_actor=nullptr)
Definition DustPool.cpp:194
bool m_is_discarded
Definition DustPool.h:90
void allocSmoke(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition DustPool.cpp:122
void allocSparks(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition DustPool.cpp:134
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
void setVisible(bool s)
Definition DustPool.cpp:87
void allocSplash(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition DustPool.cpp:172
float rates[MAX_DUSTS]
Definition DustPool.h:86
static const int MAX_DUSTS
Definition DustPool.h:66
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
void allocDrip(Ogre::Vector3 pos, Ogre::Vector3 vel, float time)
Definition DustPool.cpp:160
const TerrainPtr & GetTerrain()
void AdjustParticleSystemTimeFactor(Ogre::ParticleSystem *psys, GfxActor *gfx_actor=nullptr)
Definition GfxScene.cpp:435
Wavefield * getWater()
Definition Terrain.h:87
float GetStaticWaterHeight()
Returns static water level configured in 'terrn2'.
Definition Wavefield.cpp:75
GameContext * GetGameContext()
GfxScene * GetGfxScene()
@ DEPTHMAP_DISABLED