2
0

BlenderBMesh.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp 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 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 BlenderBMesh.cpp
  34. * @brief Conversion of Blender's new BMesh stuff
  35. */
  36. #ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
  37. #include "BlenderBMesh.h"
  38. #include "BlenderDNA.h"
  39. #include "BlenderScene.h"
  40. #include "BlenderTessellator.h"
  41. namespace Assimp {
  42. template <>
  43. const char *LogFunctions<BlenderBMeshConverter>::Prefix() {
  44. return "BLEND_BMESH: ";
  45. }
  46. } // namespace Assimp
  47. using namespace Assimp;
  48. using namespace Assimp::Blender;
  49. using namespace Assimp::Formatter;
  50. // ------------------------------------------------------------------------------------------------
  51. BlenderBMeshConverter::BlenderBMeshConverter(const Mesh *mesh) :
  52. BMesh(mesh),
  53. triMesh(nullptr) {
  54. ai_assert(nullptr != mesh);
  55. }
  56. // ------------------------------------------------------------------------------------------------
  57. BlenderBMeshConverter::~BlenderBMeshConverter() {
  58. DestroyTriMesh();
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. bool BlenderBMeshConverter::ContainsBMesh() const {
  62. // TODO - Should probably do some additional verification here
  63. return BMesh->totpoly && BMesh->totloop && BMesh->totvert;
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. const Mesh *BlenderBMeshConverter::TriangulateBMesh() {
  67. AssertValidMesh();
  68. AssertValidSizes();
  69. PrepareTriMesh();
  70. for (int i = 0; i < BMesh->totpoly; ++i) {
  71. const MPoly &poly = BMesh->mpoly[i];
  72. ConvertPolyToFaces(poly);
  73. }
  74. return triMesh;
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. void BlenderBMeshConverter::AssertValidMesh() {
  78. if (!ContainsBMesh()) {
  79. ThrowException("BlenderBMeshConverter requires a BMesh with \"polygons\" - please call BlenderBMeshConverter::ContainsBMesh to check this first");
  80. }
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. void BlenderBMeshConverter::AssertValidSizes() {
  84. if (BMesh->totpoly != static_cast<int>(BMesh->mpoly.size())) {
  85. ThrowException("BMesh poly array has incorrect size");
  86. }
  87. if (BMesh->totloop != static_cast<int>(BMesh->mloop.size())) {
  88. ThrowException("BMesh loop array has incorrect size");
  89. }
  90. }
  91. // ------------------------------------------------------------------------------------------------
  92. void BlenderBMeshConverter::PrepareTriMesh() {
  93. if (triMesh) {
  94. DestroyTriMesh();
  95. }
  96. triMesh = new Mesh(*BMesh);
  97. triMesh->totface = 0;
  98. triMesh->mface.clear();
  99. }
  100. // ------------------------------------------------------------------------------------------------
  101. void BlenderBMeshConverter::DestroyTriMesh() {
  102. delete triMesh;
  103. triMesh = nullptr;
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. void BlenderBMeshConverter::ConvertPolyToFaces(const MPoly &poly) {
  107. const MLoop *polyLoop = &BMesh->mloop[poly.loopstart];
  108. if (poly.totloop == 3 || poly.totloop == 4) {
  109. AddFace(polyLoop[0].v, polyLoop[1].v, polyLoop[2].v, poly.totloop == 4 ? polyLoop[3].v : 0);
  110. // UVs are optional, so only convert when present.
  111. if (BMesh->mloopuv.size()) {
  112. if ((poly.loopstart + poly.totloop) > static_cast<int>(BMesh->mloopuv.size())) {
  113. ThrowException("BMesh uv loop array has incorrect size");
  114. }
  115. const MLoopUV *loopUV = &BMesh->mloopuv[poly.loopstart];
  116. AddTFace(loopUV[0].uv, loopUV[1].uv, loopUV[2].uv, poly.totloop == 4 ? loopUV[3].uv : nullptr);
  117. }
  118. } else if (poly.totloop > 4) {
  119. #if ASSIMP_BLEND_WITH_GLU_TESSELLATE
  120. BlenderTessellatorGL tessGL(*this);
  121. tessGL.Tessellate(polyLoop, poly.totloop, triMesh->mvert);
  122. #elif ASSIMP_BLEND_WITH_POLY_2_TRI
  123. BlenderTessellatorP2T tessP2T(*this);
  124. tessP2T.Tessellate(polyLoop, poly.totloop, triMesh->mvert);
  125. #endif
  126. }
  127. }
  128. // ------------------------------------------------------------------------------------------------
  129. void BlenderBMeshConverter::AddFace(int v1, int v2, int v3, int v4) {
  130. MFace face;
  131. face.v1 = v1;
  132. face.v2 = v2;
  133. face.v3 = v3;
  134. face.v4 = v4;
  135. face.flag = 0;
  136. // TODO - Work out how materials work
  137. face.mat_nr = 0;
  138. triMesh->mface.push_back(face);
  139. triMesh->totface = static_cast<int>(triMesh->mface.size());
  140. }
  141. // ------------------------------------------------------------------------------------------------
  142. void BlenderBMeshConverter::AddTFace(const float *uv1, const float *uv2, const float *uv3, const float *uv4) {
  143. MTFace mtface;
  144. memcpy(&mtface.uv[0], uv1, sizeof(float) * 2);
  145. memcpy(&mtface.uv[1], uv2, sizeof(float) * 2);
  146. memcpy(&mtface.uv[2], uv3, sizeof(float) * 2);
  147. if (uv4) {
  148. memcpy(&mtface.uv[3], uv4, sizeof(float) * 2);
  149. }
  150. triMesh->mtface.push_back(mtface);
  151. }
  152. #endif // ASSIMP_BUILD_NO_BLEND_IMPORTER