M3Importer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp 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 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. m_Buffer()
  46. {
  47. // empty
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Destructor.
  51. M3Importer::~M3Importer()
  52. {
  53. m_pHead = NULL;
  54. m_pRefs = NULL;
  55. }
  56. // ------------------------------------------------------------------------------------------------
  57. // Check for readable file format.
  58. bool M3Importer::CanRead( const std::string &rFile, IOSystem* /*pIOHandler*/, bool checkSig ) const
  59. {
  60. if ( !checkSig ) {
  61. return SimpleExtensionCheck( rFile, M3Extension.c_str() );
  62. }
  63. return false;
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. void M3Importer::GetExtensionList(std::set<std::string>& extensions)
  67. {
  68. extensions.insert( M3Extension );
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. void M3Importer::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler )
  72. {
  73. ai_assert( !pFile.empty() );
  74. const std::string mode = "rb";
  75. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, mode ) );
  76. if ( NULL == file.get() ) {
  77. throw DeadlyImportError( "Failed to open file " + pFile + ".");
  78. }
  79. // Get the file-size and validate it, throwing an exception when it fails
  80. const size_t filesize = file->FileSize();
  81. if( filesize < 1 ) {
  82. throw DeadlyImportError( "M3-file is too small.");
  83. }
  84. m_Buffer.resize( filesize );
  85. size_t readsize = file->Read( &m_Buffer[ 0 ], sizeof( unsigned char ), filesize );
  86. ai_assert( readsize == filesize );
  87. m_pHead = reinterpret_cast<MD33*>( &m_Buffer[ 0 ] );
  88. m_pRefs = reinterpret_cast<ReferenceEntry*>( &m_Buffer[ 0 ] + m_pHead->ofsRefs );
  89. MODL20* pMODL20( NULL );
  90. MODL23* pMODL23( NULL );
  91. VertexExt* pVerts1( NULL );
  92. Vertex* pVerts2( NULL );
  93. DIV *pViews( NULL );
  94. Region* regions( NULL );
  95. uint16* faces( NULL );
  96. uint32 nVertices = 0;
  97. uint32 nFaces = 0;
  98. bool ok = true;
  99. switch( m_pRefs[ m_pHead->MODL.ref ].type ) {
  100. case 20:
  101. pMODL20 = GetEntries<MODL20>( m_pHead->MODL );
  102. if ( ( pMODL20->flags & 0x20000) != 0 ) { // Has vertices
  103. if( (pMODL20->flags & 0x40000) != 0 ) { // Has extra 4 byte
  104. pVerts1 = GetEntries<VertexExt>( pMODL20->vertexData );
  105. nVertices = pMODL20->vertexData.nEntries/sizeof(VertexExt);
  106. }
  107. else {
  108. pVerts2 = GetEntries<Vertex>( pMODL20->vertexData );
  109. nVertices = pMODL20->vertexData.nEntries / sizeof( Vertex );
  110. }
  111. }
  112. pViews = GetEntries<DIV>( pMODL20->views );
  113. break;
  114. case 23:
  115. pMODL23 = GetEntries<MODL23>(m_pHead->MODL );
  116. if( (pMODL23->flags & 0x20000) != 0 ) { // Has vertices
  117. if( (pMODL23->flags & 0x40000) != 0 ) { // Has extra 4 byte
  118. pVerts1 = GetEntries<VertexExt>( pMODL23->vertexData );
  119. nVertices = pMODL23->vertexData.nEntries/sizeof( VertexExt );
  120. }
  121. else {
  122. pVerts2 = GetEntries<Vertex>( pMODL23->vertexData );
  123. nVertices = pMODL23->vertexData.nEntries/sizeof( Vertex );
  124. }
  125. }
  126. pViews = GetEntries<DIV>( pMODL23->views );
  127. break;
  128. default:
  129. ok = false;
  130. break;
  131. }
  132. // Everything ok, if not throw an exception
  133. if ( !ok ) {
  134. throw DeadlyImportError( "Failed to open file " + pFile + ".");
  135. }
  136. // Get all region data
  137. regions = GetEntries<Region>( pViews->regions );
  138. // Get the face data
  139. faces = GetEntries<uint16>( pViews->faces );
  140. nFaces = pViews->faces.nEntries;
  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. if ( pVerts1 ) {
  147. vertices[ offset ].Set( pVerts1[ i ].pos.x, pVerts1[ i ].pos.y, pVerts1[ i ].pos.z );
  148. ++offset;
  149. }
  150. if ( pVerts2 ) {
  151. vertices[ offset ].Set( pVerts2[ i ].pos.x, pVerts2[ i ].pos.y, pVerts2[ i ].pos.z );
  152. ++offset;
  153. }
  154. }
  155. // Write the UV coordinates
  156. offset = 0;
  157. std::vector<aiVector3D> uvCoords;
  158. uvCoords.resize( nVertices );
  159. for( unsigned int i = 0; i < nVertices; ++i ) {
  160. if( pVerts1 ) {
  161. float u = (float) pVerts1[ i ].uv[ 0 ] / 2048;
  162. float v = (float) pVerts1[ i ].uv[ 1 ] / 2048;
  163. uvCoords[ offset ].Set( u, v, 0.0f );
  164. ++offset;
  165. }
  166. if( pVerts2 ) {
  167. float u = (float) pVerts2[ i ].uv[ 0 ] / 2048;
  168. float v = (float) pVerts2[ i ].uv[ 1 ] / 2048;
  169. uvCoords[ offset ].Set( u, v, 0.0f );
  170. ++offset;
  171. }
  172. }
  173. // Compute the normals
  174. std::vector<aiVector3D> normals;
  175. normals.resize( nVertices );
  176. float w = 0.0f;
  177. Vec3D norm;
  178. offset = 0;
  179. for( unsigned int i = 0; i < nVertices; i++ ) {
  180. w = 0.0f;
  181. if( pVerts1 ) {
  182. norm.x = (float) 2*pVerts1[ i ].normal[ 0 ]/255.0f - 1;
  183. norm.y = (float) 2*pVerts1[ i ].normal[ 1 ]/255.0f - 1;
  184. norm.z = (float) 2*pVerts1[ i ].normal[ 2 ]/255.0f - 1;
  185. w = (float) pVerts1[ i ].normal[ 3 ]/255.0f;
  186. }
  187. if( pVerts2 ) {
  188. norm.x = (float) 2*pVerts2[ i ].normal[ 0 ]/255.0f - 1;
  189. norm.y = (float) 2*pVerts2[ i ].normal[ 1 ]/255.0f - 1;
  190. norm.z = (float) 2*pVerts2[ i ].normal[ 2 ]/255.0f - 1;
  191. w = (float) pVerts2[ i ].normal[ 3 ] / 255.0f;
  192. }
  193. if ( w ) {
  194. const float invW = 1.0f / w;
  195. norm.x = norm.x * invW;
  196. norm.y = norm.y * invW;
  197. norm.z = norm.z * invW;
  198. normals[ offset ].Set( norm.x, norm.y, norm.z );
  199. ++offset;
  200. }
  201. }
  202. // Convert the data into the assimp specific data structures
  203. convertToAssimp( pFile, pScene, pViews, regions, faces, vertices, uvCoords, normals );
  204. }
  205. // ------------------------------------------------------------------------------------------------
  206. //
  207. void M3Importer::convertToAssimp( const std::string& pFile, aiScene* pScene, DIV *pViews,
  208. Region *pRegions, uint16 *pFaces,
  209. const std::vector<aiVector3D> &vertices,
  210. const std::vector<aiVector3D> &uvCoords,
  211. const std::vector<aiVector3D> &normals )
  212. {
  213. std::vector<aiMesh*> MeshArray;
  214. // Create the root node
  215. pScene->mRootNode = createNode( NULL );
  216. // Set the name of the scene
  217. pScene->mRootNode->mName.Set( pFile );
  218. aiNode *pRootNode = pScene->mRootNode;
  219. aiNode *pCurrentNode = NULL;
  220. // Lets create the nodes
  221. pRootNode->mNumChildren = pViews->regions.nEntries;
  222. if ( pRootNode->mNumChildren > 0 ) {
  223. pRootNode->mChildren = new aiNode*[ pRootNode->mNumChildren ];
  224. }
  225. for ( unsigned int i=0; i<pRootNode->mNumChildren; ++i ) {
  226. //pRegions[ i ].
  227. // Create a new node
  228. pCurrentNode = createNode( pRootNode );
  229. std::stringstream stream;
  230. stream << "Node_" << i;
  231. pCurrentNode->mName.Set( stream.str().c_str() );
  232. pRootNode->mChildren[ i ] = pCurrentNode;
  233. // Loop over the faces of the nodes
  234. unsigned int numFaces = ( ( pRegions[ i ].ofsIndices + pRegions[ i ].nIndices ) - pRegions[ i ].ofsIndices ) / 3;
  235. aiMesh *pMesh = new aiMesh;
  236. MeshArray.push_back( pMesh );
  237. pMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  238. pMesh->mNumFaces = numFaces;
  239. pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
  240. aiFace *pCurrentFace = NULL;
  241. unsigned int faceIdx = 0;
  242. for ( unsigned int j = pRegions[ i ].ofsIndices; j < ( pRegions[ i ].ofsIndices + pRegions[ i ].nIndices ); j += 3 ) {
  243. pCurrentFace = &( pMesh->mFaces[ faceIdx ] );
  244. faceIdx++;
  245. pCurrentFace->mNumIndices = 3;
  246. pCurrentFace->mIndices = new unsigned int[ 3 ];
  247. pCurrentFace->mIndices[ 0 ] = pFaces[ j ];
  248. pCurrentFace->mIndices[ 1 ] = pFaces[ j+1 ];
  249. pCurrentFace->mIndices[ 2 ] = pFaces[ j+2 ];
  250. }
  251. // Now we can create the vertex data itself
  252. pCurrentNode->mNumMeshes = 1;
  253. pCurrentNode->mMeshes = new unsigned int[ 1 ];
  254. const unsigned int meshIdx = MeshArray.size() - 1;
  255. pCurrentNode->mMeshes[ 0 ] = meshIdx;
  256. createVertexData( pMesh, vertices, uvCoords, normals );
  257. }
  258. // Copy the meshes into the scene
  259. pScene->mNumMeshes = MeshArray.size();
  260. pScene->mMeshes = new aiMesh*[ MeshArray.size() ];
  261. unsigned int pos = 0;
  262. for ( std::vector<aiMesh*>::iterator it = MeshArray.begin(); it != MeshArray.end(); ++it ) {
  263. pScene->mMeshes[ pos ] = *it;
  264. ++pos;
  265. }
  266. }
  267. // ------------------------------------------------------------------------------------------------
  268. //
  269. void M3Importer::createVertexData( aiMesh *pMesh, const std::vector<aiVector3D> &vertices,
  270. const std::vector<aiVector3D> &uvCoords,
  271. const std::vector<aiVector3D> &normals )
  272. {
  273. unsigned int numIndices = 0;
  274. pMesh->mNumVertices = pMesh->mNumFaces * 3;
  275. pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
  276. pMesh->mNumUVComponents[ 0 ] = 2;
  277. pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ];
  278. pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
  279. unsigned int pos = 0;
  280. for ( unsigned int currentFace = 0; currentFace < pMesh->mNumFaces; currentFace++ ) {
  281. aiFace *pFace = &( pMesh->mFaces[ currentFace ] );
  282. for ( unsigned int currentIdx=0; currentIdx<pFace->mNumIndices; currentIdx++ ) {
  283. const unsigned int idx = pFace->mIndices[ currentIdx ];
  284. if ( vertices.size() > idx ) {
  285. pMesh->mVertices[ pos ] = vertices[ idx ];
  286. pMesh->mNormals[ pos ] = normals[ idx ];
  287. pMesh->mTextureCoords[ 0 ]->x = uvCoords[ idx ].x;
  288. pMesh->mTextureCoords[ 0 ]->y = uvCoords[ idx ].y;
  289. pFace->mIndices[ currentIdx ] = pos;
  290. pos++;
  291. }
  292. }
  293. }
  294. }
  295. // ------------------------------------------------------------------------------------------------
  296. //
  297. aiNode *M3Importer::createNode( aiNode *pParent )
  298. {
  299. aiNode *pNode = new aiNode;
  300. if ( pParent )
  301. pNode->mParent = pParent;
  302. else
  303. pNode->mParent = NULL;
  304. return pNode;
  305. }
  306. // ------------------------------------------------------------------------------------------------
  307. } // Namespace M3
  308. } // Namespace Assimp
  309. #endif // ASSIMP_BUILD_NO_M3_IMPORTER