Irrlicht 3D Engine
IAnimatedMeshMD3.h
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012 Nikolaus Gebhardt / Thomas Alten
00002 // This file is part of the "Irrlicht Engine".
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
00004 
00005 #ifndef __I_ANIMATED_MESH_MD3_H_INCLUDED__
00006 #define __I_ANIMATED_MESH_MD3_H_INCLUDED__
00007 
00008 #include "IAnimatedMesh.h"
00009 #include "IQ3Shader.h"
00010 #include "quaternion.h"
00011 
00012 namespace irr
00013 {
00014 namespace scene
00015 {
00016 
00017     enum eMD3Models
00018     {
00019         EMD3_HEAD = 0,
00020         EMD3_UPPER,
00021         EMD3_LOWER,
00022         EMD3_WEAPON,
00023         EMD3_NUMMODELS
00024     };
00025 
00027     enum EMD3_ANIMATION_TYPE
00028     {
00029         // Animations for both lower and upper parts of the player
00030         EMD3_BOTH_DEATH_1 = 0,
00031         EMD3_BOTH_DEAD_1,
00032         EMD3_BOTH_DEATH_2,
00033         EMD3_BOTH_DEAD_2,
00034         EMD3_BOTH_DEATH_3,
00035         EMD3_BOTH_DEAD_3,
00036 
00037         // Animations for the upper part
00038         EMD3_TORSO_GESTURE,
00039         EMD3_TORSO_ATTACK_1,
00040         EMD3_TORSO_ATTACK_2,
00041         EMD3_TORSO_DROP,
00042         EMD3_TORSO_RAISE,
00043         EMD3_TORSO_STAND_1,
00044         EMD3_TORSO_STAND_2,
00045 
00046         // Animations for the lower part
00047         EMD3_LEGS_WALK_CROUCH,
00048         EMD3_LEGS_WALK,
00049         EMD3_LEGS_RUN,
00050         EMD3_LEGS_BACK,
00051         EMD3_LEGS_SWIM,
00052         EMD3_LEGS_JUMP_1,
00053         EMD3_LEGS_LAND_1,
00054         EMD3_LEGS_JUMP_2,
00055         EMD3_LEGS_LAND_2,
00056         EMD3_LEGS_IDLE,
00057         EMD3_LEGS_IDLE_CROUCH,
00058         EMD3_LEGS_TURN,
00059 
00061         EMD3_ANIMATION_COUNT
00062     };
00063 
00064     struct SMD3AnimationInfo
00065     {
00067         s32 first;
00069         s32 num;
00071         s32 looping;
00073         s32 fps;
00074     };
00075 
00076 
00077 // byte-align structures
00078 #include "irrpack.h"
00079 
00081     struct SMD3Header
00082     {
00083         c8  headerID[4];    //id of file, always "IDP3"
00084         s32 Version;    //this is a version number, always 15
00085         s8  fileName[68];   //sometimes left Blank... 65 chars, 32bit aligned == 68 chars
00086         s32 numFrames;  //number of KeyFrames
00087         s32 numTags;    //number of 'tags' per frame
00088         s32 numMeshes;  //number of meshes/skins
00089         s32 numMaxSkins;    //maximum number of unique skins used in md3 file. artefact md2
00090         s32 frameStart; //starting position of frame-structur
00091         s32 tagStart;   //starting position of tag-structures
00092         s32 tagEnd;     //ending position of tag-structures/starting position of mesh-structures
00093         s32 fileSize;
00094     } PACK_STRUCT;
00095 
00097     struct SMD3MeshHeader
00098     {
00099         c8 meshID[4];       //id, must be IDP3
00100         c8 meshName[68];    //name of mesh 65 chars, 32 bit aligned == 68 chars
00101 
00102         s32 numFrames;      //number of meshframes in mesh
00103         s32 numShader;      //number of skins in mesh
00104         s32 numVertices;    //number of vertices
00105         s32 numTriangles;   //number of Triangles
00106 
00107         s32 offset_triangles;   //starting position of Triangle data, relative to start of Mesh_Header
00108         s32 offset_shaders; //size of header
00109         s32 offset_st;      //starting position of texvector data, relative to start of Mesh_Header
00110         s32 vertexStart;    //starting position of vertex data,relative to start of Mesh_Header
00111         s32 offset_end;
00112     } PACK_STRUCT;
00113 
00114 
00116     struct SMD3Vertex
00117     {
00118         s16 position[3];
00119         u8 normal[2];
00120     } PACK_STRUCT;
00121 
00123     struct SMD3TexCoord
00124     {
00125         f32 u;
00126         f32 v;
00127     } PACK_STRUCT;
00128 
00130     struct SMD3Face
00131     {
00132         s32 Index[3];
00133     } PACK_STRUCT;
00134 
00135 
00136 // Default alignment
00137 #include "irrunpack.h"
00138 
00140     struct SMD3MeshBuffer : public IReferenceCounted
00141     {
00142         SMD3MeshHeader MeshHeader;
00143 
00144         core::stringc Shader;
00145         core::array < s32 > Indices;
00146         core::array < SMD3Vertex > Vertices;
00147         core::array < SMD3TexCoord > Tex;
00148     };
00149 
00151 
00152     struct SMD3QuaternionTag
00153     {
00154         virtual ~SMD3QuaternionTag()
00155         {
00156             position.X = 0.f;
00157         }
00158 
00159         // construct copy constructor
00160         SMD3QuaternionTag( const SMD3QuaternionTag & copyMe )
00161         {
00162             *this = copyMe;
00163         }
00164 
00165         // construct for searching
00166         SMD3QuaternionTag( const core::stringc& name )
00167             : Name ( name ) {}
00168 
00169         // construct from a position and euler angles in degrees
00170         SMD3QuaternionTag ( const core::vector3df &pos, const core::vector3df &angle )
00171             : position(pos), rotation(angle * core::DEGTORAD) {}
00172 
00173         // set to matrix
00174         void setto ( core::matrix4 &m )
00175         {
00176             rotation.getMatrix ( m, position );
00177         }
00178 
00179         bool operator == ( const SMD3QuaternionTag &other ) const
00180         {
00181             return Name == other.Name;
00182         }
00183 
00184         SMD3QuaternionTag & operator=( const SMD3QuaternionTag & copyMe )
00185         {
00186             Name = copyMe.Name;
00187             position = copyMe.position;
00188             rotation = copyMe.rotation;
00189             return *this;
00190         }
00191 
00192         core::stringc Name;
00193         core::vector3df position;
00194         core::quaternion rotation;
00195     };
00196 
00198     struct SMD3QuaternionTagList
00199     {
00200         SMD3QuaternionTagList()
00201         {
00202             Container.setAllocStrategy(core::ALLOC_STRATEGY_SAFE);
00203         }
00204 
00205         // construct copy constructor
00206         SMD3QuaternionTagList(const SMD3QuaternionTagList& copyMe)
00207         {
00208             *this = copyMe;
00209         }
00210 
00211         virtual ~SMD3QuaternionTagList() {}
00212 
00213         SMD3QuaternionTag* get(const core::stringc& name)
00214         {
00215             SMD3QuaternionTag search ( name );
00216             s32 index = Container.linear_search ( search );
00217             if ( index >= 0 )
00218                 return &Container[index];
00219             return 0;
00220         }
00221 
00222         u32 size () const
00223         {
00224             return Container.size();
00225         }
00226 
00227         void set_used(u32 new_size)
00228         {
00229             s32 diff = (s32) new_size - (s32) Container.allocated_size();
00230             if ( diff > 0 )
00231             {
00232                 SMD3QuaternionTag e("");
00233                 for ( s32 i = 0; i < diff; ++i )
00234                     Container.push_back(e);
00235             }
00236         }
00237 
00238         const SMD3QuaternionTag& operator[](u32 index) const
00239         {
00240             return Container[index];
00241         }
00242 
00243         SMD3QuaternionTag& operator[](u32 index)
00244         {
00245             return Container[index];
00246         }
00247 
00248         void push_back(const SMD3QuaternionTag& other)
00249         {
00250             Container.push_back(other);
00251         }
00252 
00253         SMD3QuaternionTagList& operator = (const SMD3QuaternionTagList & copyMe)
00254         {
00255             Container = copyMe.Container;
00256             return *this;
00257         }
00258 
00259     private:
00260         core::array < SMD3QuaternionTag > Container;
00261     };
00262 
00263 
00265     struct SMD3Mesh: public IReferenceCounted
00266     {
00267         SMD3Mesh ()
00268         {
00269             MD3Header.numFrames = 0;
00270         }
00271 
00272         virtual ~SMD3Mesh()
00273         {
00274             for (u32 i=0; i<Buffer.size(); ++i)
00275                 Buffer[i]->drop();
00276         }
00277 
00278         core::stringc Name;
00279         core::array<SMD3MeshBuffer*> Buffer;
00280         SMD3QuaternionTagList TagList;
00281         SMD3Header MD3Header;
00282     };
00283 
00284 
00286     class IAnimatedMeshMD3 : public IAnimatedMesh
00287     {
00288     public:
00289 
00291         virtual void setInterpolationShift(u32 shift, u32 loopMode) =0;
00292 
00294         virtual SMD3QuaternionTagList* getTagList(s32 frame, s32 detailLevel, s32 startFrameLoop, s32 endFrameLoop) =0;
00295 
00297         virtual SMD3Mesh* getOriginalMesh() =0;
00298     };
00299 
00300 } // end namespace scene
00301 } // end namespace irr
00302 
00303 #endif
00304