vrshoot

view libs/assimp/OgreImporter.hpp @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
line source
1 #include "BaseImporter.h"
3 #include <vector>
5 #include "OgreXmlHelper.hpp"
6 #include "irrXMLWrapper.h"
8 /// Ogre Importer TODO
9 /* - Read Vertex Colors
10 - Read multiple TexCoords
11 */
15 namespace Assimp
16 {
17 namespace Ogre
18 {
21 //Forward declarations:
22 struct Face;
23 struct Weight;
24 struct Bone;
25 struct Animation;
26 struct Track;
27 struct Keyframe;
29 ///A submesh from Ogre
30 struct SubMesh
31 {
32 bool SharedData;
34 std::string Name;
35 std::string MaterialName;
36 std::vector<Face> FaceList;
38 std::vector<aiVector3D> Positions; bool HasPositions;
39 std::vector<aiVector3D> Normals; bool HasNormals;
40 std::vector<aiVector3D> Tangents; bool HasTangents;
41 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)
43 std::vector< std::vector<Weight> > Weights;//a list(inner) of bones for each vertex(outer)
44 int MaterialIndex;///< The Index in the Assimp Materialarray from the material witch is attached to this submesh
45 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)
47 SubMesh(): SharedData(false), HasPositions(false), HasNormals(false), HasTangents(false),
48 MaterialIndex(-1), BonesUsed(0) {}//initialize everything
49 };
52 ///The Main Ogre Importer Class
53 class OgreImporter : public BaseImporter
54 {
55 public:
56 virtual bool CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const;
57 virtual void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler);
58 virtual const aiImporterDesc* GetInfo () const;
59 virtual void SetupProperties(const Importer* pImp);
60 private:
63 //-------------------------------- OgreMesh.cpp -------------------------------
64 /// Helper Functions to read parts of the XML File
65 void ReadSubMesh(SubMesh& theSubMesh, XmlReader* Reader);//the submesh reference is the result value
67 /// Reads a single Vertexbuffer and writes its data in the Submesh
68 static void ReadVertexBuffer(SubMesh &theSubMesh, XmlReader *Reader, unsigned int NumVertices);
70 /// Reads bone weights are stores them into the given submesh
71 static void ReadBoneWeights(SubMesh &theSubMesh, XmlReader *Reader);
73 /// After Loading a SubMehs some work needs to be done (make all Vertexes unique, normalize weights)
74 static void ProcessSubMesh(SubMesh &theSubMesh, SubMesh &theSharedGeometry);
76 /// Uses the bone data to convert a SubMesh into a aiMesh which will be created and returned
77 aiMesh* CreateAssimpSubMesh(const SubMesh &theSubMesh, const std::vector<Bone>& Bones) const;
80 //-------------------------------- OgreSkeleton.cpp -------------------------------
81 /// Writes the results in Bones and Animations, Filename is not const, because its call-by-value and the function will change it!
82 void LoadSkeleton(std::string FileName, std::vector<Bone> &Bones, std::vector<Animation> &Animations) const;
84 /// Converts the animations in aiAnimations and puts them into the scene
85 void PutAnimationsInScene(const std::vector<Bone> &Bones, const std::vector<Animation> &Animations);
87 /// Creates the aiskeleton in current scene
88 void CreateAssimpSkeleton(const std::vector<Bone> &Bones, const std::vector<Animation> &Animations);
90 /// Recursivly creates a filled aiNode from a given root bone
91 static aiNode* CreateAiNodeFromBone(int BoneId, const std::vector<Bone> &Bones, aiNode* ParentNode);
94 //-------------------------------- OgreMaterial.cpp -------------------------------
95 aiMaterial* LoadMaterial(const std::string MaterialName) const;
96 void ReadTechnique(std::stringstream &ss, aiMaterial* NewMaterial) const;
101 //Now we don't have to give theses parameters to all functions
102 std::string m_CurrentFilename;
103 std::string m_MaterialLibFilename;
104 bool m_TextureTypeFromFilename;
105 IOSystem* m_CurrentIOHandler;
106 aiScene *m_CurrentScene;
107 SubMesh m_SharedGeometry;///< we will just use the vertexbuffers of the submesh
108 };
110 ///For the moment just triangles, no other polygon types!
111 struct Face
112 {
113 unsigned int VertexIndices[3];
114 };
116 struct BoneAssignment
117 {
118 unsigned int BoneId;//this is, what we get from ogre
119 std::string BoneName;//this is, what we need for assimp
120 };
122 ///for a vertex->bone structur
123 struct Weight
124 {
125 unsigned int BoneId;
126 float Value;
127 };
130 /// Helper Class to describe an ogre-bone for the skeleton:
131 /** 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!*/
132 struct Bone
133 {
134 int Id;
135 int ParentId;
136 std::string Name;
137 aiVector3D Position;
138 float RotationAngle;
139 aiVector3D RotationAxis;
140 std::vector<int> Children;
141 aiMatrix4x4 BoneToWorldSpace;
143 ///ctor
144 Bone(): Id(-1), ParentId(-1), RotationAngle(0.0f) {}
145 ///this operator is needed to sort the bones after Id's
146 bool operator<(const Bone& rval) const
147 {return Id<rval.Id; }
148 ///this operator is needed to find a bone by its name in a vector<Bone>
149 bool operator==(const std::string& rval) const
150 {return Name==rval; }
151 bool operator==(const aiString& rval) const
152 {return Name==std::string(rval.data); }
154 // implemented in OgreSkeleton.cpp
155 void CalculateBoneToWorldSpaceMatrix(std::vector<Bone>& Bones);
156 };
160 ///Describes an Ogre Animation
161 struct Animation
162 {
163 std::string Name;
164 float Length;
165 std::vector<Track> Tracks;
166 };
168 ///a track (keyframes for one bone) from an animation
169 struct Track
170 {
171 std::string BoneName;
172 std::vector<Keyframe> Keyframes;
173 };
175 /// keyframe (bone transformation) from a track from a animation
176 struct Keyframe
177 {
178 float Time;
179 aiVector3D Position;
180 aiQuaternion Rotation;
181 aiVector3D Scaling;
182 };
184 }//namespace Ogre
185 }//namespace Assimp