vrshoot

diff libs/assimp/OgreImporter.hpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/OgreImporter.hpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,185 @@
     1.4 +#include "BaseImporter.h"
     1.5 +
     1.6 +#include <vector>
     1.7 +
     1.8 +#include "OgreXmlHelper.hpp"
     1.9 +#include "irrXMLWrapper.h"
    1.10 +
    1.11 +/// Ogre Importer TODO
    1.12 +/*	- Read Vertex Colors
    1.13 +	- Read multiple TexCoords
    1.14 +*/
    1.15 +
    1.16 +
    1.17 +
    1.18 +namespace Assimp
    1.19 +{
    1.20 +namespace Ogre
    1.21 +{
    1.22 +
    1.23 +
    1.24 +//Forward declarations:
    1.25 +struct Face;
    1.26 +struct Weight;
    1.27 +struct Bone;
    1.28 +struct Animation;
    1.29 +struct Track;
    1.30 +struct Keyframe;
    1.31 +
    1.32 +///A submesh from Ogre
    1.33 +struct SubMesh
    1.34 +{	
    1.35 +	bool SharedData;
    1.36 +
    1.37 +	std::string Name;
    1.38 +	std::string MaterialName;
    1.39 +	std::vector<Face> FaceList;
    1.40 +
    1.41 +	std::vector<aiVector3D> Positions; bool HasPositions;
    1.42 +	std::vector<aiVector3D> Normals; bool HasNormals;
    1.43 +	std::vector<aiVector3D> Tangents; bool HasTangents;
    1.44 +	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)
    1.45 +
    1.46 +	std::vector< std::vector<Weight> > Weights;//a list(inner) of bones for each vertex(outer)
    1.47 +	int MaterialIndex;///< The Index in the Assimp Materialarray from the material witch is attached to this submesh
    1.48 +	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)
    1.49 +
    1.50 +	SubMesh(): SharedData(false), HasPositions(false), HasNormals(false), HasTangents(false),
    1.51 +		MaterialIndex(-1), BonesUsed(0) {}//initialize everything
    1.52 +};
    1.53 +
    1.54 +
    1.55 +///The Main Ogre Importer Class
    1.56 +class OgreImporter : public BaseImporter
    1.57 +{
    1.58 +public:
    1.59 +	virtual bool CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const;
    1.60 +	virtual void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler);
    1.61 +	virtual const aiImporterDesc* GetInfo () const;
    1.62 +	virtual void SetupProperties(const Importer* pImp);
    1.63 +private:
    1.64 +
    1.65 +
    1.66 +	//-------------------------------- OgreMesh.cpp -------------------------------
    1.67 +	/// Helper Functions to read parts of the XML File
    1.68 +	void ReadSubMesh(SubMesh& theSubMesh, XmlReader* Reader);//the submesh reference is the result value
    1.69 +
    1.70 +	/// Reads a single Vertexbuffer and writes its data in the Submesh
    1.71 +	static void ReadVertexBuffer(SubMesh &theSubMesh, XmlReader *Reader, unsigned int NumVertices);
    1.72 +
    1.73 +	/// Reads bone weights are stores them into the given submesh
    1.74 +	static void ReadBoneWeights(SubMesh &theSubMesh, XmlReader *Reader);
    1.75 +
    1.76 +	/// After Loading a SubMehs some work needs to be done (make all Vertexes unique, normalize weights)
    1.77 +	static void ProcessSubMesh(SubMesh &theSubMesh, SubMesh &theSharedGeometry);
    1.78 +
    1.79 +	/// Uses the bone data to convert a SubMesh into a aiMesh which will be created and returned
    1.80 +	aiMesh* CreateAssimpSubMesh(const SubMesh &theSubMesh, const std::vector<Bone>& Bones) const;
    1.81 +	
    1.82 +
    1.83 +	//-------------------------------- OgreSkeleton.cpp -------------------------------
    1.84 +	/// Writes the results in Bones and Animations, Filename is not const, because its call-by-value and the function will change it!
    1.85 +	void LoadSkeleton(std::string FileName, std::vector<Bone> &Bones, std::vector<Animation> &Animations) const;
    1.86 +
    1.87 +	/// Converts the animations in aiAnimations and puts them into the scene
    1.88 +	void PutAnimationsInScene(const std::vector<Bone> &Bones, const std::vector<Animation> &Animations);
    1.89 +
    1.90 +	/// Creates the aiskeleton in current scene
    1.91 +	void CreateAssimpSkeleton(const std::vector<Bone> &Bones, const std::vector<Animation> &Animations);
    1.92 +
    1.93 +	/// Recursivly creates a filled aiNode from a given root bone
    1.94 +	static aiNode* CreateAiNodeFromBone(int BoneId, const std::vector<Bone> &Bones, aiNode* ParentNode);
    1.95 +	
    1.96 +
    1.97 +	//-------------------------------- OgreMaterial.cpp -------------------------------
    1.98 +	aiMaterial* LoadMaterial(const std::string MaterialName) const;
    1.99 +	void ReadTechnique(std::stringstream &ss, aiMaterial* NewMaterial) const;
   1.100 +	
   1.101 +
   1.102 +
   1.103 +
   1.104 +	//Now we don't have to give theses parameters to all functions
   1.105 +	std::string m_CurrentFilename;
   1.106 +	std::string m_MaterialLibFilename;
   1.107 +	bool m_TextureTypeFromFilename;
   1.108 +	IOSystem* m_CurrentIOHandler;
   1.109 +	aiScene *m_CurrentScene;
   1.110 +	SubMesh m_SharedGeometry;///< we will just use the vertexbuffers of the submesh
   1.111 +};
   1.112 +
   1.113 +///For the moment just triangles, no other polygon types!
   1.114 +struct Face
   1.115 +{
   1.116 +	unsigned int VertexIndices[3];
   1.117 +};
   1.118 +
   1.119 +struct BoneAssignment
   1.120 +{
   1.121 +	unsigned int BoneId;//this is, what we get from ogre
   1.122 +	std::string BoneName;//this is, what we need for assimp
   1.123 +};
   1.124 +
   1.125 +///for a vertex->bone structur
   1.126 +struct Weight
   1.127 +{
   1.128 +	unsigned int BoneId;
   1.129 +	float Value;
   1.130 +};
   1.131 +
   1.132 +
   1.133 +/// Helper Class to describe an ogre-bone for the skeleton:
   1.134 +/** 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!*/
   1.135 +struct Bone
   1.136 +{
   1.137 +	int Id;
   1.138 +	int ParentId;
   1.139 +	std::string Name;
   1.140 +	aiVector3D Position;
   1.141 +	float RotationAngle;
   1.142 +	aiVector3D RotationAxis;
   1.143 +	std::vector<int> Children;
   1.144 +	aiMatrix4x4 BoneToWorldSpace;
   1.145 +
   1.146 +	///ctor
   1.147 +	Bone(): Id(-1), ParentId(-1), RotationAngle(0.0f) {}
   1.148 +	///this operator is needed to sort the bones after Id's
   1.149 +	bool operator<(const Bone& rval) const
   1.150 +		{return Id<rval.Id; }
   1.151 +	///this operator is needed to find a bone by its name in a vector<Bone>
   1.152 +	bool operator==(const std::string& rval) const
   1.153 +		{return Name==rval; }
   1.154 +	bool operator==(const aiString& rval) const
   1.155 +	{return Name==std::string(rval.data); }
   1.156 +
   1.157 +	// implemented in OgreSkeleton.cpp
   1.158 +	void CalculateBoneToWorldSpaceMatrix(std::vector<Bone>& Bones);
   1.159 +};
   1.160 +
   1.161 +
   1.162 +
   1.163 +///Describes an Ogre Animation
   1.164 +struct Animation
   1.165 +{
   1.166 +	std::string Name;
   1.167 +	float Length;
   1.168 +	std::vector<Track> Tracks;
   1.169 +};
   1.170 +
   1.171 +///a track (keyframes for one bone) from an animation
   1.172 +struct Track
   1.173 +{
   1.174 +	std::string BoneName;
   1.175 +	std::vector<Keyframe> Keyframes;
   1.176 +};
   1.177 +
   1.178 +/// keyframe (bone transformation) from a track from a animation
   1.179 +struct Keyframe
   1.180 +{
   1.181 +	float Time;
   1.182 +	aiVector3D Position;
   1.183 +	aiQuaternion Rotation;
   1.184 +	aiVector3D Scaling;
   1.185 +};
   1.186 +
   1.187 +}//namespace Ogre
   1.188 +}//namespace Assimp