您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

216 行
6.7 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 Implementation of the post processing step "MakeVerboseFormat"
  35. */
  36. #include "AssimpPCH.h"
  37. #include "MakeVerboseFormat.h"
  38. using namespace Assimp;
  39. // ------------------------------------------------------------------------------------------------
  40. MakeVerboseFormatProcess::MakeVerboseFormatProcess()
  41. {
  42. // nothing to do here
  43. }
  44. // ------------------------------------------------------------------------------------------------
  45. MakeVerboseFormatProcess::~MakeVerboseFormatProcess()
  46. {
  47. // nothing to do here
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Executes the post processing step on the given imported data.
  51. void MakeVerboseFormatProcess::Execute( aiScene* pScene)
  52. {
  53. ai_assert(NULL != pScene);
  54. DefaultLogger::get()->debug("MakeVerboseFormatProcess begin");
  55. bool bHas = false;
  56. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  57. {
  58. if( MakeVerboseFormat( pScene->mMeshes[a]))
  59. bHas = true;
  60. }
  61. if (bHas) DefaultLogger::get()->info("MakeVerboseFormatProcess finished. There was much work to do ...");
  62. else DefaultLogger::get()->debug("MakeVerboseFormatProcess. There was nothing to do.");
  63. pScene->mFlags &= ~AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. // Executes the post processing step on the given imported data.
  67. bool MakeVerboseFormatProcess::MakeVerboseFormat(aiMesh* pcMesh)
  68. {
  69. ai_assert(NULL != pcMesh);
  70. unsigned int iOldNumVertices = pcMesh->mNumVertices;
  71. const unsigned int iNumVerts = pcMesh->mNumFaces*3;
  72. aiVector3D* pvPositions = new aiVector3D[ iNumVerts ];
  73. aiVector3D* pvNormals = NULL;
  74. if (pcMesh->HasNormals())
  75. {
  76. pvNormals = new aiVector3D[iNumVerts];
  77. }
  78. aiVector3D* pvTangents = NULL, *pvBitangents = NULL;
  79. if (pcMesh->HasTangentsAndBitangents())
  80. {
  81. pvTangents = new aiVector3D[iNumVerts];
  82. pvBitangents = new aiVector3D[iNumVerts];
  83. }
  84. aiVector3D* apvTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS] = {0};
  85. aiColor4D* apvColorSets[AI_MAX_NUMBER_OF_COLOR_SETS] = {0};
  86. unsigned int p = 0;
  87. while (pcMesh->HasTextureCoords(p))
  88. apvTextureCoords[p++] = new aiVector3D[iNumVerts];
  89. p = 0;
  90. while (pcMesh->HasVertexColors(p))
  91. apvColorSets[p++] = new aiColor4D[iNumVerts];
  92. // allocate enough memory to hold output bones and vertex weights ...
  93. std::vector<aiVertexWeight>* newWeights = new std::vector<aiVertexWeight>[pcMesh->mNumBones];
  94. for (unsigned int i = 0;i < pcMesh->mNumBones;++i) {
  95. newWeights[i].reserve(pcMesh->mBones[i]->mNumWeights*3);
  96. }
  97. // iterate through all faces and build a clean list
  98. unsigned int iIndex = 0;
  99. for (unsigned int a = 0; a< pcMesh->mNumFaces;++a)
  100. {
  101. aiFace* pcFace = &pcMesh->mFaces[a];
  102. for (unsigned int q = 0; q < pcFace->mNumIndices;++q,++iIndex)
  103. {
  104. // need to build a clean list of bones, too
  105. for (unsigned int i = 0;i < pcMesh->mNumBones;++i)
  106. {
  107. for (unsigned int a = 0; a < pcMesh->mBones[i]->mNumWeights;a++)
  108. {
  109. const aiVertexWeight& w = pcMesh->mBones[i]->mWeights[a];
  110. if(pcFace->mIndices[q] == w.mVertexId)
  111. {
  112. aiVertexWeight wNew;
  113. wNew.mVertexId = iIndex;
  114. wNew.mWeight = w.mWeight;
  115. newWeights[i].push_back(wNew);
  116. }
  117. }
  118. }
  119. pvPositions[iIndex] = pcMesh->mVertices[pcFace->mIndices[q]];
  120. if (pcMesh->HasNormals())
  121. {
  122. pvNormals[iIndex] = pcMesh->mNormals[pcFace->mIndices[q]];
  123. }
  124. if (pcMesh->HasTangentsAndBitangents())
  125. {
  126. pvTangents[iIndex] = pcMesh->mTangents[pcFace->mIndices[q]];
  127. pvBitangents[iIndex] = pcMesh->mBitangents[pcFace->mIndices[q]];
  128. }
  129. unsigned int p = 0;
  130. while (pcMesh->HasTextureCoords(p))
  131. {
  132. apvTextureCoords[p][iIndex] = pcMesh->mTextureCoords[p][pcFace->mIndices[q]];
  133. ++p;
  134. }
  135. p = 0;
  136. while (pcMesh->HasVertexColors(p))
  137. {
  138. apvColorSets[p][iIndex] = pcMesh->mColors[p][pcFace->mIndices[q]];
  139. ++p;
  140. }
  141. pcFace->mIndices[q] = iIndex;
  142. }
  143. }
  144. // build output vertex weights
  145. for (unsigned int i = 0;i < pcMesh->mNumBones;++i)
  146. {
  147. delete pcMesh->mBones[i]->mWeights;
  148. if (!newWeights[i].empty())
  149. {
  150. pcMesh->mBones[i]->mWeights = new aiVertexWeight[newWeights[i].size()];
  151. memcpy(pcMesh->mBones[i]->mWeights,&newWeights[i][0],
  152. sizeof(aiVertexWeight) * newWeights[i].size());
  153. }
  154. else pcMesh->mBones[i]->mWeights = NULL;
  155. }
  156. // delete the old members
  157. delete[] pcMesh->mVertices;
  158. pcMesh->mVertices = pvPositions;
  159. p = 0;
  160. while (pcMesh->HasTextureCoords(p))
  161. {
  162. delete pcMesh->mTextureCoords[p];
  163. pcMesh->mTextureCoords[p] = apvTextureCoords[p];
  164. ++p;
  165. }
  166. p = 0;
  167. while (pcMesh->HasVertexColors(p))
  168. {
  169. delete pcMesh->mColors[p];
  170. pcMesh->mColors[p] = apvColorSets[p];
  171. ++p;
  172. }
  173. pcMesh->mNumVertices = iNumVerts;
  174. if (pcMesh->HasNormals())
  175. {
  176. delete[] pcMesh->mNormals;
  177. pcMesh->mNormals = pvNormals;
  178. }
  179. if (pcMesh->HasTangentsAndBitangents())
  180. {
  181. delete[] pcMesh->mTangents;
  182. pcMesh->mTangents = pvTangents;
  183. delete[] pcMesh->mBitangents;
  184. pcMesh->mBitangents = pvBitangents;
  185. }
  186. return (pcMesh->mNumVertices != iOldNumVertices);
  187. }