1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __C_PARTICLE_SYSTEM_SCENE_NODE_H_INCLUDED__
6 #define __C_PARTICLE_SYSTEM_SCENE_NODE_H_INCLUDED__
7 
8 #include "IParticleSystemSceneNode.h"
9 #include "irrArray.h"
10 #include "irrList.h"
11 #include "SMeshBuffer.h"
12 
13 namespace irr
14 {
15 namespace scene
16 {
17 
18 //! A particle system scene node.
19 /** A scene node controlling a particle system. The behavior of the particles
20 can be controlled by setting the right particle emitters and affectors.
21 */
22 class CParticleSystemSceneNode : public IParticleSystemSceneNode
23 {
24 public:
25 
26 	//! constructor
27 	CParticleSystemSceneNode(bool createDefaultEmitter,
28 		ISceneNode* parent, ISceneManager* mgr, s32 id,
29 		const core::vector3df& position,
30 		const core::vector3df& rotation,
31 		const core::vector3df& scale);
32 
33 	//! destructor
34 	virtual ~CParticleSystemSceneNode();
35 
36 	//! Gets the particle emitter, which creates the particles.
37 	virtual IParticleEmitter* getEmitter();
38 
39 	//! Sets the particle emitter, which creates the particles.
40 	virtual void setEmitter(IParticleEmitter* emitter);
41 
42 	//! Adds new particle affector to the particle system.
43 	virtual void addAffector(IParticleAffector* affector);
44 
45 	//! Get a list of all particle affectors.
46 	virtual const core::list<IParticleAffector*>& getAffectors() const;
47 
48 	//! Removes all particle affectors in the particle system.
49 	virtual void removeAllAffectors();
50 
51 	//! Returns the material based on the zero based index i.
52 	virtual video::SMaterial& getMaterial(u32 i);
53 
54 	//! Returns amount of materials used by this scene node.
55 	virtual u32 getMaterialCount() const;
56 
57 	//! pre render event
58 	virtual void OnRegisterSceneNode();
59 
60 	//! render
61 	virtual void render();
62 
63 	//! returns the axis aligned bounding box of this node
64 	virtual const core::aabbox3d<f32>& getBoundingBox() const;
65 
66 	//! Creates a particle emitter for an animated mesh scene node
67 	virtual IParticleAnimatedMeshSceneNodeEmitter* createAnimatedMeshSceneNodeEmitter(
68 		scene::IAnimatedMeshSceneNode* node, bool useNormalDirection = true,
69 		const core::vector3df& direction = core::vector3df(0.0f,0.03f,0.0f),
70 		f32 normalDirectionModifier = 100.0f, s32 mbNumber = -1,
71 		bool everyMeshVertex = false, u32 minParticlesPerSecond = 5,
72 		u32 maxParticlesPerSecond = 10,
73 		const video::SColor& minStartColor = video::SColor(255,0,0,0),
74 		const video::SColor& maxStartColor = video::SColor(255,255,255,255),
75 		u32 lifeTimeMin = 2000, u32 lifeTimeMax = 4000,
76 		s32 maxAngleDegrees = 0,
77 		const core::dimension2df& minStartSize = core::dimension2df(5.0f,5.0f),
78 		const core::dimension2df& maxStartSize = core::dimension2df(5.0f,5.0f) );
79 
80 	//! Creates a box particle emitter.
81 	virtual IParticleBoxEmitter* createBoxEmitter(
82 		const core::aabbox3df& box = core::aabbox3d<f32>(-10,0,-10,5,30,10),
83 		const core::vector3df& direction = core::vector3df(0.0f,0.03f,0.0f),
84 		u32 minParticlesPerSecond = 5,
85 		u32 maxParticlesPerSecond = 10,
86 		const video::SColor& minStartColor = video::SColor(255,0,0,0),
87 		const video::SColor& maxStartColor = video::SColor(255,255,255,255),
88 		u32 lifeTimeMin=2000, u32 lifeTimeMax=4000,
89 		s32 maxAngleDegrees=0,
90 		const core::dimension2df& minStartSize = core::dimension2df(5.0f,5.0f),
91 		const core::dimension2df& maxStartSize = core::dimension2df(5.0f,5.0f) );
92 
93 	//! Creates a particle emitter for emitting from a cylinder
94 	virtual IParticleCylinderEmitter* createCylinderEmitter(
95 		const core::vector3df& center, f32 radius,
96 		const core::vector3df& normal, f32 length,
97 		bool outlineOnly = false, const core::vector3df& direction = core::vector3df(0.0f,0.5f,0.0f),
98 		u32 minParticlesPerSecond = 5, u32 maxParticlesPersSecond = 10,
99 		const video::SColor& minStartColor = video::SColor(255,0,0,0),
100 		const video::SColor& maxStartColor = video::SColor(255,255,255,255),
101 		u32 lifeTimeMin = 2000, u32 lifeTimeMax = 4000,
102 		s32 maxAngleDegrees = 0,
103 		const core::dimension2df& minStartSize = core::dimension2df(5.0f,5.0f),
104 		const core::dimension2df& maxStartSize = core::dimension2df(5.0f,5.0f) );
105 
106 	//! Creates a mesh particle emitter.
107 	virtual IParticleMeshEmitter* createMeshEmitter(
108 		scene::IMesh* mesh, bool useNormalDirection = true,
109 		const core::vector3df& direction = core::vector3df(0.0f,0.03f,0.0f),
110 		f32 normalDirectionModifier = 100.0f, s32 mbNumber = -1,
111 		bool everyMeshVertex = false,
112 		u32 minParticlesPerSecond = 5,
113 		u32 maxParticlesPerSecond = 10,
114 		const video::SColor& minStartColor = video::SColor(255,0,0,0),
115 		const video::SColor& maxStartColor = video::SColor(255,255,255,255),
116 		u32 lifeTimeMin = 2000, u32 lifeTimeMax = 4000,
117 		s32 maxAngleDegrees = 0,
118 		const core::dimension2df& minStartSize = core::dimension2df(5.0f,5.0f),
119 		const core::dimension2df& maxStartSize = core::dimension2df(5.0f,5.0f) );
120 
121 	//! Creates a point particle emitter.
122 	virtual IParticlePointEmitter* createPointEmitter(
123 		const core::vector3df& direction = core::vector3df(0.0f,0.03f,0.0f),
124 		u32 minParticlesPerSecond = 5,
125 		u32 maxParticlesPerSecond = 10,
126 		const video::SColor& minStartColor = video::SColor(255,0,0,0),
127 		const video::SColor& maxStartColor = video::SColor(255,255,255,255),
128 		u32 lifeTimeMin=2000, u32 lifeTimeMax=4000,
129 		s32 maxAngleDegrees=0,
130 		const core::dimension2df& minStartSize = core::dimension2df(5.0f,5.0f),
131 		const core::dimension2df& maxStartSize = core::dimension2df(5.0f,5.0f) );
132 
133 	//! Creates a ring particle emitter.
134 	virtual IParticleRingEmitter* createRingEmitter(
135 		const core::vector3df& center, f32 radius, f32 ringThickness,
136 		const core::vector3df& direction = core::vector3df(0.0f,0.03f,0.0f),
137 		u32 minParticlesPerSecond = 5,
138 		u32 maxParticlesPerSecond = 10,
139 		const video::SColor& minStartColor = video::SColor(255,0,0,0),
140 		const video::SColor& maxStartColor = video::SColor(255,255,255,255),
141 		u32 lifeTimeMin=2000, u32 lifeTimeMax=4000,
142 		s32 maxAngleDegrees=0,
143 		const core::dimension2df& minStartSize = core::dimension2df(5.0f,5.0f),
144 		const core::dimension2df& maxStartSize = core::dimension2df(5.0f,5.0f) );
145 
146 	//! Creates a sphere particle emitter.
147 	virtual IParticleSphereEmitter* createSphereEmitter(
148 		const core::vector3df& center, f32 radius,
149 		const core::vector3df& direction = core::vector3df(0.0f,0.03f,0.0f),
150 		u32 minParticlesPerSecond = 5,
151 		u32 maxParticlesPerSecond = 10,
152 		const video::SColor& minStartColor = video::SColor(255,0,0,0),
153 		const video::SColor& maxStartColor = video::SColor(255,255,255,255),
154 		u32 lifeTimeMin=2000, u32 lifeTimeMax=4000,
155 		s32 maxAngleDegrees=0,
156 		const core::dimension2df& minStartSize = core::dimension2df(5.0f,5.0f),
157 		const core::dimension2df& maxStartSize = core::dimension2df(5.0f,5.0f) );
158 
159 	//! Creates a point attraction affector. This affector modifies the positions of the
160 	//! particles and attracts them to a specified point at a specified speed per second.
161 	virtual IParticleAttractionAffector* createAttractionAffector(
162 		const core::vector3df& point, f32 speed = 1.0f, bool attract = true,
163 		bool affectX = true, bool affectY = true, bool affectZ = true);
164 
165 	//! Creates a scale particle affector.
166 	virtual IParticleAffector* createScaleParticleAffector(const core::dimension2df& scaleTo = core::dimension2df(1.0f, 1.0f));
167 
168 	//! Creates a fade out particle affector.
169 	virtual IParticleFadeOutAffector* createFadeOutParticleAffector(
170 		const video::SColor& targetColor = video::SColor(0,0,0,0),
171 		u32 timeNeededToFadeOut = 1000);
172 
173 	//! Creates a gravity affector.
174 	virtual IParticleGravityAffector* createGravityAffector(
175 		const core::vector3df& gravity = core::vector3df(0.0f,-0.03f,0.0f),
176 		u32 timeForceLost = 1000);
177 
178 	//! Creates a rotation affector. This affector rotates the particles
179 	//! around a specified pivot point. The speed is in Degrees per second.
180 	virtual IParticleRotationAffector* createRotationAffector(
181 		const core::vector3df& speed = core::vector3df(5.0f,5.0f,5.0f),
182 		const core::vector3df& pivotPoint = core::vector3df(0.0f,0.0f,0.0f) );
183 
184 	//! Sets the size of all particles.
185 	virtual void setParticleSize(
186 		const core::dimension2d<f32> &size = core::dimension2d<f32>(5.0f, 5.0f));
187 
188 	//! Sets if the particles should be global. If they are, the particles are affected by
189 	//! the movement of the particle system scene node too, otherwise they completely
190 	//! ignore it. Default is true.
191 	virtual void setParticlesAreGlobal(bool global=true);
192 
193 	//! Remove all currently visible particles
194 	virtual void clearParticles();
195 
196 	//! Do manually update the particles.
197  	//! This should only be called when you want to render the node outside the scenegraph,
198  	//! as the node will care about this otherwise automatically.
199 	virtual void doParticleSystem(u32 time);
200 
201 	//! Writes attributes of the scene node.
202 	virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const;
203 
204 	//! Reads attributes of the scene node.
205 	virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0);
206 
207 	//! Returns type of the scene node
getType()208 	virtual ESCENE_NODE_TYPE getType() const { return ESNT_PARTICLE_SYSTEM; }
209 
210 private:
211 
212 	void reallocateBuffers();
213 
214 	core::list<IParticleAffector*> AffectorList;
215 	IParticleEmitter* Emitter;
216 	core::array<SParticle> Particles;
217 	core::dimension2d<f32> ParticleSize;
218 	u32 LastEmitTime;
219 	s32 MaxParticles;
220 
221 	SMeshBuffer* Buffer;
222 
223 	enum E_PARTICLES_PRIMITIVE
224 	{
225 		EPP_POINT=0,
226 		EPP_BILLBOARD,
227 		EPP_POINTSPRITE
228 	};
229 	E_PARTICLES_PRIMITIVE ParticlePrimitive;
230 
231 	bool ParticlesAreGlobal;
232 };
233 
234 } // end namespace scene
235 } // end namespace irr
236 
237 
238 #endif
239 
240