llpartdata.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * @file llpartdata.h
  3. * @brief Particle system data packing
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewergpl$
  6. *
  7. * Copyright (c) 2003-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLPARTDATA_H
  33. #define LL_LLPARTDATA_H
  34. #include "llpreprocessor.h"
  35. #include "lluuid.h"
  36. #include "llvector2.h"
  37. #include "llvector3d.h"
  38. #include "llvector3.h"
  39. #include "llcolor4.h"
  40. class LLDataPacker;
  41. class LLMessageSystem;
  42. constexpr S32 PS_CUR_VERSION = 18;
  43. //
  44. // These constants are used by the script code, not by the particle system
  45. // itself
  46. //
  47. enum LLPSScriptFlags
  48. {
  49. // Flags for the different parameters of individual particles
  50. LLPS_PART_FLAGS,
  51. LLPS_PART_START_COLOR,
  52. LLPS_PART_START_ALPHA,
  53. LLPS_PART_END_COLOR,
  54. LLPS_PART_END_ALPHA,
  55. LLPS_PART_START_SCALE,
  56. LLPS_PART_END_SCALE,
  57. LLPS_PART_MAX_AGE,
  58. // Flags for the different parameters of the particle source
  59. LLPS_SRC_ACCEL,
  60. LLPS_SRC_PATTERN,
  61. LLPS_SRC_INNERANGLE,
  62. LLPS_SRC_OUTERANGLE,
  63. LLPS_SRC_TEXTURE,
  64. LLPS_SRC_BURST_RATE,
  65. LLPS_SRC_BURST_DURATION,
  66. LLPS_SRC_BURST_PART_COUNT,
  67. LLPS_SRC_BURST_RADIUS,
  68. LLPS_SRC_BURST_SPEED_MIN,
  69. LLPS_SRC_BURST_SPEED_MAX,
  70. LLPS_SRC_MAX_AGE,
  71. LLPS_SRC_TARGET_UUID,
  72. LLPS_SRC_OMEGA,
  73. LLPS_SRC_ANGLE_BEGIN,
  74. LLPS_SRC_ANGLE_END,
  75. LLPS_PART_BLEND_FUNC_SOURCE,
  76. LLPS_PART_BLEND_FUNC_DEST,
  77. LLPS_PART_START_GLOW,
  78. LLPS_PART_END_GLOW
  79. };
  80. class LLPartData
  81. {
  82. public:
  83. LLPartData()
  84. : mFlags(0),
  85. mMaxAge(0.f),
  86. mParameter(0.f)
  87. {
  88. }
  89. // Need virtual destructor so that derived classes are properly destroyed:
  90. virtual ~LLPartData()
  91. {
  92. }
  93. bool unpackLegacy(LLDataPacker& dp);
  94. bool unpack(LLDataPacker& dp);
  95. LL_INLINE bool hasGlow() const { return mStartGlow > 0.f || mEndGlow > 0.f; }
  96. LL_INLINE bool hasBlendFunc() const
  97. {
  98. return mBlendFuncSource != LLPartData::LL_PART_BF_SOURCE_ALPHA ||
  99. mBlendFuncDest != LLPartData::LL_PART_BF_ONE_MINUS_SOURCE_ALPHA;
  100. }
  101. // Masks for the different particle flags
  102. enum
  103. {
  104. LL_PART_INTERP_COLOR_MASK = 0x01,
  105. LL_PART_INTERP_SCALE_MASK = 0x02,
  106. LL_PART_BOUNCE_MASK = 0x04,
  107. LL_PART_WIND_MASK = 0x08,
  108. LL_PART_FOLLOW_SRC_MASK = 0x10, // Follows source, no rotation following (expensive!)
  109. LL_PART_FOLLOW_VELOCITY_MASK = 0x20, // Particles orient themselves with velocity
  110. LL_PART_TARGET_POS_MASK = 0x40,
  111. LL_PART_TARGET_LINEAR_MASK = 0x80, // Particle uses a direct linear interpolation
  112. LL_PART_EMISSIVE_MASK = 0x100, // Particle is "emissive", instead of being lit
  113. LL_PART_BEAM_MASK = 0x200, // Particle is a "beam" connecting source and target
  114. LL_PART_RIBBON_MASK = 0x400, // Particles are joined together into one continuous triangle strip
  115. // Not implemented yet !
  116. //LL_PART_RANDOM_ACCEL_MASK = 0x100, // Particles have random acceleration
  117. //LL_PART_RANDOM_VEL_MASK = 0x200, // Particles have random velocity shifts"
  118. //LL_PART_TRAIL_MASK = 0x400, // Particles have historical "trails"
  119. // SYSTEM SET FLAGS
  120. LL_PART_DATA_GLOW = 0x10000,
  121. LL_PART_DATA_BLEND = 0x20000,
  122. // Viewer side use only !
  123. LL_PART_HUD = 0x40000000,
  124. LL_PART_DEAD_MASK = 0x80000000,
  125. };
  126. // WARNING: this MUST match the LLRender::eBlendFactor enum !
  127. enum
  128. {
  129. LL_PART_BF_ONE = 0,
  130. LL_PART_BF_ZERO = 1,
  131. LL_PART_BF_DEST_COLOR = 2,
  132. LL_PART_BF_SOURCE_COLOR = 3,
  133. LL_PART_BF_ONE_MINUS_DEST_COLOR = 4,
  134. LL_PART_BF_ONE_MINUS_SOURCE_COLOR = 5,
  135. UNSUPPORTED_DEST_ALPHA = 6,
  136. LL_PART_BF_SOURCE_ALPHA = 7,
  137. UNSUPPORTED_ONE_MINUS_DEST_ALPHA = 8,
  138. LL_PART_BF_ONE_MINUS_SOURCE_ALPHA = 9,
  139. LL_PART_BF_COUNT = 10
  140. };
  141. static LL_INLINE bool validBlendFunc(S32 func)
  142. {
  143. return func >= 0 && func < LL_PART_BF_COUNT &&
  144. func != UNSUPPORTED_DEST_ALPHA &&
  145. func != UNSUPPORTED_ONE_MINUS_DEST_ALPHA;
  146. }
  147. LL_INLINE void setFlags(U32 flags) { mFlags = flags; }
  148. LL_INLINE void setMaxAge(F32 max_age) { mMaxAge = llclamp(max_age, 0.f, 30.f); }
  149. LL_INLINE void setStartScale(F32 xs, F32 ys)
  150. {
  151. constexpr F32 MAX_PART_SCALE = 4.f;
  152. mStartScale.mV[VX] = llmin(xs, MAX_PART_SCALE);
  153. mStartScale.mV[VY] = llmin(ys, MAX_PART_SCALE);
  154. }
  155. LL_INLINE void setEndScale(F32 xs, F32 ys)
  156. {
  157. constexpr F32 MAX_PART_SCALE = 4.f;
  158. mEndScale.mV[VX] = llmin(xs, MAX_PART_SCALE);
  159. mEndScale.mV[VY] = llmin(ys, MAX_PART_SCALE);
  160. }
  161. LL_INLINE void setStartColor(const LLVector3& rgb)
  162. {
  163. mStartColor.set(rgb.mV[0], rgb.mV[1], rgb.mV[2]);
  164. }
  165. LL_INLINE void setEndColor(const LLVector3& rgb)
  166. {
  167. mEndColor.set(rgb.mV[0], rgb.mV[1], rgb.mV[2]);
  168. }
  169. LL_INLINE void setStartAlpha(F32 alpha) { mStartColor.mV[3] = alpha; }
  170. LL_INLINE void setEndAlpha(F32 alpha) { mEndColor.mV[3] = alpha; }
  171. friend class LLPartSysData;
  172. friend class LLViewerPartSourceScript;
  173. private:
  174. S32 getSize() const;
  175. public: // These are public because I'm really lazy...
  176. U32 mFlags; // Particle state/interpolators in effect
  177. F32 mMaxAge; // Maximum age of the particle
  178. LLColor4 mStartColor; // Start color
  179. LLColor4 mEndColor; // End color
  180. LLVector2 mStartScale; // Start scale
  181. LLVector2 mEndScale; // End scale
  182. LLVector3 mPosOffset; // Offset from source if using FOLLOW_SOURCE
  183. F32 mParameter; // A single floating point parameter
  184. F32 mStartGlow;
  185. F32 mEndGlow;
  186. U8 mBlendFuncSource;
  187. U8 mBlendFuncDest;
  188. };
  189. class LLPartSysData
  190. {
  191. protected:
  192. LOG_CLASS(LLPartSysData);
  193. public:
  194. LLPartSysData();
  195. bool unpack(LLDataPacker& dp);
  196. bool unpackLegacy(LLDataPacker& dp);
  197. bool unpackBlock(S32 block_num);
  198. // Returns false if this is a "NULL" particle system (i.e. no system):
  199. static bool isNullPS(S32 block_num);
  200. LL_INLINE bool isLegacyCompatible() const { return !mPartData.hasGlow() &&
  201. !mPartData.hasBlendFunc(); }
  202. // Different masks for effects on the source
  203. enum
  204. {
  205. // Accel and velocity for particles relative object rotation:
  206. LL_PART_SRC_OBJ_REL_MASK = 0x01,
  207. // Particles uses new 'correct' angle parameters:
  208. LL_PART_USE_NEW_ANGLE = 0x02,
  209. };
  210. // The different patterns for how particles are created
  211. enum
  212. {
  213. LL_PART_SRC_PATTERN_DROP = 0x01,
  214. LL_PART_SRC_PATTERN_EXPLODE = 0x02,
  215. // Not implemented fully yet
  216. LL_PART_SRC_PATTERN_ANGLE = 0x04,
  217. LL_PART_SRC_PATTERN_ANGLE_CONE = 0x08,
  218. LL_PART_SRC_PATTERN_ANGLE_CONE_EMPTY = 0x10,
  219. };
  220. LL_INLINE void setBurstSpeedMin(F32 spd) { mBurstSpeedMin = llclamp(spd, -100.f, 100.f); }
  221. LL_INLINE void setBurstSpeedMax(F32 spd) { mBurstSpeedMax = llclamp(spd, -100.f, 100.f); }
  222. LL_INLINE void setBurstRadius(F32 rad) { mBurstRadius = llclamp(rad, 0.f, 50.f); }
  223. LL_INLINE void setPartAccel(const LLVector3& accel)
  224. {
  225. mPartAccel.mV[VX] = llclamp(accel.mV[VX], -100.f, 100.f);
  226. mPartAccel.mV[VY] = llclamp(accel.mV[VY], -100.f, 100.f);
  227. mPartAccel.mV[VZ] = llclamp(accel.mV[VZ], -100.f, 100.f);
  228. }
  229. LL_INLINE void setUseNewAngle() { mFlags |= LL_PART_USE_NEW_ANGLE; }
  230. LL_INLINE void unsetUseNewAngle() { mFlags &= ~LL_PART_USE_NEW_ANGLE; }
  231. // Since the actual particle creation rate is a combination of multiple
  232. // parameters, we need to clamp it using a separate method instead of an
  233. // accessor.
  234. void clampSourceParticleRate();
  235. friend std::ostream& operator<<(std::ostream& s,
  236. const LLPartSysData& data);
  237. private:
  238. bool unpackSystem(LLDataPacker& dp);
  239. public: // Public because I'm lazy....
  240. // There are two kinds of data for the particle system
  241. // 1. Parameters which specify parameters of the source (mSource*)
  242. // 2. Parameters which specify parameters of the particles generated by the
  243. // source (mPart*)
  244. U32 mCRC;
  245. U32 mFlags;
  246. U8 mPattern; // Pattern for particle velocity/output
  247. F32 mInnerAngle; // Inner angle for PATTERN_ANGLE
  248. F32 mOuterAngle; // Outer angle for PATTERN_ANGLE
  249. LLVector3 mAngularVelocity; // Angular velocity for emission axis (for
  250. // PATTERN_ANGLE)
  251. F32 mBurstRate; // How often to do a burst of particles
  252. U8 mBurstPartCount; // How many particles in a burst
  253. F32 mBurstRadius;
  254. F32 mBurstSpeedMin; // Minimum particle velocity
  255. F32 mBurstSpeedMax; // Maximum particle velocity
  256. F32 mMaxAge; // Maximum lifetime of this particle source
  257. LLUUID mTargetUUID; // Target UUID for the particle system
  258. F32 mStartAge; // Age at which to start the particle
  259. // system (for an update after the particle
  260. // system has started)
  261. // These are actually particle properties, but can be mutated by the
  262. // source, so are stored here instead
  263. LLVector3 mPartAccel;
  264. LLUUID mPartImageID;
  265. // The "template" partdata where we actually store the non-mutable particle
  266. // parameters
  267. LLPartData mPartData;
  268. protected:
  269. S32 mNumParticles; // Number of particles generated
  270. };
  271. #endif // LL_LLPARTDATA_H