You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

177 lines
6.4 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 to invert
  35. * all normals in meshes with infacing normals.
  36. */
  37. #include "AssimpPCH.h"
  38. // internal headers
  39. #include "FixNormalsStep.h"
  40. using namespace Assimp;
  41. // ------------------------------------------------------------------------------------------------
  42. // Constructor to be privately used by Importer
  43. FixInfacingNormalsProcess::FixInfacingNormalsProcess()
  44. {
  45. // nothing to do here
  46. }
  47. // ------------------------------------------------------------------------------------------------
  48. // Destructor, private as well
  49. FixInfacingNormalsProcess::~FixInfacingNormalsProcess()
  50. {
  51. // nothing to do here
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Returns whether the processing step is present in the given flag field.
  55. bool FixInfacingNormalsProcess::IsActive( unsigned int pFlags) const
  56. {
  57. return (pFlags & aiProcess_FixInfacingNormals) != 0;
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. // Executes the post processing step on the given imported data.
  61. void FixInfacingNormalsProcess::Execute( aiScene* pScene)
  62. {
  63. DefaultLogger::get()->debug("FixInfacingNormalsProcess begin");
  64. bool bHas = false;
  65. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  66. if(ProcessMesh( pScene->mMeshes[a],a))bHas = true;
  67. if (bHas)
  68. DefaultLogger::get()->debug("FixInfacingNormalsProcess finished. Found issues.");
  69. else DefaultLogger::get()->debug("FixInfacingNormalsProcess finished. No changes to the scene.");
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. // Apply the step to the mesh
  73. bool FixInfacingNormalsProcess::ProcessMesh( aiMesh* pcMesh, unsigned int index)
  74. {
  75. ai_assert(NULL != pcMesh);
  76. // Nothing to do if there are no model normals
  77. if (!pcMesh->HasNormals())return false;
  78. // Compute the bounding box of both the model vertices + normals and
  79. // the umodified model vertices. Then check whether the first BB
  80. // is smaller than the second. In this case we can assume that the
  81. // normals need to be flipped, although there are a few special cases ..
  82. // convex, concave, planar models ...
  83. aiVector3D vMin0 (1e10f,1e10f,1e10f);
  84. aiVector3D vMin1 (1e10f,1e10f,1e10f);
  85. aiVector3D vMax0 (-1e10f,-1e10f,-1e10f);
  86. aiVector3D vMax1 (-1e10f,-1e10f,-1e10f);
  87. for (unsigned int i = 0; i < pcMesh->mNumVertices;++i)
  88. {
  89. vMin1.x = std::min(vMin1.x,pcMesh->mVertices[i].x);
  90. vMin1.y = std::min(vMin1.y,pcMesh->mVertices[i].y);
  91. vMin1.z = std::min(vMin1.z,pcMesh->mVertices[i].z);
  92. vMax1.x = std::max(vMax1.x,pcMesh->mVertices[i].x);
  93. vMax1.y = std::max(vMax1.y,pcMesh->mVertices[i].y);
  94. vMax1.z = std::max(vMax1.z,pcMesh->mVertices[i].z);
  95. const aiVector3D vWithNormal = pcMesh->mVertices[i] + pcMesh->mNormals[i];
  96. vMin0.x = std::min(vMin0.x,vWithNormal.x);
  97. vMin0.y = std::min(vMin0.y,vWithNormal.y);
  98. vMin0.z = std::min(vMin0.z,vWithNormal.z);
  99. vMax0.x = std::max(vMax0.x,vWithNormal.x);
  100. vMax0.y = std::max(vMax0.y,vWithNormal.y);
  101. vMax0.z = std::max(vMax0.z,vWithNormal.z);
  102. }
  103. const float fDelta0_x = (vMax0.x - vMin0.x);
  104. const float fDelta0_y = (vMax0.y - vMin0.y);
  105. const float fDelta0_z = (vMax0.z - vMin0.z);
  106. const float fDelta1_x = (vMax1.x - vMin1.x);
  107. const float fDelta1_y = (vMax1.y - vMin1.y);
  108. const float fDelta1_z = (vMax1.z - vMin1.z);
  109. // Check whether the boxes are overlapping
  110. if ((fDelta0_x > 0.0f) != (fDelta1_x > 0.0f))return false;
  111. if ((fDelta0_y > 0.0f) != (fDelta1_y > 0.0f))return false;
  112. if ((fDelta0_z > 0.0f) != (fDelta1_z > 0.0f))return false;
  113. // Check whether this is a planar surface
  114. const float fDelta1_yz = fDelta1_y * fDelta1_z;
  115. if (fDelta1_x < 0.05f * sqrtf( fDelta1_yz ))return false;
  116. if (fDelta1_y < 0.05f * sqrtf( fDelta1_z * fDelta1_x ))return false;
  117. if (fDelta1_z < 0.05f * sqrtf( fDelta1_y * fDelta1_x ))return false;
  118. // now compare the volumes of the bounding boxes
  119. if (::fabsf(fDelta0_x * fDelta1_yz) <
  120. ::fabsf(fDelta1_x * fDelta1_y * fDelta1_z))
  121. {
  122. if (!DefaultLogger::isNullLogger())
  123. {
  124. char buffer[128]; // should be sufficiently large
  125. ::sprintf(buffer,"Mesh %i: Normals are facing inwards (or the mesh is planar)",index);
  126. DefaultLogger::get()->info(buffer);
  127. }
  128. // Invert normals
  129. for (unsigned int i = 0; i < pcMesh->mNumVertices;++i)
  130. pcMesh->mNormals[i] *= -1.0f;
  131. // ... and flip faces
  132. for (unsigned int i = 0; i < pcMesh->mNumFaces;++i)
  133. {
  134. aiFace& face = pcMesh->mFaces[i];
  135. for( unsigned int b = 0; b < face.mNumIndices / 2; b++)
  136. std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]);
  137. }
  138. return true;
  139. }
  140. return false;
  141. }