FBXSceneEncoder.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #ifndef FBXSCENEEENCODER_H_
  2. #define FBXSCENEEENCODER_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 "StringUtil.h"
  15. #include "Object.h"
  16. #include "Node.h"
  17. #include "Camera.h"
  18. #include "Light.h"
  19. #include "Mesh.h"
  20. #include "MeshPart.h"
  21. #include "MeshSkin.h"
  22. #include "Model.h"
  23. #include "Scene.h"
  24. #include "Animation.h"
  25. #include "AnimationChannel.h"
  26. #include "Vertex.h"
  27. #include "Matrix.h"
  28. #include "Transform.h"
  29. #include "GPBFile.h"
  30. #include "EncoderArguments.h"
  31. using namespace gameplay;
  32. /**
  33. * Class for binary encoding an FBX file.
  34. */
  35. class FBXSceneEncoder
  36. {
  37. public:
  38. static const unsigned int SCENE_SKIN_VERTEXINFLUENCES_MAX = 4;
  39. /**
  40. * Constructor.
  41. */
  42. FBXSceneEncoder();
  43. /**
  44. * Destructor.
  45. */
  46. ~FBXSceneEncoder();
  47. /**
  48. * Writes out encoded FBX file.
  49. */
  50. void write(const std::string& filepath, const EncoderArguments& arguments);
  51. /**
  52. * Writes a material file.
  53. *
  54. * @param filepath
  55. *
  56. * @return True if successful; false otherwise.
  57. */
  58. bool writeMaterial(const std::string& filepath);
  59. private:
  60. /**
  61. * Loads the scene.
  62. *
  63. * @param fbxScene The FBX scene to load.
  64. */
  65. void loadScene(FbxScene* fbxScene);
  66. /**
  67. * Loads all of the animatiosn from the given FBX scene.
  68. *
  69. * @param fbxScene The scene to load animations from.
  70. * @param arguments The command line arguments passed to the encoder.
  71. */
  72. void loadAnimations(FbxScene* fbxScene, const EncoderArguments& arguments);
  73. /**
  74. * Loads the animations from the given FBX animation layer recursively starting from fbxNode.
  75. *
  76. * @param fbxAnimLayer The FBX animation layer to load from.
  77. * @param fbxNode The node to start loading animations from.
  78. * @param arguments The command line arguments passed to the encoder.
  79. */
  80. void loadAnimationLayer(FbxAnimLayer* fbxAnimLayer, FbxNode* fbxNode, const EncoderArguments& arguments);
  81. /**
  82. * Loads animation channels from the given node and adds the channels to the given animation.
  83. *
  84. * @param pAnimLayer The FBX animation layer to load from.
  85. * @param fbxNode The node to load animation channels from.
  86. * @param animation The animation to add the channels to.
  87. */
  88. void loadAnimationChannels(FbxAnimLayer* pAnimLayer, FbxNode* fbxNode, Animation* animation);
  89. /**
  90. * Loads the bind shape for all mesh skins that have be loaded so far.
  91. *
  92. * @param fbxScene The FBX scene to read the bind shapes from.
  93. */
  94. void loadBindShapes(FbxScene* fbxScene);
  95. /**
  96. * Loads the camera 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 loadCamera(FbxNode* fbxNode, Node* node);
  102. /**
  103. * Loads the light 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 loadLight(FbxNode* fbxNode, Node* node);
  109. /**
  110. * Loads the model from the given FBX node and adds to it to the given GamePlay node.
  111. *
  112. * @param fbxNode The FBX node to load from.
  113. * @param node The GamePlay node to add to.
  114. */
  115. void loadModel(FbxNode* fbxNode, Node* node);
  116. /**
  117. * Loads materials for each node in the scene.
  118. */
  119. void loadMaterials(FbxScene* fbxScene);
  120. /**
  121. * Loads the material from the given node.
  122. */
  123. void loadMaterial(FbxNode* fbxNode);
  124. void loadMaterialTextures(FbxSurfaceMaterial* fbxMaterial, Material* material);
  125. void loadMaterialFileTexture(FbxFileTexture* fileTexture, Material* material);
  126. void loadMaterialUniforms(FbxSurfaceMaterial* fbxMaterial, Material* material);
  127. /**
  128. * Creates a material from an FbxSurfaceMaterial.
  129. *
  130. * @param name The name of the new material.
  131. * @param fbxMaterial The FBX material to copy from.
  132. * @param node The node that the material is being created for.
  133. *
  134. * @return The newly created material.
  135. */
  136. Material* createMaterial(const std::string& name, FbxSurfaceMaterial* fbxMaterial, Node* node);
  137. /**
  138. * Loads the mesh skin from the given FBX mesh and adds it to the given GamePlay model.
  139. *
  140. * @param fbxMesh The FBX mesh to load the skin from.
  141. * @param model The model to add the skin to.
  142. */
  143. void loadSkin(FbxMesh* fbxMesh, Model* model);
  144. /**
  145. * Loads the FBX Node and creates a GamePlay Node.
  146. *
  147. * @param fbxNode The FBX Node to load.
  148. *
  149. * @return The newly created Node or NULL if the node could not be loaded.
  150. */
  151. Node* loadNode(FbxNode* fbxNode);
  152. /**
  153. * Loads the FbxMesh and returns a GamePlay mesh.
  154. * If the fbxMesh has already been loaded then the same instance of mesh will be returned.
  155. *
  156. * @param fbxMesh The FBX Mesh to load.
  157. *
  158. * @return The GamePlay mesh that was loaded from the FBX Mesh.
  159. */
  160. Mesh* loadMesh(FbxMesh* fbxMesh);
  161. /**
  162. * Gets the Mesh that was saved with the given ID. Returns NULL if a match is not found.
  163. *
  164. * @param meshId The ID of the FbxMesh to search for.
  165. *
  166. * @return The mesh that was saved with the ID or NULL if none was found.
  167. */
  168. Mesh* getMesh(FbxUInt64 meshId);
  169. /**
  170. * Saves the Mesh with the given id.
  171. *
  172. * @param meshId The ID of the FbxMesh to use as a key.
  173. * @param mesh The mesh to save.
  174. */
  175. void saveMesh(FbxUInt64 meshId, Mesh* mesh);
  176. /**
  177. * Prints a message.
  178. *
  179. * @param str The string to print.
  180. */
  181. void print(const char* str);
  182. /**
  183. * Transforms the GamePlay Node using the transform data from the FBX Node.
  184. *
  185. * @param fbxNode The FBX node to get the transfrom data from
  186. * @param node The GamePlay Node to copy the transform to.
  187. */
  188. void transformNode(FbxNode* fbxNode, Node* node);
  189. /**
  190. * Gets the base material that matches the given id. Returns NULL if not found.
  191. */
  192. Material* getBaseMaterial(const char* id);
  193. /**
  194. * Finds the base material for the given material.
  195. * This will either return a previously loaded base material or
  196. * the base material will be created and returned. (Should never return NULL)
  197. */
  198. Material* findBaseMaterial(Material* material);
  199. /**
  200. * Finds the gameplay Node that was created from the given FbxNode.
  201. * Returns NULL if not found.
  202. */
  203. Node* findNode(FbxNode* fbxNode);
  204. /**
  205. * Creates a base material with the given name from the given child material.
  206. *
  207. * @param baseMaterialName The name of the base material to create.
  208. * @param childMaterial The child material that the base material is being created for.
  209. *
  210. * @return The newly created base material.
  211. */
  212. Material* createBaseMaterial(const std::string& baseMaterialName, Material* childMaterial);
  213. /**
  214. * Recursively triangules the meshes starting from the given node.
  215. *
  216. * @param fbxNode The node to start triangulating from.
  217. */
  218. static void triangulateRecursive(FbxNode* fbxNode);
  219. private:
  220. /**
  221. * The GamePlay file that is populated while reading the FBX file.
  222. */
  223. GPBFile _gamePlayFile;
  224. /**
  225. * The collection of meshes for the purpose of making sure that the same model is not loaded twice. (Mesh instancing)
  226. */
  227. std::map<FbxUInt64, Mesh*> _meshes;
  228. /**
  229. * The list of child materials that were loaded.
  230. */
  231. std::map<std::string, Material*> _materials;
  232. /**
  233. * The list of base materials that the child materials are derived from.
  234. */
  235. std::map<std::string, Material*> _baseMaterials;
  236. /**
  237. * A map for keeping track of which gameplay Node was created from a given FbxNode.
  238. */
  239. std::map<FbxNode*, Node*> _nodeMap;
  240. /**
  241. * The animation that channels should be added to if the user is using the -groupAnimation command line argument. May be NULL.
  242. */
  243. Animation* _groupAnimation;
  244. /**
  245. * Indicates if the animations for mesh skins should be grouped before writing out the GPB file.
  246. */
  247. bool _autoGroupAnimations;
  248. };
  249. #endif