SceneCombiner.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 Implements Assimp::SceneCombiner. This is a smart utility
  34. * class that can be used to combine several scenes, meshes, ...
  35. * in one.
  36. */
  37. #include "AssimpPCH.h"
  38. #include "SceneCombiner.h"
  39. #include "fast_atof.h"
  40. namespace Assimp
  41. {
  42. // ------------------------------------------------------------------------------------------------
  43. // Add a prefix to a string
  44. inline void PrefixString(aiString& string,const char* prefix, unsigned int len)
  45. {
  46. // Add the prefix
  47. ::memmove(string.data+len,string.data,string.length+1);
  48. ::memcpy (string.data, prefix, len);
  49. // And update the string's length
  50. string.length += len;
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. // Add a name prefix to all nodes in a hierarchy
  54. void SceneCombiner::AddNodePrefixes(aiNode* node, const char* prefix, unsigned int len)
  55. {
  56. ai_assert(NULL != prefix);
  57. PrefixString(node->mName,prefix,len);
  58. // Process all children recursively
  59. for (unsigned int i = 0; i < node->mNumChildren;++i)
  60. AddNodePrefixes(node->mChildren[i],prefix,len);
  61. }
  62. // ------------------------------------------------------------------------------------------------
  63. // Merges two scenes. Currently only used by the LWS loader.
  64. void SceneCombiner::MergeScenes(aiScene* dest,std::vector<aiScene*>& src,
  65. unsigned int flags)
  66. {
  67. ai_assert(NULL != dest);
  68. ai_assert(0 == dest->mNumTextures);
  69. ai_assert(0 == dest->mNumLights);
  70. ai_assert(0 == dest->mNumCameras);
  71. ai_assert(0 == dest->mNumMeshes);
  72. ai_assert(0 == dest->mNumMaterials);
  73. if (src.empty())return;
  74. // some iterators will be needed multiple times
  75. std::vector<aiScene*>::iterator begin = src.begin(),
  76. end = src.end(), cur;
  77. // this helper array is used as lookup table several times
  78. std::vector<unsigned int> offset(src.size());
  79. std::vector<unsigned int>::iterator ofsbegin = offset.begin(),
  80. offend = offset.end(), ofscur;
  81. unsigned int cnt;
  82. bool bNeedPrefix = false;
  83. // First find out how large the respective output arrays must be
  84. for ( cur = begin; cur != end; ++cur)
  85. {
  86. dest->mNumTextures += (*cur)->mNumTextures;
  87. dest->mNumMaterials += (*cur)->mNumMaterials;
  88. dest->mNumMeshes += (*cur)->mNumMeshes;
  89. dest->mNumLights += (*cur)->mNumLights;
  90. dest->mNumCameras += (*cur)->mNumCameras;
  91. dest->mNumAnimations += (*cur)->mNumAnimations;
  92. if ((*cur)->mNumAnimations > 0 ||
  93. (*cur)->mNumCameras > 0 ||
  94. (*cur)->mNumLights > 0)
  95. {
  96. bNeedPrefix = true;
  97. }
  98. }
  99. // generate the output texture list + an offset table
  100. if (dest->mNumTextures)
  101. {
  102. aiTexture** pip = dest->mTextures = new aiTexture*[dest->mNumMaterials];
  103. for ( cur = begin, ofscur = ofsbegin,cnt = 0; cur != end; ++cur,++ofscur)
  104. {
  105. for (unsigned int i = 0; i < (*cur)->mNumTextures;++i,++pip)
  106. *pip = (*cur)->mTextures[i];
  107. *ofscur = cnt;
  108. cnt += (*cur)->mNumTextures;
  109. }
  110. }
  111. // generate the output material list + an offset table
  112. if (dest->mNumMaterials)
  113. {
  114. aiMaterial** pip = dest->mMaterials = new aiMaterial*[dest->mNumMaterials];
  115. for ( cur = begin, ofscur = ofsbegin,cnt = 0; cur != end; ++cur,++ofscur)
  116. {
  117. for (unsigned int i = 0; i < (*cur)->mNumMaterials;++i,++pip)
  118. {
  119. *pip = (*cur)->mMaterials[i];
  120. if ((*cur)->mNumTextures != dest->mNumTextures)
  121. {
  122. // We need to update all texture indices of the mesh.
  123. // So we need to search for a material property like
  124. // that follows the following pattern: "$tex.file.<s>.<n>"
  125. // where s is the texture type (i.e. diffuse) and n is
  126. // the index of the texture.
  127. for (unsigned int a = 0; a < (*pip)->mNumProperties;++a)
  128. {
  129. aiMaterialProperty* prop = (*pip)->mProperties[a];
  130. if (!strncmp(prop->mKey.data,"$tex.file",9))
  131. {
  132. // Check whether this texture is an embedded texture.
  133. // In this case the property looks like this: *<n>,
  134. // where n is the index of the texture.
  135. aiString& s = *((aiString*)prop->mData);
  136. if ('*' == s.data[0])
  137. {
  138. // Offset the index and write it back ..
  139. const unsigned int idx = strtol10(&s.data[1]) + *ofscur;
  140. itoa10(&s.data[1],sizeof(s.data)-1,idx);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. *ofscur = cnt;
  147. cnt += (*cur)->mNumMaterials;
  148. }
  149. }
  150. // generate the output mesh list + again an offset table
  151. if (dest->mNumMeshes)
  152. {
  153. aiMesh** pip = dest->mMeshes = new aiMesh*[dest->mNumMeshes];
  154. for ( cur = begin, ofscur = ofsbegin,cnt = 0; cur != end; ++cur,++ofscur)
  155. {
  156. for (unsigned int i = 0; i < (*cur)->mNumMeshes;++i,++pip)
  157. {
  158. *pip = (*cur)->mMeshes[i];
  159. // update the material index of the mesh
  160. (*pip)->mMaterialIndex += *ofscur;
  161. }
  162. // reuse the offset array - store now the mesh offset in it
  163. *ofscur = cnt;
  164. cnt += (*cur)->mNumMeshes;
  165. }
  166. }
  167. // Now generate the output node graph. We need to make those
  168. // names in the graph that are referenced by anims or lights
  169. // or cameras unique. So we add a prefix to them ... $SC1_
  170. // First step for now: find out which nodes are referenced by
  171. // anim bones or cameras and add the prefix to their names.
  172. if (bNeedPrefix)
  173. {
  174. // Allocate space for light sources, cameras and animations
  175. aiLight** ppLights = dest->mLights = (dest->mNumLights
  176. ? new aiLight*[dest->mNumLights] : NULL);
  177. aiCamera** ppCameras = dest->mCameras = (dest->mNumCameras
  178. ? new aiCamera*[dest->mNumCameras] : NULL);
  179. aiAnimation** ppAnims = dest->mAnimations = (dest->mNumAnimations
  180. ? new aiAnimation*[dest->mNumAnimations] : NULL);
  181. for (cur = begin, cnt = 0; cur != end; ++cur)
  182. {
  183. char buffer[10];
  184. buffer[0] = '$';
  185. buffer[1] = 'S';
  186. buffer[2] = 'C';
  187. char* sz = &buffer[itoa10(&buffer[3],sizeof(buffer)-3, cnt++)+2];
  188. *sz++ = '_';
  189. *sz++ = '\0';
  190. const unsigned int len = (unsigned int)::strlen(buffer);
  191. AddNodePrefixes((*cur)->mRootNode,buffer,len);
  192. // Copy light sources, add the prefix to them, too
  193. for (unsigned int i = 0; i < (*cur)->mNumLights;++i,++ppLights)
  194. {
  195. *ppLights = (*cur)->mLights[i];
  196. PrefixString((*ppLights)->mName,buffer,len);
  197. }
  198. // Copy cameras, add the prefix to them, too
  199. for (unsigned int i = 0; i < (*cur)->mNumCameras;++i,++ppCameras)
  200. {
  201. *ppCameras = (*cur)->mCameras[i];
  202. PrefixString((*ppCameras)->mName,buffer,len);
  203. }
  204. // Copy animations, add the prefix to them, too
  205. for (unsigned int i = 0; i < (*cur)->mNumAnimations;++i,++ppAnims)
  206. {
  207. *ppAnims = (*cur)->mAnimations[i];
  208. PrefixString((*ppAnims)->mName,buffer,len);
  209. }
  210. }
  211. }
  212. // now delete all input scenes
  213. for (cur = begin; cur != end; ++cur)
  214. {
  215. aiScene* deleteMe = *cur;
  216. // We need to delete the arrays before the destructor is called -
  217. // we are reusing the array members
  218. delete[] deleteMe->mMeshes; deleteMe->mMeshes = NULL;
  219. delete[] deleteMe->mCameras; deleteMe->mCameras = NULL;
  220. delete[] deleteMe->mLights; deleteMe->mLights = NULL;
  221. delete[] deleteMe->mMaterials; deleteMe->mMaterials = NULL;
  222. delete[] deleteMe->mAnimations; deleteMe->mAnimations = NULL;
  223. delete[] deleteMe->mRootNode->mChildren;
  224. deleteMe->mRootNode->mChildren = NULL;
  225. // Now we can safely delete the scene
  226. delete[] deleteMe;
  227. AI_DEBUG_INVALIDATE_PTR(*cur);
  228. }
  229. }
  230. // ------------------------------------------------------------------------------------------------
  231. void SceneCombiner::MergeMeshes(aiMesh* dest,std::vector<aiMesh*>& src,
  232. unsigned int flags)
  233. {
  234. }
  235. }