2
0

MakeVerboseFormat.cpp 8.5 KB

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