ParticleEmitter.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright © 2011-2020 Frictional Games
  3. *
  4. * This file is part of Amnesia: A Machine For Pigs.
  5. *
  6. * Amnesia: A Machine For Pigs is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. * Amnesia: A Machine For Pigs 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 Amnesia: A Machine For Pigs. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #ifndef HPL_PARTICLE_EMITTER_H
  19. #define HPL_PARTICLE_EMITTER_H
  20. #include "graphics/Renderable.h"
  21. #include "scene/Entity3D.h"
  22. #include "math/MathTypes.h"
  23. #include "system/SystemTypes.h"
  24. #include "graphics/GraphicsTypes.h"
  25. namespace hpl {
  26. class cFrameSubImage;
  27. class cGraphics;
  28. class cResources;
  29. class cWorld;
  30. class cParticleSystem;
  31. //-------------------------------------------------------------------
  32. enum ePEType
  33. {
  34. ePEType_Normal,
  35. ePEType_Beam,
  36. ePEType_LastEnum,
  37. };
  38. //-------------------------------------------------------------------
  39. enum eParticleEmitterType
  40. {
  41. eParticleEmitterType_FixedPoint,
  42. eParticleEmitterType_DynamicPoint,
  43. eParticleEmitterType_Line,
  44. eParticleEmitterType_Axis,
  45. eParticleEmitterType_LastEnum,
  46. };
  47. enum eParticleEmitterCoordSystem
  48. {
  49. eParticleEmitterCoordSystem_World,
  50. eParticleEmitterCoordSystem_Local,
  51. eParticleEmitterCoordSystem_LastEnum,
  52. };
  53. //------------------------------------------
  54. class cPESubDivision
  55. {
  56. public:
  57. cVector3f mvUV[4];
  58. };
  59. //-------------------------------------------------------------------
  60. enum ePENoiseType
  61. {
  62. ePENoiseType_LowFreq,
  63. ePENoiseType_HighFreq,
  64. ePENoiseType_Both,
  65. ePENoiseType_None,
  66. ePENoiseType_LastEnum,
  67. };
  68. typedef struct
  69. {
  70. float fRelToBeamPos;
  71. float fRelToBendPos;
  72. int lLowFreqNoiseIdx;
  73. int lHighFreqNoiseIdx;
  74. ePENoiseType noiseType;
  75. } tBeamNoisePoint;
  76. //-------------------------------------------------------------------
  77. //////////////////////////////////////////////////////
  78. /////////////// PARTICLE /////////////////////////////
  79. //////////////////////////////////////////////////////
  80. //-------------------------------------------------------------------
  81. class cParticle
  82. {
  83. public:
  84. cParticle(){}
  85. cVector3f mvPos;
  86. cVector3f mvLastPos;
  87. cVector3f mvLastCollidePos;
  88. cVector3f mvAcc;
  89. cVector3f mvVel;
  90. float mfSpeedMul;
  91. float mfMaxSpeed;
  92. cColor mStartColor;
  93. cColor mColor;
  94. cVector2f mvStartSize;
  95. cVector2f mvSize;
  96. float mfStartLife;
  97. float mfLife;
  98. float mfLifeSize_MiddleStart;
  99. float mfLifeSize_MiddleEnd;
  100. float mfLifeColor_MiddleStart;
  101. float mfLifeColor_MiddleEnd;
  102. int mlSubDivNum;
  103. float mfBounceAmount;
  104. int mlBounceCount;
  105. cVector3f mvExtra;
  106. // NEW
  107. float mfSpin;
  108. float mfSpinVel;
  109. float mfSpinFactor;
  110. cVector3f mvRevolutionVel;
  111. // Beam Specific
  112. int mlLowFreqPoints;
  113. int mlHighFreqPoints;
  114. std::vector<cVector3f> mvBeamPoints;
  115. // ---
  116. };
  117. //-------------------------------------------------------------------
  118. typedef std::vector<cParticle*> tParticleVec;
  119. typedef tParticleVec::iterator tParticleVecIt;
  120. //-------------------------------------------------------------------
  121. //////////////////////////////////////////////////////
  122. /////////////// PARTICLE SYSTEM //////////////////////
  123. //////////////////////////////////////////////////////
  124. class iParticleEmitter :public iRenderable
  125. {
  126. public:
  127. iParticleEmitter( tString asName,tMaterialVec* avMaterials,unsigned int alMaxParticles,
  128. cVector3f avSize, cGraphics* apGraphics,cResources *apResources);
  129. virtual ~iParticleEmitter();
  130. void UpdateLogic(float afTimeStep);
  131. void Render(){}
  132. void SetSubDivUV(const cVector2l &avSubDiv);
  133. void SetWorld(cWorld *apWorld) { mpWorld = apWorld;}
  134. void SetSystem(cParticleSystem *apSystem){ mpParentSystem = apSystem;}
  135. virtual bool IsDead(){ return mlNumOfParticles==0 && mbDying;}
  136. virtual bool IsDying(){ return mbDying;}
  137. virtual void Kill(){ mbDying = true;}
  138. void KillInstantly();
  139. void SetDataName(const tString &asName){msDataName = asName;}
  140. void SetDataSize(const cVector3f &avSize){mvDataSize = avSize;}
  141. int GetParticleNum(){ return mlNumOfParticles;}
  142. //Entity implementation
  143. tString GetEntityType(){ return "ParticleEmitter"; }
  144. bool IsVisible();
  145. //Renderable implementation
  146. bool UpdateGraphicsForViewport(cFrustum *apFrustum,float afFrameTime);
  147. cMaterial *GetMaterial();
  148. iVertexBuffer* GetVertexBuffer();
  149. cBoundingVolume* GetBoundingVolume();
  150. cMatrixf* GetModelMatrix(cFrustum *apFrustum);
  151. int GetMatrixUpdateCount(){return GetTransformUpdateCount();}
  152. eRenderableType GetRenderType(){ return eRenderableType_ParticleEmitter;}
  153. protected:
  154. void SwapRemove(unsigned int alIndex);
  155. cParticle* CreateParticle();
  156. virtual void UpdateMotion(float afTimeStep)=0;
  157. virtual void SetParticleDefaults(cParticle *apParticle)=0;
  158. cGraphics *mpGraphics;
  159. cResources *mpResources;
  160. tString msDataName;
  161. cVector3f mvDataSize;
  162. tParticleVec mvParticles;
  163. unsigned int mlNumOfParticles;
  164. unsigned int mlMaxParticles;
  165. cMatrixf m_mtxTemp;
  166. tMaterialVec* mvMaterials;
  167. int mlSleepCount;
  168. //Vars for easier updating.
  169. bool mbDying;
  170. float mfFrame;
  171. bool mbUpdateGfx;
  172. bool mbUpdateBV;
  173. iVertexBuffer *mpVtxBuffer;
  174. std::vector<cPESubDivision> mvSubDivUV;
  175. cVector3f mvDirection;
  176. int mlDirectionUpdateCount;
  177. cVector3f mvRight;
  178. cVector3f mvForward;
  179. int mlAxisDrawUpdateCount;
  180. cParticleSystem *mpParentSystem;
  181. //Set by the particle system implementation.
  182. cVector2f mvDrawSize;
  183. cVector2f mvMaxDrawSize;
  184. eParticleEmitterType mDrawType;
  185. eParticleEmitterCoordSystem mCoordSystem;
  186. bool mbMultiplyRGBWithAlpha;
  187. cWorld *mpWorld;
  188. bool mbUsesDirection;
  189. bool mbUsePartSpin;
  190. bool mbUseRevolution;
  191. ePEType mPEType;
  192. };
  193. //-----------------------------------------------------------------
  194. typedef std::list<iParticleEmitter*> tParticleEmitterList;
  195. typedef tParticleEmitterList::iterator tParticleEmitterListIt;
  196. //-----------------------------------------------------------------
  197. //////////////////////////////////////////////////////
  198. /////////////// PARTICLE EMITTER DATA ////////////////
  199. //////////////////////////////////////////////////////
  200. //-----------------------------------------------------------------
  201. class cResources;
  202. class iParticleEmitterData
  203. {
  204. friend class cParticleSystemData;
  205. public:
  206. /**
  207. * This inits the data needed for the particles system type
  208. * \param &asName name of the type
  209. * \param apResources
  210. * \param apGraphics
  211. */
  212. iParticleEmitterData(const tString &asName,cResources* apResources,cGraphics *apGraphics);
  213. virtual ~iParticleEmitterData();
  214. void AddMaterial(cMaterial *apMaterial);
  215. const tString& GetName(){ return msName;}
  216. virtual iParticleEmitter* Create(tString asName, cVector3f avSize)=0;
  217. float GetWarmUpTime() const { return mfWarmUpTime;}
  218. float GetWarmUpStepsPerSec() const { return mfWarmUpStepsPerSec;}
  219. protected:
  220. cResources *mpResources;
  221. cGraphics *mpGraphics;
  222. tString msName;
  223. tMaterialVec mvMaterials;
  224. float mfWarmUpTime;
  225. float mfWarmUpStepsPerSec;
  226. };
  227. //-----------------------------------------------------------------
  228. typedef std::map<tString,iParticleEmitterData*> tParticleEmitterDataMap;
  229. typedef tParticleEmitterDataMap::iterator tParticleEmitterDataMapIt;
  230. //-----------------------------------------------------------------
  231. };
  232. #endif // HPL_PARTICLE_EMITTER_H