forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.h
2264 lines (1815 loc) · 75.1 KB
/
particles.h
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: particle system definitions
//
//===========================================================================//
#ifndef PARTICLES_H
#define PARTICLES_H
#ifdef _WIN32
#pragma once
#endif
#include "mathlib/mathlib.h"
#include "mathlib/vector.h"
#include "mathlib/ssemath.h"
#include "materialsystem/imaterialsystem.h"
#include "dmxloader/dmxelement.h"
#include "tier1/utlintrusivelist.h"
#include "vstdlib/random.h"
#include "tier1/utlobjectreference.h"
#include "tier1/UtlStringMap.h"
#include "tier1/utlmap.h"
#include "materialsystem/MaterialSystemUtil.h"
#include "trace.h"
#include "tier1/utlsoacontainer.h"
#if defined( CLIENT_DLL )
#include "c_pixel_visibility.h"
#endif
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
struct DmxElementUnpackStructure_t;
class CParticleSystemDefinition;
class CParticleCollection;
class CParticleOperatorInstance;
class CParticleSystemDictionary;
class CUtlBuffer;
class IParticleOperatorDefinition;
class CSheet;
class CMeshBuilder;
extern float s_pRandomFloats[];
//-----------------------------------------------------------------------------
// Random numbers
//-----------------------------------------------------------------------------
#define MAX_RANDOM_FLOATS 4096
#define RANDOM_FLOAT_MASK ( MAX_RANDOM_FLOATS - 1 )
//-----------------------------------------------------------------------------
// Particle attributes
//-----------------------------------------------------------------------------
#define MAX_PARTICLE_ATTRIBUTES 32
#define DEFPARTICLE_ATTRIBUTE( name, bit ) \
const int PARTICLE_ATTRIBUTE_##name##_MASK = (1 << bit); \
const int PARTICLE_ATTRIBUTE_##name = bit;
// required
DEFPARTICLE_ATTRIBUTE( XYZ, 0 );
// particle lifetime (duration) of particle as a float.
DEFPARTICLE_ATTRIBUTE( LIFE_DURATION, 1 );
// prev coordinates for verlet integration
DEFPARTICLE_ATTRIBUTE( PREV_XYZ, 2 );
// radius of particle
DEFPARTICLE_ATTRIBUTE( RADIUS, 3 );
// rotation angle of particle
DEFPARTICLE_ATTRIBUTE( ROTATION, 4 );
// rotation speed of particle
DEFPARTICLE_ATTRIBUTE( ROTATION_SPEED, 5 );
// tint of particle
DEFPARTICLE_ATTRIBUTE( TINT_RGB, 6 );
// alpha tint of particle
DEFPARTICLE_ATTRIBUTE( ALPHA, 7 );
// creation time stamp (relative to particle system creation)
DEFPARTICLE_ATTRIBUTE( CREATION_TIME, 8 );
// sequnece # (which animation sequence number this particle uses )
DEFPARTICLE_ATTRIBUTE( SEQUENCE_NUMBER, 9 );
// length of the trail
DEFPARTICLE_ATTRIBUTE( TRAIL_LENGTH, 10 );
// unique particle identifier
DEFPARTICLE_ATTRIBUTE( PARTICLE_ID, 11 );
// unique rotation around up vector
DEFPARTICLE_ATTRIBUTE( YAW, 12 );
// second sequnece # (which animation sequence number this particle uses )
DEFPARTICLE_ATTRIBUTE( SEQUENCE_NUMBER1, 13 );
// hit box index
DEFPARTICLE_ATTRIBUTE( HITBOX_INDEX, 14 );
DEFPARTICLE_ATTRIBUTE( HITBOX_RELATIVE_XYZ, 15 );
DEFPARTICLE_ATTRIBUTE( ALPHA2, 16 );
// particle trace caching fields
DEFPARTICLE_ATTRIBUTE( TRACE_P0, 17 ); // start pnt of trace
DEFPARTICLE_ATTRIBUTE( TRACE_P1, 18 ); // end pnt of trace
DEFPARTICLE_ATTRIBUTE( TRACE_HIT_T, 19 ); // 0..1 if hit
DEFPARTICLE_ATTRIBUTE( TRACE_HIT_NORMAL, 20 ); // 0 0 0 if no hit
#define MAX_PARTICLE_CONTROL_POINTS 64
#define ATTRIBUTES_WHICH_ARE_VEC3S_MASK ( PARTICLE_ATTRIBUTE_TRACE_P0_MASK | PARTICLE_ATTRIBUTE_TRACE_P1_MASK | \
PARTICLE_ATTRIBUTE_TRACE_HIT_NORMAL | PARTICLE_ATTRIBUTE_XYZ_MASK | \
PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_TINT_RGB_MASK | \
PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK )
#define ATTRIBUTES_WHICH_ARE_0_TO_1 (PARTICLE_ATTRIBUTE_ALPHA_MASK | PARTICLE_ATTRIBUTE_ALPHA2_MASK)
#define ATTRIBUTES_WHICH_ARE_ANGLES (PARTICLE_ATTRIBUTE_ROTATION_MASK | PARTICLE_ATTRIBUTE_YAW_MASK )
#define ATTRIBUTES_WHICH_ARE_INTS (PARTICLE_ATTRIBUTE_PARTICLE_ID_MASK | PARTICLE_ATTRIBUTE_HITBOX_INDEX_MASK )
#if defined( _X360 )
#define MAX_PARTICLES_IN_A_SYSTEM 2000
#else
#define MAX_PARTICLES_IN_A_SYSTEM 5000
#endif
// Set this to 1 or 0 to enable or disable particle profiling.
// Note that this profiling is expensive on Linux, and some anti-virus
// products can make this *extremely* expensive on Windows.
#define MEASURE_PARTICLE_PERF 0
//-----------------------------------------------------------------------------
// Particle function types
//-----------------------------------------------------------------------------
enum ParticleFunctionType_t
{
FUNCTION_RENDERER = 0,
FUNCTION_OPERATOR,
FUNCTION_INITIALIZER,
FUNCTION_EMITTER,
FUNCTION_CHILDREN, // NOTE: This one is a fake function type, only here to help eliminate a ton of duplicated code in the editor
FUNCTION_FORCEGENERATOR,
FUNCTION_CONSTRAINT,
PARTICLE_FUNCTION_COUNT
};
struct CParticleVisibilityInputs
{
float m_flCameraBias;
float m_flInputMin;
float m_flInputMax;
float m_flAlphaScaleMin;
float m_flAlphaScaleMax;
float m_flRadiusScaleMin;
float m_flRadiusScaleMax;
float m_flProxyRadius;
float m_flBBoxScale;
bool m_bUseBBox;
int m_nCPin;
};
struct ModelHitBoxInfo_t
{
Vector m_vecBoxMins;
Vector m_vecBoxMaxes;
matrix3x4_t m_Transform;
};
class CModelHitBoxesInfo
{
public:
float m_flLastUpdateTime;
float m_flPrevLastUpdateTime;
int m_nNumHitBoxes;
int m_nNumPrevHitBoxes;
ModelHitBoxInfo_t *m_pHitBoxes;
ModelHitBoxInfo_t *m_pPrevBoxes;
bool CurAndPrevValid( void ) const
{
return ( m_nNumHitBoxes && ( m_nNumPrevHitBoxes == m_nNumHitBoxes ) );
}
CModelHitBoxesInfo( void )
{
m_flLastUpdateTime = -1;
m_nNumHitBoxes = 0;
m_nNumPrevHitBoxes = 0;
m_pHitBoxes = NULL;
m_pPrevBoxes = NULL;
}
~CModelHitBoxesInfo( void )
{
if ( m_pHitBoxes )
delete[] m_pHitBoxes;
if ( m_pPrevBoxes )
delete[] m_pPrevBoxes;
}
};
//-----------------------------------------------------------------------------
// Interface to allow the particle system to call back into the client
//-----------------------------------------------------------------------------
#define PARTICLE_SYSTEM_QUERY_INTERFACE_VERSION "VParticleSystemQuery001"
class IParticleSystemQuery : public IAppSystem
{
public:
virtual void GetLightingAtPoint( const Vector& vecOrigin, Color &tint ) = 0;
virtual void TraceLine( const Vector& vecAbsStart,
const Vector& vecAbsEnd, unsigned int mask,
const class IHandleEntity *ignore,
int collisionGroup,
CBaseTrace *ptr ) = 0;
// given a possible spawn point, tries to movie it to be on or in the source object. returns
// true if it succeeded
virtual bool MovePointInsideControllingObject( CParticleCollection *pParticles,
void *pObject,
Vector *pPnt )
{
return true;
}
virtual bool IsPointInControllingObjectHitBox(
CParticleCollection *pParticles,
int nControlPointNumber, Vector vecPos, bool bBBoxOnly = false )
{
return true;
}
virtual int GetCollisionGroupFromName( const char *pszCollisionGroupName )
{
return 0; // == COLLISION_GROUP_NONE
}
virtual void GetRandomPointsOnControllingObjectHitBox(
CParticleCollection *pParticles,
int nControlPointNumber,
int nNumPtsOut,
float flBBoxScale,
int nNumTrysToGetAPointInsideTheModel,
Vector *pPntsOut,
Vector vecDirectionBias,
Vector *pHitBoxRelativeCoordOut = NULL,
int *pHitBoxIndexOut = NULL ) = 0;
virtual int GetControllingObjectHitBoxInfo(
CParticleCollection *pParticles,
int nControlPointNumber,
int nBufSize, // # of output slots available
ModelHitBoxInfo_t *pHitBoxOutputBuffer )
{
// returns number of hit boxes output
return 0;
}
virtual Vector GetLocalPlayerPos( void )
{
return vec3_origin;
}
virtual void GetLocalPlayerEyeVectors( Vector *pForward, Vector *pRight = NULL, Vector *pUp = NULL )
{
*pForward = vec3_origin;
*pRight = vec3_origin;
*pUp = vec3_origin;
}
virtual float GetPixelVisibility( int *pQueryHandle, const Vector &vecOrigin, float flScale ) = 0;
virtual void SetUpLightingEnvironment( const Vector& pos )
{
}
};
//-----------------------------------------------------------------------------
//
// Particle system manager. Using a class because tools need it that way
// so the SFM and PET tools can share managers despite being linked to
// separate particle system .libs
//
//-----------------------------------------------------------------------------
typedef int ParticleSystemHandle_t;
class CParticleSystemMgr
{
public:
// Constructor, destructor
CParticleSystemMgr();
~CParticleSystemMgr();
// Initialize the particle system
bool Init( IParticleSystemQuery *pQuery );
// methods to add builtin operators. If you don't call these at startup, you won't be able to sim or draw. These are done separately from Init, so that
// the server can omit the code needed for rendering/simulation, if desired.
void AddBuiltinSimulationOperators( void );
void AddBuiltinRenderingOperators( void );
// Registration of known operators
void AddParticleOperator( ParticleFunctionType_t nOpType, IParticleOperatorDefinition *pOpFactory );
// Read a particle config file, add it to the list of particle configs
bool ReadParticleConfigFile( const char *pFileName, bool bPrecache, bool bDecommitTempMemory = true );
bool ReadParticleConfigFile( CUtlBuffer &buf, bool bPrecache, bool bDecommitTempMemory = true, const char *pFileName = NULL );
void DecommitTempMemory();
// For recording, write a specific particle system to a CUtlBuffer in DMX format
bool WriteParticleConfigFile( const char *pParticleSystemName, CUtlBuffer &buf, bool bPreventNameBasedLookup = false );
bool WriteParticleConfigFile( const DmObjectId_t& id, CUtlBuffer &buf, bool bPreventNameBasedLookup = false );
// create a particle system by name. returns null if one of that name does not exist
CParticleCollection *CreateParticleCollection( const char *pParticleSystemName, float flDelay = 0.0f, int nRandomSeed = 0 );
// create a particle system given a particle system id
CParticleCollection *CreateParticleCollection( const DmObjectId_t &id, float flDelay = 0.0f, int nRandomSeed = 0 );
// Is a particular particle system defined?
bool IsParticleSystemDefined( const char *pParticleSystemName );
bool IsParticleSystemDefined( const DmObjectId_t &id );
// Returns the index of the specified particle system.
ParticleSystemHandle_t GetParticleSystemIndex( const char *pParticleSystemName );
// Returns the name of the specified particle system.
const char *GetParticleSystemNameFromIndex( ParticleSystemHandle_t iIndex );
// Return the number of particle systems in our dictionary
int GetParticleSystemCount( void );
// call to get available particle operator definitions
// NOTE: FUNCTION_CHILDREN will return a faked one, for ease of writing the editor
CUtlVector< IParticleOperatorDefinition *> &GetAvailableParticleOperatorList( ParticleFunctionType_t nWhichList );
// Returns the unpack structure for a particle system definition
const DmxElementUnpackStructure_t *GetParticleSystemDefinitionUnpackStructure();
// Particle sheet management
void ShouldLoadSheets( bool bLoadSheets );
CSheet *FindOrLoadSheet( char const *pszFname, ITexture *pTexture );
CSheet *FindOrLoadSheet( IMaterial *pMaterial );
void FlushAllSheets( void );
// Render cache used to render opaque particle collections
void ResetRenderCache( void );
void AddToRenderCache( CParticleCollection *pParticles );
void DrawRenderCache( bool bShadowDepth );
IParticleSystemQuery *Query( void ) { return m_pQuery; }
// return the particle field name
const char* GetParticleFieldName( int nParticleField ) const;
// WARNING: the pointer returned by this function may be invalidated
// *at any time* by the editor, so do not ever cache it.
CParticleSystemDefinition* FindParticleSystem( const char *pName );
CParticleSystemDefinition* FindParticleSystem( const DmObjectId_t& id );
void CommitProfileInformation( bool bCommit ); // call after simulation, if you want
// sim time recorded. if oyu pass
// flase, info will be thrown away and
// uncomitted time reset. Having this
// function lets you only record
// profile data for slow frames if
// desired.
void DumpProfileInformation( void ); // write particle_profile.csv
// Cache/uncache materials used by particle systems
void PrecacheParticleSystem( const char *pName );
void UncacheAllParticleSystems();
// Sets the last simulation time, used for particle system sleeping logic
void SetLastSimulationTime( float flTime );
float GetLastSimulationTime() const;
int Debug_GetTotalParticleCount() const;
bool Debug_FrameWarningNeededTestAndReset();
float ParticleThrottleScaling() const; // Returns 1.0 = not restricted, 0.0 = fully restricted (i.e. don't draw!)
bool ParticleThrottleRandomEnable() const; // Retruns a randomish bool to say if you should draw this particle.
void TallyParticlesRendered( int nVertexCount, int nIndexCount = 0 );
private:
struct RenderCache_t
{
IMaterial *m_pMaterial;
CUtlVector< CParticleCollection * > m_ParticleCollections;
};
struct BatchStep_t
{
CParticleCollection *m_pParticles;
CParticleOperatorInstance *m_pRenderer;
void *m_pContext;
int m_nFirstParticle;
int m_nParticleCount;
int m_nVertCount;
};
struct Batch_t
{
int m_nVertCount;
int m_nIndexCount;
CUtlVector< BatchStep_t > m_BatchStep;
};
// Unserialization-related methods
bool ReadParticleDefinitions( CUtlBuffer &buf, const char *pFileName, bool bPrecache, bool bDecommitTempMemory );
void AddParticleSystem( CDmxElement *pParticleSystem );
// Serialization-related methods
CDmxElement *CreateParticleDmxElement( const DmObjectId_t &id );
CDmxElement *CreateParticleDmxElement( const char *pParticleSystemName );
bool WriteParticleConfigFile( CDmxElement *pParticleSystem, CUtlBuffer &buf, bool bPreventNameBasedLookup );
// Builds a list of batches to render
void BuildBatchList( int iRenderCache, IMatRenderContext *pRenderContext, CUtlVector< Batch_t >& batches );
// Known operators
CUtlVector<IParticleOperatorDefinition *> m_ParticleOperators[PARTICLE_FUNCTION_COUNT];
// Particle system dictionary
CParticleSystemDictionary *m_pParticleSystemDictionary;
// typedef CUtlMap< ITexture *, CSheet* > SheetsCache;
typedef CUtlStringMap< CSheet* > SheetsCache_t;
SheetsCache_t m_SheetList;
// attaching and dtaching killlists. when simulating, a particle system gets a kill list. after
// simulating, the memory for that will be used for the next particle system. This matters for
// threaded particles, because we don't want to share the same kill list between simultaneously
// simulating particle systems.
void AttachKillList( CParticleCollection *pParticles);
void DetachKillList( CParticleCollection *pParticles);
// For visualization (currently can only visualize one operator at a time)
CParticleCollection *m_pVisualizedParticles;
DmObjectId_t m_VisualizedOperatorId;
IParticleSystemQuery *m_pQuery;
CUtlVector< RenderCache_t > m_RenderCache;
IMaterial *m_pShadowDepthMaterial;
float m_flLastSimulationTime;
bool m_bDidInit;
bool m_bUsingDefaultQuery;
bool m_bShouldLoadSheets;
int m_nNumFramesMeasured;
enum { c_nNumFramesTracked = 10 };
int m_nParticleVertexCountHistory[c_nNumFramesTracked];
float m_fParticleCountScaling;
int m_nParticleIndexCount;
int m_nParticleVertexCount;
bool m_bFrameWarningNeeded;
friend class CParticleSystemDefinition;
friend class CParticleCollection;
};
extern CParticleSystemMgr *g_pParticleSystemMgr;
//-----------------------------------------------------------------------------
// A particle system can only have 1 operator using a particular ID
//-----------------------------------------------------------------------------
enum ParticleOperatorId_t
{
// Generic IDs
OPERATOR_GENERIC = -2, // Can have as many of these as you want
OPERATOR_SINGLETON = -1, // Can only have 1 operator with the same name as this one
// Renderer operator IDs
// Operator IDs
// Initializer operator IDs
OPERATOR_PI_POSITION, // Particle initializer: position (can only have 1 position setter)
OPERATOR_PI_RADIUS,
OPERATOR_PI_ALPHA,
OPERATOR_PI_TINT_RGB,
OPERATOR_PI_ROTATION,
OPERATOR_PI_YAW,
// Emitter IDs
OPERATOR_ID_COUNT,
};
//-----------------------------------------------------------------------------
// Class factory for particle operators
//-----------------------------------------------------------------------------
class IParticleOperatorDefinition
{
public:
virtual const char *GetName() const = 0;
virtual CParticleOperatorInstance *CreateInstance( const DmObjectId_t &id ) const = 0;
// virtual void DestroyInstance( CParticleOperatorInstance *pInstance ) const = 0;
virtual const DmxElementUnpackStructure_t* GetUnpackStructure() const = 0;
virtual ParticleOperatorId_t GetId() const = 0;
virtual bool IsObsolete() const = 0;
virtual size_t GetClassSize() const = 0;
#if MEASURE_PARTICLE_PERF
// performance monitoring
float m_flMaxExecutionTime;
float m_flTotalExecutionTime;
float m_flUncomittedTime;
FORCEINLINE void RecordExecutionTime( float flETime )
{
m_flUncomittedTime += flETime;
m_flMaxExecutionTime = MAX( m_flMaxExecutionTime, flETime );
}
FORCEINLINE float TotalRecordedExecutionTime( void ) const
{
return m_flTotalExecutionTime;
}
FORCEINLINE float MaximumRecordedExecutionTime( void ) const
{
return m_flMaxExecutionTime;
}
#endif
};
//-----------------------------------------------------------------------------
// Particle operators
//-----------------------------------------------------------------------------
class CParticleOperatorInstance
{
public:
// custom allocators so we can be simd aligned
void *operator new( size_t nSize );
void* operator new( size_t size, int nBlockUse, const char *pFileName, int nLine );
void operator delete( void *pData );
void operator delete( void* p, int nBlockUse, const char *pFileName, int nLine );
// unpack structure will be applied by creator. add extra initialization needed here
virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
{
}
virtual size_t GetRequiredContextBytes( ) const
{
return 0;
}
virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
{
}
virtual uint32 GetWrittenAttributes( void ) const = 0;
virtual uint32 GetReadAttributes( void ) const = 0;
virtual uint64 GetReadControlPointMask() const
{
return 0;
}
// Used when an operator needs to read the attributes of a particle at spawn time
virtual uint32 GetReadInitialAttributes( void ) const
{
return 0;
}
// a particle simulator does this
virtual void Operate( CParticleCollection *pParticles, float flOpStrength, void *pContext ) const
{
}
// a renderer overrides this
virtual void Render( IMatRenderContext *pRenderContext,
CParticleCollection *pParticles, void *pContext ) const
{
}
virtual bool IsBatchable() const
{
return true;
}
virtual void RenderUnsorted( CParticleCollection *pParticles, void *pContext, IMatRenderContext *pRenderContext, CMeshBuilder &meshBuilder, int nVertexOffset, int nFirstParticle, int nParticleCount ) const
{
}
// Returns the number of verts + indices to render
virtual int GetParticlesToRender( CParticleCollection *pParticles, void *pContext, int nFirstParticle, int nRemainingVertices, int nRemainingIndices, int *pVertsUsed, int *pIndicesUsed ) const
{
*pVertsUsed = 0;
*pIndicesUsed = 0;
return 0;
}
// emitters over-ride this. Return a mask of what fields you initted
virtual uint32 Emit( CParticleCollection *pParticles, float flOpCurStrength,
void *pContext ) const
{
return 0;
}
// emitters over-ride this.
virtual void StopEmission( CParticleCollection *pParticles, void *pContext, bool bInfiniteOnly = false ) const
{
}
virtual void StartEmission( CParticleCollection *pParticles, void *pContext, bool bInfiniteOnly = false ) const
{
}
virtual void Restart( CParticleCollection *pParticles, void *pContext ) {}
// initters over-ride this
virtual void InitParticleSystem( CParticleCollection *pParticles, void *pContext ) const
{
}
// a force generator does this. It accumulates in the force array
virtual void AddForces( FourVectors *AccumulatedForces,
CParticleCollection *pParticles,
int nBlocks,
float flCurStrength,
void *pContext ) const
{
}
// this is called for each constarint every frame. It can set up data like nearby world traces,
// etc
virtual void SetupConstraintPerFrameData( CParticleCollection *pParticles,
void *pContext ) const
{
}
// a constraint overrides this. It shold return a true if it did anything
virtual bool EnforceConstraint( int nStartBlock,
int nNumBlocks,
CParticleCollection *pParticles,
void *pContext,
int nNumValidParticlesInLastChunk ) const
{
return false;
}
// should the constraint be run only once after all other constraints?
virtual bool IsFinalConstraint( void ) const
{
return false;
}
// determines if a mask needs to be initialized multiple times.
virtual bool InitMultipleOverride()
{
return false;
}
// Indicates if this initializer is scrub-safe (initializers don't use random numbers, for example)
virtual bool IsScrubSafe()
{
return false;
}
// particle-initters over-ride this
virtual void InitNewParticlesScalar( CParticleCollection *pParticles, int nFirstParticle, int n_particles, int attribute_write_mask, void *pContext ) const
{
}
// init new particles in blocks of 4. initters that have sse smarts should over ride this. the scalar particle initter will still be cllaed for head/tail.
virtual void InitNewParticlesBlock( CParticleCollection *pParticles, int start_block, int n_blocks, int attribute_write_mask, void *pContext ) const
{
// default behaviour is to call the scalar one 4x times
InitNewParticlesScalar( pParticles, 4*start_block, 4*n_blocks, attribute_write_mask, pContext );
}
// splits particle initialization up into scalar and block sections, callingt he right code
void InitNewParticles( CParticleCollection *pParticles, int nFirstParticle, int n_particles, int attribute_write_mask , void *pContext) const;
// this function is queried to determine if a particle system is over and doen with. A particle
// system is done with when it has noparticles and no operators intend to create any more
virtual bool MayCreateMoreParticles( CParticleCollection *pParticles, void *pContext ) const
{
return false;
}
// Returns the operator definition that spawned this operator
const IParticleOperatorDefinition *GetDefinition()
{
return m_pDef;
}
virtual bool ShouldRunBeforeEmitters( void ) const
{
return false;
}
// Does this operator require that particles remain in the order they were emitted?
virtual bool RequiresOrderInvariance( void ) const
{
return false;
}
// Called when the SFM wants to skip forward in time
virtual void SkipToTime( float flTime, CParticleCollection *pParticles, void *pContext ) const {}
// Returns a unique ID for this definition
const DmObjectId_t& GetId() { return m_Id; }
// Used for editing + debugging to visualize the operator in 3D
virtual void Render( CParticleCollection *pParticles ) const {}
// Used as a debugging mechanism to prevent bogus calls to RandomInt or RandomFloat inside operators
// Use CParticleCollection::RandomInt/RandomFloat instead
int RandomInt( int nMin, int nMax )
{
// NOTE: Use CParticleCollection::RandomInt!
Assert(0);
return 0;
}
float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f )
{
// NOTE: Use CParticleCollection::RandomFloat!
Assert(0);
return 0.0f;
}
float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f )
{
// NOTE: Use CParticleCollection::RandomFloatExp!
Assert(0);
return 0.0f;
}
float m_flOpStartFadeInTime;
float m_flOpEndFadeInTime;
float m_flOpStartFadeOutTime;
float m_flOpEndFadeOutTime;
float m_flOpFadeOscillatePeriod;
virtual ~CParticleOperatorInstance( void )
{
// so that sheet references, etc can be cleaned up
}
protected:
// utility function for initting a scalar attribute to a random range in an sse fashion
void InitScalarAttributeRandomRangeBlock( int nAttributeId, float fMinValue, float fMaxValue,
CParticleCollection *pParticles, int nStartBlock, int nBlockCount ) const;
void InitScalarAttributeRandomRangeExpBlock( int nAttributeId, float fMinValue, float fMaxValue, float fExp,
CParticleCollection *pParticles, int nStartBlock, int nBlockCount ) const;
void AddScalarAttributeRandomRangeBlock( int nAttributeId, float fMinValue, float fMaxValue, float fExp,
CParticleCollection *pParticles, int nStartBlock, int nBlockCount, bool bRandomlyInvert ) const;
private:
friend class CParticleCollection;
const IParticleOperatorDefinition *m_pDef;
void SetDefinition( const IParticleOperatorDefinition * pDef, const DmObjectId_t &id )
{
m_pDef = pDef;
CopyUniqueId( id, &m_Id );
}
DmObjectId_t m_Id;
template <typename T> friend class CParticleOperatorDefinition;
};
class CParticleRenderOperatorInstance : public CParticleOperatorInstance
{
public:
CParticleVisibilityInputs VisibilityInputs;
};
//-----------------------------------------------------------------------------
// Helper macro for creating particle operator factories
//-----------------------------------------------------------------------------
template < class T >
class CParticleOperatorDefinition : public IParticleOperatorDefinition
{
public:
CParticleOperatorDefinition( const char *pFactoryName, ParticleOperatorId_t id, bool bIsObsolete ) : m_pFactoryName( pFactoryName ), m_Id( id )
{
#if MEASURE_PARTICLE_PERF
m_flTotalExecutionTime = 0.0f;
m_flMaxExecutionTime = 0.0f;
m_flUncomittedTime = 0.0f;
#endif
m_bIsObsolete = bIsObsolete;
}
virtual const char *GetName() const
{
return m_pFactoryName;
}
virtual ParticleOperatorId_t GetId() const
{
return m_Id;
}
virtual CParticleOperatorInstance *CreateInstance( const DmObjectId_t &id ) const
{
CParticleOperatorInstance *pOp = new T;
pOp->SetDefinition( this, id );
return pOp;
}
virtual const DmxElementUnpackStructure_t* GetUnpackStructure() const
{
return m_pUnpackParams;
}
// Editor won't display obsolete operators
virtual bool IsObsolete() const
{
return m_bIsObsolete;
}
virtual size_t GetClassSize() const
{
return sizeof( T );
}
private:
const char *m_pFactoryName;
ParticleOperatorId_t m_Id;
bool m_bIsObsolete;
static DmxElementUnpackStructure_t *m_pUnpackParams;
};
#define DECLARE_PARTICLE_OPERATOR( _className ) \
DECLARE_DMXELEMENT_UNPACK() \
friend class CParticleOperatorDefinition<_className >
#define DEFINE_PARTICLE_OPERATOR( _className, _operatorName, _id ) \
static CParticleOperatorDefinition<_className> s_##_className##Factory( _operatorName, _id, false )
#define DEFINE_PARTICLE_OPERATOR_OBSOLETE( _className, _operatorName, _id ) \
static CParticleOperatorDefinition<_className> s_##_className##Factory( _operatorName, _id, true )
#define BEGIN_PARTICLE_OPERATOR_UNPACK( _className ) \
BEGIN_DMXELEMENT_UNPACK( _className ) \
DMXELEMENT_UNPACK_FIELD( "operator start fadein","0", float, m_flOpStartFadeInTime ) \
DMXELEMENT_UNPACK_FIELD( "operator end fadein","0", float, m_flOpEndFadeInTime ) \
DMXELEMENT_UNPACK_FIELD( "operator start fadeout","0", float, m_flOpStartFadeOutTime ) \
DMXELEMENT_UNPACK_FIELD( "operator end fadeout","0", float, m_flOpEndFadeOutTime ) \
DMXELEMENT_UNPACK_FIELD( "operator fade oscillate","0", float, m_flOpFadeOscillatePeriod )
#define END_PARTICLE_OPERATOR_UNPACK( _className ) \
END_DMXELEMENT_UNPACK_TEMPLATE( _className, CParticleOperatorDefinition<_className>::m_pUnpackParams )
#define BEGIN_PARTICLE_RENDER_OPERATOR_UNPACK( _className ) \
BEGIN_PARTICLE_OPERATOR_UNPACK( _className ) \
DMXELEMENT_UNPACK_FIELD( "Visibility Proxy Input Control Point Number", "-1", int, VisibilityInputs.m_nCPin ) \
DMXELEMENT_UNPACK_FIELD( "Visibility Proxy Radius", "1.0", float, VisibilityInputs.m_flProxyRadius ) \
DMXELEMENT_UNPACK_FIELD( "Visibility input minimum","0", float, VisibilityInputs.m_flInputMin ) \
DMXELEMENT_UNPACK_FIELD( "Visibility input maximum","1", float, VisibilityInputs.m_flInputMax ) \
DMXELEMENT_UNPACK_FIELD( "Visibility Alpha Scale minimum","0", float, VisibilityInputs.m_flAlphaScaleMin ) \
DMXELEMENT_UNPACK_FIELD( "Visibility Alpha Scale maximum","1", float, VisibilityInputs.m_flAlphaScaleMax ) \
DMXELEMENT_UNPACK_FIELD( "Visibility Radius Scale minimum","1", float, VisibilityInputs.m_flRadiusScaleMin ) \
DMXELEMENT_UNPACK_FIELD( "Visibility Radius Scale maximum","1", float, VisibilityInputs.m_flRadiusScaleMax ) \
DMXELEMENT_UNPACK_FIELD( "Visibility Camera Depth Bias", "0", float, VisibilityInputs.m_flCameraBias )
// DMXELEMENT_UNPACK_FIELD( "Visibility Use Bounding Box for Proxy", "0", bool, VisibilityInputs.m_bUseBBox )
// DMXELEMENT_UNPACK_FIELD( "Visibility Bounding Box Scale", "1.0", float, VisibilityInputs.m_flBBoxScale )
#define REGISTER_PARTICLE_OPERATOR( _type, _className ) \
g_pParticleSystemMgr->AddParticleOperator( _type, &s_##_className##Factory )
// need to think about particle constraints in terms of segregating affected particles so as to
// run multi-pass constraints on only a subset
//-----------------------------------------------------------------------------
// flags for particle systems
//-----------------------------------------------------------------------------
enum
{
PCFLAGS_FIRST_FRAME = 0x1,
PCFLAGS_PREV_CONTROL_POINTS_INITIALIZED = 0x2,
};
#define DEBUG_PARTICLE_SORT 0
// sorting functionality for rendering. Call GetRenderList( bool bSorted ) to get the list of
// particles to render (sorted or not, including children).
// **do not casually change this structure**. The sorting code treats it interchangably as an SOA
// and accesses it using sse. Any changes to this struct need the sort code updated.**
struct ParticleRenderData_t
{
float m_flSortKey; // what we sort by
int m_nIndex; // index or fudged index (for child particles)
float m_flRadius; // effective radius, using visibility
#if VALVE_LITTLE_ENDIAN
uint8 m_nAlpha; // effective alpha, combining alpha and alpha2 and vis. 0 - 255
uint8 m_nAlphaPad[3]; // this will be written to
#else
uint8 m_nAlphaPad[3]; // this will be written to
uint8 m_nAlpha; // effective alpha, combining alpha and alpha2 and vis. 0 - 255
#endif
};
struct ExtendedParticleRenderData_t : ParticleRenderData_t
{
float m_flX;
float m_flY;
float m_flZ;
float m_flPad;
};
typedef struct ALIGN16 _FourInts
{
int32 m_nValue[4];
} ALIGN16_POST FourInts;
// structure describing the parameter block used by operators which use the path between two points to
// control particles.
struct CPathParameters
{
int m_nStartControlPointNumber;
int m_nEndControlPointNumber;
int m_nBulgeControl;
float m_flBulge;
float m_flMidPoint;
void ClampControlPointIndices( void )
{
m_nStartControlPointNumber = MAX(0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nStartControlPointNumber ) );
m_nEndControlPointNumber = MAX(0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nEndControlPointNumber ) );
}
};
struct CParticleVisibilityData
{
float m_flAlphaVisibility;
float m_flRadiusVisibility;
float m_flCameraBias;
bool m_bUseVisibility;
};
struct CParticleControlPoint
{
Vector m_Position;
Vector m_PrevPosition;
// orientation
Vector m_ForwardVector;
Vector m_UpVector;
Vector m_RightVector;
// reference to entity or whatever this control point comes from
void *m_pObject;
// parent for hierarchies
int m_nParent;
};
// struct for simd xform to transform a point from an identitiy coordinate system to that of the control point
struct CParticleSIMDTransformation
{
FourVectors m_v4Origin;
FourVectors m_v4Fwd;
FourVectors m_v4Up;