M3Importer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "AssimpPCH.h"
  34. #ifndef ASSIMP_BUILD_NO_M3_IMPORTER
  35. #include "M3Importer.h"
  36. #include <sstream>
  37. namespace Assimp {
  38. namespace M3 {
  39. static const std::string M3Extension = "m3";
  40. // ------------------------------------------------------------------------------------------------
  41. // Constructor.
  42. M3Importer::M3Importer() :
  43. m_pHead( NULL ),
  44. m_pRefs( NULL )
  45. {
  46. }
  47. // ------------------------------------------------------------------------------------------------
  48. // Destructor.
  49. M3Importer::~M3Importer()
  50. {
  51. m_pHead = NULL;
  52. m_pRefs = NULL;
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Check for readable file format.
  56. bool M3Importer::CanRead( const std::string &rFile, IOSystem* /*pIOHandler*/, bool checkSig ) const
  57. {
  58. if ( !checkSig )
  59. return SimpleExtensionCheck( rFile, M3Extension.c_str() );
  60. return true;
  61. }
  62. // ------------------------------------------------------------------------------------------------
  63. void M3Importer::GetExtensionList(std::set<std::string>& extensions)
  64. {
  65. extensions.insert("m3");
  66. }
  67. // ------------------------------------------------------------------------------------------------
  68. void M3Importer::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler )
  69. {
  70. const std::string mode = "rb";
  71. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, mode ) );
  72. if ( NULL == file.get() )
  73. throw DeadlyImportError( "Failed to open file " + pFile + ".");
  74. // Get the file-size and validate it, throwing an exception when it fails
  75. size_t filesize = file->FileSize();
  76. if( filesize < 1 )
  77. throw DeadlyImportError( "M3-file is too small.");
  78. m_Buffer.resize( filesize );
  79. size_t readsize = file->Read( &m_Buffer[ 0 ], sizeof( unsigned char ), filesize );
  80. ai_assert( readsize == filesize );
  81. m_pHead = (MD33*)( &m_Buffer[ 0 ] );
  82. m_pRefs = (ReferenceEntry*)( &m_Buffer[ 0 ] + m_pHead->ofsRefs );
  83. MODL20* pMODL20( NULL );
  84. MODL23* pMODL23( NULL );
  85. VertexExt* pVerts1( NULL );
  86. Vertex* pVerts2( NULL );
  87. DIV *pViews( NULL );
  88. Region* regions( NULL );
  89. uint16* faces( NULL );
  90. uint32 nVertices = 0;
  91. uint32 nFaces = 0;
  92. bool ok = true;
  93. switch( m_pRefs[ m_pHead->MODL.ref ].type )
  94. {
  95. case 20:
  96. pMODL20 = GetEntries<MODL20>( m_pHead->MODL );
  97. if ( ( pMODL20->flags & 0x20000) != 0 ) // Has vertices
  98. {
  99. if( (pMODL20->flags & 0x40000) != 0 ) // Has extra 4 byte
  100. {
  101. pVerts1 = GetEntries<VertexExt>( pMODL20->vertexData );
  102. nVertices = pMODL20->vertexData.nEntries/sizeof(VertexExt);
  103. }
  104. else
  105. {
  106. pVerts2 = GetEntries<Vertex>(pMODL20->vertexData);
  107. nVertices = pMODL20->vertexData.nEntries/sizeof(Vertex);
  108. }
  109. }
  110. pViews = GetEntries<DIV>( pMODL20->views );
  111. break;
  112. case 23:
  113. pMODL23 = GetEntries<MODL23>(m_pHead->MODL );
  114. if( (pMODL23->flags & 0x20000) != 0 ) // Has vertices
  115. {
  116. if( (pMODL23->flags & 0x40000) != 0 ) // Has extra 4 byte
  117. {
  118. pVerts1 = GetEntries<VertexExt>(pMODL23->vertexData);
  119. nVertices = pMODL23->vertexData.nEntries/sizeof(VertexExt);
  120. }
  121. else
  122. {
  123. pVerts2 = GetEntries<Vertex>(pMODL23->vertexData);
  124. nVertices = pMODL23->vertexData.nEntries/sizeof(Vertex);
  125. }
  126. }
  127. pViews = GetEntries<DIV>( pMODL23->views );
  128. break;
  129. default:
  130. ok = false;
  131. return;
  132. }
  133. // Get all region data
  134. regions = GetEntries<Region>( pViews->regions );
  135. // Get the face data
  136. faces = GetEntries<uint16>( pViews->faces );
  137. nFaces = pViews->faces.nEntries;
  138. // Everything ok, if not throw an exception
  139. if ( !ok )
  140. throw DeadlyImportError( "Failed to open file " + pFile + ".");
  141. // Convert the vertices
  142. std::vector<aiVector3D> vertices;
  143. vertices.resize( nVertices );
  144. unsigned int offset = 0;
  145. for ( unsigned int i = 0; i < nVertices; i++ )
  146. {
  147. if ( pVerts1 )
  148. {
  149. vertices[ offset ].Set( pVerts1[i].pos.x, pVerts1[i].pos.y, pVerts1[i].pos.z );
  150. offset++;
  151. }
  152. if ( pVerts2 )
  153. {
  154. vertices[ offset ].Set( pVerts2[ i ].pos.x, pVerts2[ i ].pos.y, pVerts2[ i ].pos.z );
  155. offset++;
  156. }
  157. }
  158. // Compute the normals
  159. std::vector<aiVector3D> normals;
  160. normals.resize( nVertices );
  161. float w = 0.0f;
  162. Vec3D norm;
  163. for( unsigned int i = 0; i < nVertices; i++ )
  164. {
  165. w = 0.0f;
  166. if( pVerts1 )
  167. {
  168. norm.x = (float) 2*pVerts1[ i ].normal[ 0 ]/255.0f - 1;
  169. norm.y = (float) 2*pVerts1[ i ].normal[ 1 ]/255.0f - 1;
  170. norm.z = (float) 2*pVerts1[ i ].normal[ 2 ]/255.0f - 1;
  171. w = (float) pVerts1[ i ].normal[ 3 ]/255.0f;
  172. }
  173. if( pVerts2 )
  174. {
  175. norm.x = (float) 2*pVerts2[ i ].normal[ 0 ]/255.0f - 1;
  176. norm.y = (float) 2*pVerts2[ i ].normal[ 1 ]/255.0f - 1;
  177. norm.z = (float) 2*pVerts2[ i ].normal[ 2 ]/255.0f - 1;
  178. w = (float) pVerts2[ i ].normal[ 3 ] / 255.0f;
  179. }
  180. if ( w )
  181. {
  182. const float invW = 1.0f / w;
  183. norm.x = norm.x * invW;
  184. norm.y = norm.y * invW;
  185. norm.z = norm.z * invW;
  186. }
  187. normals[ i ].Set( norm.x, norm.y, norm.z );
  188. }
  189. // Convert the data into the assimp specific data structures
  190. convertToAssimp( pFile, pScene, pViews, regions, faces, vertices, normals );
  191. }
  192. // ------------------------------------------------------------------------------------------------
  193. //
  194. void M3Importer::convertToAssimp( const std::string& pFile, aiScene* pScene, DIV *pViews,
  195. Region *pRegions, uint16 *pFaces,
  196. const std::vector<aiVector3D> &vertices,
  197. const std::vector<aiVector3D> &normals )
  198. {
  199. std::vector<aiMesh*> MeshArray;
  200. // Create the root node
  201. pScene->mRootNode = createNode( NULL );
  202. // Set the name of the scene
  203. ai_assert( !pFile.empty() );
  204. pScene->mRootNode->mName.Set( pFile );
  205. aiNode *pRootNode = pScene->mRootNode;
  206. aiNode *pCurrentNode = NULL;
  207. // Lets create the nodes
  208. pRootNode->mNumChildren = pViews->regions.nEntries;
  209. if ( pRootNode->mNumChildren > 0 )
  210. pRootNode->mChildren = new aiNode*[ pRootNode->mNumChildren ];
  211. for ( unsigned int i=0; i<pViews->regions.nEntries; ++i )
  212. {
  213. // Create a new node
  214. pCurrentNode = createNode( pRootNode );
  215. std::stringstream stream;
  216. stream << "Node_" << i;
  217. pCurrentNode->mName.Set( stream.str().c_str() );
  218. pRootNode->mChildren[ i ] = pCurrentNode;
  219. // Loop over the faces of the nodes
  220. unsigned int numFaces = ( pRegions[ i ].ofsIndices + pRegions[ i ].nIndices ) - pRegions[ i ].ofsIndices;
  221. aiMesh *pMesh = new aiMesh;
  222. MeshArray.push_back( pMesh );
  223. pMesh->mNumFaces = numFaces;
  224. pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
  225. aiFace *pCurrentFace = NULL;
  226. unsigned int faceIdx = 0;
  227. for ( unsigned int j = pRegions[ i ].ofsIndices; j < ( pRegions[ i ].ofsIndices + pRegions[ i ].nIndices ); j += 3 )
  228. {
  229. pCurrentFace = &( pMesh->mFaces[ faceIdx ] );
  230. faceIdx++;
  231. pCurrentFace->mNumIndices = 3;
  232. pCurrentFace->mIndices = new unsigned int[ 3 ];
  233. pCurrentFace->mIndices[ 0 ] = pFaces[ j ]+1;
  234. pCurrentFace->mIndices[ 1 ] = pFaces[ j+1 ] + 1;
  235. pCurrentFace->mIndices[ 2 ] = pFaces[ j+2 ] + 1;
  236. }
  237. // Now we can create the vertex data itself
  238. pCurrentNode->mNumMeshes = 1;
  239. pCurrentNode->mMeshes = new unsigned int[ 1 ];
  240. pCurrentNode->mMeshes[ 0 ] = MeshArray.size() - 1;
  241. createVertexData( pMesh, vertices, normals );
  242. }
  243. // Copy the meshes into the scene
  244. pScene->mNumMeshes = MeshArray.size();
  245. pScene->mMeshes = new aiMesh*[ MeshArray.size() ];
  246. unsigned int pos = 0;
  247. for ( std::vector<aiMesh*>::iterator it = MeshArray.begin(); it != MeshArray.end(); ++it )
  248. {
  249. pScene->mMeshes[ pos ] = *it;
  250. pos++;
  251. }
  252. }
  253. // ------------------------------------------------------------------------------------------------
  254. //
  255. void M3Importer::createVertexData( aiMesh *pMesh, const std::vector<aiVector3D> &vertices,
  256. const std::vector<aiVector3D> &normals )
  257. {
  258. unsigned int numIndices = 0;
  259. pMesh->mNumVertices = pMesh->mNumFaces * 3;
  260. pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
  261. pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
  262. unsigned int pos = 0;
  263. for ( unsigned int currentFace = 0; currentFace < pMesh->mNumFaces; currentFace++ )
  264. {
  265. aiFace *pFace = &( pMesh->mFaces[ currentFace ] );
  266. for ( unsigned int currentIdx=0; currentIdx<pFace->mNumIndices; currentIdx++ )
  267. {
  268. unsigned int idx = pFace->mIndices[ currentIdx ];
  269. if ( vertices.size() > idx )
  270. {
  271. pMesh->mVertices[ pos ] = vertices[ idx ];
  272. pMesh->mNormals[ pos ] = normals[ idx ];
  273. pFace->mIndices[ currentIdx ] = pos;
  274. pos++;
  275. }
  276. }
  277. }
  278. }
  279. // ------------------------------------------------------------------------------------------------
  280. //
  281. aiNode *M3Importer::createNode( aiNode *pParent )
  282. {
  283. aiNode *pNode = new aiNode;
  284. if ( pParent )
  285. pNode->mParent = pParent;
  286. else
  287. pNode->mParent = NULL;
  288. return pNode;
  289. }
  290. // ------------------------------------------------------------------------------------------------
  291. } // Namespace M3
  292. } // Namespace Assimp
  293. #endif // ASSIMP_BUILD_NO_M3_IMPORTER