FBXReader.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #pragma once
  2. #ifndef BF_NO_FBX
  3. #define FBXSDK_SHARED
  4. #include "Common.h"
  5. #include "fbxsdk.h"
  6. #include "Util/Vector.h"
  7. #include "Util/Matrix4.h"
  8. #include "Util/Quaternion.h"
  9. NS_BF_BEGIN;
  10. #define BF_MAX_NUM_BONES 256
  11. #define BF_MODEL_VERSION 1
  12. struct FBXBoneWeight
  13. {
  14. public:
  15. int mBoneIdx;
  16. float mBoneWeight;
  17. };
  18. typedef std::vector<FBXBoneWeight> BoneWeightVector;
  19. class FBXVertexData
  20. {
  21. public:
  22. Vector3 mCoords;
  23. uint32 mColor;
  24. std::vector<TexCoords> mTexCoords;
  25. Vector3 mNormal;
  26. std::vector<TexCoords> mBumpTexCoords;
  27. Vector3 mTangent;
  28. BoneWeightVector mBoneWeights;
  29. bool operator==(const FBXVertexData& check) const
  30. {
  31. if (mCoords != check.mCoords)
  32. return false;
  33. if (mNormal != check.mNormal)
  34. return false;
  35. if (mTexCoords.size() != check.mTexCoords.size())
  36. return false;
  37. for (int i = 0; i < (int)mTexCoords.size(); i++)
  38. if ((mTexCoords[i].mU != check.mTexCoords[i].mU) ||
  39. (mTexCoords[i].mV != check.mTexCoords[i].mV))
  40. return false;
  41. return true;
  42. }
  43. };
  44. class FBXMaterial
  45. {
  46. public:
  47. String mTexFileName;
  48. String mBumpFileName;
  49. };
  50. class FBXMesh
  51. {
  52. public:
  53. FBXMaterial mMaterial;
  54. String mName;
  55. std::vector<FBXVertexData> mVertexData;
  56. std::vector<int> mIndexData;
  57. };
  58. struct FBXJoint
  59. {
  60. String name;
  61. int id;
  62. FbxNode *pNode;
  63. FbxAMatrix globalBindPose, localBindPose;
  64. FbxAMatrix bindPose;
  65. Matrix4 mCurMatrix;
  66. float mBoneLength; // Length to parent
  67. int parentIndex;
  68. double posx, posy, posz;
  69. //double angle;
  70. //double axisx,axisy,axisz;
  71. double quatw, quatx, quaty, quatz;
  72. float scalex, scaley, scalez;
  73. bool bInheritScale;
  74. // Used when loading from an Ogre Skeleton.
  75. String parentName;
  76. };
  77. enum FBXTrackType
  78. {
  79. TT_SKELETON,
  80. TT_MORPH,
  81. TT_POSE
  82. };
  83. enum FBXTarget
  84. {
  85. T_MESH,
  86. T_SUBMESH
  87. };
  88. struct FBXVertexPosition
  89. {
  90. float x, y, z;
  91. };
  92. struct FBXVertexPoseRef
  93. {
  94. int poseIndex;
  95. float poseWeight;
  96. };
  97. struct FBXVertexKeyframe
  98. {
  99. float time;
  100. std::vector<FBXVertexPosition> positions;
  101. std::vector<FBXVertexPoseRef> poserefs;
  102. };
  103. struct FBXSkeletonKeyframe
  104. {
  105. float time;
  106. double tx, ty, tz;
  107. double quat_w, quat_x, quat_y, quat_z;
  108. float sx, sy, sz;
  109. };
  110. class FBXTrack
  111. {
  112. public:
  113. FBXTrackType mType;
  114. FBXTarget mTarget;
  115. int mIndex;
  116. String mBone;
  117. std::vector<FBXVertexKeyframe> mVertexKeyframes;
  118. std::vector<FBXSkeletonKeyframe> mSkeletonKeyframes;
  119. public:
  120. FBXTrack()
  121. {
  122. Clear();
  123. }
  124. void Clear()
  125. {
  126. mType = TT_SKELETON;
  127. mTarget = T_MESH;
  128. mIndex = 0;
  129. mBone = "";
  130. mVertexKeyframes.clear();
  131. mSkeletonKeyframes.clear();
  132. }
  133. };
  134. class FBXAnimation
  135. {
  136. public:
  137. //public members
  138. String mName;
  139. float mLength;
  140. std::vector<FBXTrack> mTracks;
  141. };
  142. class ModelDef;
  143. class FBXReader
  144. {
  145. public:
  146. ModelDef* mModelDef;
  147. FbxManager* mFBXManager;
  148. FbxScene* mFBXScene;
  149. std::vector<FBXMesh*> mMeshes;
  150. std::map<String, int> mJointIndexMap;
  151. std::vector<FBXJoint> mFBXJoints;
  152. std::vector<FBXAnimation> mAnimations;
  153. int mParamBindframe;
  154. float mParamLum;
  155. float mFPS;
  156. float mFrameRate;
  157. float mAnimStart;
  158. float mAnimStop;
  159. protected:
  160. FBXMesh* LoadMesh(FbxNode* fbxNode, FbxMesh* fbxMesh);
  161. void TranslateNode(FbxNode* fbxNode);
  162. public:
  163. FBXReader(ModelDef* modelDef);
  164. ~FBXReader();
  165. bool WriteBFFile(const StringImpl& fileName, const StringImpl& checkFile, const StringImpl& checkFile2);
  166. bool ReadBFFile(const StringImpl& fileName);
  167. bool ReadFile(const StringImpl& fileName, bool loadAnims = true);
  168. bool FBXLoadJoint(FbxNode* pNode, FbxAMatrix globalBindPose);
  169. int FBXGetJointIndex(FbxNode* pNode);
  170. bool FBXLoadClip(String clipName, float start, float stop, float rate);
  171. bool FBXLoadClipAnim(String clipName, float start, float stop, float rate, FBXAnimation& a);
  172. FBXSkeletonKeyframe FBXLoadKeyframe(FBXJoint& j, float time);
  173. FbxAMatrix CalculateGlobalTransformWithBind(FbxNode* pNode, FbxTime time);
  174. void GetAnimationBounds();
  175. void CalculateLocalTransforms(FbxNode* pRootNode);
  176. void ComputeBindPoseBoundingBox();
  177. void SetParentIndexes();
  178. void SortAndPruneJoints();
  179. void SortJoint(FBXJoint j, std::map<String, int> &sortedJointIndexMap, std::vector<FBXJoint>& sorted_joints);
  180. void AddParentsOfExistingJoints();
  181. void LoadBindPose();
  182. bool GetVertexBoneWeights(FbxNode* pNode, FbxMesh *pMesh, std::vector<BoneWeightVector>& boneWeightsVector);
  183. FbxAMatrix GetBindPose(FbxNode *pNode, FbxMesh *pMesh);
  184. void FindJoints(FbxNode* fbxNode);
  185. };
  186. NS_BF_END;
  187. #endif