DAESceneEncoder.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef DAESCENEEENCODER_H_
  2. #define DAESCENEEENCODER_H_
  3. #include "StringUtil.h"
  4. #include "Object.h"
  5. #include "Node.h"
  6. #include "Camera.h"
  7. #include "Light.h"
  8. #include "Mesh.h"
  9. #include "MeshPart.h"
  10. #include "MeshSkin.h"
  11. #include "Model.h"
  12. #include "Scene.h"
  13. #include "Animation.h"
  14. #include "AnimationChannel.h"
  15. #include "Vertex.h"
  16. #include "Matrix.h"
  17. #include "Transform.h"
  18. #include "DAEChannelTarget.h"
  19. #include "GPBFile.h"
  20. #include "DAEUtil.h"
  21. #include "EncoderArguments.h"
  22. namespace gameplay
  23. {
  24. /**
  25. * Class for binary encoding a Collada (DAE) file.
  26. */
  27. class DAESceneEncoder
  28. {
  29. public:
  30. static const unsigned int SCENE_SKIN_VERTEXINFLUENCES_MAX = 4;
  31. /**
  32. * Constructor.
  33. */
  34. DAESceneEncoder();
  35. /**
  36. * Destructor.
  37. */
  38. ~DAESceneEncoder();
  39. /**
  40. * Writes out encoded Collada 1.4 file.
  41. */
  42. void write(const std::string& filepath, const EncoderArguments& arguments);
  43. private:
  44. class DAEPolygonInput
  45. {
  46. public:
  47. DAEPolygonInput(void);
  48. /**
  49. * Destructor.
  50. */
  51. virtual ~DAEPolygonInput(void);
  52. unsigned int offset;
  53. int type;
  54. domListOfFloats sourceValues;
  55. domAccessor* accessor;
  56. };
  57. class SkinnedVertexWeightPair
  58. {
  59. public:
  60. float BlendWeight;
  61. unsigned int BlendIndex;
  62. SkinnedVertexWeightPair(float blendWeight, unsigned int blendIndex) : BlendWeight(blendWeight), BlendIndex(blendIndex)
  63. {
  64. }
  65. bool operator < (const SkinnedVertexWeightPair& value) const
  66. {
  67. return value.BlendWeight < BlendWeight;
  68. }
  69. };
  70. /**
  71. * Optimizes the COLLADA dom based on the arguments passed to the encoder.
  72. *
  73. * @param arguments The command line arguments passed to the encoder.
  74. * @param dom The COLLADA dom.
  75. */
  76. void optimizeCOLLADA(const EncoderArguments& arguments, domCOLLADA* dom);
  77. void triangulate(DAE* dae);
  78. void createTrianglesFromPolygons(domMesh* domMesh, domPolygons* domPolygons);
  79. void createTrianglesFromPolylist(domMesh* domMesh, domPolylist* domPolylist);
  80. Node* loadNode(domNode* node, Node* parent);
  81. void loadScene(const domVisual_scene* visualScene);
  82. void loadCameraInstance(const domNode* n, Node* node);
  83. void loadControllerInstance(const domNode* n, Node* node);
  84. void loadLightInstance(const domNode* n, Node* node);
  85. void loadGeometryInstance(const domNode* n, Node* node);
  86. /**
  87. * Loads all animations from the given COLLADA dom into the GamePlayFile.
  88. */
  89. void loadAnimations(const domCOLLADA* dom);
  90. /**
  91. * Loads a COLLADA animation element.
  92. *
  93. * @param animationRef The animation dom element to load from.
  94. * @param altId The id string to use if the animation doesn't have an id.
  95. */
  96. void loadAnimation(const domAnimationRef animationRef, const char* altId = NULL);
  97. Camera* loadCamera(const domCamera* cameraRef);
  98. Light* loadLight(const domLight* lightRef);
  99. Model* loadSkin(const domSkin* skinElement);
  100. Model* loadGeometry(const domGeometry* geometry, const domBind_materialRef bindMaterial);
  101. void loadSkeleton(domNode* rootNode, MeshSkin* skin);
  102. void loadSkeleton(domInstance_controller::domSkeleton* skeletonElement, MeshSkin* skin);
  103. /**
  104. * Loads interpolation curve data from the given source into the animation.
  105. *
  106. * @param source The source dom element to load interpolation curves from.
  107. * @param animation The destination animation to copy to.
  108. */
  109. void loadInterpolation(const domSourceRef source, AnimationChannel* animation);
  110. /**
  111. * Returns the active camera node for the given scene.
  112. *
  113. * @param visualScene The collada visual scene node.
  114. * @param scene The gameplay scene node.
  115. *
  116. * @return The active camera node or NULL if one was not found.
  117. */
  118. Node* findSceneActiveCameraNode(const domVisual_scene* visualScene, Scene* scene);
  119. /**
  120. * Loads and returns a mesh (geometry). If the mesh has already been loaded, it is simply returned.
  121. */
  122. Mesh* loadMesh(const domMesh* meshElement, const std::string& geometryId);
  123. /**
  124. * Sets the transform of node from the domNode transform.
  125. */
  126. void transformNode(domNode* domNode, Node* node);
  127. /**
  128. * Calculates the transform from a domNode. (<translate>, <rotate>, <scale>)
  129. */
  130. void calcTransform(domNode* domNode, Matrix& dstTransform);
  131. /**
  132. * Loads the target data into the animation from the given channel's target.
  133. * Example: <channel target="Cube/location.X" />
  134. * Target ID is "Cube"
  135. * The target attribute is "location.X"
  136. *
  137. * @param channelRef The channel dom element.
  138. * @param animationChannel The animation to copy to.
  139. *
  140. * @return True if the animation channel data was loaded without error; false if there was an error.
  141. */
  142. bool loadTarget(const domChannelRef& channelRef, AnimationChannel* animationChannel);
  143. void begin();
  144. void end(const char* str);
  145. /**
  146. * Copies float values from a domFloat_array to a std::vector<float>.
  147. *
  148. * @param source Source of float values to copy from.
  149. * @param target Destination float vector to copy to.
  150. */
  151. static void copyFloats(const domFloat_array* source, std::vector<float>* target);
  152. /**
  153. * Returns the VertexUsage value for the given semantic string.
  154. *
  155. * @param semantic The semantic attribute string from the COLLADA <input> element.
  156. *
  157. * @return The VertexUsage or -1 if the string was not recognized.
  158. */
  159. static int getVertexUsageType(const std::string& semantic);
  160. private:
  161. DAE* _collada; // Collada datastore in memory to read from.
  162. domCOLLADA* _dom;
  163. FILE* file; // Output file to write to.
  164. GPBFile _gamePlayFile;
  165. std::map<std::string, int> _jointLookupTable;
  166. std::vector<Matrix>_jointInverseBindPoseMatrices;
  167. float* _vertexBlendWeights;
  168. unsigned int* _vertexBlendIndices;
  169. std::vector<std::string> _tempGroupAnimationIds;
  170. clock_t _begin;
  171. };
  172. }
  173. #endif