Particle.h
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:4k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: particle.h
  4. //
  5. // Author: Zeng fancy All rights reserved
  6. //
  7. // System: Pentium M 1.73G, 256 DDR, ATI X700, Windows XP, MSVC++ 7.0 
  8. //
  9. // Desc: Implement a particle system, most of the code come from Frank Luna's book--"Introduction to Game 
  10. //
  11. // Programming in DirectX9.0"
  12. //
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. #ifndef __particleH__included__
  15. #define __particleH__included__
  16. namespace ParticleSystem
  17. {
  18. //
  19. struct ParticleVertex
  20. {
  21. D3DXVECTOR3 _position;
  22. FLOAT       _size;     //size of the particle
  23. D3DCOLOR    _color;
  24. static const DWORD FVF;//vertex format
  25. };
  26. //the structure of a particle are specific to what kind of particle system you want to modeling, not all the members
  27. //are useful to a particular system, and some additional infomation may not be listed here. for example, "_velocity" and
  28. //"_acceleration" are useless when modeling clouds.
  29. struct Particle
  30. {
  31. Particle()
  32. {
  33. _age      = 0.0f;
  34. _isAlive  = true;
  35. }
  36. FLOAT       _size;
  37. D3DXVECTOR3 _position;
  38. D3DXVECTOR3 _velocity;
  39. D3DXVECTOR3 _acceleration; 
  40. float       _lifeTime;     // how long the particle lives for before dying  
  41. float       _age;          // current age of the particle  
  42. D3DXCOLOR   _color;        // current color of the particle   
  43. D3DXCOLOR   _colorFade;    // how the color fades with respect to time
  44. bool        _isAlive;    
  45. };
  46. //抽象类,构造函数和析构函数为空
  47. class CParticleSystem
  48. {
  49. public:
  50. CParticleSystem();
  51. virtual ~CParticleSystem();
  52. virtual bool init(IDirect3DDevice9* device, LPCTSTR texFileName);
  53. virtual void reset();//reset all the particles in the system
  54. virtual void addParticle();
  55. virtual void render();
  56. virtual void update(float timeDelta) = 0;//abstract method
  57. int getParticleNum();
  58. bool isEmpty();
  59. bool isDead();
  60. protected:
  61. virtual void removeDeadParticles();
  62. // sometimes we don't want to free the memory of a dead particle,
  63. // but rather respawn it instead.
  64. virtual void resetParticle(Particle* particle) = 0;//reset or init a particle
  65. virtual void preRender();
  66. virtual void postRender();
  67. protected:
  68. IDirect3DDevice9*       _device;
  69. // D3DXVECTOR3             _origin;
  70. // float                   _emitRate;   // rate new particles are added to system
  71. float                   _size;       // size of particles
  72. IDirect3DTexture9*      _tex;
  73. IDirect3DVertexBuffer9* _vb;
  74. std::list<Particle>     _particleList;
  75. // int                     _maxParticles; // max allowed particles system can have
  76. //
  77. // Following three data elements used for rendering the p-system efficiently
  78. // initialized in derived class's constructor
  79. DWORD _vbSize;      // size of vb
  80. DWORD _vbOffset;    // offset in vb to lock   
  81. DWORD _vbBatchSize; // number of vertices to lock starting at _vbOffset
  82. };
  83. class CSnowSystem : public CParticleSystem
  84. {
  85. public:
  86. CSnowSystem(int numParticles);//will initialize _boundingBox with a minPoint = (0,0,0), maxPoint = (1,1,1)
  87. CSnowSystem(BoundingBox* pBoundingBox, int numParticles);
  88. ~CSnowSystem();
  89. void resetBoundingBox(BoundingBox& boundingBox);
  90. void update(float timeDelta);
  91. protected:
  92. void resetParticle(Particle* pParticle);
  93. BoundingBox _boundingBox;
  94. private:
  95. float m_yVelocity;
  96. float m_xzVel_max;
  97. float m_yAcceleration;
  98. float m_xzAcc_max;
  99. };
  100. class CRainSystem : public CParticleSystem
  101. {
  102. public:
  103. CRainSystem(int numParticles);//will initialize _boundingBox with a minPoint = (0,0,0), maxPoint = (1,1,1)
  104. CRainSystem(BoundingBox* pBoundingBox, int numParticles);
  105. ~CRainSystem();
  106. void resetBoundingBox(BoundingBox& boundingBox);
  107. void update(float timeDelta);
  108. protected:
  109. void resetParticle(Particle* pParticle);
  110. BoundingBox _boundingBox;
  111. private:
  112. float m_yVelocity;
  113. float m_xzVel_max;
  114. float m_yAcceleration;
  115. float m_xzAcc_max;
  116. };
  117. }
  118. #endif //__particleH__included__