2008-05-22 04:51:37 -07:00
|
|
|
// Copyright (C) 2002-2008 Nikolaus Gebhardt
|
2007-05-20 11:03:49 -07:00
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "CParticleSystemSceneNode.h"
|
|
|
|
#include "os.h"
|
|
|
|
#include "ISceneManager.h"
|
|
|
|
#include "ICameraSceneNode.h"
|
|
|
|
#include "IVideoDriver.h"
|
|
|
|
|
2007-08-29 09:13:38 -07:00
|
|
|
#include "CParticleAnimatedMeshSceneNodeEmitter.h"
|
2007-05-20 11:03:49 -07:00
|
|
|
#include "CParticleBoxEmitter.h"
|
2007-08-29 09:13:38 -07:00
|
|
|
#include "CParticleCylinderEmitter.h"
|
|
|
|
#include "CParticleMeshEmitter.h"
|
|
|
|
#include "CParticlePointEmitter.h"
|
|
|
|
#include "CParticleRingEmitter.h"
|
|
|
|
#include "CParticleSphereEmitter.h"
|
2007-08-28 17:17:46 -07:00
|
|
|
#include "CParticleAttractionAffector.h"
|
2007-05-20 11:03:49 -07:00
|
|
|
#include "CParticleFadeOutAffector.h"
|
|
|
|
#include "CParticleGravityAffector.h"
|
2007-08-28 17:17:46 -07:00
|
|
|
#include "CParticleRotationAffector.h"
|
2007-05-20 11:03:49 -07:00
|
|
|
#include "SViewFrustum.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
|
|
|
//! constructor
|
|
|
|
CParticleSystemSceneNode::CParticleSystemSceneNode(bool createDefaultEmitter,
|
|
|
|
ISceneNode* parent, ISceneManager* mgr, s32 id,
|
|
|
|
const core::vector3df& position, const core::vector3df& rotation,
|
|
|
|
const core::vector3df& scale)
|
|
|
|
: IParticleSystemSceneNode(parent, mgr, id, position, rotation, scale),
|
2008-08-06 07:45:22 -07:00
|
|
|
Emitter(0), LastEmitTime(0), MaxParticles(0xffff), Buffer(0),
|
|
|
|
ParticlesAreGlobal(true)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
setDebugName("CParticleSystemSceneNode");
|
|
|
|
#endif
|
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer = new SMeshBuffer();
|
2007-05-20 11:03:49 -07:00
|
|
|
if (createDefaultEmitter)
|
|
|
|
{
|
|
|
|
IParticleEmitter* e = createBoxEmitter();
|
|
|
|
setEmitter(e);
|
|
|
|
e->drop();
|
|
|
|
}
|
|
|
|
|
|
|
|
setParticleSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! destructor
|
|
|
|
CParticleSystemSceneNode::~CParticleSystemSceneNode()
|
|
|
|
{
|
|
|
|
if (Emitter)
|
|
|
|
Emitter->drop();
|
2008-08-06 07:45:22 -07:00
|
|
|
if (Buffer)
|
|
|
|
Buffer->drop();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
removeAllAffectors();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-20 01:24:52 -07:00
|
|
|
//! Gets the particle emitter, which creates the particles.
|
|
|
|
IParticleEmitter* CParticleSystemSceneNode::getEmitter()
|
|
|
|
{
|
|
|
|
return Emitter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Sets the particle emitter, which creates the particles.
|
|
|
|
void CParticleSystemSceneNode::setEmitter(IParticleEmitter* emitter)
|
|
|
|
{
|
|
|
|
if (Emitter)
|
|
|
|
Emitter->drop();
|
|
|
|
|
|
|
|
Emitter = emitter;
|
|
|
|
|
|
|
|
if (Emitter)
|
|
|
|
Emitter->grab();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Adds new particle effector to the particle system.
|
|
|
|
void CParticleSystemSceneNode::addAffector(IParticleAffector* affector)
|
|
|
|
{
|
|
|
|
affector->grab();
|
|
|
|
AffectorList.push_back(affector);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Removes all particle affectors in the particle system.
|
|
|
|
void CParticleSystemSceneNode::removeAllAffectors()
|
|
|
|
{
|
|
|
|
core::list<IParticleAffector*>::Iterator it = AffectorList.begin();
|
|
|
|
while (it != AffectorList.end())
|
|
|
|
{
|
|
|
|
(*it)->drop();
|
|
|
|
it = AffectorList.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Returns the material based on the zero based index i.
|
|
|
|
video::SMaterial& CParticleSystemSceneNode::getMaterial(u32 i)
|
|
|
|
{
|
2008-08-06 07:45:22 -07:00
|
|
|
return Buffer->Material;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Returns amount of materials used by this scene node.
|
2007-09-16 16:41:55 -07:00
|
|
|
u32 CParticleSystemSceneNode::getMaterialCount() const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-29 09:13:38 -07:00
|
|
|
//! Creates a particle emitter for an animated mesh scene node
|
|
|
|
IParticleAnimatedMeshSceneNodeEmitter*
|
|
|
|
CParticleSystemSceneNode::createAnimatedMeshSceneNodeEmitter(
|
|
|
|
scene::IAnimatedMeshSceneNode* node, bool useNormalDirection,
|
|
|
|
const core::vector3df& direction, f32 normalDirectionModifier,
|
|
|
|
s32 mbNumber, bool everyMeshVertex,
|
|
|
|
u32 minParticlesPerSecond, u32 maxParticlesPerSecond,
|
|
|
|
const video::SColor& minStartColor, const video::SColor& maxStartColor,
|
|
|
|
u32 lifeTimeMin, u32 lifeTimeMax, s32 maxAngleDegrees )
|
|
|
|
{
|
|
|
|
return new CParticleAnimatedMeshSceneNodeEmitter( node,
|
|
|
|
useNormalDirection, direction, normalDirectionModifier,
|
|
|
|
mbNumber, everyMeshVertex,
|
|
|
|
minParticlesPerSecond, maxParticlesPerSecond,
|
|
|
|
minStartColor, maxStartColor,
|
|
|
|
lifeTimeMin, lifeTimeMax, maxAngleDegrees );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Creates a box particle emitter.
|
|
|
|
IParticleBoxEmitter* CParticleSystemSceneNode::createBoxEmitter(
|
|
|
|
const core::aabbox3df& box, const core::vector3df& direction,
|
|
|
|
u32 minParticlesPerSecond, u32 maxParticlesPerSecond,
|
|
|
|
const video::SColor& minStartColor, const video::SColor& maxStartColor,
|
|
|
|
u32 lifeTimeMin, u32 lifeTimeMax,
|
|
|
|
s32 maxAngleDegrees)
|
|
|
|
{
|
|
|
|
return new CParticleBoxEmitter(box, direction, minParticlesPerSecond,
|
|
|
|
maxParticlesPerSecond, minStartColor, maxStartColor,
|
|
|
|
lifeTimeMin, lifeTimeMax, maxAngleDegrees);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Creates a particle emitter for emitting from a cylinder
|
|
|
|
IParticleCylinderEmitter* CParticleSystemSceneNode::createCylinderEmitter(
|
|
|
|
const core::vector3df& center, f32 radius,
|
|
|
|
const core::vector3df& normal, f32 length,
|
|
|
|
bool outlineOnly, const core::vector3df& direction,
|
|
|
|
u32 minParticlesPerSecond, u32 maxParticlesPerSecond,
|
|
|
|
const video::SColor& minStartColor, const video::SColor& maxStartColor,
|
|
|
|
u32 lifeTimeMin, u32 lifeTimeMax, s32 maxAngleDegrees )
|
|
|
|
{
|
|
|
|
return new CParticleCylinderEmitter( center, radius, normal, length,
|
|
|
|
outlineOnly, direction,
|
|
|
|
minParticlesPerSecond, maxParticlesPerSecond,
|
|
|
|
minStartColor, maxStartColor,
|
|
|
|
lifeTimeMin, lifeTimeMax, maxAngleDegrees );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Creates a mesh particle emitter.
|
|
|
|
IParticleMeshEmitter* CParticleSystemSceneNode::createMeshEmitter(
|
|
|
|
scene::IMesh* mesh, bool useNormalDirection,
|
|
|
|
const core::vector3df& direction, f32 normalDirectionModifier,
|
|
|
|
s32 mbNumber, bool everyMeshVertex,
|
|
|
|
u32 minParticlesPerSecond, u32 maxParticlesPerSecond,
|
|
|
|
const video::SColor& minStartColor, const video::SColor& maxStartColor,
|
|
|
|
u32 lifeTimeMin, u32 lifeTimeMax, s32 maxAngleDegrees )
|
|
|
|
{
|
|
|
|
return new CParticleMeshEmitter( mesh, useNormalDirection, direction,
|
|
|
|
normalDirectionModifier, mbNumber, everyMeshVertex,
|
|
|
|
minParticlesPerSecond, maxParticlesPerSecond,
|
|
|
|
minStartColor, maxStartColor,
|
|
|
|
lifeTimeMin, lifeTimeMax, maxAngleDegrees );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Creates a point particle emitter.
|
2007-08-29 09:13:38 -07:00
|
|
|
IParticlePointEmitter* CParticleSystemSceneNode::createPointEmitter(
|
2007-05-20 11:03:49 -07:00
|
|
|
const core::vector3df& direction, u32 minParticlesPerSecond,
|
2007-08-29 09:13:38 -07:00
|
|
|
u32 maxParticlesPerSecond, const video::SColor& minStartColor,
|
|
|
|
const video::SColor& maxStartColor, u32 lifeTimeMin, u32 lifeTimeMax,
|
2007-05-20 11:03:49 -07:00
|
|
|
s32 maxAngleDegrees)
|
|
|
|
{
|
|
|
|
return new CParticlePointEmitter(direction, minParticlesPerSecond,
|
2007-08-29 09:13:38 -07:00
|
|
|
maxParticlesPerSecond, minStartColor, maxStartColor,
|
2007-05-20 11:03:49 -07:00
|
|
|
lifeTimeMin, lifeTimeMax, maxAngleDegrees);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-29 09:13:38 -07:00
|
|
|
//! Creates a ring particle emitter.
|
|
|
|
IParticleRingEmitter* CParticleSystemSceneNode::createRingEmitter(
|
|
|
|
const core::vector3df& center, f32 radius, f32 ringThickness,
|
|
|
|
const core::vector3df& direction,
|
|
|
|
u32 minParticlesPerSecond, u32 maxParticlesPerSecond,
|
|
|
|
const video::SColor& minStartColor, const video::SColor& maxStartColor,
|
|
|
|
u32 lifeTimeMin, u32 lifeTimeMax, s32 maxAngleDegrees )
|
|
|
|
{
|
|
|
|
return new CParticleRingEmitter( center, radius, ringThickness, direction,
|
|
|
|
minParticlesPerSecond, maxParticlesPerSecond, minStartColor,
|
|
|
|
maxStartColor, lifeTimeMin, lifeTimeMax, maxAngleDegrees );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Creates a sphere particle emitter.
|
|
|
|
IParticleSphereEmitter* CParticleSystemSceneNode::createSphereEmitter(
|
|
|
|
const core::vector3df& center, f32 radius, const core::vector3df& direction,
|
|
|
|
u32 minParticlesPerSecond, u32 maxParticlesPerSecond,
|
|
|
|
const video::SColor& minStartColor, const video::SColor& maxStartColor,
|
2007-05-20 11:03:49 -07:00
|
|
|
u32 lifeTimeMin, u32 lifeTimeMax,
|
|
|
|
s32 maxAngleDegrees)
|
|
|
|
{
|
2007-08-29 09:13:38 -07:00
|
|
|
return new CParticleSphereEmitter(center, radius, direction,
|
|
|
|
minParticlesPerSecond, maxParticlesPerSecond,
|
|
|
|
minStartColor, maxStartColor,
|
|
|
|
lifeTimeMin, lifeTimeMax, maxAngleDegrees);
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-28 17:17:46 -07:00
|
|
|
//! Creates a point attraction affector. This affector modifies the positions of the
|
|
|
|
//! particles and attracts them to a specified point at a specified speed per second.
|
|
|
|
IParticleAttractionAffector* CParticleSystemSceneNode::createAttractionAffector(
|
|
|
|
const core::vector3df& point, f32 speed, bool attract,
|
|
|
|
bool affectX, bool affectY, bool affectZ )
|
|
|
|
{
|
|
|
|
return new CParticleAttractionAffector( point, speed, attract, affectX, affectY, affectZ );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Creates a fade out particle affector.
|
2007-08-28 17:17:46 -07:00
|
|
|
IParticleFadeOutAffector* CParticleSystemSceneNode::createFadeOutParticleAffector(
|
|
|
|
const video::SColor& targetColor, u32 timeNeededToFadeOut)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
return new CParticleFadeOutAffector(targetColor, timeNeededToFadeOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Creates a gravity affector.
|
2007-08-28 17:17:46 -07:00
|
|
|
IParticleGravityAffector* CParticleSystemSceneNode::createGravityAffector(
|
2007-05-20 11:03:49 -07:00
|
|
|
const core::vector3df& gravity, u32 timeForceLost)
|
|
|
|
{
|
|
|
|
return new CParticleGravityAffector(gravity, timeForceLost);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-28 17:17:46 -07:00
|
|
|
//! Creates a rotation affector. This affector rotates the particles around a specified pivot
|
|
|
|
//! point. The speed represents Degrees of rotation per second.
|
|
|
|
IParticleRotationAffector* CParticleSystemSceneNode::createRotationAffector(
|
|
|
|
const core::vector3df& speed, const core::vector3df& pivotPoint )
|
|
|
|
{
|
|
|
|
return new CParticleRotationAffector( speed, pivotPoint );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! pre render event
|
|
|
|
void CParticleSystemSceneNode::OnRegisterSceneNode()
|
|
|
|
{
|
|
|
|
doParticleSystem(os::Timer::getTime());
|
|
|
|
|
|
|
|
if (IsVisible && (Particles.size() != 0))
|
|
|
|
{
|
|
|
|
SceneManager->registerNodeForRendering(this);
|
|
|
|
ISceneNode::OnRegisterSceneNode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! render
|
|
|
|
void CParticleSystemSceneNode::render()
|
|
|
|
{
|
|
|
|
video::IVideoDriver* driver = SceneManager->getVideoDriver();
|
|
|
|
ICameraSceneNode* camera = SceneManager->getActiveCamera();
|
|
|
|
|
|
|
|
if (!camera || !driver)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// calculate vectors for letting particles look to camera
|
|
|
|
core::vector3df view(camera->getTarget() - camera->getAbsolutePosition());
|
|
|
|
view.normalize();
|
|
|
|
|
|
|
|
core::vector3df horizontal = camera->getUpVector().crossProduct(view);
|
|
|
|
horizontal.normalize();
|
|
|
|
horizontal *= 0.5f * ParticleSize.Width;
|
|
|
|
|
|
|
|
core::vector3df vertical = horizontal.crossProduct(view);
|
|
|
|
vertical.normalize();
|
|
|
|
vertical *= 0.5f * ParticleSize.Height;
|
|
|
|
|
|
|
|
view *= -1.0f;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
const core::matrix4 &m = camera->getViewFrustum()->Matrices [ video::ETS_VIEW ];
|
|
|
|
|
|
|
|
f32 f;
|
|
|
|
|
|
|
|
f = 0.5f * ParticleSize.Width;
|
|
|
|
const core::vector3df horizontal ( m[0] * f, m[4] * f, m[8] * f );
|
|
|
|
|
|
|
|
f = -0.5f * ParticleSize.Height;
|
|
|
|
const core::vector3df vertical ( m[1] * f, m[5] * f, m[9] * f );
|
|
|
|
|
|
|
|
const core::vector3df view ( -m[2], -m[6] , -m[10] );
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// reallocate arrays, if they are too small
|
|
|
|
reallocateBuffers();
|
|
|
|
|
|
|
|
// create particle vertex data
|
|
|
|
s32 idx = 0;
|
|
|
|
for (u32 i=0; i<Particles.size(); ++i)
|
|
|
|
{
|
|
|
|
const SParticle& particle = Particles[i];
|
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->Vertices[0+idx].Pos = particle.pos + horizontal + vertical;
|
|
|
|
Buffer->Vertices[0+idx].Color = particle.color;
|
|
|
|
Buffer->Vertices[0+idx].Normal = view;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->Vertices[1+idx].Pos = particle.pos + horizontal - vertical;
|
|
|
|
Buffer->Vertices[1+idx].Color = particle.color;
|
|
|
|
Buffer->Vertices[1+idx].Normal = view;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->Vertices[2+idx].Pos = particle.pos - horizontal - vertical;
|
|
|
|
Buffer->Vertices[2+idx].Color = particle.color;
|
|
|
|
Buffer->Vertices[2+idx].Normal = view;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->Vertices[3+idx].Pos = particle.pos - horizontal + vertical;
|
|
|
|
Buffer->Vertices[3+idx].Color = particle.color;
|
|
|
|
Buffer->Vertices[3+idx].Normal = view;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
idx +=4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// render all
|
|
|
|
core::matrix4 mat;
|
|
|
|
if (!ParticlesAreGlobal)
|
|
|
|
mat.setTranslation(AbsoluteTransformation.getTranslation());
|
|
|
|
driver->setTransform(video::ETS_WORLD, mat);
|
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
driver->setMaterial(Buffer->Material);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
driver->drawVertexPrimitiveList(Buffer->getVertices(), Particles.size()*4,
|
2008-08-27 21:00:22 -07:00
|
|
|
Buffer->getIndices(), Particles.size()*2, video::EVT_STANDARD, EPT_TRIANGLES,Buffer->getIndexType());
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// for debug purposes only:
|
|
|
|
if ( DebugDataVisible & scene::EDS_BBOX )
|
|
|
|
{
|
|
|
|
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
|
2007-09-18 07:01:28 -07:00
|
|
|
video::SMaterial deb_m;
|
|
|
|
deb_m.Lighting = false;
|
|
|
|
driver->setMaterial(deb_m);
|
2008-08-06 07:45:22 -07:00
|
|
|
driver->draw3DBox(Buffer->BoundingBox, video::SColor(0,255,255,255));
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! returns the axis aligned bounding box of this node
|
|
|
|
const core::aabbox3d<f32>& CParticleSystemSceneNode::getBoundingBox() const
|
|
|
|
{
|
2008-08-06 07:45:22 -07:00
|
|
|
return Buffer->getBoundingBox();
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CParticleSystemSceneNode::doParticleSystem(u32 time)
|
|
|
|
{
|
|
|
|
if (LastEmitTime==0)
|
|
|
|
{
|
|
|
|
LastEmitTime = time;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 now = time;
|
|
|
|
u32 timediff = time - LastEmitTime;
|
|
|
|
LastEmitTime = time;
|
|
|
|
|
|
|
|
// run emitter
|
|
|
|
|
|
|
|
if (Emitter && IsVisible)
|
|
|
|
{
|
|
|
|
SParticle* array = 0;
|
|
|
|
s32 newParticles = Emitter->emitt(now, timediff, array);
|
|
|
|
|
|
|
|
if (newParticles && array)
|
|
|
|
{
|
|
|
|
s32 j=Particles.size();
|
|
|
|
if (newParticles > 16250-j)
|
|
|
|
newParticles=16250-j;
|
|
|
|
Particles.set_used(j+newParticles);
|
|
|
|
for (s32 i=j; i<j+newParticles; ++i)
|
|
|
|
{
|
|
|
|
Particles[i]=array[i-j];
|
|
|
|
AbsoluteTransformation.rotateVect(Particles[i].startVector);
|
|
|
|
if (ParticlesAreGlobal)
|
|
|
|
AbsoluteTransformation.transformVect(Particles[i].pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run affectors
|
|
|
|
core::list<IParticleAffector*>::Iterator ait = AffectorList.begin();
|
|
|
|
for (; ait != AffectorList.end(); ++ait)
|
|
|
|
(*ait)->affect(now, Particles.pointer(), Particles.size());
|
|
|
|
|
|
|
|
if (ParticlesAreGlobal)
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->BoundingBox.reset(AbsoluteTransformation.getTranslation());
|
2007-05-20 11:03:49 -07:00
|
|
|
else
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->BoundingBox.reset(core::vector3df(0,0,0));
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// animate all particles
|
|
|
|
f32 scale = (f32)timediff;
|
|
|
|
|
2008-03-02 06:28:48 -08:00
|
|
|
for (u32 i=0; i<Particles.size();)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
if (now > Particles[i].endTime)
|
|
|
|
Particles.erase(i);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Particles[i].pos += (Particles[i].vector * scale);
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->BoundingBox.addInternalPoint(Particles[i].pos);
|
2007-05-20 11:03:49 -07:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-25 06:46:22 -07:00
|
|
|
const f32 m = (ParticleSize.Width > ParticleSize.Height ? ParticleSize.Width : ParticleSize.Height) * 0.5f;
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->BoundingBox.MaxEdge.X += m;
|
|
|
|
Buffer->BoundingBox.MaxEdge.Y += m;
|
|
|
|
Buffer->BoundingBox.MaxEdge.Z += m;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->BoundingBox.MinEdge.X -= m;
|
|
|
|
Buffer->BoundingBox.MinEdge.Y -= m;
|
|
|
|
Buffer->BoundingBox.MinEdge.Z -= m;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
if (ParticlesAreGlobal)
|
|
|
|
{
|
2008-08-25 06:46:22 -07:00
|
|
|
core::matrix4 absinv( AbsoluteTransformation, core::matrix4::EM4CONST_INVERSE );
|
|
|
|
absinv.transformBoxEx(Buffer->BoundingBox);
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Sets if the particles should be global. If it is, the particles are affected by
|
|
|
|
//! the movement of the particle system scene node too, otherwise they completely
|
|
|
|
//! ignore it. Default is true.
|
|
|
|
void CParticleSystemSceneNode::setParticlesAreGlobal(bool global)
|
|
|
|
{
|
|
|
|
ParticlesAreGlobal = global;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Sets the size of all particles.
|
|
|
|
void CParticleSystemSceneNode::setParticleSize(const core::dimension2d<f32> &size)
|
|
|
|
{
|
|
|
|
ParticleSize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CParticleSystemSceneNode::reallocateBuffers()
|
|
|
|
{
|
2008-08-06 07:45:22 -07:00
|
|
|
if (Particles.size() * 4 > Buffer->getVertexCount() ||
|
|
|
|
Particles.size() * 6 > Buffer->getIndexCount())
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-08-06 07:45:22 -07:00
|
|
|
u32 oldSize = Buffer->getVertexCount();
|
|
|
|
Buffer->Vertices.set_used(Particles.size() * 4);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
// fill remaining vertices
|
2008-08-06 07:45:22 -07:00
|
|
|
for (i=oldSize; i<Buffer->Vertices.size(); i+=4)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->Vertices[0+i].TCoords.set(0.0f, 0.0f);
|
|
|
|
Buffer->Vertices[1+i].TCoords.set(0.0f, 1.0f);
|
|
|
|
Buffer->Vertices[2+i].TCoords.set(1.0f, 1.0f);
|
|
|
|
Buffer->Vertices[3+i].TCoords.set(1.0f, 0.0f);
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// fill remaining indices
|
2008-08-06 07:45:22 -07:00
|
|
|
u32 oldIdxSize = Buffer->getIndexCount();
|
2007-05-20 11:03:49 -07:00
|
|
|
u32 oldvertices = oldSize;
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->Indices.set_used(Particles.size() * 6);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
for (i=oldIdxSize; i<Buffer->Indices.size(); i+=6)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-08-06 07:45:22 -07:00
|
|
|
Buffer->Indices[0+i] = 0+oldvertices;
|
|
|
|
Buffer->Indices[1+i] = 2+oldvertices;
|
|
|
|
Buffer->Indices[2+i] = 1+oldvertices;
|
|
|
|
Buffer->Indices[3+i] = 0+oldvertices;
|
|
|
|
Buffer->Indices[4+i] = 3+oldvertices;
|
|
|
|
Buffer->Indices[5+i] = 2+oldvertices;
|
2007-05-20 11:03:49 -07:00
|
|
|
oldvertices += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Writes attributes of the scene node.
|
2007-09-14 15:25:59 -07:00
|
|
|
void CParticleSystemSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
IParticleSystemSceneNode::serializeAttributes(out, options);
|
|
|
|
|
|
|
|
out->addBool("GlobalParticles", ParticlesAreGlobal);
|
|
|
|
out->addFloat("ParticleWidth", ParticleSize.Width);
|
|
|
|
out->addFloat("ParticleHeight", ParticleSize.Height);
|
|
|
|
|
|
|
|
// write emitter
|
|
|
|
|
|
|
|
E_PARTICLE_EMITTER_TYPE type = EPET_COUNT;
|
|
|
|
if (Emitter)
|
|
|
|
type = Emitter->getType();
|
|
|
|
|
|
|
|
out->addEnum("Emitter", (s32)type, ParticleEmitterTypeNames);
|
|
|
|
|
|
|
|
if (Emitter)
|
|
|
|
Emitter->serializeAttributes(out, options);
|
|
|
|
|
|
|
|
// write affectors
|
|
|
|
|
|
|
|
E_PARTICLE_AFFECTOR_TYPE atype = EPAT_NONE;
|
|
|
|
|
2007-09-23 16:12:06 -07:00
|
|
|
for (core::list<IParticleAffector*>::ConstIterator it = AffectorList.begin();
|
2008-03-02 06:28:48 -08:00
|
|
|
it != AffectorList.end(); ++it)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
atype = (*it)->getType();
|
|
|
|
|
|
|
|
out->addEnum("Affector", (s32)atype, ParticleAffectorTypeNames);
|
|
|
|
|
|
|
|
(*it)->serializeAttributes(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add empty affector to make it possible to add further affectors
|
|
|
|
|
|
|
|
if (options && options->Flags & io::EARWF_FOR_EDITOR)
|
|
|
|
out->addEnum("Affector", EPAT_NONE, ParticleAffectorTypeNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Reads attributes of the scene node.
|
|
|
|
void CParticleSystemSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
|
|
|
|
{
|
|
|
|
IParticleSystemSceneNode::deserializeAttributes(in, options);
|
|
|
|
|
|
|
|
ParticlesAreGlobal = in->getAttributeAsBool("GlobalParticles");
|
|
|
|
ParticleSize.Width = in->getAttributeAsFloat("ParticleWidth");
|
|
|
|
ParticleSize.Height = in->getAttributeAsFloat("ParticleHeight");
|
|
|
|
|
|
|
|
// read emitter
|
|
|
|
|
|
|
|
int emitterIdx = in->findAttribute("Emitter");
|
|
|
|
if (emitterIdx == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Emitter)
|
|
|
|
Emitter->drop();
|
|
|
|
Emitter = 0;
|
|
|
|
|
|
|
|
E_PARTICLE_EMITTER_TYPE type = (E_PARTICLE_EMITTER_TYPE)
|
|
|
|
in->getAttributeAsEnumeration("Emitter", ParticleEmitterTypeNames);
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case EPET_POINT:
|
|
|
|
Emitter = createPointEmitter();
|
|
|
|
break;
|
|
|
|
case EPET_BOX:
|
|
|
|
Emitter = createBoxEmitter();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-09-14 15:25:59 -07:00
|
|
|
u32 idx = 0;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
if (Emitter)
|
|
|
|
idx = Emitter->deserializeAttributes(idx, in);
|
|
|
|
|
|
|
|
++idx;
|
|
|
|
|
|
|
|
// read affectors
|
|
|
|
|
|
|
|
removeAllAffectors();
|
2007-09-14 15:25:59 -07:00
|
|
|
u32 cnt = in->getAttributeCount();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
while(idx < cnt)
|
|
|
|
{
|
|
|
|
const char* name = in->getAttributeName(idx);
|
|
|
|
|
|
|
|
if (!name || strcmp("Affector", name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
E_PARTICLE_AFFECTOR_TYPE atype =
|
|
|
|
(E_PARTICLE_AFFECTOR_TYPE)in->getAttributeAsEnumeration(idx, ParticleAffectorTypeNames);
|
|
|
|
|
|
|
|
IParticleAffector* aff = 0;
|
|
|
|
|
|
|
|
switch(atype)
|
|
|
|
{
|
|
|
|
case EPAT_FADE_OUT:
|
|
|
|
aff = createFadeOutParticleAffector();
|
|
|
|
break;
|
|
|
|
case EPAT_GRAVITY:
|
|
|
|
aff = createGravityAffector();
|
|
|
|
break;
|
|
|
|
case EPAT_NONE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++idx;
|
|
|
|
|
|
|
|
if (aff)
|
|
|
|
{
|
|
|
|
idx = aff->deserializeAttributes(idx, in, options);
|
|
|
|
++idx;
|
|
|
|
addAffector(aff);
|
|
|
|
aff->drop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
|