Importer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /** @file Implementation of the CPP-API class #Importer */
  2. #include <fstream>
  3. #include <string>
  4. #include "../include/assimp.hpp"
  5. #include "../include/aiScene.h"
  6. #include "BaseImporter.h"
  7. #include "BaseProcess.h"
  8. #include "DefaultIOStream.h"
  9. #include "DefaultIOSystem.h"
  10. #include "XFileImporter.h"
  11. #include "3DSLoader.h"
  12. #include "MD3Loader.h"
  13. #include "MD2Loader.h"
  14. #include "PlyLoader.h"
  15. #include "ObjFileImporter.h"
  16. #include "CalcTangentsProcess.h"
  17. #include "JoinVerticesProcess.h"
  18. #include "ConvertToLHProcess.h"
  19. #include "TriangulateProcess.h"
  20. #include "GenFaceNormalsProcess.h"
  21. #include "GenVertexNormalsProcess.h"
  22. #include "KillNormalsProcess.h"
  23. #include "SplitLargeMeshes.h"
  24. using namespace Assimp;
  25. // ------------------------------------------------------------------------------------------------
  26. // Constructor.
  27. Importer::Importer() :
  28. mIOHandler(NULL),
  29. mScene(NULL),
  30. mErrorString("")
  31. {
  32. // default IO handler
  33. mIOHandler = new DefaultIOSystem;
  34. // add an instance of each worker class here
  35. mImporter.push_back( new XFileImporter());
  36. mImporter.push_back( new ObjFileImporter());
  37. mImporter.push_back( new Dot3DSImporter());
  38. mImporter.push_back( new MD3Importer());
  39. mImporter.push_back( new MD2Importer());
  40. mImporter.push_back( new PLYImporter());
  41. // add an instance of each post processing step here in the order of sequence it is executed
  42. mPostProcessingSteps.push_back( new TriangulateProcess());
  43. mPostProcessingSteps.push_back( new SplitLargeMeshesProcess());
  44. mPostProcessingSteps.push_back( new KillNormalsProcess());
  45. mPostProcessingSteps.push_back( new GenFaceNormalsProcess());
  46. mPostProcessingSteps.push_back( new GenVertexNormalsProcess());
  47. mPostProcessingSteps.push_back( new CalcTangentsProcess());
  48. mPostProcessingSteps.push_back( new JoinVerticesProcess());
  49. mPostProcessingSteps.push_back( new ConvertToLHProcess());
  50. }
  51. // ------------------------------------------------------------------------------------------------
  52. // Destructor.
  53. Importer::~Importer()
  54. {
  55. for( unsigned int a = 0; a < mImporter.size(); a++)
  56. delete mImporter[a];
  57. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  58. delete mPostProcessingSteps[a];
  59. delete mIOHandler;
  60. // kill imported scene. Destructors should do that recursivly
  61. delete mScene;
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // Supplies a custom IO handler to the importer to open and access files.
  65. void Importer::SetIOHandler( IOSystem* pIOHandler)
  66. {
  67. delete mIOHandler;
  68. mIOHandler = pIOHandler;
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // Reads the given file and returns its contents if successful.
  72. const aiScene* Importer::ReadFile( const std::string& pFile, unsigned int pFlags)
  73. {
  74. // first check if the file is accessable at all
  75. if( !mIOHandler->Exists( pFile))
  76. {
  77. mErrorString = "Unable to open file \"" + pFile + "\".";
  78. return NULL;
  79. }
  80. // find an worker class which can handle the file
  81. BaseImporter* imp = NULL;
  82. for( unsigned int a = 0; a < mImporter.size(); a++)
  83. {
  84. if( mImporter[a]->CanRead( pFile, mIOHandler))
  85. {
  86. imp = mImporter[a];
  87. break;
  88. }
  89. }
  90. // put a proper error message if no suitable importer was found
  91. if( !imp)
  92. {
  93. mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  94. return NULL;
  95. }
  96. // dispatch the reading to the worker class for this format
  97. mScene = imp->ReadFile( pFile, mIOHandler);
  98. // if failed, extract the error string
  99. if( !mScene)
  100. mErrorString = imp->GetErrorText();
  101. // if successful, apply all active post processing steps to the imported data
  102. if( mScene)
  103. {
  104. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  105. {
  106. BaseProcess* process = mPostProcessingSteps[a];
  107. if( process->IsActive( pFlags))
  108. process->Execute( mScene);
  109. }
  110. }
  111. // either successful or failure - the pointer expresses it anyways
  112. return mScene;
  113. }
  114. // ------------------------------------------------------------------------------------------------
  115. // Empty and rpivate copy constructor
  116. Importer::Importer(const Importer &other)
  117. {
  118. // empty
  119. }