2
0

GenFaceNormalsProcess.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2024, 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 generate face
  35. * normals for all imported faces.
  36. */
  37. #include "GenFaceNormalsProcess.h"
  38. #include <assimp/Exceptional.h>
  39. #include <assimp/postprocess.h>
  40. #include <assimp/qnan.h>
  41. #include <assimp/scene.h>
  42. #include <assimp/DefaultLogger.hpp>
  43. using namespace Assimp;
  44. // ------------------------------------------------------------------------------------------------
  45. // Returns whether the processing step is present in the given flag field.
  46. bool GenFaceNormalsProcess::IsActive(unsigned int pFlags) const {
  47. force_ = (pFlags & aiProcess_ForceGenNormals) != 0;
  48. flippedWindingOrder_ = (pFlags & aiProcess_FlipWindingOrder) != 0;
  49. leftHanded_ = (pFlags & aiProcess_MakeLeftHanded) != 0;
  50. return (pFlags & aiProcess_GenNormals) != 0;
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. // Executes the post processing step on the given imported data.
  54. void GenFaceNormalsProcess::Execute(aiScene *pScene) {
  55. ASSIMP_LOG_DEBUG("GenFaceNormalsProcess begin");
  56. if (pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
  57. throw DeadlyImportError("Post-processing order mismatch: expecting pseudo-indexed (\"verbose\") vertices here");
  58. }
  59. bool bHas = false;
  60. for (unsigned int a = 0; a < pScene->mNumMeshes; a++) {
  61. if (this->GenMeshFaceNormals(pScene->mMeshes[a])) {
  62. bHas = true;
  63. }
  64. }
  65. if (bHas) {
  66. ASSIMP_LOG_INFO("GenFaceNormalsProcess finished. "
  67. "Face normals have been calculated");
  68. } else {
  69. ASSIMP_LOG_DEBUG("GenFaceNormalsProcess finished. "
  70. "Normals are already there");
  71. }
  72. }
  73. // ------------------------------------------------------------------------------------------------
  74. // Executes the post processing step on the given imported data.
  75. bool GenFaceNormalsProcess::GenMeshFaceNormals(aiMesh *pMesh) {
  76. if (nullptr != pMesh->mNormals) {
  77. if (force_) {
  78. delete[] pMesh->mNormals;
  79. } else {
  80. return false;
  81. }
  82. }
  83. // If the mesh consists of lines and/or points but not of
  84. // triangles or higher-order polygons the normal vectors
  85. // are undefined.
  86. if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON))) {
  87. ASSIMP_LOG_INFO("Normal vectors are undefined for line and point meshes");
  88. return false;
  89. }
  90. // allocate an array to hold the output normals
  91. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  92. const float qnan = get_qnan();
  93. // iterate through all faces and compute per-face normals but store them per-vertex.
  94. for (unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  95. const aiFace &face = pMesh->mFaces[a];
  96. if (face.mNumIndices < 3) {
  97. // either a point or a line -> no well-defined normal vector
  98. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  99. pMesh->mNormals[face.mIndices[i]] = aiVector3D(qnan);
  100. }
  101. continue;
  102. }
  103. const aiVector3D *pV1 = &pMesh->mVertices[face.mIndices[0]];
  104. const aiVector3D *pV2 = &pMesh->mVertices[face.mIndices[1]];
  105. const aiVector3D *pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices - 1]];
  106. // Boolean XOR - if either but not both of these flags is set, then the winding order has
  107. // changed and the cross product to calculate the normal needs to be reversed
  108. if (flippedWindingOrder_ != leftHanded_)
  109. std::swap(pV2, pV3);
  110. const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe();
  111. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  112. pMesh->mNormals[face.mIndices[i]] = vNor;
  113. }
  114. }
  115. return true;
  116. }