ObjFileImporter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include "ObjFileImporter.h"
  2. #include "ObjFileParser.h"
  3. #include "ObjFileData.h"
  4. #include "../include/IOStream.h"
  5. #include "../include/IOSystem.h"
  6. #include "../include/aiMesh.h"
  7. #include "../include/aiScene.h"
  8. #include "aiAssert.h"
  9. #include "MaterialSystem.h"
  10. #include "DefaultLogger.h"
  11. #include <boost/scoped_ptr.hpp>
  12. #include <boost/format.hpp>
  13. namespace Assimp
  14. {
  15. // ------------------------------------------------------------------------------------------------
  16. using namespace std;
  17. //! Obj-file-format extention
  18. const string ObjFileImporter::OBJ_EXT = "obj";
  19. // ------------------------------------------------------------------------------------------------
  20. // Default constructor
  21. ObjFileImporter::ObjFileImporter() :
  22. m_pRootObject(NULL),
  23. m_strAbsPath("\\")
  24. {
  25. DefaultLogger::create( "test.log", DefaultLogger::VERBOSE );
  26. DefaultLogger::get()->info("Start logging");
  27. }
  28. // ------------------------------------------------------------------------------------------------
  29. // Destructor
  30. ObjFileImporter::~ObjFileImporter()
  31. {
  32. // Release root object instance
  33. if (NULL != m_pRootObject)
  34. {
  35. delete m_pRootObject;
  36. m_pRootObject = NULL;
  37. }
  38. DefaultLogger::kill();
  39. }
  40. // ------------------------------------------------------------------------------------------------
  41. // Returns true, fi file is an obj file
  42. bool ObjFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  43. {
  44. if (pFile.empty())
  45. return false;
  46. DefaultLogger::get()->info("ObjFileImporter::CanRead");
  47. string::size_type pos = pFile.find_last_of(".");
  48. if (string::npos == pos)
  49. return false;
  50. const string ext = pFile.substr(pos+1, pFile.size() - pos - 1);
  51. if (ext == OBJ_EXT)
  52. return true;
  53. return false;
  54. }
  55. // ------------------------------------------------------------------------------------------------
  56. // Obj-file import implementation
  57. void ObjFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  58. {
  59. // Read file into memory
  60. const std::string mode = "rb";
  61. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, mode));
  62. if (NULL == file.get())
  63. throw new ImportErrorException( "Failed to open file " + pFile + ".");
  64. // Get the filesize and vaslidate it, throwing an exception when failes
  65. size_t fileSize = file->FileSize();
  66. if( fileSize < 16)
  67. throw new ImportErrorException( "OBJ-file is too small.");
  68. // Allocate buffer and read file into it
  69. m_Buffer.resize( fileSize );
  70. const size_t readsize = file->Read(&m_Buffer.front(), sizeof(char), fileSize);
  71. assert (readsize == fileSize);
  72. //
  73. std::string strDirectory("\\"), strModelName;
  74. std::string::size_type pos = pFile.find_last_of("\\");
  75. if (pos != std::string::npos)
  76. {
  77. strDirectory = pFile.substr(0, pos);
  78. strModelName = pFile.substr(pos+1, pFile.size() - pos - 1);
  79. }
  80. else
  81. {
  82. strModelName = pFile;
  83. }
  84. // parse the file into a temporary representation
  85. ObjFileParser parser(m_Buffer, strDirectory, strModelName);
  86. // And create the proper return structures out of it
  87. CreateDataFromImport(parser.GetModel(), pScene);
  88. }
  89. // ------------------------------------------------------------------------------------------------
  90. // Create the data from parsed obj-file
  91. void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene)
  92. {
  93. if (0L == pModel)
  94. return;
  95. // Create the root node of the scene
  96. pScene->mRootNode = new aiNode();
  97. if (!pModel->m_ModelName.empty())
  98. {
  99. // Set the name of the scene
  100. pScene->mRootNode->mName.Set(pModel->m_ModelName);
  101. }
  102. else
  103. {
  104. // This is an error, so break down the application
  105. assert (false);
  106. }
  107. // Create nodes for the whole scene
  108. std::vector<aiMesh*> MeshArray;
  109. for (size_t index = 0; index < pModel->m_Objects.size(); index++)
  110. {
  111. createNodes(pModel, pModel->m_Objects[ index ], pScene->mRootNode, pScene, MeshArray);
  112. }
  113. // Create mesh pointer buffer for this scene
  114. if (pScene->mNumMeshes > 0)
  115. {
  116. pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ];
  117. for (size_t index =0; index < pScene->mNumMeshes; index++)
  118. {
  119. pScene->mMeshes [ index ] = MeshArray[ index ];
  120. }
  121. }
  122. // Create all materials
  123. for (size_t index = 0; index < pModel->m_Objects.size(); index++)
  124. {
  125. createMaterial(pModel, pModel->m_Objects[ index ], pScene);
  126. }
  127. }
  128. // ------------------------------------------------------------------------------------------------
  129. // Creates all nodes of the model
  130. aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pData,
  131. aiNode *pParent, aiScene* pScene,
  132. std::vector<aiMesh*> &MeshArray)
  133. {
  134. if (NULL == pData)
  135. return NULL;
  136. // Store older mesh size to be able to computate mesh offsets for new mesh instances
  137. size_t oldMeshSize = MeshArray.size();
  138. aiNode *pNode = new aiNode();
  139. if (pParent != NULL)
  140. this->appendChildToParentNode(pParent, pNode);
  141. aiMesh *pMesh = new aiMesh();
  142. MeshArray.push_back(pMesh);
  143. createTopology(pModel, pData, pMesh);
  144. // Create all nodes from the subobjects stored in the current object
  145. if (!pData->m_SubObjects.empty())
  146. {
  147. pNode->mNumChildren = pData->m_SubObjects.size();
  148. pNode->mChildren = new aiNode*[pData->m_SubObjects.size()];
  149. pNode->mNumMeshes = 1;
  150. pNode->mMeshes = new unsigned int[1];
  151. // Loop over all child objects
  152. for (size_t index = 0; index < pData->m_SubObjects.size(); index++)
  153. {
  154. // Create all child nodes
  155. pNode->mChildren[index] = createNodes(pModel, pData, pNode, pScene, MeshArray);
  156. // Create meshes of this object
  157. pMesh = new aiMesh();
  158. MeshArray.push_back(pMesh);
  159. createTopology(pModel, pData->m_SubObjects[ index ], pMesh);
  160. // Create material of this object
  161. createMaterial(pModel, pData->m_SubObjects[ index ], pScene);
  162. }
  163. }
  164. // Set mesh instances into scene- and node-instances
  165. const size_t meshSizeDiff = MeshArray.size()- oldMeshSize;
  166. if (meshSizeDiff > 0 )
  167. {
  168. pNode->mMeshes = new unsigned int[ meshSizeDiff ];
  169. pNode->mNumMeshes++;
  170. size_t index = 0;
  171. for (size_t i = oldMeshSize; i < MeshArray.size(); i++)
  172. {
  173. pNode->mMeshes[ index ] = pScene->mNumMeshes;
  174. pScene->mNumMeshes++;
  175. index++;
  176. }
  177. }
  178. return pNode;
  179. }
  180. // ------------------------------------------------------------------------------------------------
  181. // Create topology data
  182. void ObjFileImporter::createTopology(const ObjFile::Model* pModel, const ObjFile::Object* pData,
  183. aiMesh* pMesh)
  184. {
  185. if (NULL == pData)
  186. return;
  187. // Create mesh vertices
  188. createVertexArray(pModel, pData, pMesh);
  189. // Create faces
  190. pMesh->mNumFaces = pData->m_Faces.size();
  191. pMesh->mFaces = new aiFace[pMesh->mNumFaces];
  192. for (size_t index = 0; index < pMesh->mNumFaces; index++)
  193. {
  194. aiFace *pFace = &pMesh->mFaces[ index ];
  195. pFace->mNumIndices = pData->m_Faces[index]->m_pVertices->size();
  196. if (pFace->mNumIndices > 0)
  197. {
  198. pFace->mIndices = new unsigned int[pMesh->mFaces[index].mNumIndices];
  199. ObjFile::Face::IndexArray *pArray = pData->m_Faces[index]->m_pVertices;
  200. // TODO: Should be implement much better
  201. //memcpy(pFace->mIndices, &pData->m_Faces[index]->m_pVertices[0], pFace->mNumIndices * sizeof(unsigned int));
  202. if (pArray != NULL)
  203. {
  204. for (size_t a=0; a<pFace->mNumIndices; a++)
  205. {
  206. pFace->mIndices[a] = pArray->at( a );
  207. }
  208. }
  209. else
  210. {
  211. ai_assert (false);
  212. }
  213. }
  214. }
  215. }
  216. // ------------------------------------------------------------------------------------------------
  217. void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
  218. const ObjFile::Object* pCurrentObject,
  219. aiMesh* pMesh)
  220. {
  221. // Break, if no faces are stored in object
  222. if (pCurrentObject->m_Faces.empty())
  223. return;
  224. // Copy all stored vertices, normals and so on
  225. pMesh->mNumVertices = pModel->m_Vertices.size();
  226. pMesh->mVertices = new aiVector3D_t[pMesh->mNumVertices];
  227. for (size_t index=0; index < pModel->m_Vertices.size(); index++)
  228. {
  229. pMesh->mVertices[ index ] = *pModel->m_Vertices[ index ];
  230. }
  231. if (!pModel->m_Normals.empty())
  232. {
  233. pMesh->mNormals = new aiVector3D_t[pModel->m_Normals.size()];
  234. for (size_t index = 0; index < pModel->m_Normals.size(); index++)
  235. {
  236. pMesh->mNormals[ index ] = *pModel->m_Normals[ index ];
  237. }
  238. }
  239. if (!pModel->m_TextureCoord.empty())
  240. {
  241. // TODO: Implement texture coodinates
  242. }
  243. }
  244. // ------------------------------------------------------------------------------------------------
  245. void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes)
  246. {
  247. iNumMeshes = 0;
  248. if (rObjects.empty())
  249. return;
  250. iNumMeshes += rObjects.size();
  251. for (std::vector<ObjFile::Object*>::const_iterator it = rObjects.begin();
  252. it != rObjects.end();
  253. ++it)
  254. {
  255. if (!(*it)->m_SubObjects.empty())
  256. {
  257. countObjects((*it)->m_SubObjects, iNumMeshes);
  258. }
  259. }
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. void ObjFileImporter::createMaterial(const ObjFile::Model* pModel, const ObjFile::Object* pData,
  263. aiScene* pScene)
  264. {
  265. ai_assert (NULL != pScene);
  266. if (NULL == pData)
  267. return;
  268. // Create only a dumy material to enshure a running viewer
  269. pScene->mNumMaterials = 1;
  270. Assimp::MaterialHelper* mat = new Assimp::MaterialHelper;
  271. // Create a new material
  272. pScene->mMaterials = new aiMaterial*[1];
  273. pScene->mMaterials[0] = mat;
  274. }
  275. // ------------------------------------------------------------------------------------------------
  276. // Appends this node to the parent node
  277. void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild)
  278. {
  279. // Checking preconditions
  280. ai_assert (NULL != pParent);
  281. ai_assert (NULL != pChild);
  282. // Assign parent to child
  283. pChild->mParent = pParent;
  284. size_t sNumChildren = 0;
  285. // If already children was assigned to the parent node, store them in a
  286. std::vector<aiNode*> temp;
  287. if (pParent->mChildren != NULL)
  288. {
  289. sNumChildren = pParent->mNumChildren;
  290. ai_assert (0 != sNumChildren);
  291. for (size_t index = 0; index < pParent->mNumChildren; index++)
  292. {
  293. temp.push_back(pParent->mChildren [ index ] );
  294. }
  295. delete [] pParent->mChildren;
  296. }
  297. // Copy node instances into parent node
  298. pParent->mNumChildren++;
  299. pParent->mChildren = new aiNode*[ pParent->mNumChildren ];
  300. for (size_t index = 0; index < pParent->mNumChildren-1; index++)
  301. {
  302. pParent->mChildren[ index ] = temp [ index ];
  303. }
  304. pParent->mChildren[ pParent->mNumChildren-1 ] = pChild;
  305. }
  306. // ------------------------------------------------------------------------------------------------
  307. } // Namespace Assimp