FBXSceneEncoder.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #ifndef FBXSCENEEENCODER_H_
  2. #define FBXSCENEEENCODER_H_
  3. #ifdef USE_FBX
  4. #define FBXSDK_NEW_API
  5. #include <iostream>
  6. #include <list>
  7. #include <vector>
  8. #include <ctime>
  9. #ifdef WIN32
  10. #pragma warning( disable : 4100 )
  11. #pragma warning( disable : 4512 )
  12. #endif
  13. #include <fbxsdk.h>
  14. #include "Base.h"
  15. #include "StringUtil.h"
  16. #include "Object.h"
  17. #include "Node.h"
  18. #include "Camera.h"
  19. #include "Light.h"
  20. #include "Mesh.h"
  21. #include "MeshPart.h"
  22. #include "MeshSkin.h"
  23. #include "Model.h"
  24. #include "Scene.h"
  25. #include "Animation.h"
  26. #include "AnimationChannel.h"
  27. #include "Vertex.h"
  28. #include "Matrix.h"
  29. #include "Transform.h"
  30. #include "GPBFile.h"
  31. #include "EncoderArguments.h"
  32. using namespace gameplay;
  33. /**
  34. * Class for binary encoding an FBX file.
  35. */
  36. class FBXSceneEncoder
  37. {
  38. public:
  39. static const unsigned int SCENE_SKIN_VERTEXINFLUENCES_MAX = 4;
  40. /**
  41. * Constructor.
  42. */
  43. FBXSceneEncoder();
  44. /**
  45. * Destructor.
  46. */
  47. ~FBXSceneEncoder();
  48. /**
  49. * Writes out encoded FBX file.
  50. */
  51. void write(const std::string& filepath, const EncoderArguments& arguments);
  52. private:
  53. /**
  54. * Loads the scene.
  55. *
  56. * @param fbxScene The FBX scene to load.
  57. */
  58. void loadScene(FbxScene* fbxScene);
  59. /**
  60. * Loads all of the animatiosn from the given FBX scene.
  61. *
  62. * @param fbxScene The scene to load animations from.
  63. * @param arguments The command line arguments passed to the encoder.
  64. */
  65. void loadAnimations(FbxScene* fbxScene, const EncoderArguments& arguments);
  66. /**
  67. * Loads the animations from the given FBX animation layer recursively starting from fbxNode.
  68. *
  69. * @param fbxAnimLayer The FBX animation layer to load from.
  70. * @param fbxNode The node to start loading animations from.
  71. * @param arguments The command line arguments passed to the encoder.
  72. */
  73. void loadAnimationLayer(FbxAnimLayer* fbxAnimLayer, FbxNode* fbxNode, const EncoderArguments& arguments);
  74. /**
  75. * Loads animation channels from the given node and adds the channels to the given animation.
  76. *
  77. * @param pAnimLayer The FBX animation layer to load from.
  78. * @param fbxNode The node to load animation channels from.
  79. * @param animation The animation to add the channels to.
  80. */
  81. void loadAnimationChannels(FbxAnimLayer* pAnimLayer, FbxNode* fbxNode, Animation* animation);
  82. /**
  83. * Loads the bind shape for all mesh skins that have be loaded so far.
  84. *
  85. * @param fbxScene The FBX scene to read the bind shapes from.
  86. */
  87. void loadBindShapes(FbxScene* fbxScene);
  88. /**
  89. * Loads the camera from the given FBX node and adds to it to the given GamePlay node.
  90. *
  91. * @param fbxNode The FBX node to load from.
  92. * @param node The GamePlay node to add to.
  93. */
  94. void loadCamera(FbxNode* fbxNode, Node* node);
  95. /**
  96. * Loads the light from the given FBX node and adds to it to the given GamePlay node.
  97. *
  98. * @param fbxNode The FBX node to load from.
  99. * @param node The GamePlay node to add to.
  100. */
  101. void loadLight(FbxNode* fbxNode, Node* node);
  102. /**
  103. * Loads the model from the given FBX node and adds to it to the given GamePlay node.
  104. *
  105. * @param fbxNode The FBX node to load from.
  106. * @param node The GamePlay node to add to.
  107. */
  108. void loadModel(FbxNode* fbxNode, Node* node);
  109. /**
  110. * Loads the mesh skin from the given FBX mesh and adds it to the given GamePlay model.
  111. *
  112. * @param fbxMesh The FBX mesh to load the skin from.
  113. * @param model The model to add the skin to.
  114. */
  115. void loadSkin(FbxMesh* fbxMesh, Model* model);
  116. /**
  117. * Loads the FBX Node and creates a GamePlay Node.
  118. *
  119. * @param fbxNode The FBX Node to load.
  120. *
  121. * @return The newly created Node or NULL if the node could not be loaded.
  122. */
  123. Node* loadNode(FbxNode* fbxNode);
  124. /**
  125. * Loads the FbxMesh and returns a GamePlay mesh.
  126. * If the fbxMesh has already been loaded then the same instance of mesh will be returned.
  127. *
  128. * @param fbxMesh The FBX Mesh to load.
  129. *
  130. * @return The GamePlay mesh that was loaded from the FBX Mesh.
  131. */
  132. Mesh* loadMesh(FbxMesh* fbxMesh);
  133. /**
  134. * Gets the Mesh that was saved with the given ID. Returns NULL if a match is not found.
  135. *
  136. * @param meshId The ID of the FbxMesh to search for.
  137. *
  138. * @return The mesh that was saved with the ID or NULL if none was found.
  139. */
  140. Mesh* getMesh(FbxUInt64 meshId);
  141. /**
  142. * Saves the Mesh with the given id.
  143. *
  144. * @param meshId The ID of the FbxMesh to use as a key.
  145. * @param mesh The mesh to save.
  146. */
  147. void saveMesh(FbxUInt64 meshId, Mesh* mesh);
  148. /**
  149. * Prints a message.
  150. *
  151. * @param str The string to print.
  152. */
  153. void print(const char* str);
  154. /**
  155. * Transforms the GamePlay Node using the transform data from the FBX Node.
  156. *
  157. * @param fbxNode The FBX node to get the transfrom data from
  158. * @param node The GamePlay Node to copy the transform to.
  159. */
  160. void transformNode(FbxNode* fbxNode, Node* node);
  161. /**
  162. * Recursively triangules the meshes starting from the given node.
  163. *
  164. * @param fbxNode The node to start triangulating from.
  165. */
  166. static void triangulateRecursive(FbxNode* fbxNode);
  167. private:
  168. /**
  169. * The GamePlay file that is populated while reading the FBX file.
  170. */
  171. GPBFile _gamePlayFile;
  172. /**
  173. * The collection of meshes for the purpose of making sure that the same model is not loaded twice. (Mesh instancing)
  174. */
  175. std::map<FbxUInt64, Mesh*> _meshes;
  176. /**
  177. * The animation that channels should be added to if the user is using the -groupAnimation command line argument. May be NULL.
  178. */
  179. Animation* _groupAnimation;
  180. /**
  181. * Indicates if the animations for mesh skins should be grouped before writing out the GPB file.
  182. */
  183. bool _autoGroupAnimations;
  184. };
  185. #endif
  186. #endif