Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

244 righe
8.3 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file OptimizeMeshes.cpp
  35. * @brief Implementation of the aiProcess_OptimizeMeshes step
  36. */
  37. #include "AssimpPCH.h"
  38. #ifndef ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS
  39. using namespace Assimp;
  40. #include "OptimizeMeshes.h"
  41. #include "ProcessHelper.h"
  42. #include "SceneCombiner.h"
  43. // ------------------------------------------------------------------------------------------------
  44. // Constructor to be privately used by Importer
  45. OptimizeMeshesProcess::OptimizeMeshesProcess()
  46. : pts (false)
  47. , max_verts (0xffffffff)
  48. , max_faces (0xffffffff)
  49. {}
  50. // ------------------------------------------------------------------------------------------------
  51. // Destructor, private as well
  52. OptimizeMeshesProcess::~OptimizeMeshesProcess()
  53. {}
  54. // ------------------------------------------------------------------------------------------------
  55. // Returns whether the processing step is present in the given flag field.
  56. bool OptimizeMeshesProcess::IsActive( unsigned int pFlags) const
  57. {
  58. // Our behaviour needs to be different if the SortByPType or SplitLargeMeshes
  59. // steps are active. Thus we need to query their flags here and store the
  60. // information, although we're breaking const-correctness.
  61. // That's a serious design flaw, consider redesign.
  62. if( 0 != (pFlags & aiProcess_OptimizeMeshes) ) {
  63. pts = (0 != (pFlags & aiProcess_SortByPType));
  64. max_verts = (0 != (pFlags & aiProcess_SplitLargeMeshes)) ? 0xdeadbeef : max_verts;
  65. return true;
  66. }
  67. return false;
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. // Setup properties for the postprocessing step
  71. void OptimizeMeshesProcess::SetupProperties(const Importer* pImp)
  72. {
  73. if (max_verts == 0xdeadbeef /* magic hack */) {
  74. max_faces = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT,AI_SLM_DEFAULT_MAX_TRIANGLES);
  75. max_verts = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
  76. }
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Execute step
  80. void OptimizeMeshesProcess::Execute( aiScene* pScene)
  81. {
  82. const unsigned int num_old = pScene->mNumMeshes;
  83. if (num_old <= 1) {
  84. DefaultLogger::get()->debug("Skipping OptimizeMeshesProcess");
  85. return;
  86. }
  87. DefaultLogger::get()->debug("OptimizeMeshesProcess begin");
  88. mScene = pScene;
  89. // need to clear persistent members from previous runs
  90. merge_list.clear();
  91. output.clear();
  92. merge_list.reserve(pScene->mNumMeshes);
  93. output.reserve(pScene->mNumMeshes);
  94. // Prepare lookup tables
  95. meshes.resize(pScene->mNumMeshes);
  96. FindInstancedMeshes(pScene->mRootNode);
  97. if (max_verts == 0xdeadbeef) /* undo the magic hack */
  98. max_verts = 0xffffffff;
  99. // ... instanced meshes are immediately processed and added to the output list
  100. for (unsigned int i = 0, n = 0; i < pScene->mNumMeshes;++i) {
  101. meshes[i].vertex_format = GetMeshVFormatUnique(pScene->mMeshes[i]);
  102. if (meshes[i].instance_cnt > 1 && meshes[i].output_id == 0xffffffff) {
  103. meshes[i].output_id = n++;
  104. output.push_back(mScene->mMeshes[i]);
  105. }
  106. }
  107. // and process all nodes in the scenegraoh recursively
  108. ProcessNode(pScene->mRootNode);
  109. if (!output.size()) {
  110. throw DeadlyImportError("OptimizeMeshes: No meshes remaining; there's definitely something wrong");
  111. }
  112. meshes.clear();
  113. ai_assert(output.size() <= num_old);
  114. mScene->mNumMeshes = output.size();
  115. std::copy(output.begin(),output.end(),mScene->mMeshes);
  116. if (output.size() != num_old) {
  117. char tmp[512];
  118. ::sprintf(tmp,"OptimizeMeshesProcess finished. Input meshes: %i, Output meshes: %i",num_old,pScene->mNumMeshes);
  119. DefaultLogger::get()->info(tmp);
  120. }
  121. else DefaultLogger::get()->debug("OptimizeMeshesProcess finished");
  122. }
  123. // ------------------------------------------------------------------------------------------------
  124. // Process meshes for a single node
  125. void OptimizeMeshesProcess::ProcessNode( aiNode* pNode)
  126. {
  127. for (unsigned int i = 0; i < pNode->mNumMeshes;++i) {
  128. unsigned int& im = pNode->mMeshes[i];
  129. if (meshes[im].instance_cnt > 1) {
  130. im = meshes[im].output_id;
  131. }
  132. else {
  133. merge_list.clear();
  134. unsigned int verts = 0, faces = 0;
  135. // Find meshes to merge with us
  136. for (unsigned int a = i+1; a < pNode->mNumMeshes;++a) {
  137. register unsigned int am = pNode->mMeshes[a];
  138. if (meshes[am].instance_cnt == 1 && CanJoin(im,am,verts,faces)) {
  139. merge_list.push_back(mScene->mMeshes[am]);
  140. verts += mScene->mMeshes[am]->mNumVertices;
  141. faces += mScene->mMeshes[am]->mNumFaces;
  142. --pNode->mNumMeshes;
  143. for (unsigned int n = a; n < pNode->mNumMeshes; ++n)
  144. pNode->mMeshes[n] = pNode->mMeshes[n+1];
  145. --a;
  146. }
  147. }
  148. // and merge all meshes which we found, replace the old ones
  149. if (!merge_list.empty()) {
  150. merge_list.push_back(mScene->mMeshes[im]);
  151. aiMesh* out;
  152. SceneCombiner::MergeMeshes(&out,0,merge_list.begin(),merge_list.end());
  153. output.push_back(out);
  154. }
  155. else {
  156. output.push_back(mScene->mMeshes[im]);
  157. }
  158. im = output.size()-1;
  159. }
  160. }
  161. for (unsigned int i = 0; i < pNode->mNumChildren; ++i)
  162. ProcessNode(pNode->mChildren[i]);
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. // Check whether two meshes can be joined
  166. bool OptimizeMeshesProcess::CanJoin ( unsigned int a, unsigned int b, unsigned int verts, unsigned int faces )
  167. {
  168. if (meshes[a].vertex_format != meshes[b].vertex_format)
  169. return false;
  170. aiMesh* ma = mScene->mMeshes[a], *mb = mScene->mMeshes[b];
  171. if ((0xffffffff != max_verts && verts+mb->mNumVertices > max_verts) ||
  172. (0xffffffff != max_faces && faces+mb->mNumFaces > max_faces)) {
  173. return false;
  174. }
  175. // Never merge unskinned meshes with skinned meshes
  176. if (ma->mMaterialIndex != mb->mMaterialIndex || ma->HasBones() != mb->HasBones())
  177. return false;
  178. // Never merge meshes with different kinds of primitives if SortByPType did already
  179. // do its work. We would destroy everything again ...
  180. if (pts && ma->mPrimitiveTypes != mb->mPrimitiveTypes)
  181. return false;
  182. // If both meshes are skinned, check whether we have many bones defined in both meshes.
  183. // If yes, we can savely join them.
  184. if (ma->HasBones()) {
  185. // TODO
  186. return false;
  187. }
  188. return true;
  189. }
  190. // ------------------------------------------------------------------------------------------------
  191. // Buidl a LUT of all instanced meshes
  192. void OptimizeMeshesProcess::FindInstancedMeshes (aiNode* pNode)
  193. {
  194. for (unsigned int i = 0; i < pNode->mNumMeshes;++i)
  195. ++meshes[pNode->mMeshes[i]].instance_cnt;
  196. for (unsigned int i = 0; i < pNode->mNumChildren; ++i)
  197. FindInstancedMeshes(pNode->mChildren[i]);
  198. }
  199. #endif // !! ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS