ObjFileImporter.cpp 10 KB

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