Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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):
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
69
70void 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
97void 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
110void 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
123void 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//
135void 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
149void 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
161void 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
173void 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
184void 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}
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:80
void Discard(Ogre::SceneManager *sm)
Definition DustPool.cpp:70
void allocRipple(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition DustPool.cpp:184
Ogre::Vector3 positions[MAX_DUSTS]
Definition DustPool.h:82
Ogre::Vector3 velocities[MAX_DUSTS]
Velocity in wall time, ignoring the time scale.
Definition DustPool.h:83
int types[MAX_DUSTS]
Definition DustPool.h:87
void allocVapour(Ogre::Vector3 pos, Ogre::Vector3 vel, float time)
Definition DustPool.cpp:149
Ogre::ParticleSystem * pss[MAX_DUSTS]
Definition DustPool.h:79
Ogre::SceneNode * parent_snode
Definition DustPool.h:81
Ogre::ColourValue colours[MAX_DUSTS]
Definition DustPool.h:78
bool m_is_discarded
Definition DustPool.h:88
void allocSmoke(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition DustPool.cpp:123
DustPool(Ogre::SceneManager *sm, const char *dname, int dsize)
Definition DustPool.cpp:39
void allocSparks(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition DustPool.cpp:135
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
void setVisible(bool s)
Definition DustPool.cpp:88
void allocSplash(Ogre::Vector3 pos, Ogre::Vector3 vel)
Definition DustPool.cpp:173
float rates[MAX_DUSTS]
Definition DustPool.h:84
static const int MAX_DUSTS
Definition DustPool.h:64
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
void allocDrip(Ogre::Vector3 pos, Ogre::Vector3 vel, float time)
Definition DustPool.cpp:161
const TerrainPtr & GetTerrain()
void AdjustParticleSystemTimeFactor(Ogre::ParticleSystem *psys)
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