part_buf.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***************************************************************************
  19. *** 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 ***
  20. ***************************************************************************
  21. * *
  22. * Project Name : G *
  23. * *
  24. * $Archive:: /VSS_Sync/ww3d2/part_buf.h $*
  25. * *
  26. * $Author:: Vss_sync $*
  27. * *
  28. * $Modtime:: 8/29/01 7:29p $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef PART_BUF_H
  39. #define PART_BUF_H
  40. #include "rendobj.h"
  41. #include "pointgr.h"
  42. #include "seglinerenderer.h"
  43. class ParticleEmitterClass;
  44. template<class T> struct ParticlePropertyStruct;
  45. /**
  46. ** NewParticleStruct: structure for passing new particles from the particle
  47. ** emitter to the particle buffer. Since the emitter always continues
  48. ** emitting (unless stopped) but the buffer may not update for long periods,
  49. ** the emitter may emit more particles than the buffer can contain. However,
  50. ** in this case the older particles can be ignored. Therefore
  51. ** ParticleBufferClass contains a circular buffer of NewParticleStructs, and
  52. ** new ones overwrite the oldest in the case of overflows.
  53. */
  54. struct NewParticleStruct
  55. {
  56. Vector3 Position; // Particle position in worldspace.
  57. Vector3 Velocity; // Particle velocity in worldspace.
  58. unsigned int TimeStamp; // Millisecond time at creation.
  59. // These are needed by DynamicVectorClass (will probably never be used).
  60. bool operator != (const NewParticleStruct & p)
  61. {
  62. return (p.TimeStamp != TimeStamp) || (p.Position != Position);
  63. }
  64. bool operator == (const NewParticleStruct & p)
  65. {
  66. return (p.TimeStamp == TimeStamp) && (p.Position == Position);
  67. }
  68. };
  69. /**
  70. ** ParticleBufferClass: This is a renderobject which contains the particles
  71. ** emitted by a given renderer. The particle emitter is a different
  72. ** renderobject, a ParticleEmitterClass (there is one particle emitter per
  73. ** particle buffer). This separation is so that the bounding volumes of the
  74. ** particle group and the object containing the emitter (emitters will
  75. ** typically be inserted into a hierarchy object or some such) will remain
  76. ** separate.
  77. */
  78. class ParticleBufferClass : public RenderObjClass
  79. {
  80. public:
  81. ParticleBufferClass(ParticleEmitterClass *emitter, unsigned int buffer_size,
  82. ParticlePropertyStruct<Vector3> &color, ParticlePropertyStruct<float> &opacity,
  83. ParticlePropertyStruct<float> &size, ParticlePropertyStruct<float> &rotation,
  84. float orient_rnd, ParticlePropertyStruct<float> &frame, Vector3 accel,
  85. float max_age, TextureClass *tex, ShaderClass shader, bool pingpong,
  86. int render_mode, int frame_mode, const W3dEmitterLinePropertiesStruct * line_props);
  87. ParticleBufferClass(const ParticleBufferClass & src);
  88. ParticleBufferClass & operator = (const ParticleBufferClass &);
  89. virtual ~ParticleBufferClass(void);
  90. /*
  91. ** RenderObjClass Interface:
  92. */
  93. virtual RenderObjClass * Clone(void) const;
  94. virtual int Class_ID(void) const { return CLASSID_PARTICLEBUFFER; }
  95. virtual int Get_Num_Polys(void) const;
  96. int Get_Particle_Count(void) const;
  97. // Update particle state and draw the particles.
  98. virtual void Render(RenderInfoClass & rinfo);
  99. // Scales the size of the individual particles but doesn't affect their
  100. // position (and therefore the size of the particle system as a whole)
  101. virtual void Scale(float scale);
  102. // The particle buffer never receives a Set_Transform/Position call,
  103. // evem though its bounding volume changes. Since bounding volume
  104. // invalidations ordinarily occur when these functions are called,
  105. // the cached bounding volumes will not be invalidated unless we do
  106. // it elsewhere (such as here). We also need to call the particle
  107. // emitter's Emit() function (done here to avoid order dependence).
  108. virtual void On_Frame_Update(void);
  109. virtual void Notify_Added(SceneClass * scene);
  110. virtual void Notify_Removed(SceneClass * scene);
  111. virtual void Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const;
  112. virtual void Get_Obj_Space_Bounding_Box(AABoxClass & box) const;
  113. /////////////////////////////////////////////////////////////////////////////
  114. // Render Object Interface - Predictive LOD
  115. /////////////////////////////////////////////////////////////////////////////
  116. virtual void Prepare_LOD(CameraClass &camera);
  117. virtual void Increment_LOD(void);
  118. virtual void Decrement_LOD(void);
  119. virtual float Get_Cost(void) const;
  120. virtual float Get_Value(void) const;
  121. virtual float Get_Post_Increment_Value(void) const;
  122. virtual void Set_LOD_Level(int lod);
  123. virtual int Get_LOD_Level(void) const;
  124. virtual int Get_LOD_Count(void) const;
  125. virtual void Set_LOD_Bias(float bias) { LodBias = MAX(bias, 0.0f); }
  126. virtual int Calculate_Cost_Value_Arrays(float screen_area, float *values, float *costs) const;
  127. /*
  128. ** These members are not part of the RenderObjClass Interface:
  129. */
  130. void Reset_Colors(ParticlePropertyStruct<Vector3> &new_props);
  131. void Reset_Opacity(ParticlePropertyStruct<float> &new_props);
  132. void Reset_Size(ParticlePropertyStruct<float> &new_props);
  133. void Reset_Rotations(ParticlePropertyStruct<float> &new_rotations, float orient_rnd);
  134. void Reset_Frames(ParticlePropertyStruct<float> &new_frames);
  135. // This informs the buffer that the emitter is dead, so it can release
  136. // its pointer to it and be removed itself after all its particles dies
  137. // out.
  138. void Emitter_Is_Dead(void);
  139. // This set's the buffer's current emitter - this should usually be
  140. // called only by the emitter's copy constructor after it clones a
  141. // buffer.
  142. void Set_Emitter(ParticleEmitterClass *emitter);
  143. // from RenderObj...
  144. virtual bool Is_Complete(void) { return IsEmitterDead && !NonNewNum && !NewNum; }
  145. // This adds an uninitialized NewParticleStuct to the new particle
  146. // buffer and returns its address so the particle emitter can
  147. // initialize it. This is how the emitter sends new particles to the
  148. // buffer - it is done this way to avoid needless copying.
  149. NewParticleStruct * Add_Uninitialized_New_Particle(void);
  150. // Change the acceleration of the particles on the fly
  151. void Set_Acceleration (const Vector3 &acceleration) { Accel = acceleration; HasAccel = ((Accel.X != 0) || (Accel.Y != 0) || (Accel.Z != 0)); }
  152. //
  153. // Inline accessors.
  154. // These methods are provided as a means to get the emitter's settings.
  155. //
  156. int Get_Render_Mode (void) const { return RenderMode; }
  157. int Get_Frame_Mode (void) const { return FrameMode; }
  158. float Get_Particle_Size (void) const { return SizeKeyFrameValues[0]; }
  159. Vector3 Get_Acceleration (void) const { return Accel * 1000000.0F; }
  160. float Get_Lifetime (void) const { return (float(MaxAge)) / 1000.0F; }
  161. Vector3 Get_Start_Color (void) const { return ColorKeyFrameValues[0]; }
  162. float Get_Start_Opacity (void) const { return AlphaKeyFrameValues[0]; }
  163. Vector3 Get_End_Color (void) const { return (NumColorKeyFrames > 1) ? ColorKeyFrameValues[NumColorKeyFrames - 1] : ColorKeyFrameValues[0]; }
  164. float Get_End_Opacity (void) const { return (NumAlphaKeyFrames > 1) ? AlphaKeyFrameValues[NumAlphaKeyFrames - 1] : AlphaKeyFrameValues[0]; }
  165. TextureClass * Get_Texture (void) const { return PointGroup->Get_Texture (); }
  166. void Set_Texture (TextureClass *tex) { PointGroup->Set_Texture(tex); }
  167. float Get_Fade_Time (void) const { return (NumColorKeyFrames > 1) ? (((float)ColorKeyFrameTimes[1]) / 1000.0f) : 0.0f; }
  168. ShaderClass Get_Shader (void) const { return PointGroup->Get_Shader (); }
  169. //
  170. // Line rendering properties. These functions will always return
  171. // a default value if line rendering is not enabled.
  172. //
  173. int Get_Line_Texture_Mapping_Mode(void) const;
  174. int Is_Merge_Intersections(void) const;
  175. int Is_Freeze_Random(void) const;
  176. int Is_Sorting_Disabled(void) const;
  177. int Are_End_Caps_Enabled(void) const;
  178. int Get_Subdivision_Level(void) const;
  179. float Get_Noise_Amplitude(void) const;
  180. float Get_Merge_Abort_Factor(void) const;
  181. float Get_Texture_Tile_Factor(void) const;
  182. Vector2 Get_UV_Offset_Rate(void) const;
  183. // This is a utility function only meant to be called by the particle emitter.
  184. unsigned int Get_Buffer_Size(void) const { return MaxNum; }
  185. // Note: Caller IS RESPONSIBLE for freeing any memory allocated by these calls
  186. void Get_Color_Key_Frames (ParticlePropertyStruct<Vector3> &colors) const;
  187. void Get_Opacity_Key_Frames (ParticlePropertyStruct<float> &opacities) const;
  188. void Get_Size_Key_Frames (ParticlePropertyStruct<float> &sizes) const;
  189. void Get_Rotation_Key_Frames (ParticlePropertyStruct<float> &rotations) const;
  190. void Get_Frame_Key_Frames (ParticlePropertyStruct<float> &frames) const;
  191. float Get_Initial_Orientation_Random (void) const { return InitialOrientationRandom; }
  192. // Total Active Particle Buffer Count
  193. static unsigned int Get_Total_Active_Count( void ) { return TotalActiveCount; }
  194. // Global control of particle LOD.
  195. static void Set_LOD_Max_Screen_Size(int lod_level,float max_screen_size);
  196. static float Get_LOD_Max_Screen_Size(int lod_level);
  197. protected:
  198. virtual void Update_Cached_Bounding_Volumes(void) const;
  199. // render the particle system as a collection of particles
  200. void Render_Particles(RenderInfoClass & rinfo);
  201. // render the particle system as a line
  202. void Render_Line(RenderInfoClass & rinfo);
  203. // Update the kinematic particle state. This includes getting new
  204. // particles from the new particle queue, updating velocity/position
  205. // for any existing particles, killing old ones, and updating
  206. // LastUpdateTime.
  207. void Update_Kinematic_Particle_State(void);
  208. // Update the visual particle state. This includes updating color/size
  209. // for all existing particles. Only needs to happen at rendering time.
  210. void Update_Visual_Particle_State(void);
  211. // Update the bounding box. (Updates the particle state if it needs to).
  212. void Update_Bounding_Box(void);
  213. // Get new particles from the emitter and write them into the circular
  214. // particle buffer, possibly overwriting older particles. Perform
  215. // partial-interval upddate on them as well.
  216. void Get_New_Particles(void);
  217. // Kill all remaining particles which will be above their maxage at the
  218. // end of this time interval.
  219. void Kill_Old_Particles(void);
  220. // Update all living non-new particles according to time elapsed since
  221. // last update.
  222. void Update_Non_New_Particles(unsigned int elapsed);
  223. // Seperate circular buffer used by the emitter to pass new particles.
  224. // It is implemented as an array, start and end indices and a count (to
  225. // differentiate between completely full and completely empty).
  226. NewParticleStruct * NewParticleQueue;
  227. unsigned int NewParticleQueueStart;
  228. unsigned int NewParticleQueueEnd;
  229. int NewParticleQueueCount;
  230. // State global to the entire particle buffer.
  231. int RenderMode; // rendering mode being used (settings found in w3d_file.h)
  232. int FrameMode; // frame mode (settings found in w3d_file.h - 1x1..16x16)
  233. Vector3 Accel; // Worldspace acceleration per ms^2.
  234. bool HasAccel; // Is the acceleration non-zero?
  235. unsigned int MaxAge; // Maximum age in milliseconds.
  236. unsigned int LastUpdateTime;// Time at last update.
  237. bool IsEmitterDead;
  238. float MaxSize; // Used for BBox calculations
  239. // Circular buffer implementation. This is actually 2 sequential
  240. // circular buffers: one for non-new particles and one for new
  241. // particles (the distinction is needed because the two types of
  242. // particles are updated differently).
  243. // Besides the head/tail indices, a count is used for each buffer to
  244. // distinguish between full and empty.
  245. unsigned int MaxNum; // Maximum number of particles.
  246. unsigned int Start; // Start of existing (non-new) particles.
  247. unsigned int End; // End of existing (non-new) particles.
  248. unsigned int NewEnd; // End of new particles.
  249. int NonNewNum; // Non-new entry count (to know when empty).
  250. int NewNum; // New entry count (to know when empty).
  251. // Worldspace-aligned bounding box:
  252. AABoxClass BoundingBox;
  253. bool BoundingBoxDirty;
  254. // At least one keyframe must exist for each property (time 0).
  255. // If a randomizer is zero and there are no additional keyframes for
  256. // that property (or the keyframes are all equal), all the arrays for
  257. // that property are NULL (since they will never be used), except for
  258. // the Values array which will have one entry (the constant value).
  259. // Note that the rotation and orientation properties are different -
  260. // only orientation is used in rendering. The rotation data is only
  261. // used to compute the orientations. So the condition is different -
  262. // if rotation and orientation randomizers, and all rotation keyframes
  263. // are all zero, then all of the arrays will be NULL (including the
  264. // Values array).
  265. unsigned int NumColorKeyFrames;
  266. unsigned int * ColorKeyFrameTimes; // 0th entry is always 0
  267. Vector3 * ColorKeyFrameValues;
  268. Vector3 * ColorKeyFrameDeltas;
  269. unsigned int NumAlphaKeyFrames;
  270. unsigned int * AlphaKeyFrameTimes; // 0th entry is always 0
  271. float * AlphaKeyFrameValues;
  272. float * AlphaKeyFrameDeltas;
  273. unsigned int NumSizeKeyFrames;
  274. unsigned int * SizeKeyFrameTimes; // 0th entry is always 0
  275. float * SizeKeyFrameValues;
  276. float * SizeKeyFrameDeltas;
  277. unsigned int NumRotationKeyFrames;
  278. unsigned int * RotationKeyFrameTimes; // 0th entry is always 0
  279. float * RotationKeyFrameValues; // In rotations per millisecond
  280. float * HalfRotationKeyFrameDeltas; // (* 0.5f)
  281. float * OrientationKeyFrameValues; // Rotation preintegrated to keyframe times
  282. unsigned int NumFrameKeyFrames;
  283. unsigned int * FrameKeyFrameTimes; // 0th entry is always 0
  284. float * FrameKeyFrameValues;
  285. float * FrameKeyFrameDeltas;
  286. // These tables are indexed by the array position in the particle buffer.
  287. // The table size is either the smallest power of two equal or larger
  288. // than the buffer size, or MAX_RANDOM_ENTRIES (defined in the .cpp
  289. // file - MUST be a power of two), whichever is smaller. Note that if a
  290. // randomizer is zero, the table will have one entry (containing zero),
  291. // which is why each property has its own NumXXXRandomEntries variable.
  292. // If a randomizer is zero and the property has no keyframes, the table
  293. // will be NULL since it will never be used (property is constant)).
  294. unsigned int NumRandomColorEntriesMinus1; // 2^n - 1 so can be used as a mask also
  295. Vector3 * RandomColorEntries;
  296. unsigned int NumRandomAlphaEntriesMinus1; // 2^n - 1 so can be used as a mask also
  297. float * RandomAlphaEntries;
  298. unsigned int NumRandomSizeEntriesMinus1; // 2^n - 1 so can be used as a mask also
  299. float * RandomSizeEntries;
  300. unsigned int NumRandomRotationEntriesMinus1; // 2^n - 1 so can be used as a mask also
  301. float * RandomRotationEntries;
  302. unsigned int NumRandomOrientationEntriesMinus1; // 2^n - 1 so can be used as a mask also
  303. float * RandomOrientationEntries;
  304. unsigned int NumRandomFrameEntriesMinus1; // 2^n - 1 so can be used as a mask also
  305. float * RandomFrameEntries;
  306. Vector3 ColorRandom;
  307. float OpacityRandom;
  308. float SizeRandom;
  309. float RotationRandom;
  310. float FrameRandom;
  311. float InitialOrientationRandom;
  312. // This object implements particle rendering
  313. PointGroupClass * PointGroup;
  314. // This object implements line rendering
  315. SegLineRendererClass * LineRenderer;
  316. // These are shared with the point group. The position, color and alpha
  317. // arrays serve double duty: they are used to store and update particle
  318. // state and also to pass point information to the point group. The
  319. // active point table is used to communicate to the point group which
  320. // points are active (it is only used if all are not active)..
  321. ShareBufferClass<Vector3> * Position[2]; // Only [0] used unless pingpong enabled
  322. ShareBufferClass<Vector4> * Diffuse; // passed into point group
  323. ShareBufferClass<Vector3> * Color;
  324. ShareBufferClass<float> * Alpha;
  325. ShareBufferClass<float> * Size;
  326. ShareBufferClass<uint8> * Frame;
  327. ShareBufferClass<uint8> * Orientation;
  328. ShareBufferClass<unsigned int> * APT;
  329. // Do we keep two ping-pong position buffers (for collision and possibly other effects
  330. // which need the previous frames position as well as this frames)
  331. bool PingPongPosition;
  332. // Additional per-particle state:
  333. Vector3 * Velocity; // World units per millisecond.
  334. unsigned int * TimeStamp; // Millisecond time at creation.
  335. // This pointer is used for synchronization - the emitter is called to
  336. // add new particles at the start of the buffers render function - to
  337. // prevent behavior which is dependent on the relative time order of
  338. // the emitter and buffer in the rendering list.
  339. ParticleEmitterClass * Emitter;
  340. // These are used for decimating particles for LOD purposes. The
  341. // threshold is compared vs. an array filled with a random permutation
  342. // of the numbers 0 to 15, and any particle whose entry (modulo 16) is
  343. // less than the threshold is not rendered. So if DecimationThreshold
  344. // is 0 (the minimum value), all particles are rendered - if it is 16
  345. // (the maximum value) none are rendered.
  346. unsigned int DecimationThreshold;
  347. static const unsigned int PermutationArray[16];
  348. // LOD values
  349. unsigned int LodCount;
  350. float Cost[17]; // Cost array needs one entry for each LOD level
  351. float Value[18]; // Value array needs one more entry than # of LODs
  352. float LodBias;
  353. // Projected area, used for LOD purposes
  354. float ProjectedArea;
  355. // Total Active Particle Buffer Count
  356. static unsigned int TotalActiveCount;
  357. // Static array of screen-size clamps for the 17 possible LOD levels a
  358. // particle buffer can have. We can change these from being global to
  359. // being per-buffer later if we wish. Default is NO_MAX_SCREEN_SIZE.
  360. static float LODMaxScreenSizes[17];
  361. };
  362. #endif // PART_BUF_H