OgreImporter.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "BaseImporter.h"
  2. #include <vector>
  3. #include "OgreXmlHelper.h"
  4. #include "irrXMLWrapper.h"
  5. namespace Assimp
  6. {
  7. namespace Ogre
  8. {
  9. //Forward declarations:
  10. struct SubMesh;
  11. struct Face;
  12. struct Weight;
  13. struct Bone;
  14. struct Animation;
  15. struct Track;
  16. struct Keyframe;
  17. ///The Main Ogre Importer Class
  18. class OgreImporter : public BaseImporter
  19. {
  20. public:
  21. virtual bool CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const;
  22. virtual void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler);
  23. virtual void GetExtensionList(std::set<std::string>& extensions);
  24. virtual void SetupProperties(const Importer* pImp);
  25. private:
  26. /// Helper Functions to read parts of the XML File
  27. void ReadSubMesh(SubMesh& theSubMesh, XmlReader* Reader);//the submesh reference is the result value
  28. /// writes the results in Bones and Animations, Filename is not const, because its call-by-value and the function will change it!
  29. void LoadSkeleton(std::string FileName, std::vector<Bone> &Bones, std::vector<Animation> &Animations) const;
  30. /// converts the animations in aiAnimations and puts them into the scene
  31. void PutAnimationsInScene(const std::vector<Bone> &Bones, const std::vector<Animation> &Animations);
  32. /// uses the bone data to convert a SubMesh into a aiMesh which will be created and returned
  33. aiMesh* CreateAssimpSubMesh(const SubMesh &theSubMesh, const std::vector<Bone>& Bones) const;
  34. //creates the aiskeleton in current scene
  35. void CreateAssimpSkeleton(const std::vector<Bone> &Bones, const std::vector<Animation> &Animations);
  36. aiMaterial* LoadMaterial(const std::string MaterialName) const;
  37. ///Recursivly creates a filled aiNode from a given root bone
  38. aiNode* CreateAiNodeFromBone(int BoneId, const std::vector<Bone> &Bones, aiNode* ParentNode) const;
  39. //Now we don't have to give theses parameters to all functions
  40. std::string m_CurrentFilename;
  41. std::string m_MaterialLibFilename;
  42. IOSystem* m_CurrentIOHandler;
  43. aiScene *m_CurrentScene;
  44. };
  45. ///A submesh from Ogre
  46. struct SubMesh
  47. {
  48. std::string Name;
  49. std::string MaterialName;
  50. std::vector<Face> FaceList;
  51. std::vector<aiVector3D> Positions; bool HasPositions;
  52. std::vector<aiVector3D> Normals; bool HasNormals;
  53. std::vector<aiVector3D> Uvs; unsigned int NumUvs;//nearly always 2d, but assimp has always 3d texcoords
  54. std::vector< std::vector<Weight> > Weights;//a list of bones for each vertex
  55. int MaterialIndex;///< The Index in the Assimp Materialarray from the material witch is attached to this submesh
  56. unsigned int BonesUsed;//the highest index of a bone from a bone weight, this is needed to create the assimp bone structur (converting from Vertex-Bones to Bone-Vertices)
  57. SubMesh(): HasPositions(false), HasNormals(false), NumUvs(0), MaterialIndex(-1), BonesUsed(0) {}//initialize everything
  58. };
  59. ///For the moment just triangles, no other polygon types!
  60. struct Face
  61. {
  62. unsigned int VertexIndices[3];
  63. };
  64. struct BoneAssignment
  65. {
  66. unsigned int BoneId;//this is, what we get from ogre
  67. std::string BoneName;//this is, what we need for assimp
  68. };
  69. ///for a vertex->bone structur
  70. struct Weight
  71. {
  72. unsigned int BoneId;
  73. float Value;
  74. };
  75. /// Helper Class to describe an ogre-bone for the skeleton:
  76. /** All Id's are signed ints, because than we have -1 as a simple INVALID_ID Value (we start from 0 so 0 is a valid bone ID!*/
  77. struct Bone
  78. {
  79. int Id;
  80. int ParentId;
  81. std::string Name;
  82. aiVector3D Position;
  83. float RotationAngle;
  84. aiVector3D RotationAxis;
  85. std::vector<int> Children;
  86. aiMatrix4x4 BoneToWorldSpace;
  87. ///ctor
  88. Bone(): Id(-1), ParentId(-1), RotationAngle(0.0f) {}
  89. ///this operator is needed to sort the bones after Id's
  90. bool operator<(const Bone& rval) const
  91. {return Id<rval.Id; }
  92. ///this operator is needed to find a bone by its name in a vector<Bone>
  93. bool operator==(const std::string& rval) const
  94. {return Name==rval; }
  95. bool operator==(const aiString& rval) const
  96. {return Name==std::string(rval.data); }
  97. void CalculateBoneToWorldSpaceMatrix(std::vector<Bone>& Bones);
  98. };
  99. ///Describes an Ogre Animation
  100. struct Animation
  101. {
  102. std::string Name;
  103. float Length;
  104. std::vector<Track> Tracks;
  105. };
  106. ///a track (keyframes for one bone) from an animation
  107. struct Track
  108. {
  109. std::string BoneName;
  110. std::vector<Keyframe> Keyframes;
  111. };
  112. /// keyframe (bone transformation) from a track from a animation
  113. struct Keyframe
  114. {
  115. float Time;
  116. aiVector3D Position;
  117. aiQuaternion Rotation;
  118. aiVector3D Scaling;
  119. };
  120. }//namespace Ogre
  121. }//namespace Assimp