OgreImporter.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "BaseImporter.h"
  2. #include <vector>
  3. #include "OgreXmlHelper.hpp"
  4. #include "irrXMLWrapper.h"
  5. /// Ogre Importer TODO
  6. /* - Read Vertex Colors
  7. - Read multiple TexCoords
  8. */
  9. namespace Assimp
  10. {
  11. namespace Ogre
  12. {
  13. //Forward declarations:
  14. struct Face;
  15. struct Weight;
  16. struct Bone;
  17. struct Animation;
  18. struct Track;
  19. struct Keyframe;
  20. ///A submesh from Ogre
  21. struct SubMesh
  22. {
  23. bool SharedData;
  24. std::string Name;
  25. std::string MaterialName;
  26. std::vector<Face> FaceList;
  27. std::vector<aiVector3D> Positions; bool HasPositions;
  28. std::vector<aiVector3D> Normals; bool HasNormals;
  29. std::vector<aiVector3D> Tangents; bool HasTangents;
  30. std::vector<std::vector<aiVector3D> > Uvs;//arbitrary number of texcoords, they are nearly always 2d, but assimp has always 3d texcoords, n vectors(outer) with texcoords for each vertex(inner)
  31. std::vector< std::vector<Weight> > Weights;//a list(inner) of bones for each vertex(outer)
  32. int MaterialIndex;///< The Index in the Assimp Materialarray from the material witch is attached to this submesh
  33. 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)
  34. SubMesh(): SharedData(false), HasPositions(false), HasNormals(false), HasTangents(false),
  35. MaterialIndex(-1), BonesUsed(0) {}//initialize everything
  36. };
  37. ///The Main Ogre Importer Class
  38. class OgreImporter : public BaseImporter
  39. {
  40. public:
  41. virtual bool CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const;
  42. virtual void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler);
  43. virtual const aiImporterDesc* GetInfo () const;
  44. virtual void SetupProperties(const Importer* pImp);
  45. private:
  46. //-------------------------------- OgreMesh.cpp -------------------------------
  47. /// Helper Functions to read parts of the XML File
  48. void ReadSubMesh(SubMesh& theSubMesh, XmlReader* Reader);//the submesh reference is the result value
  49. /// Reads a single Vertexbuffer and writes its data in the Submesh
  50. static void ReadVertexBuffer(SubMesh &theSubMesh, XmlReader *Reader, unsigned int NumVertices);
  51. /// Reads bone weights are stores them into the given submesh
  52. static void ReadBoneWeights(SubMesh &theSubMesh, XmlReader *Reader);
  53. /// After Loading a SubMehs some work needs to be done (make all Vertexes unique, normalize weights)
  54. static void ProcessSubMesh(SubMesh &theSubMesh, SubMesh &theSharedGeometry);
  55. /// Uses the bone data to convert a SubMesh into a aiMesh which will be created and returned
  56. aiMesh* CreateAssimpSubMesh(const SubMesh &theSubMesh, const std::vector<Bone>& Bones) const;
  57. //-------------------------------- OgreSkeleton.cpp -------------------------------
  58. /// Writes the results in Bones and Animations, Filename is not const, because its call-by-value and the function will change it!
  59. void LoadSkeleton(std::string FileName, std::vector<Bone> &Bones, std::vector<Animation> &Animations) const;
  60. /// Converts the animations in aiAnimations and puts them into the scene
  61. void PutAnimationsInScene(const std::vector<Bone> &Bones, const std::vector<Animation> &Animations);
  62. /// Creates the aiskeleton in current scene
  63. void CreateAssimpSkeleton(const std::vector<Bone> &Bones, const std::vector<Animation> &Animations);
  64. /// Recursivly creates a filled aiNode from a given root bone
  65. static aiNode* CreateAiNodeFromBone(int BoneId, const std::vector<Bone> &Bones, aiNode* ParentNode);
  66. //-------------------------------- OgreMaterial.cpp -------------------------------
  67. aiMaterial* LoadMaterial(const std::string MaterialName) const;
  68. void ReadTechnique(std::stringstream &ss, aiMaterial* NewMaterial) const;
  69. //Now we don't have to give theses parameters to all functions
  70. std::string m_CurrentFilename;
  71. std::string m_MaterialLibFilename;
  72. bool m_TextureTypeFromFilename;
  73. IOSystem* m_CurrentIOHandler;
  74. aiScene *m_CurrentScene;
  75. SubMesh m_SharedGeometry;///< we will just use the vertexbuffers of the submesh
  76. };
  77. ///For the moment just triangles, no other polygon types!
  78. struct Face
  79. {
  80. unsigned int VertexIndices[3];
  81. };
  82. struct BoneAssignment
  83. {
  84. unsigned int BoneId;//this is, what we get from ogre
  85. std::string BoneName;//this is, what we need for assimp
  86. };
  87. ///for a vertex->bone structur
  88. struct Weight
  89. {
  90. unsigned int BoneId;
  91. float Value;
  92. };
  93. /// Helper Class to describe an ogre-bone for the skeleton:
  94. /** 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!*/
  95. struct Bone
  96. {
  97. int Id;
  98. int ParentId;
  99. std::string Name;
  100. aiVector3D Position;
  101. float RotationAngle;
  102. aiVector3D RotationAxis;
  103. std::vector<int> Children;
  104. aiMatrix4x4 BoneToWorldSpace;
  105. ///ctor
  106. Bone(): Id(-1), ParentId(-1), RotationAngle(0.0f) {}
  107. ///this operator is needed to sort the bones after Id's
  108. bool operator<(const Bone& rval) const
  109. {return Id<rval.Id; }
  110. ///this operator is needed to find a bone by its name in a vector<Bone>
  111. bool operator==(const std::string& rval) const
  112. {return Name==rval; }
  113. bool operator==(const aiString& rval) const
  114. {return Name==std::string(rval.data); }
  115. // implemented in OgreSkeleton.cpp
  116. void CalculateBoneToWorldSpaceMatrix(std::vector<Bone>& Bones);
  117. };
  118. ///Describes an Ogre Animation
  119. struct Animation
  120. {
  121. std::string Name;
  122. float Length;
  123. std::vector<Track> Tracks;
  124. };
  125. ///a track (keyframes for one bone) from an animation
  126. struct Track
  127. {
  128. std::string BoneName;
  129. std::vector<Keyframe> Keyframes;
  130. };
  131. /// keyframe (bone transformation) from a track from a animation
  132. struct Keyframe
  133. {
  134. float Time;
  135. aiVector3D Position;
  136. aiQuaternion Rotation;
  137. aiVector3D Scaling;
  138. };
  139. }//namespace Ogre
  140. }//namespace Assimp