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.
 
 
 
 
 
 

139 line
5.0 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 generate face
  35. * normals for all imported faces.
  36. */
  37. #include "AssimpPCH.h"
  38. #include "GenFaceNormalsProcess.h"
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. // Constructor to be privately used by Importer
  42. GenFaceNormalsProcess::GenFaceNormalsProcess()
  43. {
  44. // nothing to do here
  45. }
  46. // ------------------------------------------------------------------------------------------------
  47. // Destructor, private as well
  48. GenFaceNormalsProcess::~GenFaceNormalsProcess()
  49. {
  50. // nothing to do here
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. // Returns whether the processing step is present in the given flag field.
  54. bool GenFaceNormalsProcess::IsActive( unsigned int pFlags) const
  55. {
  56. return (pFlags & aiProcess_GenNormals) != 0;
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. // Executes the post processing step on the given imported data.
  60. void GenFaceNormalsProcess::Execute( aiScene* pScene)
  61. {
  62. DefaultLogger::get()->debug("GenFaceNormalsProcess begin");
  63. if (pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
  64. throw DeadlyImportError("Post-processing order mismatch: expecting pseudo-indexed (\"verbose\") vertices here");
  65. }
  66. bool bHas = false;
  67. for( unsigned int a = 0; a < pScene->mNumMeshes; a++) {
  68. if(this->GenMeshFaceNormals( pScene->mMeshes[a])) {
  69. bHas = true;
  70. }
  71. }
  72. if (bHas) {
  73. DefaultLogger::get()->info("GenFaceNormalsProcess finished. "
  74. "Face normals have been calculated");
  75. }
  76. else DefaultLogger::get()->debug("GenFaceNormalsProcess finished. "
  77. "Normals are already there");
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. // Executes the post processing step on the given imported data.
  81. bool GenFaceNormalsProcess::GenMeshFaceNormals (aiMesh* pMesh)
  82. {
  83. if (NULL != pMesh->mNormals) {
  84. return false;
  85. }
  86. // If the mesh consists of lines and/or points but not of
  87. // triangles or higher-order polygons the normal vectors
  88. // are undefined.
  89. if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON))) {
  90. DefaultLogger::get()->info("Normal vectors are undefined for line and point meshes");
  91. return false;
  92. }
  93. // allocate an array to hold the output normals
  94. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  95. const float qnan = get_qnan();
  96. // iterate through all faces and compute per-face normals but store them per-vertex.
  97. for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  98. const aiFace& face = pMesh->mFaces[a];
  99. if (face.mNumIndices < 3) {
  100. // either a point or a line -> no well-defined normal vector
  101. for (unsigned int i = 0;i < face.mNumIndices;++i) {
  102. pMesh->mNormals[face.mIndices[i]] = aiVector3D(qnan);
  103. }
  104. continue;
  105. }
  106. const aiVector3D* pV1 = &pMesh->mVertices[face.mIndices[0]];
  107. const aiVector3D* pV2 = &pMesh->mVertices[face.mIndices[1]];
  108. const aiVector3D* pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices-1]];
  109. const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).Normalize();
  110. for (unsigned int i = 0;i < face.mNumIndices;++i) {
  111. pMesh->mNormals[face.mIndices[i]] = vNor;
  112. }
  113. }
  114. return true;
  115. }