TriangulateProcess.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /** @file Implementation of the post processing step to split up
  2. * all faces with more than three indices into triangles.
  3. */
  4. #include <vector>
  5. #include <assert.h>
  6. #include "TriangulateProcess.h"
  7. #include "../include/aiPostProcess.h"
  8. #include "../include/aiMesh.h"
  9. #include "../include/aiScene.h"
  10. using namespace Assimp;
  11. // ------------------------------------------------------------------------------------------------
  12. // Constructor to be privately used by Importer
  13. TriangulateProcess::TriangulateProcess()
  14. {
  15. // nothing to do here
  16. }
  17. // ------------------------------------------------------------------------------------------------
  18. // Destructor, private as well
  19. TriangulateProcess::~TriangulateProcess()
  20. {
  21. // nothing to do here
  22. }
  23. // ------------------------------------------------------------------------------------------------
  24. // Returns whether the processing step is present in the given flag field.
  25. bool TriangulateProcess::IsActive( unsigned int pFlags) const
  26. {
  27. return (pFlags & aiProcess_Triangulate) != 0;
  28. }
  29. // ------------------------------------------------------------------------------------------------
  30. // Executes the post processing step on the given imported data.
  31. void TriangulateProcess::Execute( aiScene* pScene)
  32. {
  33. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  34. TriangulateMesh( pScene->mMeshes[a]);
  35. }
  36. // ------------------------------------------------------------------------------------------------
  37. // Triangulates the given mesh.
  38. void TriangulateProcess::TriangulateMesh( aiMesh* pMesh)
  39. {
  40. std::vector<aiFace> newFaces;
  41. newFaces.reserve( pMesh->mNumFaces*2);
  42. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  43. {
  44. const aiFace& face = pMesh->mFaces[a];
  45. // if it's a simple primitive, just copy it
  46. if( face.mNumIndices == 3)
  47. {
  48. newFaces.push_back( aiFace());
  49. aiFace& nface = newFaces.back();
  50. nface.mNumIndices = face.mNumIndices;
  51. nface.mIndices = new unsigned int[nface.mNumIndices];
  52. memcpy( nface.mIndices, face.mIndices, nface.mNumIndices * sizeof( unsigned int));
  53. } else
  54. {
  55. assert( face.mNumIndices > 3);
  56. for( unsigned int b = 0; b < face.mNumIndices - 2; b++)
  57. {
  58. newFaces.push_back( aiFace());
  59. aiFace& nface = newFaces.back();
  60. nface.mNumIndices = 3;
  61. nface.mIndices = new unsigned int[3];
  62. nface.mIndices[0] = face.mIndices[0];
  63. nface.mIndices[1] = face.mIndices[b+1];
  64. nface.mIndices[2] = face.mIndices[b+2];
  65. }
  66. }
  67. }
  68. // kill the old faces
  69. delete [] pMesh->mFaces;
  70. // and insert our newly generated faces
  71. pMesh->mNumFaces = newFaces.size();
  72. pMesh->mFaces = new aiFace[pMesh->mNumFaces];
  73. for( unsigned int a = 0; a < newFaces.size(); a++)
  74. pMesh->mFaces[a] = newFaces[a];
  75. }