GenFaceNormalsProcess.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /** @file Implementation of the post processing step to generate face
  2. * normals for all imported faces.
  3. */
  4. #include "GenFaceNormalsProcess.h"
  5. #include "../include/aiPostProcess.h"
  6. #include "../include/aiMesh.h"
  7. #include "../include/aiScene.h"
  8. using namespace Assimp;
  9. // Constructor to be privately used by Importer
  10. GenFaceNormalsProcess::GenFaceNormalsProcess()
  11. {
  12. }
  13. // Destructor, private as well
  14. GenFaceNormalsProcess::~GenFaceNormalsProcess()
  15. {
  16. // nothing to do here
  17. }
  18. // -------------------------------------------------------------------
  19. // Returns whether the processing step is present in the given flag field.
  20. bool GenFaceNormalsProcess::IsActive( unsigned int pFlags) const
  21. {
  22. return (pFlags & aiProcess_GenNormals) != 0;
  23. }
  24. // -------------------------------------------------------------------
  25. // Executes the post processing step on the given imported data.
  26. void GenFaceNormalsProcess::Execute( aiScene* pScene)
  27. {
  28. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  29. this->GenMeshFaceNormals( pScene->mMeshes[a]);
  30. }
  31. // -------------------------------------------------------------------
  32. // Executes the post processing step on the given imported data.
  33. void GenFaceNormalsProcess::GenMeshFaceNormals (aiMesh* pMesh)
  34. {
  35. if (NULL != pMesh->mNormals)return;
  36. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  37. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  38. {
  39. const aiFace& face = pMesh->mFaces[a];
  40. // assume it is a triangle
  41. aiVector3D* pV1 = &pMesh->mVertices[face.mIndices[0]];
  42. aiVector3D* pV2 = &pMesh->mVertices[face.mIndices[1]];
  43. aiVector3D* pV3 = &pMesh->mVertices[face.mIndices[2]];
  44. aiVector3D pDelta1 = *pV2 - *pV1;
  45. aiVector3D pDelta2 = *pV3 - *pV1;
  46. aiVector3D vNor = pDelta1 ^ pDelta2;
  47. // NOTE: Never normalize here. Causes problems ...
  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. return;
  55. }