FBXUtil.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef FBXUTIL_H_
  2. #define FBXUTIL_H_
  3. #define FBXSDK_NEW_API
  4. #include <iostream>
  5. #include <list>
  6. #include <vector>
  7. #include <ctime>
  8. #ifdef WIN32
  9. #pragma warning( disable : 4100 )
  10. #pragma warning( disable : 4512 )
  11. #endif
  12. #include <fbxsdk.h>
  13. #include "Base.h"
  14. #include "Vertex.h"
  15. #include "Animation.h"
  16. #include "AnimationChannel.h"
  17. #include "EncoderArguments.h"
  18. using namespace gameplay;
  19. /**
  20. * Returns the aspect ratio from the given camera.
  21. *
  22. * @param fbxCamera The FBX camera to get the aspect ratio from.
  23. *
  24. * @return The aspect ratio from the camera.
  25. */
  26. float getAspectRatio(FbxCamera* fbxCamera);
  27. /**
  28. * Returns the field of view Y from the given camera.
  29. *
  30. * @param fbxCamera The camera to get the fiew of view from.
  31. *
  32. * @return The field of view Y.
  33. */
  34. float getFieldOfView(FbxCamera* fbxCamera);
  35. /**
  36. * Loads the texture coordinates from given mesh's polygon part into the vertex.
  37. *
  38. * @param fbxMesh The mesh to get the polygon from.
  39. * @param uvs The UV list to load tex coords from.
  40. * @param uvSetIndex The UV set index of the uvs.
  41. * @param polyIndex The index of the polygon in the mesh.
  42. * @param posInPoly The position of the vertex in the polygon.
  43. * @param meshVertexIndex The index of the vertex in the mesh.
  44. * @param vertex The vertex to copy the texture coordinates to.
  45. */
  46. void loadTextureCoords(FbxMesh* fbxMesh, const FbxGeometryElementUV* uvs, int uvSetIndex, int polyIndex, int posInPoly, int meshVertexIndex, Vertex* vertex);
  47. /**
  48. * Loads the normal from the mesh and adds it to the given vertex.
  49. *
  50. * @param fbxMesh The mesh to get the polygon from.
  51. * @param vertexIndex The vertex index in the mesh.
  52. * @param controlPointIndex The control point index.
  53. * @param vertex The vertex to copy to.
  54. */
  55. void loadNormal(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex);
  56. /**
  57. * Loads the tangent from the mesh and adds it to the given vertex.
  58. *
  59. * @param fbxMesh The mesh to load from.
  60. * @param vertexIndex The index of the vertex within fbxMesh.
  61. * @param controlPointIndex The control point index.
  62. * @param vertex The vertex to copy to.
  63. */
  64. void loadTangent(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex);
  65. /**
  66. * Loads the binormal from the mesh and adds it to the given vertex.
  67. *
  68. * @param fbxMesh The mesh to load from.
  69. * @param vertexIndex The index of the vertex within fbxMesh.
  70. * @param controlPointIndex The control point index.
  71. * @param vertex The vertex to copy to.
  72. */
  73. void loadBinormal(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex);
  74. /**
  75. * Loads the vertex diffuse color from the mesh and adds it to the given vertex.
  76. *
  77. * @param fbxMesh The mesh to load from.
  78. * @param vertexIndex The index of the vertex within fbxMesh.
  79. * @param controlPointIndex The control point index.
  80. * @param vertex The vertex to copy to.
  81. */
  82. void loadVertexColor(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex);
  83. /**
  84. * Loads the blend weight and blend indices data into the vertex.
  85. *
  86. * @param vertexWeights List of vertex weights. The x member contains the blendIndices. The y member contains the blendWeights.
  87. * @param vertex The vertex to copy the blend data to.
  88. */
  89. void loadBlendData(const std::vector<Vector2>& vertexWeights, Vertex* vertex);
  90. /**
  91. * Loads the blend weights and blend indices from the given mesh.
  92. *
  93. * Each element of weights is a list of Vector2s where "x" is the blend index and "y" is the blend weight.
  94. *
  95. * @param fbxMesh The mesh to load from.
  96. * @param weights List of blend weights and blend indices for each vertex.
  97. *
  98. * @return True if this mesh has a mesh skin, false otherwise.
  99. */
  100. bool loadBlendWeights(FbxMesh* fbxMesh, std::vector<std::vector<Vector2> >& weights);
  101. /**
  102. * Copies from an FBX matrix to a float[16] array.
  103. */
  104. void copyMatrix(const FbxMatrix& fbxMatrix, float* matrix);
  105. /**
  106. * Copies from an FBX matrix to a gameplay matrix.
  107. */
  108. void copyMatrix(const FbxMatrix& fbxMatrix, Matrix& matrix);
  109. /**
  110. * Finds the min and max start time and stop time of the given animation curve.
  111. *
  112. * startTime is updated if the animation curve contains a start time that is less than startTime.
  113. * stopTime is updated if the animation curve contains a stop time that is greater than stopTime.
  114. * frameRate is updated if the animation curve contains a frame rate that is greater than frameRate.
  115. *
  116. * @param animCurve The animation curve to read from.
  117. * @param startTime The min start time. (in/out)
  118. * @param stopTime The max stop time. (in/out)
  119. * @param frameRate The frame rate. (in/out)
  120. */
  121. void findMinMaxTime(FbxAnimCurve* animCurve, float* startTime, float* stopTime, float* frameRate);
  122. /**
  123. * Appends key frame data to the given node for the specified animation target attribute.
  124. *
  125. * @param fbxNode The node to get the matrix transform from.
  126. * @param channel The aniamtion channel to write values into.
  127. * @param time The time of the keyframe.
  128. * @param scale The evaluated scale for the keyframe.
  129. * @param rotation The evalulated rotation for the keyframe.
  130. * @param translation The evalulated translation for the keyframe.
  131. */
  132. void appendKeyFrame(FbxNode* fbxNode, AnimationChannel* channel, float time, const Vector3& scale, const Quaternion& rotation, const Vector3& translation);
  133. /**
  134. * Decomposes the given node's matrix transform at the given time and copies to scale, rotation and translation.
  135. *
  136. * @param fbxNode The node to get the matrix transform from.
  137. * @param time The time to get the matrix transform from.
  138. * @param scale The scale to copy to.
  139. * @param rotation The rotation to copy to.
  140. * @param translation The translation to copy to.
  141. */
  142. void decompose(FbxNode* fbxNode, float time, Vector3* scale, Quaternion* rotation, Vector3* translation);
  143. /**
  144. * Creates an animation channel that targets the given node and target attribute using the given key times and key values.
  145. *
  146. * @param fbxNode The node to target.
  147. * @param targetAttrib The attribute type to target.
  148. * @param keyTimes The key times for the animation channel.
  149. * @param keyValues The key values for the animation channel.
  150. *
  151. * @return The newly created animation channel.
  152. */
  153. AnimationChannel* createAnimationChannel(FbxNode* fbxNode, unsigned int targetAttrib, const std::vector<float>& keyTimes, const std::vector<float>& keyValues);
  154. void addScaleChannel(Animation* animation, FbxNode* fbxNode, float startTime, float stopTime);
  155. void addTranslateChannel(Animation* animation, FbxNode* fbxNode, float startTime, float stopTime);
  156. /**
  157. * Determines if it is possible to automatically group animations for mesh skins.
  158. *
  159. * @param fbxScene The FBX scene to search.
  160. *
  161. * @return True if there is at least one mesh skin that has animations that can be grouped.
  162. */
  163. bool isGroupAnimationPossible(FbxScene* fbxScene);
  164. bool isGroupAnimationPossible(FbxNode* fbxNode);
  165. bool isGroupAnimationPossible(FbxMesh* fbxMesh);
  166. bool isBlack(FbxDouble3& fbxDouble);
  167. /**
  168. * Recursively generates the tangents and binormals for all nodes that were specified in the command line arguments.
  169. */
  170. void generateTangentsAndBinormals(FbxNode* fbxNode, const EncoderArguments& arguments);
  171. FbxAnimCurve* getCurve(FbxPropertyT<FbxDouble3>& prop, FbxAnimLayer* animLayer, const char* pChannel);
  172. std::string toString(const FbxDouble3& fbxDouble);
  173. std::string toString(const FbxDouble3& fbxDouble, double d);
  174. std::string toString(double value);
  175. #endif