GenVertexNormalsProcess.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /** @file Implementation of the post processing step to generate face
  2. * normals for all imported faces.
  3. */
  4. #include "GenVertexNormalsProcess.h"
  5. #include "SpatialSort.h"
  6. #include "../include/aiPostProcess.h"
  7. #include "../include/aiMesh.h"
  8. #include "../include/aiScene.h"
  9. using namespace Assimp;
  10. // Constructor to be privately used by Importer
  11. GenVertexNormalsProcess::GenVertexNormalsProcess()
  12. {
  13. }
  14. // Destructor, private as well
  15. GenVertexNormalsProcess::~GenVertexNormalsProcess()
  16. {
  17. // nothing to do here
  18. }
  19. // -------------------------------------------------------------------
  20. // Returns whether the processing step is present in the given flag field.
  21. bool GenVertexNormalsProcess::IsActive( unsigned int pFlags) const
  22. {
  23. return (pFlags & aiProcess_GenSmoothNormals) != 0;
  24. }
  25. // -------------------------------------------------------------------
  26. // Executes the post processing step on the given imported data.
  27. void GenVertexNormalsProcess::Execute( aiScene* pScene)
  28. {
  29. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  30. this->GenMeshVertexNormals( pScene->mMeshes[a]);
  31. }
  32. // -------------------------------------------------------------------
  33. // Executes the post processing step on the given imported data.
  34. void GenVertexNormalsProcess::GenMeshVertexNormals (aiMesh* pMesh)
  35. {
  36. if (NULL != pMesh->mNormals)return;
  37. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  38. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  39. {
  40. const aiFace& face = pMesh->mFaces[a];
  41. // assume it is a triangle
  42. aiVector3D* pV1 = &pMesh->mVertices[face.mIndices[0]];
  43. aiVector3D* pV2 = &pMesh->mVertices[face.mIndices[1]];
  44. aiVector3D* pV3 = &pMesh->mVertices[face.mIndices[2]];
  45. aiVector3D pDelta1 = *pV2 - *pV1;
  46. aiVector3D pDelta2 = *pV3 - *pV1;
  47. aiVector3D vNor = pDelta1 ^ pDelta2;
  48. float fLength = vNor.Length();
  49. if (0.0f != fLength)vNor /= fLength;
  50. pMesh->mNormals[face.mIndices[0]] = vNor;
  51. pMesh->mNormals[face.mIndices[1]] = vNor;
  52. pMesh->mNormals[face.mIndices[2]] = vNor;
  53. }
  54. // calculate the position bounds so we have a reliable epsilon to
  55. // check position differences against
  56. aiVector3D minVec( 1e10f, 1e10f, 1e10f), maxVec( -1e10f, -1e10f, -1e10f);
  57. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  58. {
  59. minVec.x = std::min( minVec.x, pMesh->mVertices[a].x);
  60. minVec.y = std::min( minVec.y, pMesh->mVertices[a].y);
  61. minVec.z = std::min( minVec.z, pMesh->mVertices[a].z);
  62. maxVec.x = std::max( maxVec.x, pMesh->mVertices[a].x);
  63. maxVec.y = std::max( maxVec.y, pMesh->mVertices[a].y);
  64. maxVec.z = std::max( maxVec.z, pMesh->mVertices[a].z);
  65. }
  66. const float posEpsilon = (maxVec - minVec).Length() * 1e-5f;
  67. // set up a SpatialSort to quickly find all vertices close to a given position
  68. SpatialSort vertexFinder( pMesh->mVertices, pMesh->mNumVertices, sizeof( aiVector3D));
  69. std::vector<unsigned int> verticesFound;
  70. aiVector3D* pcNew = new aiVector3D[pMesh->mNumVertices];
  71. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  72. {
  73. const aiVector3D& posThis = pMesh->mVertices[i];
  74. // get all vertices that share this one ...
  75. vertexFinder.FindPositions( posThis, posEpsilon, verticesFound);
  76. aiVector3D pcNor;
  77. for (unsigned int a = 0; a < verticesFound.size(); ++a)
  78. {
  79. unsigned int vidx = verticesFound[a];
  80. pcNor += pMesh->mNormals[vidx];
  81. }
  82. pcNor /= (float) verticesFound.size();
  83. pcNew[i] = pcNor;
  84. }
  85. delete pMesh->mNormals;
  86. pMesh->mNormals = pcNew;
  87. return;
  88. }