1
// Copyright (C) 2002-2011 Nikolaus Gebhardt
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
5
#include "CParticleAnimatedMeshSceneNodeEmitter.h"
6
#include "IAnimatedMeshSceneNode.h"
16
CParticleAnimatedMeshSceneNodeEmitter::CParticleAnimatedMeshSceneNodeEmitter(
17
IAnimatedMeshSceneNode* node, bool useNormalDirection,
18
const core::vector3df& direction, f32 normalDirectionModifier,
19
s32 mbNumber, bool everyMeshVertex,
20
u32 minParticlesPerSecond, u32 maxParticlesPerSecond,
21
const video::SColor& minStartColor, const video::SColor& maxStartColor,
22
u32 lifeTimeMin, u32 lifeTimeMax, s32 maxAngleDegrees,
23
const core::dimension2df& minStartSize, const core::dimension2df& maxStartSize )
24
: Node(0), AnimatedMesh(0), BaseMesh(0), TotalVertices(0), MBCount(0), MBNumber(mbNumber),
25
Direction(direction), NormalDirectionModifier(normalDirectionModifier),
26
MinParticlesPerSecond(minParticlesPerSecond), MaxParticlesPerSecond(maxParticlesPerSecond),
27
MinStartColor(minStartColor), MaxStartColor(maxStartColor),
28
MinLifeTime(lifeTimeMin), MaxLifeTime(lifeTimeMax),
29
MaxStartSize(maxStartSize), MinStartSize(minStartSize),
30
Time(0), Emitted(0), MaxAngleDegrees(maxAngleDegrees),
31
EveryMeshVertex(everyMeshVertex), UseNormalDirection(useNormalDirection)
34
setDebugName("CParticleAnimatedMeshSceneNodeEmitter");
36
setAnimatedMeshSceneNode(node);
40
//! Prepares an array with new particles to emitt into the system
41
//! and returns how much new particles there are.
42
s32 CParticleAnimatedMeshSceneNodeEmitter::emitt(u32 now, u32 timeSinceLastCall, SParticle*& outArray)
44
Time += timeSinceLastCall;
46
const u32 pps = (MaxParticlesPerSecond - MinParticlesPerSecond);
47
const f32 perSecond = pps ? ((f32)MinParticlesPerSecond + os::Randomizer::frand() * pps) : MinParticlesPerSecond;
48
const f32 everyWhatMillisecond = 1000.0f / perSecond;
50
if(Time > everyWhatMillisecond)
52
Particles.set_used(0);
53
u32 amount = (u32)((Time / everyWhatMillisecond) + 0.5f);
57
if(amount > MaxParticlesPerSecond * 2)
58
amount = MaxParticlesPerSecond * 2;
60
// Get Mesh for this frame
61
IMesh* frameMesh = AnimatedMesh->getMesh( core::floor32(Node->getFrameNr()),
62
255, Node->getStartFrame(), Node->getEndFrame() );
63
for(u32 i=0; i<amount; ++i)
67
for( u32 j=0; j<frameMesh->getMeshBufferCount(); ++j )
69
for( u32 k=0; k<frameMesh->getMeshBuffer(j)->getVertexCount(); ++k )
71
p.pos = frameMesh->getMeshBuffer(j)->getPosition(k);
72
if( UseNormalDirection )
73
p.vector = frameMesh->getMeshBuffer(j)->getNormal(k) /
74
NormalDirectionModifier;
82
core::vector3df tgt = p.vector;
83
tgt.rotateXYBy(os::Randomizer::frand() * MaxAngleDegrees);
84
tgt.rotateYZBy(os::Randomizer::frand() * MaxAngleDegrees);
85
tgt.rotateXZBy(os::Randomizer::frand() * MaxAngleDegrees);
89
p.endTime = now + MinLifeTime;
90
if (MaxLifeTime != MinLifeTime)
91
p.endTime += os::Randomizer::rand() % (MaxLifeTime - MinLifeTime);
93
if (MinStartColor==MaxStartColor)
94
p.color=MinStartColor;
96
p.color = MinStartColor.getInterpolated(MaxStartColor, os::Randomizer::frand());
98
p.startColor = p.color;
99
p.startVector = p.vector;
101
if (MinStartSize==MaxStartSize)
102
p.startSize = MinStartSize;
104
p.startSize = MinStartSize.getInterpolated(MaxStartSize, os::Randomizer::frand());
105
p.size = p.startSize;
107
Particles.push_back(p);
115
randomMB = os::Randomizer::rand() % MBCount;
119
u32 vertexNumber = frameMesh->getMeshBuffer(randomMB)->getVertexCount();
122
vertexNumber = os::Randomizer::rand() % vertexNumber;
124
p.pos = frameMesh->getMeshBuffer(randomMB)->getPosition(vertexNumber);
125
if( UseNormalDirection )
126
p.vector = frameMesh->getMeshBuffer(randomMB)->getNormal(vertexNumber) /
127
NormalDirectionModifier;
129
p.vector = Direction;
133
if( MaxAngleDegrees )
135
core::vector3df tgt = Direction;
136
tgt.rotateXYBy(os::Randomizer::frand() * MaxAngleDegrees);
137
tgt.rotateYZBy(os::Randomizer::frand() * MaxAngleDegrees);
138
tgt.rotateXZBy(os::Randomizer::frand() * MaxAngleDegrees);
142
p.endTime = now + MinLifeTime;
143
if (MaxLifeTime != MinLifeTime)
144
p.endTime += os::Randomizer::rand() % (MaxLifeTime - MinLifeTime);
146
if (MinStartColor==MaxStartColor)
147
p.color=MinStartColor;
149
p.color = MinStartColor.getInterpolated(MaxStartColor, os::Randomizer::frand());
151
p.startColor = p.color;
152
p.startVector = p.vector;
154
if (MinStartSize==MaxStartSize)
155
p.startSize = MinStartSize;
157
p.startSize = MinStartSize.getInterpolated(MaxStartSize, os::Randomizer::frand());
158
p.size = p.startSize;
160
Particles.push_back(p);
164
outArray = Particles.pointer();
166
return Particles.size();
173
//! Set Mesh to emit particles from
174
void CParticleAnimatedMeshSceneNodeEmitter::setAnimatedMeshSceneNode( IAnimatedMeshSceneNode* node )
180
VertexPerMeshBufferList.clear();
186
AnimatedMesh = node->getMesh();
187
BaseMesh = AnimatedMesh->getMesh(0);
189
MBCount = BaseMesh->getMeshBufferCount();
190
VertexPerMeshBufferList.reallocate(MBCount);
191
for( u32 i = 0; i < MBCount; ++i )
193
VertexPerMeshBufferList.push_back( BaseMesh->getMeshBuffer(i)->getVertexCount() );
194
TotalVertices += BaseMesh->getMeshBuffer(i)->getVertexCount();
198
} // end namespace scene
199
} // end namespace irr