-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTrailSource.cpp
542 lines (417 loc) · 16.5 KB
/
TrailSource.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// ----------------------------------------------------------------------------
#include "TrailSource.h"
#include "Max.h"
#include "ParticleState.h"
#include "decomp.h"
// ----------------------------------------------------------------------------
// The minimum a trail can move before it is no longer stationary
// (for mesh culling porpoises)
//
float SplineTrailSource::MIN_TRAIL_MOVEMENT = 0.001f;
// ----------------------------------------------------------------------------
SplineTrailSource::SplineTrailSource(INode* node) { pShapeNode = node; }
// ----------------------------------------------------------------------------
int SplineTrailSource::numTrails() {
// there is only one trail for normal spline trails, the one
// hanging off the spline.
return 1;
// return 3;
}
// ----------------------------------------------------------------------------
Matrix3 SplineTrailSource::getTrailTM(
int idx, TimeValue t, std::vector<TimeValue>& levelTimes) {
// Return the TM of the spline at time t.
assert(pShapeNode != NULL);
if (levelTimes.size() <= 0) return Matrix3(1);
// return pShapeNode->GetObjectTM(t);
Matrix3 objTM = pShapeNode->GetObjectTM(t);
#ifdef CAM_TRAILS
// reference time for calculating camera-space trails
TimeValue cameraTrailRefTime = levelTimes[0];
if (levelTimes[0] < levelTimes[levelTimes.GetSize() - 1]) {
// moving trails and anchored trails put the leveltimes array in different
// orders (from newest to oldest). We need to flip the ref time for
// anchored trails.
cameraTrailRefTime = levelTimes[levelTimes.GetSize() - 1];
}
INode* pCam = GetCOREInterface()->GetINodeByName("Camera01");
if (pCam) {
// Works for moving trails
objTM = pShapeNode->GetObjectTM(t) * Inverse(pCam->GetNodeTM(t)) *
pCam->GetNodeTM(cameraTrailRefTime);
}
#endif
return objTM;
}
// ----------------------------------------------------------------------------
BOOL SplineTrailSource::isTrailStationary(
int trailIdx, TimeValue startT, TimeValue endT, TimeValue keyStep,
std::vector<TimeValue>& levelTimes) {
#ifdef CAM_TRAILS
// VV CAMTRAIL
return FALSE;
#else
// Return TRUE if the trail is stationary over the interval startT->endT,
// FALSE otherwise. This is used to cull zero-length trails from mesh
// generation. Sample the motion every keyStep ticks.
// TODO: cache values here?
TimeValue curT = startT;
// get the start position
Point3 startPos = getTMPos(pShapeNode->GetObjectTM(startT));
while (curT <= endT) {
curT += keyStep;
Point3 curPos = getTMPos(pShapeNode->GetObjectTM(curT));
if (Length(startPos - curPos) > SplineTrailSource::MIN_TRAIL_MOVEMENT) {
// trail has moved
return FALSE;
}
}
return TRUE;
#endif
}
// ----------------------------------------------------------------------------
// Return the number of levels at the tail of the trail where the trail isn't
// moving. Used to correctly do stretch mapping.
//
int SplineTrailSource::getNumStationaryLevels(
int trailIdx, std::vector<TimeValue>& levelTimes) {
int count = 0;
Matrix3 lastTM;
Matrix3 curTM;
#ifdef CAM_TRAILS
// VV CAMTRAIL
return 0;
#else
for (int level = (levelTimes.size() - 1); level > 0; --level) {
if (level == (levelTimes.size() - 1)) {
lastTM = getTrailTM(trailIdx, levelTimes[level], levelTimes);
} else {
curTM = getTrailTM(trailIdx, levelTimes[level], levelTimes);
if (curTM.Equals(lastTM)) {
// No change
count++;
lastTM = curTM;
} else
return count;
}
}
return 0;
#endif
}
// ----------------------------------------------------------------------------
Point3 SplineTrailSource::getTMPos(const Matrix3& m) {
// helper function to decompose a Matrix3 and return its
// translation component.
AffineParts parts;
decomp_affine(m, &parts);
return parts.t;
}
// ----------------------------------------------------------------------------
TimeValue SplineTrailSource::getTrailAgeAtTime(int, TimeValue) {
// not meaningful for spline trails.
return -1;
}
// ----------------------------------------------------------------------------
TimeValue SplineTrailSource::getTrailLifeTime(int) {
// not meaningful for spline trails.
return -1;
}
int SplineTrailSource::getTrailMtlID(int trailIdx) {
// There is no extra data for spline trails to define Material Index.
return 1;
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
ParticleTrailSource::ParticleTrailSource(ParticleSystemState* pState) {
pPartState = pState;
}
// ----------------------------------------------------------------------------
int ParticleTrailSource::numTrails() {
if (!pPartState)
return 0;
else {
return pPartState->numParticles();
}
}
// ----------------------------------------------------------------------------
void ParticleTrailSource::findSurroundingKeys(ParticleState& particle,
TimeValue t, int& pLeftKeyIdx,
int& pRightKeyIdx) {
// Find the two keys on either side of TimeValue t and set pLeftKeyIdx and
// pRightKeyIdx to their indices. In the case of an exact time match, set
// pLeftKeyIdx to the matching key and pRightKey to -1. Also do this for
// requests outside the animation range (ie before the first or after the last
// key).
// NB: Assumes the keys are evenly spaced in time (and in ascending order)!!
pLeftKeyIdx = pRightKeyIdx = -1;
int numKeys = particle.numKeys();
if (numKeys == 0) return;
if (numKeys == 1) {
pLeftKeyIdx = 0;
return;
}
TimeValue startTime = particle.getKey(0).getTime();
TimeValue endTime = particle.getKey(numKeys - 1).getTime();
// before range?
if (t <= startTime) {
pLeftKeyIdx = 0;
return;
}
// after range
if (t >= endTime) {
pLeftKeyIdx = numKeys - 1;
return;
}
// get the nearest key that has time < t.
int targetKeyIdx = (int)((float)numKeys * ((float)t - (float)startTime) /
((float)endTime - (float)startTime));
if (targetKeyIdx >= numKeys) targetKeyIdx = numKeys - 1;
const ParticlePositionKey* foundKey = &particle.getKey(targetKeyIdx);
// step back to a key before time t if we're not there yet.
while ((foundKey->getTime() > t) && (targetKeyIdx >= 0)) {
targetKeyIdx--;
foundKey = &particle.getKey(targetKeyIdx);
}
// Have we got an exact match (or are we on the last key)?
if ((foundKey->getTime() == t) || (targetKeyIdx == (numKeys - 1))) {
pLeftKeyIdx = targetKeyIdx;
return;
}
const ParticlePositionKey* nextKey = &particle.getKey(targetKeyIdx + 1);
// sanity check
if (!((foundKey->getTime() <= t) && (nextKey->getTime() >= t))) {
// The two keys don't straddle the requested time
// How can this be?
pLeftKeyIdx = targetKeyIdx;
return;
}
pLeftKeyIdx = targetKeyIdx;
pRightKeyIdx = targetKeyIdx + 1;
}
// ----------------------------------------------------------------------------
Matrix3 ParticleTrailSource::getTrailTM(int idx, TimeValue t,
std::vector<TimeValue>&) {
if (!pPartState) return Matrix3(TRUE);
int realPartIdx = idx;
ParticleState& particle = pPartState->getParticle(realPartIdx);
int leftKeyIdx = -1;
int rightKeyIdx = -1;
findSurroundingKeys(particle, t, leftKeyIdx, rightKeyIdx);
Matrix3 tm(TRUE);
if (leftKeyIdx == -1) {
// Couldn't find any keys???
return tm;
} else if (rightKeyIdx == -1) {
tm = particle.getKey(leftKeyIdx).getTM();
} else {
// To smoothly interpolate, we need the four keys surrounding the
// desired time.
//
// leftminusoneTM....leftTM...t...rightTM...rightplusoneTM
const Matrix3* leftminusoneTM = 0;
const Matrix3* leftTM = 0;
const Matrix3* rightTM = 0;
const Matrix3* rightplusoneTM = 0;
ParticlePositionKey& leftKey = particle.getKey(leftKeyIdx);
ParticlePositionKey& rightKey = particle.getKey(rightKeyIdx);
leftTM = &leftKey.getTM();
rightTM = &rightKey.getTM();
if (leftKeyIdx > 0) {
leftminusoneTM = &(particle.getKey(leftKeyIdx - 1).getTM());
}
if (rightKeyIdx < (particle.numKeys() - 1)) {
rightplusoneTM = &(particle.getKey(rightKeyIdx + 1).getTM());
}
tm = interpolateMatrices(leftminusoneTM, leftTM, rightTM, rightplusoneTM,
leftKey.getTime(), rightKey.getTime(), t);
}
return tm;
}
// ----------------------------------------------------------------------------
static float CubicInterpolate(float y0, float y1, float y2, float y3,
float mu) {
float a0, a1, a2, a3, mu2;
mu2 = mu * mu;
a0 = y3 - y2 - y0 + y1;
a1 = y0 - y1 - a0;
a2 = y2 - y0;
a3 = y1;
return (a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3);
}
// ----------------------------------------------------------------------------
static double CosineInterpolate(double y1, double y2, double mu) {
double mu2;
mu2 = (1 - cos(mu * PI)) / 2;
return (y1 * (1 - mu2) + y2 * mu2);
}
// ----------------------------------------------------------------------------
/*
Tension: 1 is high, 0 normal, -1 is low
Bias: 0 is even,
positive is towards first segment,
negative towards the other
*/
static float HermiteInterpolate(float y0, float y1, float y2, float y3,
float mu, float tension = 0.0,
float bias = 0.0) {
float m0, m1, mu2, mu3;
float a0, a1, a2, a3;
mu2 = mu * mu;
mu3 = mu2 * mu;
m0 = (y1 - y0) * (1 + bias) * (1 - tension) / 2;
m0 += (y2 - y1) * (1 - bias) * (1 - tension) / 2;
m1 = (y2 - y1) * (1 + bias) * (1 - tension) / 2;
m1 += (y3 - y2) * (1 - bias) * (1 - tension) / 2;
a0 = 2 * mu3 - 3 * mu2 + 1;
a1 = mu3 - 2 * mu2 + mu;
a2 = mu3 - mu2;
a3 = -2 * mu3 + 3 * mu2;
return (a0 * y1 + a1 * m0 + a2 * m1 + a3 * y2);
}
// ----------------------------------------------------------------------------
Matrix3 ParticleTrailSource::interpolateMatrices(
const Matrix3* leftminusoneTM, const Matrix3* leftTM,
const Matrix3* rightTM, const Matrix3* rightplusoneTM, TimeValue lTime,
TimeValue rTime, TimeValue cTime) {
// return the matrix gained by interpolating from (lTime,lMat)
// to (rTime,rMat) at time cTime.
//
// Assumes lTime <= cTime <= rTime
//
if (!leftTM || !rightTM) return Matrix3(1); // Identity
if (cTime == lTime) return *leftTM;
if (cTime == rTime) return *rightTM;
if (lTime == rTime) return *leftTM;
if ((cTime < lTime) || (cTime > rTime)) return Matrix3(1); // Identity
float interpFactor = fabs((float)(cTime - lTime) / (float)(rTime - lTime));
AffineParts lminusoneParts;
AffineParts lParts;
AffineParts rParts;
AffineParts rplusoneParts;
decomp_affine(*leftTM, &lParts);
decomp_affine(*rightTM, &rParts);
if (leftminusoneTM)
decomp_affine(*leftminusoneTM, &lminusoneParts);
else {
// If there's no key before the left key extrapolate along
// the last known direction for the porpoises of interpolation.
lminusoneParts.q = lParts.q;
lminusoneParts.t = lParts.t - (rParts.t - lParts.t);
lminusoneParts.k = lParts.k - (rParts.k - lParts.k);
}
if (rightplusoneTM)
decomp_affine(*rightplusoneTM, &rplusoneParts);
else {
// If there's no key after the right key extrapolate along
// the last known direction for the porpoises of interpolation.
rplusoneParts.q = rParts.q;
rplusoneParts.t = rParts.t + (rParts.t - lParts.t);
rplusoneParts.k = rParts.k + (rParts.k - lParts.k);
}
Point3 interpPos(
HermiteInterpolate(lminusoneParts.t.x, lParts.t.x, rParts.t.x,
rplusoneParts.t.x, interpFactor),
HermiteInterpolate(lminusoneParts.t.y, lParts.t.y, rParts.t.y,
rplusoneParts.t.y, interpFactor),
HermiteInterpolate(lminusoneParts.t.z, lParts.t.z, rParts.t.z,
rplusoneParts.t.z, interpFactor));
Point3 interpScale(
HermiteInterpolate(lminusoneParts.k.x, lParts.k.x, rParts.k.x,
rplusoneParts.k.x, interpFactor),
HermiteInterpolate(lminusoneParts.k.y, lParts.k.y, rParts.k.y,
rplusoneParts.k.y, interpFactor),
HermiteInterpolate(lminusoneParts.k.z, lParts.k.z, rParts.k.z,
rplusoneParts.k.z, interpFactor));
// Interpolate the rotation part.
rParts.q.MakeClosest(lParts.q);
Quat interpRot = Slerp(lParts.q, rParts.q, interpFactor);
Matrix3 interpMat(1);
interpMat.SetScale(interpScale);
Matrix3 rotMat(1);
rotMat.SetRotate(interpRot);
interpMat = interpMat * rotMat;
interpMat.Translate(interpPos);
return interpMat;
}
// ----------------------------------------------------------------------------
BOOL ParticleTrailSource::isTrailStationary(
int trailIdx, TimeValue startT, TimeValue endT, TimeValue keyStep,
std::vector<TimeValue>& levelTimes) {
// Return TRUE if the trail is stationary over the interval startT->endT,
// FALSE otherwise. This is used to cull zero-length trails from mesh
// generation. Sample the motion every keyStep ticks.
if (!pPartState) return TRUE;
ParticleState& particle = pPartState->getParticle(trailIdx);
// Do (startT, endT) and (particle->getBirthTime(), particle->getDeathTime())
// intersect?
Interval partInterval(particle.getBirthTime(), particle.getDeathTime());
Interval qryInterval(startT, endT);
Interval intersection = partInterval & qryInterval;
if (intersection.Empty() == TRUE) {
// No overlap - outside particle lifetime so no motion
return TRUE;
}
// If we overlap the particle's lifetime let's find out
// whether it moves (we're only checking the start and end times,
// which should be good enough - particle don't usually retrace
// their steps).
Matrix3 startTM = getTrailTM(trailIdx, startT, levelTimes);
Matrix3 endTM = getTrailTM(trailIdx, endT, levelTimes);
if (Length(startTM.GetTrans() - endTM.GetTrans()) < 0.001f)
return TRUE;
else
return FALSE;
}
// ----------------------------------------------------------------------------
// Get the number of frames for which the particle is stationary. This is
// just equal to the length of the trail minus the age of the particle (ie
// the particle is stationary before it is born
int ParticleTrailSource::getNumStationaryLevels(
int trailIdx, std::vector<TimeValue>& levelTimes) {
int count = 0;
Matrix3 lastTM;
Matrix3 curTM;
for (int level = (levelTimes.size() - 1); level > 0; --level) {
if (level == (levelTimes.size() - 1)) {
lastTM = getTrailTM(trailIdx, levelTimes[level], levelTimes);
} else {
curTM = getTrailTM(trailIdx, levelTimes[level], levelTimes);
if (curTM.Equals(lastTM)) {
// No change
count++;
lastTM = curTM;
} else
return count;
}
}
return 0;
}
// ----------------------------------------------------------------------------
// Get the age of the trail (in ticks) at time t.
// Return -1 to indicate outside the trail's lifetime.
//
TimeValue ParticleTrailSource::getTrailAgeAtTime(int trailIdx, TimeValue t) {
if (!pPartState) return -1;
const ParticleState& particle = pPartState->getParticle(trailIdx);
if ((t >= particle.getBirthTime()) && (t <= particle.getDeathTime()))
return t - particle.getBirthTime();
else
return -1;
}
// ----------------------------------------------------------------------------
// Get the trail lifetime of trail trailIdx
//
TimeValue ParticleTrailSource::getTrailLifeTime(int trailIdx) {
if (!pPartState) return -1;
const ParticleState& particle = pPartState->getParticle(trailIdx);
return particle.getDeathTime() - particle.getBirthTime();
}
// ----------------------------------------------------------------------------
int ParticleTrailSource::getTrailMtlID(int trailIdx) {
if (!pPartState) {
return 1;
}
const ParticleState& particle = pPartState->getParticle(trailIdx);
return particle.getMtlID();
}