/
RenderableParticleStage.cpp
261 lines (216 loc) · 6.64 KB
/
RenderableParticleStage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "RenderableParticleStage.h"
namespace particles
{
RenderableParticleStage::RenderableParticleStage(
const IStageDef& stage,
Rand48& random,
const Vector3& direction,
const Vector3& entityColour) :
_stageDef(stage),
_numSeeds(32),
_seeds(_numSeeds),
_bunches(2), // two bunches
_viewRotation(Matrix4::getIdentity()), // is re-calculated each update anyway
_localToWorld(Matrix4::getIdentity()),
_direction(direction),
_entityColour(entityColour)
{
// Generate our vector of random numbers used seed particle bunches
// using the random number generator as provided by our parent particle system
for (std::size_t i = 0; i < _numSeeds; ++i)
{
_seeds[i] = random();
}
}
// Generate particle geometry, time is absolute in msecs
void RenderableParticleStage::update(std::size_t time, const Matrix4& viewRotation)
{
// Invalidate our bounds information
_bounds = AABB();
// Check time offset (msecs)
std::size_t timeOffset = static_cast<std::size_t>(SEC2MS(_stageDef.getTimeOffset()));
if (time < timeOffset)
{
// We're still in the timeoffset zone where particle spawn is inhibited
_bunches[0].reset();
_bunches[1].reset();
return;
}
// Time >= timeOffset at this point
// Get rid of the time offset
std::size_t localtimeMsec = time - timeOffset;
// Consider stage orientation (x,y,z,view,aimed)
calculateStageViewRotation(viewRotation);
// Make sure the correct bunches are allocated for this stage time
ensureBunches(localtimeMsec);
// The 0 bunch is the active one, the 1 bunch is the previous one if not null
// Tell the particle batches to update their geometry
if (_bunches[0])
{
// Get one of our seed values
_bunches[0]->update(localtimeMsec);
}
if (_bunches[1])
{
_bunches[1]->update(localtimeMsec);
}
}
void RenderableParticleStage::submitGeometry(const ShaderPtr& shader, const Matrix4& localToWorld)
{
_localToWorld = localToWorld;
RenderableGeometry::update(shader);
}
void RenderableParticleStage::updateGeometry()
{
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
auto numQuads = (_bunches[0] ? _bunches[0]->getNumQuads() : 0) +
(_bunches[1] ? _bunches[1]->getNumQuads() : 0);
if (numQuads == 0)
{
RenderableGeometry::updateGeometryWithData(render::GeometryType::Triangles, vertices,
indices);
return;
}
vertices.reserve(numQuads * 4);
indices.reserve(numQuads * 6);
if (_bunches[0])
{
_bunches[0]->addVertexData(vertices, indices, _localToWorld);
}
if (_bunches[1])
{
_bunches[1]->addVertexData(vertices, indices, _localToWorld);
}
RenderableGeometry::updateGeometryWithData(render::GeometryType::Triangles, vertices, indices);
}
const AABB& RenderableParticleStage::getBounds()
{
if (!_bounds.isValid())
{
calculateBounds();
}
return _bounds;
}
const IStageDef& RenderableParticleStage::getDef() const
{
return _stageDef;
}
void RenderableParticleStage::calculateStageViewRotation(const Matrix4& viewRotation)
{
switch (_stageDef.getOrientationType())
{
case IStageDef::ORIENTATION_AIMED:
_viewRotation = viewRotation;
break;
case IStageDef::ORIENTATION_VIEW:
_viewRotation = viewRotation;
break;
case IStageDef::ORIENTATION_X:
// Rotate the z vector such that it faces the x axis, and use that as transform
// To keep the up/down orientation of the material, rotate it 90 degrees around z
// before applying the z-to-x tilt (issue #4792)
_viewRotation = Matrix4::getRotation(Vector3(0, 0, 1), Vector3(1, 0, 0))
.getMultipliedBy(
Matrix4::getRotationAboutZ(math::Degrees(-90))
);
break;
case IStageDef::ORIENTATION_Y:
// Rotate the z vector such that it faces the y axis, and use that as transform
_viewRotation = Matrix4::getRotation(Vector3(0,0,1), Vector3(0,1,0));
break;
case IStageDef::ORIENTATION_Z:
// Particles are already facing the z axis by default
_viewRotation = Matrix4::getIdentity();
break;
default:
_viewRotation = Matrix4::getIdentity();
};
}
void RenderableParticleStage::ensureBunches(std::size_t localTimeMSec)
{
// Check which bunches is active at this time
float cycleFrac = floor(static_cast<float>(localTimeMSec) / _stageDef.getCycleMsec());
std::size_t curCycleIndex = static_cast<std::size_t>(cycleFrac);
if (curCycleIndex == 0)
{
// This is the only active bunch (the first one), there is no previous cycle
// it's possible that this one is already existing.
if (_bunches[0] == NULL || _bunches[0]->getIndex() != curCycleIndex)
{
// First bunch is not matching, re-assign
_bunches[0] = createBunch(curCycleIndex);
}
// Reset the previous bunch in any case
_bunches[1].reset();
}
else
{
// Current cycle > 0, this means we have possibly two active ones
std::size_t prevCycleIndex = curCycleIndex - 1;
// Reuse any existing instances, to avoid re-instancing them all over again
RenderableParticleBunchPtr cur = getExistingBunchByIndex(curCycleIndex);
RenderableParticleBunchPtr prev = getExistingBunchByIndex(prevCycleIndex);
std::size_t numCycles = static_cast<std::size_t>(_stageDef.getCycles());
if (numCycles > 0 && curCycleIndex > numCycles)
{
// We've exceeded the maximum number of cycles
_bunches[0].reset();
}
else if (cur != NULL)
{
_bunches[0] = cur;
}
else
{
_bunches[0] = createBunch(curCycleIndex);
}
if (numCycles > 0 && prevCycleIndex > numCycles)
{
// We've exceeded the maximum number of cycles
_bunches[1].reset();
}
else if (prev != NULL)
{
_bunches[1] = prev;
}
else
{
_bunches[1] = createBunch(prevCycleIndex);
}
}
}
RenderableParticleBunchPtr RenderableParticleStage::createBunch(std::size_t cycleIndex)
{
return RenderableParticleBunchPtr(new RenderableParticleBunch(
cycleIndex, getSeed(cycleIndex), _stageDef, _viewRotation, _direction, _entityColour));
}
Rand48::result_type RenderableParticleStage::getSeed(std::size_t cycleIndex)
{
return _seeds[cycleIndex % _seeds.size()];
}
RenderableParticleBunchPtr RenderableParticleStage::getExistingBunchByIndex(std::size_t index)
{
if (_bunches[0] != NULL && _bunches[0]->getIndex() == index)
{
return _bunches[0];
}
else if (_bunches[1] != NULL && _bunches[1]->getIndex() == index)
{
return _bunches[1];
}
return RenderableParticleBunchPtr();
}
void RenderableParticleStage::calculateBounds()
{
if (_bunches[0] != NULL)
{
// Get one of our seed values
_bounds.includeAABB(_bunches[0]->getBounds());
}
if (_bunches[1] != NULL)
{
_bounds.includeAABB(_bunches[1]->getBounds());
}
}
} // namespace