~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to source/Irrlicht/CParticleAnimatedMeshSceneNodeEmitter.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 
 
5
#include "CParticleAnimatedMeshSceneNodeEmitter.h"
 
6
#include "IAnimatedMeshSceneNode.h"
 
7
#include "IMesh.h"
 
8
#include "os.h"
 
9
 
 
10
namespace irr
 
11
{
 
12
namespace scene
 
13
{
 
14
 
 
15
//! constructor
 
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)
 
32
{
 
33
        #ifdef _DEBUG
 
34
        setDebugName("CParticleAnimatedMeshSceneNodeEmitter");
 
35
        #endif
 
36
        setAnimatedMeshSceneNode(node);
 
37
}
 
38
 
 
39
 
 
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)
 
43
{
 
44
        Time += timeSinceLastCall;
 
45
 
 
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;
 
49
 
 
50
        if(Time > everyWhatMillisecond)
 
51
        {
 
52
                Particles.set_used(0);
 
53
                u32 amount = (u32)((Time / everyWhatMillisecond) + 0.5f);
 
54
                Time = 0;
 
55
                SParticle p;
 
56
 
 
57
                if(amount > MaxParticlesPerSecond * 2)
 
58
                        amount = MaxParticlesPerSecond * 2;
 
59
 
 
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)
 
64
                {
 
65
                        if( EveryMeshVertex )
 
66
                        {
 
67
                                for( u32 j=0; j<frameMesh->getMeshBufferCount(); ++j )
 
68
                                {
 
69
                                        for( u32 k=0; k<frameMesh->getMeshBuffer(j)->getVertexCount(); ++k )
 
70
                                        {
 
71
                                                p.pos = frameMesh->getMeshBuffer(j)->getPosition(k);
 
72
                                                if( UseNormalDirection )
 
73
                                                        p.vector = frameMesh->getMeshBuffer(j)->getNormal(k) /
 
74
                                                                NormalDirectionModifier;
 
75
                                                else
 
76
                                                        p.vector = Direction;
 
77
 
 
78
                                                p.startTime = now;
 
79
 
 
80
                                                if( MaxAngleDegrees )
 
81
                                                {
 
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);
 
86
                                                        p.vector = tgt;
 
87
                                                }
 
88
 
 
89
                                                p.endTime = now + MinLifeTime;
 
90
                                                if (MaxLifeTime != MinLifeTime)
 
91
                                                        p.endTime += os::Randomizer::rand() % (MaxLifeTime - MinLifeTime);
 
92
 
 
93
                                                if (MinStartColor==MaxStartColor)
 
94
                                                        p.color=MinStartColor;
 
95
                                                else
 
96
                                                        p.color = MinStartColor.getInterpolated(MaxStartColor, os::Randomizer::frand());
 
97
 
 
98
                                                p.startColor = p.color;
 
99
                                                p.startVector = p.vector;
 
100
 
 
101
                                                if (MinStartSize==MaxStartSize)
 
102
                                                        p.startSize = MinStartSize;
 
103
                                                else
 
104
                                                        p.startSize = MinStartSize.getInterpolated(MaxStartSize, os::Randomizer::frand());
 
105
                                                p.size = p.startSize;
 
106
 
 
107
                                                Particles.push_back(p);
 
108
                                        }
 
109
                                }
 
110
                        }
 
111
                        else
 
112
                        {
 
113
                                s32 randomMB = 0;
 
114
                                if( MBNumber < 0 )
 
115
                                        randomMB = os::Randomizer::rand() % MBCount;
 
116
                                else
 
117
                                        randomMB = MBNumber;
 
118
 
 
119
                                u32 vertexNumber = frameMesh->getMeshBuffer(randomMB)->getVertexCount();
 
120
                                if (!vertexNumber)
 
121
                                        continue;
 
122
                                vertexNumber = os::Randomizer::rand() % vertexNumber;
 
123
 
 
124
                                p.pos = frameMesh->getMeshBuffer(randomMB)->getPosition(vertexNumber);
 
125
                                if( UseNormalDirection )
 
126
                                        p.vector = frameMesh->getMeshBuffer(randomMB)->getNormal(vertexNumber) /
 
127
                                                NormalDirectionModifier;
 
128
                                else
 
129
                                        p.vector = Direction;
 
130
 
 
131
                                p.startTime = now;
 
132
 
 
133
                                if( MaxAngleDegrees )
 
134
                                {
 
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);
 
139
                                        p.vector = tgt;
 
140
                                }
 
141
 
 
142
                                p.endTime = now + MinLifeTime;
 
143
                                if (MaxLifeTime != MinLifeTime)
 
144
                                        p.endTime += os::Randomizer::rand() % (MaxLifeTime - MinLifeTime);
 
145
 
 
146
                                if (MinStartColor==MaxStartColor)
 
147
                                        p.color=MinStartColor;
 
148
                                else
 
149
                                        p.color = MinStartColor.getInterpolated(MaxStartColor, os::Randomizer::frand());
 
150
 
 
151
                                p.startColor = p.color;
 
152
                                p.startVector = p.vector;
 
153
 
 
154
                                if (MinStartSize==MaxStartSize)
 
155
                                        p.startSize = MinStartSize;
 
156
                                else
 
157
                                        p.startSize = MinStartSize.getInterpolated(MaxStartSize, os::Randomizer::frand());
 
158
                                p.size = p.startSize;
 
159
 
 
160
                                Particles.push_back(p);
 
161
                        }
 
162
                }
 
163
 
 
164
                outArray = Particles.pointer();
 
165
 
 
166
                return Particles.size();
 
167
        }
 
168
 
 
169
        return 0;
 
170
}
 
171
 
 
172
 
 
173
//! Set Mesh to emit particles from
 
174
void CParticleAnimatedMeshSceneNodeEmitter::setAnimatedMeshSceneNode( IAnimatedMeshSceneNode* node )
 
175
{
 
176
        Node = node;
 
177
        AnimatedMesh = 0;
 
178
        BaseMesh = 0;
 
179
        TotalVertices = 0;
 
180
        VertexPerMeshBufferList.clear();
 
181
        if ( !node )
 
182
        {
 
183
                return;
 
184
        }
 
185
 
 
186
        AnimatedMesh = node->getMesh();
 
187
        BaseMesh = AnimatedMesh->getMesh(0);
 
188
 
 
189
        MBCount = BaseMesh->getMeshBufferCount();
 
190
        VertexPerMeshBufferList.reallocate(MBCount);
 
191
        for( u32 i = 0; i < MBCount; ++i )
 
192
        {
 
193
                VertexPerMeshBufferList.push_back( BaseMesh->getMeshBuffer(i)->getVertexCount() );
 
194
                TotalVertices += BaseMesh->getMeshBuffer(i)->getVertexCount();
 
195
        }
 
196
}
 
197
 
 
198
} // end namespace scene
 
199
} // end namespace irr
 
200