GenVertexNormalsProcess.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 generate face
  35. * normals for all imported faces.
  36. */
  37. #include "GenVertexNormalsProcess.h"
  38. #include "SpatialSort.h"
  39. #include "../include/DefaultLogger.h"
  40. #include "../include/aiPostProcess.h"
  41. #include "../include/aiMesh.h"
  42. #include "../include/aiScene.h"
  43. #include "../include/assimp.hpp"
  44. using namespace Assimp;
  45. // ------------------------------------------------------------------------------------------------
  46. // Constructor to be privately used by Importer
  47. GenVertexNormalsProcess::GenVertexNormalsProcess()
  48. {
  49. this->configMaxAngle = AI_DEG_TO_RAD(175.f);
  50. }
  51. // ------------------------------------------------------------------------------------------------
  52. // Destructor, private as well
  53. GenVertexNormalsProcess::~GenVertexNormalsProcess()
  54. {
  55. // nothing to do here
  56. }
  57. // ------------------------------------------------------------------------------------------------
  58. // Returns whether the processing step is present in the given flag field.
  59. bool GenVertexNormalsProcess::IsActive( unsigned int pFlags) const
  60. {
  61. return (pFlags & aiProcess_GenSmoothNormals) != 0;
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // Executes the post processing step on the given imported data.
  65. void GenVertexNormalsProcess::SetupProperties(const Importer* pImp)
  66. {
  67. // get the current value of the property
  68. this->configMaxAngle = pImp->GetPropertyFloat(AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE,175.f);
  69. this->configMaxAngle = std::max(std::min(this->configMaxAngle,175.0f),0.0f);
  70. this->configMaxAngle = AI_DEG_TO_RAD(this->configMaxAngle);
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. // Executes the post processing step on the given imported data.
  74. void GenVertexNormalsProcess::Execute( aiScene* pScene)
  75. {
  76. DefaultLogger::get()->debug("GenVertexNormalsProcess begin");
  77. bool bHas = false;
  78. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  79. {
  80. if(this->GenMeshVertexNormals( pScene->mMeshes[a]))
  81. bHas = true;
  82. }
  83. if (bHas)
  84. {
  85. DefaultLogger::get()->info("GenVertexNormalsProcess finished. "
  86. "Vertex normals have been calculated");
  87. }
  88. else DefaultLogger::get()->debug("GenVertexNormalsProcess finished. "
  89. "Normals are already there");
  90. }
  91. // ------------------------------------------------------------------------------------------------
  92. // Executes the post processing step on the given imported data.
  93. bool GenVertexNormalsProcess::GenMeshVertexNormals (aiMesh* pMesh)
  94. {
  95. if (NULL != pMesh->mNormals)return false;
  96. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  97. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  98. {
  99. const aiFace& face = pMesh->mFaces[a];
  100. aiVector3D* pV1 = &pMesh->mVertices[face.mIndices[0]];
  101. aiVector3D* pV2 = &pMesh->mVertices[face.mIndices[1]];
  102. aiVector3D* pV3 = &pMesh->mVertices[face.mIndices[2]];
  103. aiVector3D pDelta1 = *pV2 - *pV1;
  104. aiVector3D pDelta2 = *pV3 - *pV1;
  105. aiVector3D vNor = pDelta1 ^ pDelta2;
  106. vNor.Normalize();
  107. for (unsigned int i = 0;i < face.mNumIndices;++i)
  108. pMesh->mNormals[face.mIndices[i]] = vNor;
  109. }
  110. // calculate the position bounds so we have a reliable epsilon to
  111. // check position differences against
  112. aiVector3D minVec( 1e10f, 1e10f, 1e10f), maxVec( -1e10f, -1e10f, -1e10f);
  113. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  114. {
  115. minVec.x = std::min( minVec.x, pMesh->mVertices[a].x);
  116. minVec.y = std::min( minVec.y, pMesh->mVertices[a].y);
  117. minVec.z = std::min( minVec.z, pMesh->mVertices[a].z);
  118. maxVec.x = std::max( maxVec.x, pMesh->mVertices[a].x);
  119. maxVec.y = std::max( maxVec.y, pMesh->mVertices[a].y);
  120. maxVec.z = std::max( maxVec.z, pMesh->mVertices[a].z);
  121. }
  122. const float posEpsilon = (maxVec - minVec).Length() * 1e-5f;
  123. // set up a SpatialSort to quickly find all vertices close to a given position
  124. SpatialSort vertexFinder( pMesh->mVertices, pMesh->mNumVertices, sizeof( aiVector3D));
  125. std::vector<unsigned int> verticesFound;
  126. const float fLimit = cosf(this->configMaxAngle);
  127. aiVector3D* pcNew = new aiVector3D[pMesh->mNumVertices];
  128. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  129. {
  130. const aiVector3D& posThis = pMesh->mVertices[i];
  131. // get all vertices that share this one ...
  132. vertexFinder.FindPositions( posThis, posEpsilon, verticesFound);
  133. aiVector3D pcNor;
  134. unsigned int div = 0;
  135. for (unsigned int a = 0; a < verticesFound.size(); ++a)
  136. {
  137. unsigned int vidx = verticesFound[a];
  138. // check whether the angle between the two normals is not too large
  139. if (pMesh->mNormals[vidx] * pMesh->mNormals[i] < fLimit)
  140. continue;
  141. pcNor += pMesh->mNormals[vidx];
  142. ++div;
  143. }
  144. pcNor.Normalize();
  145. pcNew[i] = pcNor;
  146. }
  147. delete[] pMesh->mNormals;
  148. pMesh->mNormals = pcNew;
  149. return true;
  150. }