forked from GeneralsOnlineDevelopmentTeam/GameClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart_emt.cpp
More file actions
875 lines (739 loc) · 25.9 KB
/
Copy pathpart_emt.cpp
File metadata and controls
875 lines (739 loc) · 25.9 KB
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/*
** Command & Conquer Generals(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/***************************************************************************
*** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
***************************************************************************
* *
* Project Name : G *
* *
* $Archive:: /Commando/Code/ww3d2/part_emt.cpp $*
* *
* $Author:: Naty_h $*
* *
* $Modtime:: 8/01/01 3:36p $*
* *
* $Revision:: 12 $*
* *
*-------------------------------------------------------------------------*
* Functions: *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include "part_emt.h"
#include "wwdebug.h"
#include "ww3d.h"
#include "assetmgr.h"
#include "part_ldr.h"
#include "w3derr.h"
#include "scene.h"
#include "texture.h"
#include "wwprofile.h"
#include <limits.h>
#include <gcd_lcm.h>
// Global variable which is only used to communicate the worldspace emitter
// velocity from ParticleEmitterClass::Create_New_Particles() to
// ParticleEmitterClass::Initialize_Particle(), for velocity inheritance.
Vector3 InheritedWorldSpaceEmitterVel;
// This debug setting disables particles from being generated
bool ParticleEmitterClass::DebugDisable = false;
// This is used to set the global behavior of emitters...
// Should they be removed from the scene when they complete their
// emissions, or should they stay in the scene. (For editing purposes)
// (gth) 09/17/2000 - particle emitters now have a local RemoveOnComplete flag
// which is initialized to the state of DefaultRemoveOnComplete.
bool ParticleEmitterClass::DefaultRemoveOnComplete = true;
ParticleEmitterClass::ParticleEmitterClass(float emit_rate, unsigned int burst_size,
Vector3Randomizer *pos_rnd, Vector3 base_vel, Vector3Randomizer *vel_rnd, float out_vel,
float vel_inherit_factor,
ParticlePropertyStruct<Vector3> &color,
ParticlePropertyStruct<float> &opacity,
ParticlePropertyStruct<float> &size,
ParticlePropertyStruct<float> &rotation, float orient_rnd,
ParticlePropertyStruct<float> &frames,
ParticlePropertyStruct<float> &blur_times,
Vector3 accel, float max_age, float future_start, TextureClass *tex, ShaderClass shader, int max_particles,
int max_buffer_size, bool pingpong,int render_mode,int frame_mode,
const W3dEmitterLinePropertiesStruct * line_props
) :
OneTimeBurstSize(1),
OneTimeBurst(false),
PosRand(pos_rnd),
BaseVel(base_vel * 0.001f),
VelRand(vel_rnd),
OutwardVel(out_vel * 0.001f),
VelInheritFactor(vel_inherit_factor),
EmitRemain(0U),
PrevQ(true),
PrevOrig(0.0, 0.0, 0.0),
Active(false),
FirstTime(true),
ParticlesLeft(max_particles),
MaxParticles(max_particles),
IsComplete(false),
RemoveOnComplete(DefaultRemoveOnComplete),
NameString(nullptr),
UserString(nullptr),
IsInScene(false)
{
EmitRate = emit_rate > 0.0f ? (unsigned int)(1000.0f / emit_rate) : 1000U;
BurstSize = burst_size != 0 ? burst_size : 1;
max_age = max_age > 0.0f ? max_age : 1.0f;
VelRand->Scale(0.001f);
// The maximum number of particles is determined by the emission rate, burst size and lifetime.
// However, it is capped both by the particle cap and by the maximum buffer size, if these are
// active.
int max_num = BurstSize * emit_rate * (max_age + 1);
if (max_particles > 0) max_num = MIN(max_num, max_particles);
if (max_buffer_size > 0) max_num = MIN(max_num, max_buffer_size);
max_num = MAX(max_num, 2); // max_num of 1 causes problems
Buffer = W3DNEW ParticleBufferClass(this, max_num, color, opacity, size, rotation, orient_rnd,
frames, blur_times, accel/1000000.0f,max_age, tex, shader, pingpong, render_mode, frame_mode,
line_props);
SET_REF_OWNER( Buffer );
BufferSceneNeeded = true;
NameString = ::_strdup ("ParticleEmitter");
}
ParticleEmitterClass::ParticleEmitterClass(const ParticleEmitterClass & src) :
IsInScene(false),
RenderObjClass(src)
{
EmitRate = src.EmitRate;
BurstSize = src.BurstSize;
OneTimeBurstSize = src.OneTimeBurstSize;
OneTimeBurst = src.OneTimeBurst;
if (src.PosRand) {
PosRand = src.PosRand->Clone();
} else {
PosRand = nullptr;
}
BaseVel = src.BaseVel;
if (src.VelRand) {
VelRand = src.VelRand->Clone();
} else {
VelRand = nullptr;
}
OutwardVel = src.OutwardVel;
VelInheritFactor = src.VelInheritFactor;
EmitRemain = src.EmitRemain;
PrevQ = src.PrevQ;
PrevOrig = src.PrevOrig;
MaxParticles = src.MaxParticles;
ParticlesLeft = src.ParticlesLeft;
Buffer = (ParticleBufferClass *) src.Buffer->Clone();
Buffer->Set_Emitter(this);
SET_REF_OWNER( Buffer );
BufferSceneNeeded = true;
Active = true; // default to on
FirstTime = true;
IsComplete = false;
#ifdef __APPLE__
NameString = src.NameString ? ::_strdup (src.NameString) : nullptr;
#else
NameString = ::_strdup (src.NameString);
#endif
}
ParticleEmitterClass & ParticleEmitterClass::operator = (const ParticleEmitterClass & that)
{
RenderObjClass::operator = (that);
if (this != &that) {
assert(0); // TODO: if you hit this assert, please implement me !!!;-)
}
return * this;
}
ParticleEmitterClass::~ParticleEmitterClass()
{
Buffer->Emitter_Is_Dead();
Buffer->Release_Ref();
delete PosRand;
PosRand = nullptr;
delete VelRand;
VelRand = nullptr;
if (NameString != nullptr) {
::free (NameString);
NameString = nullptr;
}
return ;
}
ParticleEmitterClass *
ParticleEmitterClass::Create_From_Definition (const ParticleEmitterDefClass &definition)
{
// Assume failure
ParticleEmitterClass *pemitter = nullptr;
// Attempt to load the texture for this emitter
const char *ptexture_filename = definition.Get_Texture_Filename ();
TextureClass *ptexture = nullptr;
if (ptexture_filename && ptexture_filename[0]) {
ptexture = WW3DAssetManager::Get_Instance()->Get_Texture
(
ptexture_filename,
MIP_LEVELS_ALL,
WW3D_FORMAT_UNKNOWN
);
// false); // no compression for particle textures!
}
ShaderClass shader;
definition.Get_Shader (shader);
if (WW3DAssetManager::Get_Instance()->Get_Activate_Fog_On_Load()) {
shader.Enable_Fog ("ParticleEmitterClass");
}
/*if (ptexture) {
// If texture has an alpha channel do alpha blending instead of additive
// (which is the default for point groups):
srTextureIFace::Dimensions dimensions;
ptexture->getDimensions(dimensions);
if (dimensions.pixelFormat.aBits > 0) {
shader = ShaderClass::_PresetAlphaSpriteShader;
}
}*/
//
// Peek at the definition's keyframes
//
ParticlePropertyStruct<Vector3> color_keys;
ParticlePropertyStruct<float> opacity_keys;
ParticlePropertyStruct<float> size_keys;
ParticlePropertyStruct<float> rotation_keys;
ParticlePropertyStruct<float> frame_keys;
ParticlePropertyStruct<float> blur_time_keys;
definition.Get_Color_Keyframes (color_keys);
definition.Get_Opacity_Keyframes (opacity_keys);
definition.Get_Size_Keyframes (size_keys);
definition.Get_Rotation_Keyframes (rotation_keys);
definition.Get_Frame_Keyframes (frame_keys);
definition.Get_Blur_Time_Keyframes (blur_time_keys);
//
// Create the emitter
//
pemitter = NEW_REF( ParticleEmitterClass, ( definition.Get_Emission_Rate (),
definition.Get_Burst_Size (),
definition.Get_Creation_Volume (),
definition.Get_Velocity (),
definition.Get_Velocity_Random (),
definition.Get_Outward_Vel (),
definition.Get_Vel_Inherit (),
color_keys,
opacity_keys,
size_keys,
rotation_keys,
definition.Get_Initial_Orientation_Random(),
frame_keys,
blur_time_keys,
definition.Get_Acceleration (),
definition.Get_Lifetime (),
0.0f,
ptexture,
shader,
definition.Get_Max_Emissions (),
0,
false,
definition.Get_Render_Mode (),
definition.Get_Frame_Mode (),
definition.Get_Line_Properties ()) );
delete [] color_keys.KeyTimes;
delete [] color_keys.Values;
delete [] opacity_keys.KeyTimes;
delete [] opacity_keys.Values;
delete [] size_keys.KeyTimes;
delete [] size_keys.Values;
delete [] rotation_keys.KeyTimes;
delete [] rotation_keys.Values;
delete [] frame_keys.KeyTimes;
delete [] frame_keys.Values;
delete [] blur_time_keys.KeyTimes;
delete [] blur_time_keys.Values;
// Pass the name along to the emitter
pemitter->Set_Name (definition.Get_Name ());
// release our reference to particle texture.
REF_PTR_RELEASE(ptexture);
// Return a pointer to the new emitter
return pemitter;
}
RenderObjClass * ParticleEmitterClass::Clone() const
{
return W3DNEW ParticleEmitterClass(*this);
}
void ParticleEmitterClass::Restart()
{
// calling Start will cause all internal counters to reset
Start();
}
void ParticleEmitterClass::Notify_Added(SceneClass * scene)
{
RenderObjClass::Notify_Added(scene);
scene->Register(this,SceneClass::ON_FRAME_UPDATE);
if (FirstTime == false) {
Active = true;
}
IsInScene = true;
}
void ParticleEmitterClass::Notify_Removed(SceneClass * scene)
{
scene->Unregister(this,SceneClass::ON_FRAME_UPDATE);
RenderObjClass::Notify_Removed(scene);
Active = false;
IsInScene = false;
//Buffer->Emitter_Is_Dead();
}
// Scales the size of all particles and effects positions/velocities of
// particles emitted after the Scale() call (but not before)
void ParticleEmitterClass::Scale(float scale)
{
// Scale all velosity and position parameters
if (PosRand) PosRand->Scale(scale);
BaseVel *= scale;
if (VelRand) VelRand->Scale(scale);
OutwardVel *= scale;
// Scale sizes of all particles
Buffer->Scale(scale);
}
// Put particle buffer in scene if this is the first time (clunky code
// - hopefully can be rewritten more cleanly in future)...
void ParticleEmitterClass::On_Frame_Update()
{
if (Active && !IsComplete) {
if (FirstTime) {
// The particle buffer doesn't have a valid Scene yet - the emitter
// finds out what scene it belongs to (goes up the container tree
// until it finds a non-null Scene), and then adds the particle
// buffer to it.
if ( BufferSceneNeeded ) {
if (Is_In_Scene()) {
Buffer->Add(Scene);
BufferSceneNeeded = false;
} else {
return;
}
}
BufferSceneNeeded = false;
// Initialize previous transform:
PrevQ = Build_Quaternion(Get_Transform());
PrevOrig = Get_Transform().Get_Translation();
FirstTime = false;
}
}
if (Is_Complete()) {
if (Is_In_Scene() && Is_Remove_On_Complete_Enabled()) {
Scene->Register(this,SceneClass::RELEASE);
}
}
}
void ParticleEmitterClass::Reset()
{
// Note: This flag needs to be set first thing, otherwise
// getting the transform will result in an 'update_x' call
// which in turn results in a 'Set_Animation_Hidden' call, which
// in turn will cause the Update_Visibility function to call
// Start(). This won't cause a stack overflow like in Start
// but it would do some extra work.
Active = true;
// Initialize previous transform:
PrevQ = Build_Quaternion(Get_Transform());
PrevOrig = Get_Transform().Get_Translation();
// Reset the number of particles to emit
ParticlesLeft = MaxParticles;
EmitRemain = 0;
IsComplete = false;
}
void ParticleEmitterClass::Start()
{
// Note: This flag needs to be set first thing, otherwise
// getting the transform will result in an 'update_x' call
// which in turn results in a 'Set_Animation_Hidden' call, which
// in turn will cause the Update_Visibility function to call
// this method. And then... Stack Overflow! ;)
Active = true;
// Initialize previous transform:
PrevQ = Build_Quaternion(Get_Transform());
PrevOrig = Get_Transform().Get_Translation();
// Reset the number of particles to emit (if necessary)
if (IsComplete == true) {
ParticlesLeft = MaxParticles;
IsComplete = false;
}
}
void ParticleEmitterClass::Stop()
{
Active = false;
}
bool ParticleEmitterClass::Is_Stopped()
{
return (Active == false);
}
void ParticleEmitterClass::Set_Position_Randomizer(Vector3Randomizer *rand)
{
delete PosRand;
PosRand = rand;
}
void ParticleEmitterClass::Set_Velocity_Randomizer(Vector3Randomizer *rand)
{
delete VelRand;
VelRand = rand;
if (VelRand) {
VelRand->Scale(0.001f); // Convert from seconds to ms
}
}
Vector3Randomizer *ParticleEmitterClass::Get_Creation_Volume () const
{
Vector3Randomizer *randomizer = nullptr;
if (PosRand != nullptr) {
randomizer = PosRand->Clone ();
//randomizer->Scale (1000.0F);
}
return randomizer;
}
Vector3Randomizer *ParticleEmitterClass::Get_Velocity_Random () const
{
Vector3Randomizer *randomizer = nullptr;
if (VelRand != nullptr) {
randomizer = VelRand->Clone ();
randomizer->Scale (1000.0F);
}
return randomizer;
}
void ParticleEmitterClass::Set_Base_Velocity(const Vector3& base_vel)
{
BaseVel = base_vel * 0.001f; // Convert from seconds to ms
}
void ParticleEmitterClass::Set_Outwards_Velocity(float out_vel)
{
OutwardVel = out_vel * 0.001f; // Convert from seconds to ms
}
void ParticleEmitterClass::Set_Velocity_Inheritance_Factor(float inh_factor)
{
VelInheritFactor = inh_factor;
}
// Emit particles (put in particle buffer). This is called by the particle
// buffer On_Frame_Update() function to avoid order dependence.
void ParticleEmitterClass::Emit()
{
WWPROFILE("PartlicleEmitter::Emit");
#ifdef WWDEBUG
if (DebugDisable == true) {
return;
}
#endif
if (Active && !IsComplete) {
Quaternion curr_quat; // Quaternion form of orientation.
Vector3 curr_orig; // Origin.
// Convert current matrix into quaternion + origin form.
curr_quat = Build_Quaternion(Get_Transform());
curr_orig = Get_Transform().Get_Translation();
Create_New_Particles(curr_quat, curr_orig);
PrevQ = curr_quat;
PrevOrig = curr_orig;
} else {
// These need to be updated each frame no matter what
PrevQ = Build_Quaternion(Get_Transform());
PrevOrig = Get_Transform().Get_Translation();
}
}
// Collision sphere is a point - emitter emits also when not visible, so this
// is only important to avoid affecting the collision spheres of composite
// objects into which the emitter is inserted.
void ParticleEmitterClass::Update_Cached_Bounding_Volumes() const
{
CachedBoundingSphere.Init(Get_Position(),0.0);
CachedBoundingBox.Center = Get_Position();
CachedBoundingBox.Extent.Set(0,0,0);
Validate_Cached_Bounding_Volumes();
}
// Note that creation location and velocity are in local coordinates, so new
// particles need to be transformed into worldspace. It is important to get
// the correct transform at the exact time of particle creation (for frame-
// rate independence), so the current emitter transform is calculated by
// time-based interpolation between the transforms at the beginning and end
// of the current frame. This interpolation is performed via quaternion-
// slerping the orientation and lerping the origin.
void ParticleEmitterClass::Create_New_Particles(const Quaternion & curr_quat, const Vector3 & curr_orig)
{
Quaternion quat;
Vector3 orig;
// The emit remainder from the previous interval (the time remaining in
// the previous interval when the last particle was emitted) is added to
// the size of the current frame to yield the time currently available
// for emitting particles.
unsigned int frametime = WW3D::Get_Sync_Frame_Time();
// Since the particles are written into a wraparound buffer, we can take the time modulo a time
// constant which represents the time it takes to fill up the entire buffer with new particles.
// We will do this so we don't run into performance problems with very large frame times.
if (frametime > 100 * EmitRate) { // If the loop will run over 100 times
unsigned int buf_size = Buffer->Get_Buffer_Size();
unsigned int gcd = Greatest_Common_Divisor(buf_size, BurstSize);
unsigned int bursts = buf_size / gcd;
unsigned int cycle_time = EmitRate * bursts;
if (cycle_time > 1) {
frametime = frametime % cycle_time;
} else {
frametime = 1;
}
}
EmitRemain += frametime;
// The interpolation factor (0: start of interval: 1: end of interval).
// Possibly negative at this point, but after the delta is added to it, it
// will be positive.
float fl_frametime = (float)frametime;
float alpha = 1 - ((float)EmitRemain / fl_frametime);
float d_alpha = (float)EmitRate / fl_frametime;
// Setup the slerp between the two quaternions.
SlerpInfoStruct slerp_info;
Slerp_Setup(PrevQ, curr_quat, &slerp_info);
// Find the velocity of the emitter (for velocity inheritance).
// InheritedWorldSpaceEmitterVel is a global variable which is only used
// to pass this into the following Initialize_Particle() calls without
// having to set it as an argument for each call.
if (VelInheritFactor) {
InheritedWorldSpaceEmitterVel = (curr_orig - PrevOrig) * (VelInheritFactor / fl_frametime);
} else {
InheritedWorldSpaceEmitterVel.Set(0.0, 0.0, 0.0);
}
for (; EmitRemain > EmitRate;) {
// Calculate the new remainder.
EmitRemain -= EmitRate;
// Interpolate the start and end transforms to find the transform at
// the moment of particle creation.
alpha += d_alpha;
quat = Cached_Slerp(PrevQ, curr_quat, alpha, &slerp_info);
Vector3::Lerp(PrevOrig, curr_orig, alpha, &orig);
// Initialize BurstSize new particles with the given age and emitter
// transform (expressed as a quaternion and origin vector), and add it
// to the particle buffer's new particle vector.
unsigned int age = WW3D::Get_Sync_Time() - EmitRemain;
unsigned int burst_size = BurstSize;
if (OneTimeBurst) {
burst_size = OneTimeBurstSize;
OneTimeBurst = false;
}
if ( ParticlesLeft > 0 ) { // if we are counting,
if (burst_size > (unsigned int)ParticlesLeft) {
burst_size = (unsigned int)ParticlesLeft;
ParticlesLeft = 0;
} else {
ParticlesLeft -= burst_size;
}
if ( ParticlesLeft <= 0 ) { // count and if done
IsComplete = true; // stop
}
}
for (unsigned int i = 0; i < burst_size; i++) {
Initialize_Particle(Buffer->Add_Uninitialized_New_Particle(), age, quat, orig);
}
if (IsComplete) break;
}
}
// Initialize one new particle at the given NewParticleStruct address, with
// the given age and emitter transform (expressed as a quaternion and origin
// vector). (must check if address is nullptr).
void ParticleEmitterClass::Initialize_Particle(NewParticleStruct * newpart,
unsigned int timestamp, const Quaternion & quat, const Vector3 & orig)
{
// Set time stamp.
newpart->TimeStamp = timestamp;
// Set starting (random) local position.
Vector3 rand_pos;
if (PosRand) {
PosRand->Get_Vector(rand_pos);
} else {
rand_pos.Set(0.0, 0.0, 0.0);
}
// Transform position to worldspace, using the transform at moment of
// particle creation.
newpart->Position = quat.Rotate_Vector(rand_pos) + orig;
// Set (random) local velocity.
Vector3 rand_vel;
if (VelRand) {
VelRand->Get_Vector(rand_vel);
} else {
rand_vel.Set(0.0, 0.0, 0.0);
}
// Add outwards velocity to emitterspace velocity
if (OutwardVel) {
// Find vector pointing outwards (from origin to creation position)
Vector3 outwards;
float pos_l2 = rand_pos.Length2();
if (pos_l2) {
outwards = rand_pos * (OutwardVel * WWMath::Inv_Sqrt(pos_l2));
} else {
outwards.X = OutwardVel;
outwards.Y = 0.0f;
outwards.Z = 0.0f;
}
rand_vel += outwards;
}
// Add base velocity to emitterspace velocity
rand_vel += BaseVel;
// Rotate velocity to worldspace and add emitter's inherited velocity.
newpart->Velocity = InheritedWorldSpaceEmitterVel + quat.Rotate_Vector(rand_vel);
}
ParticleEmitterDefClass *
ParticleEmitterClass::Build_Definition () const
{
// Allocate a new emitter definition object
ParticleEmitterDefClass *pdefinition = W3DNEW ParticleEmitterDefClass;
WWASSERT (pdefinition != nullptr);
if (pdefinition != nullptr) {
// Set the texture's filename
TextureClass *ptexture = Get_Texture ();
if (ptexture != nullptr) {
pdefinition->Set_Texture_Filename (ptexture->Get_Texture_Name());
REF_PTR_RELEASE(ptexture);
}
// Now fill the definition with data from this emitter instance
pdefinition->Set_Render_Mode (Get_Render_Mode());
pdefinition->Set_Frame_Mode (Get_Frame_Mode());
pdefinition->Set_Name (Get_Name ());
pdefinition->Set_Lifetime (Get_Lifetime ());
pdefinition->Set_Emission_Rate (Get_Emission_Rate ());
pdefinition->Set_Max_Emissions (Get_Max_Particles ());
pdefinition->Set_Fade_Time (Get_Fade_Time ());
pdefinition->Set_Gravity (0);
pdefinition->Set_Elasticity (0);
pdefinition->Set_Velocity (Get_Start_Velocity ());
pdefinition->Set_Acceleration (Get_Acceleration ());
pdefinition->Set_Burst_Size (Get_Burst_Size ());
pdefinition->Set_Outward_Vel (Get_Outwards_Vel ());
pdefinition->Set_Vel_Inherit (Get_Velocity_Inherit ());
pdefinition->Set_Shader (Get_Shader ());
//
// Pass the creation volume onto the definition
//
Vector3Randomizer *randomizer = Get_Creation_Volume ();
pdefinition->Set_Creation_Volume (randomizer);
//
// Pass the velocity randomizer onto the definition
//
randomizer = Get_Velocity_Random ();
pdefinition->Set_Velocity_Random (randomizer);
//
// Pass the color keyframes onto the definition
//
ParticlePropertyStruct<Vector3> colors;
Get_Color_Key_Frames (colors);
pdefinition->Set_Color_Keyframes (colors);
delete [] colors.KeyTimes;
delete [] colors.Values;
//
// Pass the opacity keyframes onto the definition
//
ParticlePropertyStruct<float> opacities;
Get_Opacity_Key_Frames (opacities);
pdefinition->Set_Opacity_Keyframes (opacities);
delete [] opacities.KeyTimes;
delete [] opacities.Values;
//
// Pass the size keyframes onto the definition
//
ParticlePropertyStruct<float> sizes;
Get_Size_Key_Frames (sizes);
pdefinition->Set_Size_Keyframes (sizes);
delete [] sizes.KeyTimes;
delete [] sizes.Values;
//
// Pass the rotation keyframes onto the definition
//
ParticlePropertyStruct<float> rotations;
Get_Rotation_Key_Frames (rotations);
pdefinition->Set_Rotation_Keyframes (rotations, Get_Initial_Orientation_Random());
delete [] rotations.KeyTimes;
delete [] rotations.Values;
//
// Pass the frame keyframes onto the definition
//
ParticlePropertyStruct<float> frames;
Get_Frame_Key_Frames (frames);
pdefinition->Set_Frame_Keyframes (frames);
delete [] frames.KeyTimes;
delete [] frames.Values;
//
// Pass the blur time keyframes onto the definition
//
ParticlePropertyStruct<float> blur_times;
Get_Blur_Time_Key_Frames (blur_times);
pdefinition->Set_Blur_Time_Keyframes (blur_times);
delete [] blur_times.KeyTimes;
delete [] blur_times.Values;
//
// Set up the line parameters
//
pdefinition->Set_Line_Texture_Mapping_Mode(Get_Line_Texture_Mapping_Mode());
pdefinition->Set_Merge_Intersections(Is_Merge_Intersections());
pdefinition->Set_Freeze_Random(Is_Freeze_Random());
pdefinition->Set_Disable_Sorting(Is_Sorting_Disabled());
pdefinition->Set_End_Caps(Are_End_Caps_Enabled());
pdefinition->Set_Subdivision_Level(Get_Subdivision_Level());
pdefinition->Set_Noise_Amplitude(Get_Noise_Amplitude());
pdefinition->Set_Merge_Abort_Factor(Get_Merge_Abort_Factor());
pdefinition->Set_Texture_Tile_Factor(Get_Texture_Tile_Factor());
pdefinition->Set_UV_Offset_Rate(Get_UV_Offset_Rate());
}
// Return a pointer to the new definition
return pdefinition;
}
WW3DErrorType
ParticleEmitterClass::Save (ChunkSaveClass &chunk_save) const
{
// Assume error
WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
// Build a definition from this emitter instance, and save it
// to the chunk.
ParticleEmitterDefClass *pdefinition = Build_Definition ();
if (pdefinition != nullptr) {
ret_val = pdefinition->Save_W3D (chunk_save);
}
// Return the WW3DErrorType return code
return ret_val;
}
void
ParticleEmitterClass::Set_Name (const char *pname)
{
// Free the old name if necessary
if (NameString != nullptr) {
::free (NameString);
NameString = nullptr;
}
// Copy the provided name
NameString = ::_strdup (pname);
return ;
}
void
ParticleEmitterClass::Update_On_Visibility()
{
// Simply start or stop the emission based on
// the visibility state of the emitter.
if (Is_Not_Hidden_At_All () && Is_Stopped () && IsInScene) {
Start ();
} else if ((Is_Not_Hidden_At_All () == false) && (Is_Stopped () == false)) {
Stop ();
}
return ;
}
void
ParticleEmitterClass::Add_Dependencies_To_List
(
DynamicVectorClass<StringClass> &file_list,
bool textures_only
)
{
//
// Get the texture the emitter is using and add it to our list
//
TextureClass *texture = Get_Texture ();
if (texture != nullptr) {
file_list.Add (texture->Get_Full_Path ());
REF_PTR_RELEASE(texture);
}
// Allow the base class to process this call (extremely important)
RenderObjClass::Add_Dependencies_To_List (file_list, textures_only);
return ;
}