2
0

SceneCombiner.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Declares a helper class, "SceneCombiner" providing various
  34. * utilities to merge scenes.
  35. */
  36. #ifndef AI_SCENE_COMBINER_H_INC
  37. #define AI_SCENE_COMBINER_H_INC
  38. #include "../include/aiAssert.h"
  39. namespace Assimp {
  40. // ---------------------------------------------------------------------------
  41. /** \brief Helper data structure for SceneCombiner.
  42. *
  43. * Describes to which node a scene must be attached to.
  44. */
  45. struct AttachmentInfo
  46. {
  47. AttachmentInfo()
  48. : scene (NULL)
  49. , attachToNode (NULL)
  50. {}
  51. AttachmentInfo(aiScene* _scene, aiNode* _attachToNode)
  52. : scene (_scene)
  53. , attachToNode (_attachToNode)
  54. {}
  55. aiScene* scene;
  56. aiNode* attachToNode;
  57. };
  58. // ---------------------------------------------------------------------------
  59. struct NodeAttachmentInfo
  60. {
  61. NodeAttachmentInfo()
  62. : node (NULL)
  63. , attachToNode (NULL)
  64. {}
  65. NodeAttachmentInfo(aiNode* _scene, aiNode* _attachToNode)
  66. : node (_scene)
  67. , attachToNode (_attachToNode)
  68. {}
  69. aiNode* node;
  70. aiNode* attachToNode;
  71. };
  72. // generate unique names for all named scene items
  73. #define AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES 0x1
  74. // generate unique names for materials, too
  75. #define AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES 0x2
  76. // use deep copies of duplicate scenes
  77. #define AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY 0x4
  78. typedef std::pair<aiBone*,unsigned int> BoneSrcIndex;
  79. // ---------------------------------------------------------------------------
  80. /** \brief Helper data structure for SceneCombiner::MergeBones.
  81. *
  82. */
  83. struct BoneWithHash : public std::pair<uint32_t,aiString*>
  84. {
  85. std::vector<BoneSrcIndex> pSrcBones;
  86. };
  87. // ---------------------------------------------------------------------------
  88. /** \brief Static helper class providing various utilities to merge two
  89. * scenes. It is intended as internal utility and NOT for use by
  90. * applications.
  91. *
  92. * The class is currently being used by various postprocessing steps
  93. * and loaders (ie. LWS).
  94. */
  95. class ASSIMP_API SceneCombiner
  96. {
  97. // class cannot be instanced
  98. SceneCombiner() {}
  99. public:
  100. // -------------------------------------------------------------------
  101. /** Merges two or more scenes.
  102. *
  103. * @param dest Receives a pointer to the destination scene. If the
  104. * pointer doesn't point to NULL when the function is called, the
  105. * existing scene is cleared and refilled.
  106. * @param src Non-empty list of scenes to be merged. The function
  107. * deletes the input scenes afterwards. There may be duplicate scenes.
  108. * @param flags Combination of the AI_INT_MERGE_SCENE flags defined above
  109. */
  110. static void MergeScenes(aiScene** dest,std::vector<aiScene*>& src,
  111. unsigned int flags = 0);
  112. // -------------------------------------------------------------------
  113. /** Merges two or more scenes and attaches all sceenes to a specific
  114. * position in the node graph of the masteer scene.
  115. *
  116. * @param dest Receives a pointer to the destination scene. If the
  117. * pointer doesn't point to NULL when the function is called, the
  118. * existing scene is cleared and refilled.
  119. * @param master Master scene. It will be deleted afterwards. All
  120. * other scenes will be inserted in its node graph.
  121. * @param src Non-empty list of scenes to be merged along with their
  122. * corresponding attachment points in the master scene. The function
  123. * deletes the input scenes afterwards. There may be duplicate scenes.
  124. * @param flags Combination of the AI_INT_MERGE_SCENE flags defined above
  125. */
  126. static void MergeScenes(aiScene** dest, aiScene* master,
  127. std::vector<AttachmentInfo>& src,
  128. unsigned int flags = 0);
  129. // -------------------------------------------------------------------
  130. /** Merges two or more meshes
  131. *
  132. * The meshes should have equal vertex formats. Only components
  133. * that are provided by ALL meshes will be present in the output mesh.
  134. * An exception is made for VColors - they are set to black. The
  135. * meshes should have the same material indices, too. The output
  136. * material index is always the material index of the first mesh.
  137. *
  138. * @param dest Destination mesh. Must be empty.
  139. * @param flags Currently no parameters
  140. * @param begin First mesh to be processed
  141. * @param end Points to the mesh after the last mesh to be processed
  142. */
  143. static void MergeMeshes(aiMesh** dest,unsigned int flags,
  144. std::vector<aiMesh*>::const_iterator begin,
  145. std::vector<aiMesh*>::const_iterator end);
  146. // -------------------------------------------------------------------
  147. /** Merges two or more bones
  148. *
  149. * @param out Mesh to receive the output bone list
  150. * @param flags Currently no parameters
  151. * @param begin First mesh to be processed
  152. * @param end Points to the mesh after the last mesh to be processed
  153. */
  154. static void MergeBones(aiMesh* out,std::vector<aiMesh*>::const_iterator it,
  155. std::vector<aiMesh*>::const_iterator end);
  156. // -------------------------------------------------------------------
  157. /** Builds a list of uniquely named bones in a mesh list
  158. *
  159. * @param asBones Receives the output list
  160. * @param it First mesh to be processed
  161. * @param end Last mesh to be processed
  162. */
  163. static void BuildUniqueBoneList(std::list<BoneWithHash>& asBones,
  164. std::vector<aiMesh*>::const_iterator it,
  165. std::vector<aiMesh*>::const_iterator end);
  166. // -------------------------------------------------------------------
  167. /** Add a name prefix to all nodes in a scene.
  168. *
  169. * @param Current node. This function is called recursively.
  170. * @param prefix Prefix to be added to all nodes
  171. * @param len STring length
  172. */
  173. static void AddNodePrefixes(aiNode* node, const char* prefix,
  174. unsigned int len);
  175. // -------------------------------------------------------------------
  176. /** Add an offset to all mesh indices in a node graph
  177. *
  178. * @param Current node. This function is called recursively.
  179. * @param offset Offset to be added to all mesh indices
  180. */
  181. static void OffsetNodeMeshIndices (aiNode* node, unsigned int offset);
  182. // -------------------------------------------------------------------
  183. /** Attach a list of node graphs to well-defined nodes in a master
  184. * graph. This is a helper for MergeScenes()
  185. *
  186. * @param master Master scene
  187. * @param srcList List of source scenes along with their attachment
  188. * points. If an attachment point is NULL (or does not exist in
  189. * the master graph), a scene is attached to the root of the master
  190. * graph (as an additional child node)
  191. * @duplicates List of duplicates. If elem[n] == n the scene is not
  192. * a duplicate. Otherwise elem[n] links scene n to its first occurence.
  193. */
  194. static void AttachToGraph ( aiScene* master,
  195. std::vector<NodeAttachmentInfo>& srcList);
  196. static void AttachToGraph (aiNode* attach,
  197. std::vector<NodeAttachmentInfo>& srcList);
  198. // -------------------------------------------------------------------
  199. /** Get a deep copy of a scene
  200. *
  201. * @param dest Receives a pointer to the destination scene
  202. * @param src Source scene - remains unmodified.
  203. */
  204. static void CopyScene(aiScene** dest,aiScene* source);
  205. // -------------------------------------------------------------------
  206. /** Get a flat copy of a scene
  207. *
  208. * Only the first hierarchy layer is copied. All pointer members of
  209. * aiScene are shared by source and destination scene. If the
  210. * pointer doesn't point to NULL when the function is called, the
  211. * existing scene is cleared and refilled.
  212. * @param dest Receives a pointer to the destination scene
  213. * @param src Source scene - remains unmodified.
  214. */
  215. static void CopySceneFlat(aiScene** dest,aiScene* source);
  216. // -------------------------------------------------------------------
  217. /** Get a deep copy of a mesh
  218. *
  219. * @param dest Receives a pointer to the destination mesh
  220. * @param src Source mesh - remains unmodified.
  221. */
  222. static void Copy (aiMesh** dest, const aiMesh* src);
  223. // similar to Copy():
  224. static void Copy (aiMaterial** dest, const aiMaterial* src);
  225. static void Copy (aiTexture** dest, const aiTexture* src);
  226. static void Copy (aiAnimation** dest, const aiAnimation* src);
  227. static void Copy (aiCamera** dest, const aiCamera* src);
  228. static void Copy (aiBone** dest, const aiBone* src);
  229. static void Copy (aiLight** dest, const aiLight* src);
  230. static void Copy (aiNodeAnim** dest, const aiNodeAnim* src);
  231. // recursive, of course
  232. static void Copy (aiNode** dest, const aiNode* src);
  233. };
  234. }
  235. #endif // !! AI_SCENE_COMBINER_H_INC