Particle.h
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:4k
源码类别:
DirextX编程
开发平台:
Visual C++
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // File: particle.h
- //
- // Author: Zeng fancy All rights reserved
- //
- // System: Pentium M 1.73G, 256 DDR, ATI X700, Windows XP, MSVC++ 7.0
- //
- // Desc: Implement a particle system, most of the code come from Frank Luna's book--"Introduction to Game
- //
- // Programming in DirectX9.0"
- //
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #ifndef __particleH__included__
- #define __particleH__included__
- namespace ParticleSystem
- {
- //
- struct ParticleVertex
- {
- D3DXVECTOR3 _position;
- FLOAT _size; //size of the particle
- D3DCOLOR _color;
- static const DWORD FVF;//vertex format
- };
- //the structure of a particle are specific to what kind of particle system you want to modeling, not all the members
- //are useful to a particular system, and some additional infomation may not be listed here. for example, "_velocity" and
- //"_acceleration" are useless when modeling clouds.
- struct Particle
- {
- Particle()
- {
- _age = 0.0f;
- _isAlive = true;
- }
- FLOAT _size;
- D3DXVECTOR3 _position;
- D3DXVECTOR3 _velocity;
- D3DXVECTOR3 _acceleration;
- float _lifeTime; // how long the particle lives for before dying
- float _age; // current age of the particle
- D3DXCOLOR _color; // current color of the particle
- D3DXCOLOR _colorFade; // how the color fades with respect to time
- bool _isAlive;
- };
- //抽象类,构造函数和析构函数为空
- class CParticleSystem
- {
- public:
- CParticleSystem();
- virtual ~CParticleSystem();
- virtual bool init(IDirect3DDevice9* device, LPCTSTR texFileName);
- virtual void reset();//reset all the particles in the system
- virtual void addParticle();
- virtual void render();
- virtual void update(float timeDelta) = 0;//abstract method
- int getParticleNum();
- bool isEmpty();
- bool isDead();
- protected:
- virtual void removeDeadParticles();
- // sometimes we don't want to free the memory of a dead particle,
- // but rather respawn it instead.
- virtual void resetParticle(Particle* particle) = 0;//reset or init a particle
- virtual void preRender();
- virtual void postRender();
- protected:
- IDirect3DDevice9* _device;
- // D3DXVECTOR3 _origin;
- // float _emitRate; // rate new particles are added to system
- float _size; // size of particles
- IDirect3DTexture9* _tex;
- IDirect3DVertexBuffer9* _vb;
- std::list<Particle> _particleList;
- // int _maxParticles; // max allowed particles system can have
- //
- // Following three data elements used for rendering the p-system efficiently
- // initialized in derived class's constructor
- DWORD _vbSize; // size of vb
- DWORD _vbOffset; // offset in vb to lock
- DWORD _vbBatchSize; // number of vertices to lock starting at _vbOffset
- };
- class CSnowSystem : public CParticleSystem
- {
- public:
- CSnowSystem(int numParticles, float size = 0.15f);//will initialize _boundingBox with a minPoint = (0,0,0), maxPoint = (1,1,1)
- CSnowSystem(BoundingBox* pBoundingBox, int numParticles, float size = 0.15f);
- ~CSnowSystem();
- void resetBoundingBox(BoundingBox& boundingBox);
- void update(float timeDelta);
- protected:
- void resetParticle(Particle* pParticle);
- BoundingBox _boundingBox;
- private:
- float m_yVelocity;
- float m_xzVel_max;
- float m_yAcceleration;
- float m_xzAcc_max;
- };
- class CRainSystem : public CParticleSystem
- {
- public:
- CRainSystem(int numParticles, float size = 0.15f);//will initialize _boundingBox with a minPoint = (0,0,0), maxPoint = (1,1,1)
- CRainSystem(BoundingBox* pBoundingBox, int numParticles, float size = 0.15f);
- ~CRainSystem();
- void resetBoundingBox(BoundingBox& boundingBox);
- void update(float timeDelta);
- protected:
- void resetParticle(Particle* pParticle);
- BoundingBox _boundingBox;
- private:
- float m_yVelocity;
- float m_xzVel_max;
- float m_yAcceleration;
- float m_xzAcc_max;
- };
- }
- #endif //__particleH__included__