DAESceneEncoder.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "CameraInstance.h"
  8. #include "Light.h"
  9. #include "LightInstance.h"
  10. #include "Mesh.h"
  11. #include "MeshPart.h"
  12. #include "MeshSkin.h"
  13. #include "Model.h"
  14. #include "Scene.h"
  15. #include "Animation.h"
  16. #include "AnimationChannel.h"
  17. #include "Vertex.h"
  18. #include "Matrix.h"
  19. #include "Transform.h"
  20. #include "DAEChannelTarget.h"
  21. #include "GPBFile.h"
  22. #include "DAEUtil.h"
  23. #include "EncoderArguments.h"
  24. namespace gameplay
  25. {
  26. /**
  27. * Class for binary encoding a Collada (DAE) file.
  28. */
  29. class DAESceneEncoder
  30. {
  31. public:
  32. static const unsigned int SCENE_SKIN_VERTEXINFLUENCES_MAX = 4;
  33. /**
  34. * Constructor.
  35. */
  36. DAESceneEncoder();
  37. /**
  38. * Destructor.
  39. */
  40. ~DAESceneEncoder();
  41. /**
  42. * Writes out encoded Collada 1.4 file.
  43. */
  44. void write(const std::string& filepath, const EncoderArguments& arguments);
  45. private:
  46. class DAEPolygonInput
  47. {
  48. public:
  49. DAEPolygonInput(void);
  50. /**
  51. * Destructor.
  52. */
  53. virtual ~DAEPolygonInput(void);
  54. unsigned int offset;
  55. int type;
  56. domListOfFloats sourceValues;
  57. domAccessor* accessor;
  58. };
  59. class SkinnedVertexWeightPair
  60. {
  61. public:
  62. float BlendWeight;
  63. unsigned int BlendIndex;
  64. SkinnedVertexWeightPair(float blendWeight, unsigned int blendIndex) : BlendWeight(blendWeight), BlendIndex(blendIndex)
  65. {
  66. }
  67. bool operator < (const SkinnedVertexWeightPair& value) const
  68. {
  69. return value.BlendWeight < BlendWeight;
  70. }
  71. };
  72. /**
  73. * Optimizes the COLLADA dom based on the arguments passed to the encoder.
  74. *
  75. * @param arguments The command line arguments passed to the encoder.
  76. * @param dom The COLLADA dom.
  77. */
  78. void optimizeCOLLADA(const EncoderArguments& arguments, domCOLLADA* dom);
  79. void triangulate(DAE* dae);
  80. void createTrianglesFromPolygons(domMesh* domMesh, domPolygons* domPolygons);
  81. void createTrianglesFromPolylist(domMesh* domMesh, domPolylist* domPolylist);
  82. Node* loadNode(domNode* node, Node* parent);
  83. void loadScene(const domVisual_scene* visualScene);
  84. void loadCameraInstance(const domNode* n, Node* node);
  85. void loadControllerInstance(const domNode* n, Node* node);
  86. void loadLightInstance(const domNode* n, Node* node);
  87. void loadGeometryInstance(const domNode* n, Node* node);
  88. /**
  89. * Loads all animations from the given COLLADA dom into the GamePlayFile.
  90. */
  91. void loadAnimations(const domCOLLADA* dom);
  92. /**
  93. * Loads a COLLADA animation element.
  94. *
  95. * @param animationRef The animation dom element to load from.
  96. */
  97. void loadAnimation(const domAnimationRef animationRef);
  98. CameraInstance* loadCamera(const domCamera* cameraRef);
  99. LightInstance* loadLight(const domLight* lightRef);
  100. Model* loadSkin(const domSkin* skinElement);
  101. Model* loadGeometry(const domGeometry* geometry, const domBind_materialRef bindMaterial);
  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. void warning(const std::string& message);
  132. void warning(const char* message);
  133. /**
  134. * Loads the target data into the animation from the given channel's target.
  135. * Example: <channel target="Cube/location.X" />
  136. * Target ID is "Cube"
  137. * The target attribute is "location.X"
  138. *
  139. * @param channelRef The channel dom element.
  140. * @param animationChannel The animation to copy to.
  141. *
  142. * @return True if the animation channel data was loaded without error; false if there was an error.
  143. */
  144. bool loadTarget(const domChannelRef& channelRef, AnimationChannel* animationChannel);
  145. void begin();
  146. void end(const char* str);
  147. /**
  148. * Copies float values from a domFloat_array to a std::vector<float>.
  149. *
  150. * @param source Source of float values to copy from.
  151. * @param target Destination float vector to copy to.
  152. */
  153. static void copyFloats(const domFloat_array* source, std::vector<float>* target);
  154. /**
  155. * Returns the VertexUsage value for the given semantic string.
  156. *
  157. * @param semantic The semantic attribute string from the COLLADA <input> element.
  158. * @param The VertexUsage or -1 if the string was not recognized.
  159. */
  160. static int getVertexUsageType(const std::string& semantic);
  161. private:
  162. DAE* _collada; // Collada datastore in memory to read from.
  163. domCOLLADA* _dom;
  164. FILE* file; // Output file to write to.
  165. GPBFile _gamePlayFile;
  166. std::map<std::string, int> _jointLookupTable;
  167. std::vector<Matrix>_jointInverseBindPoseMatrices;
  168. float* _vertexBlendWeights;
  169. unsigned int* _vertexBlendIndices;
  170. clock_t _begin;
  171. };
  172. }
  173. #endif