FBXSceneEncoder.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef FBXSCENEEENCODER_H_
  2. #define FBXSCENEEENCODER_H_
  3. #ifdef USE_FBX
  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. private:
  52. /**
  53. * Loads the scene.
  54. *
  55. * @param fbxScene The FBX scene to load.
  56. */
  57. void loadScene(KFbxScene* fbxScene);
  58. /**
  59. * Loads all of the animatiosn from the given FBX scene.
  60. *
  61. * @param fbxScene The scene to load animations from.
  62. * @param arguments The command line arguments passed to the encoder.
  63. */
  64. void loadAnimations(KFbxScene* fbxScene, const EncoderArguments& arguments);
  65. /**
  66. * Loads the animations from the given FBX animation layer recursively starting from fbxNode.
  67. *
  68. * @param fbxAnimLayer The FBX animation layer to load from.
  69. * @param fbxNode The node to start loading animations from.
  70. * @param arguments The command line arguments passed to the encoder.
  71. */
  72. void loadAnimationLayer(KFbxAnimLayer* fbxAnimLayer, KFbxNode* fbxNode, const EncoderArguments& arguments);
  73. /**
  74. * Loads animation channels from the given node and adds the channels to the given animation.
  75. *
  76. * @param pAnimLayer The FBX animation layer to load from.
  77. * @param fbxNode The node to load animation channels from.
  78. * @param animation The animation to add the channels to.
  79. */
  80. void loadAnimationChannels(KFbxAnimLayer* pAnimLayer, KFbxNode* fbxNode, Animation* animation);
  81. /**
  82. * Loads the bind shape for all mesh skins that have be loaded so far.
  83. *
  84. * @param fbxScene The FBX scene to read the bind shapes from.
  85. */
  86. void loadBindShapes(KFbxScene* fbxScene);
  87. /**
  88. * Loads the camera from the given FBX node and adds to it to the given GamePlay node.
  89. *
  90. * @param fbxNode The FBX node to load from.
  91. * @param node The GamePlay node to add to.
  92. */
  93. void loadCamera(KFbxNode* fbxNode, Node* node);
  94. /**
  95. * Loads the light from the given FBX node and adds to it to the given GamePlay node.
  96. *
  97. * @param fbxNode The FBX node to load from.
  98. * @param node The GamePlay node to add to.
  99. */
  100. void loadLight(KFbxNode* fbxNode, Node* node);
  101. /**
  102. * Loads the model from the given FBX node and adds to it to the given GamePlay node.
  103. *
  104. * @param fbxNode The FBX node to load from.
  105. * @param node The GamePlay node to add to.
  106. */
  107. void loadModel(KFbxNode* fbxNode, Node* node);
  108. /**
  109. * Loads the mesh skin from the given FBX mesh and adds it to the given GamePlay model.
  110. *
  111. * @param fbxMesh The FBX mesh to load the skin from.
  112. * @param model The model to add the skin to.
  113. */
  114. void loadSkin(KFbxMesh* fbxMesh, Model* model);
  115. /**
  116. * Loads the FBX Node and creates a GamePlay Node.
  117. *
  118. * @param fbxNode The FBX Node to load.
  119. *
  120. * @return The newly created Node or NULL if the node could not be loaded.
  121. */
  122. Node* loadNode(KFbxNode* fbxNode);
  123. /**
  124. * Loads the FbxMesh and returns a GamePlay mesh.
  125. * If the fbxMesh has already been loaded then the same instance of mesh will be returned.
  126. *
  127. * @param fbxMesh The FBX Mesh to load.
  128. *
  129. * @return The GamePlay mesh that was loaded from the FBX Mesh.
  130. */
  131. Mesh* loadMesh(KFbxMesh* fbxMesh);
  132. /**
  133. * Gets the Mesh that was saved with the given ID. Returns NULL if a match is not found.
  134. *
  135. * @param meshId The ID of the FbxMesh to search for.
  136. *
  137. * @return The mesh that was saved with the ID or NULL if none was found.
  138. */
  139. Mesh* getMesh(size_t meshId);
  140. /**
  141. * Saves the Mesh with the given id.
  142. *
  143. * @param meshId The ID of the FbxMesh to use as a key.
  144. * @param mesh The mesh to save.
  145. */
  146. void saveMesh(size_t meshId, Mesh* mesh);
  147. /**
  148. * Prints a message.
  149. *
  150. * @param str The string to print.
  151. */
  152. void print(const char* str);
  153. /**
  154. * Transforms the GamePlay Node using the transform data from the FBX Node.
  155. *
  156. * @param fbxNode The FBX node to get the transfrom data from
  157. * @param node The GamePlay Node to copy the transform to.
  158. */
  159. void transformNode(KFbxNode* fbxNode, Node* node);
  160. /**
  161. * Recursively triangules the meshes starting from the given node.
  162. *
  163. * @param fbxNode The node to start triangulating from.
  164. */
  165. static void triangulateRecursive(KFbxNode* fbxNode);
  166. /**
  167. * Prints a warning message.
  168. */
  169. static void warning(const std::string& message);
  170. /**
  171. * Prints a warning message.
  172. */
  173. static void warning(const char* message);
  174. private:
  175. /**
  176. * The GamePlay file that is populated while reading the FBX file.
  177. */
  178. GPBFile _gamePlayFile;
  179. /**
  180. * The collection of meshes for the purpose of making sure that the same model is not loaded twice. (Mesh instancing)
  181. */
  182. std::map<size_t, Mesh*> _meshes;
  183. /**
  184. * The animation that channels should be added to it the user is using the -groupAnimation command line argument. May be NULL.
  185. */
  186. Animation* _groupAnimation;
  187. };
  188. #endif
  189. #endif