source: OGRE/trunk/ogrenew/Tools/MayaExport/include/particles.h @ 692

Revision 692, 7.1 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/****************************************************************
2 Thanks to Bandures for the particle exporter
3 ****************************************************************/
4
5#ifndef _PARTICLES_H
6#define _PARTICLES_H
7
8#include <math.h>
9#include <vector>
10#include <hash_map>
11#include <maya/MDagPath.h>
12#include "paramList.h"
13#include "mayaExportLayer.h"
14#pragma warning(disable: 4996)
15
16namespace OgreMayaExporter
17{
18////////////////////////////////////////////////////////////////////////////////////////////////////
19inline float fabs( float fVal ) { return ::fabs( fVal ); }
20////////////////////////////////////////////////////////////////////////////////////////////////////
21struct SPos
22{
23        float x;
24        float y;
25        float z;
26
27        SPos(): x(0), y(0), z(0) {}
28        SPos( float _x, float _y, float _z ): x(_x), y(_y), z(_z) {}
29};
30inline const SPos operator-( const SPos &in ) { return SPos( -in.x, -in.y, -in.z ); }
31inline const SPos operator+( const SPos &in1, const SPos &in2 ) { return SPos( in1.x + in2.x, in1.y + in2.y, in1.z + in2.z ); }
32inline const SPos operator-( const SPos &in1, const SPos &in2 ) { return SPos( in1.x - in2.x, in1.y - in2.y, in1.z - in2.z ); }
33inline float fabs2( const SPos &in ) { return in.x * in.x + in.y * in.y + in.z * in.z; }
34inline float fabs( const SPos &in ) { return float( sqrt( fabs2( in ) ) ); }
35////////////////////////////////////////////////////////////////////////////////////////////////////
36struct SColor
37{
38        union
39        {
40                struct
41                {
42                        float x, y, z, w;
43                };
44                struct
45                {
46                        float r, g, b, a;
47                };
48        };
49
50        SColor(): r(0), g(0), b(0), a(0) {}
51        SColor( float _r, float _g, float _b, float _a ): r(_r), g(_g), b(_b), a(_a) {}
52};
53inline const SColor operator-( const SColor &in1) { return SColor( -in1.x, -in1.y, -in1.z, -in1.w ); }
54inline const SColor operator+( const SColor &in1, const SColor &in2 ) { return SColor( in1.x + in2.x, in1.y + in2.y, in1.z + in2.z, in1.w + in2.w ); }
55inline const SColor operator-( const SColor &in1, const SColor &in2 ) { return SColor( in1.x - in2.x, in1.y - in2.y, in1.z - in2.z, in1.w - in2.w ); }
56inline float fabs2( const SColor &in ) { return in.x * in.x + in.y * in.y + in.z * in.z + in.w * in.w; }
57inline float fabs( const SColor &in ) { return float( sqrt( fabs2( in ) ) ); }
58////////////////////////////////////////////////////////////////////////////////////////////////////
59struct SScale
60{
61        float x;
62        float y;
63
64        SScale(): x(0), y(0) {}
65        SScale( float _x, float _y ): x(_x), y(_y) {}
66};
67inline const SScale operator+( const SScale &in1, const SScale &in2 ) { return SScale( in1.x + in2.x, in1.y + in2.y ); }
68inline const SScale operator-( const SScale &in1, const SScale &in2 ) { return SScale( in1.x - in2.x, in1.y - in2.y ); }
69inline float fabs2( const SScale &in ) { return in.x * in.x + in.y * in.y; }
70inline float fabs( const SScale &in ) { return float( sqrt( fabs2( in ) ) ); }
71////////////////////////////////////////////////////////////////////////////////////////////////////
72struct SParticleData
73{
74        int nFrame;
75        int nSprite;
76        SPos pos;
77        SColor color;
78        SScale scale;
79        float fRotation;
80        ////
81        SParticleData(): nFrame( 0 ), nSprite( 0 ), pos( 0, 0, 0 ), color( 1, 1, 1, 1 ), scale( 1, 1 ), fRotation( 0 ) {}
82};
83typedef std::vector<SParticleData> CParticlesTrack;
84typedef std::hash_map<int, CParticlesTrack> CParticlesData;
85////////////////////////////////////////////////////////////////////////////////////////////////////
86template <class T>
87inline void Interpolate( const T &v1, const T &v2, float fCoeff, T *pRes )
88{
89        pRes->Interpolate( v1, v2, fCoeff );
90}
91////////////////////////////////////////////////////////////////////////////////////////////////////
92inline void Interpolate( const int &v1, const int &v2, float fCoeff, int *pRes )
93{
94        *pRes = v1;
95}
96////////////////////////////////////////////////////////////////////////////////////////////////////
97inline void Interpolate( const float &v1, const float &v2, float fCoeff, float *pRes )
98{
99        *pRes = ( 1 - fCoeff ) * v1 + fCoeff * v2;
100}
101////////////////////////////////////////////////////////////////////////////////////////////////////
102inline void Interpolate( const SPos &v1, const SPos &v2, float fCoeff, SPos *pRes )
103{
104        Interpolate( v1.x, v2.x, fCoeff, &pRes->x );
105        Interpolate( v1.y, v2.y, fCoeff, &pRes->y );
106        Interpolate( v1.z, v2.z, fCoeff, &pRes->z );
107}
108////////////////////////////////////////////////////////////////////////////////////////////////////
109inline void Interpolate( const SColor &v1, const SColor &v2, float fCoeff, SColor *pRes )
110{
111        Interpolate( v1.r, v2.r, fCoeff, &pRes->r );
112        Interpolate( v1.g, v2.g, fCoeff, &pRes->g );
113        Interpolate( v1.b, v2.b, fCoeff, &pRes->b );
114        Interpolate( v1.a, v2.a, fCoeff, &pRes->a );
115}
116////////////////////////////////////////////////////////////////////////////////////////////////////
117inline void Interpolate( const SScale &v1, const SScale &v2, float fCoeff, SScale *pRes )
118{
119        Interpolate( v1.x, v2.x, fCoeff, &pRes->x );
120        Interpolate( v1.y, v2.y, fCoeff, &pRes->y );
121}
122////////////////////////////////////////////////////////////////////////////////////////////////////
123template <class T>
124class TKey
125{
126public:
127        T value;
128        int nTime;
129};
130////////////////////////////////////////////////////////////////////////////////////////////////////
131template <class T>
132class TKeyTrack
133{
134public:
135        std::vector<TKey<T> > keys;
136
137protected:
138        void GetValueBinSearch( float fTime, T *pRes ) const
139        {
140                int nLeft = 0, nRight = keys.size() - 1;
141                int nTime = int( fTime - 0.5f );
142                while( nLeft - nRight > 1 )
143                {
144                        int nTemp = ( nLeft + nRight ) / 2;
145                        if ( keys[nTemp].nTime <= nTime )
146                                nLeft = nTemp;
147                        else
148                                nRight = nTemp;
149                }
150                ////
151                const TKey<T> &end = keys[nRight];
152                const TKey<T> &start = keys[nLeft];
153                float fCoeff = ( fTime - start.nTime ) / ( end.nTime - start.nTime );
154                Interpolate( start.value, end.value, fCoeff, pRes );
155        }
156
157public:
158        void GetValue( float fTime, T *pRes ) const
159        {
160                if ( keys.size() == 1 )
161                        *pRes = keys[0].value;
162                else
163                        GetValueBinSearch( fTime, pRes );
164        }
165};
166////////////////////////////////////////////////////////////////////////////////////////////////////
167struct SParticle
168{
169        int nEndTime;
170        int nStartTime;
171        TKeyTrack<int> sprite;
172        TKeyTrack<SPos> pos;
173        TKeyTrack<SColor> color;
174        TKeyTrack<SScale> scale;
175        TKeyTrack<float> rotation;
176};
177////////////////////////////////////////////////////////////////////////////////////////////////////
178// Particles
179////////////////////////////////////////////////////////////////////////////////////////////////////
180class Particles
181{
182private:
183        CParticlesData data;
184        ////
185        int nFrames;
186        std::vector<SParticle> particleTracks;
187
188protected:
189        MStatus ExportFrame( MDagPath &dagPath, int nFrame );
190        MStatus FinalizeData( int nMinFrame, int nMaxFrame );
191
192public:
193        Particles();
194        virtual ~Particles();
195
196        MStatus load( MDagPath& dagPath, ParamList& params );
197        MStatus writeToXML( ParamList& params );
198        void clear();
199};
200////////////////////////////////////////////////////////////////////////////////////////////////////
201}; // end of namespace
202////////////////////////////////////////////////////////////////////////////////////////////////////
203#endif
Note: See TracBrowser for help on using the repository browser.