M3Importer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. bool ok = true;
  98. switch( m_pRefs[ m_pHead->MODL.ref ].type ) {
  99. case 20:
  100. pMODL20 = GetEntries<MODL20>( m_pHead->MODL );
  101. if ( ( pMODL20->flags & 0x20000) != 0 ) { // Has vertices
  102. if( (pMODL20->flags & 0x40000) != 0 ) { // Has extra 4 byte
  103. pVerts1 = GetEntries<VertexExt>( pMODL20->vertexData );
  104. nVertices = pMODL20->vertexData.nEntries/sizeof(VertexExt);
  105. }
  106. else {
  107. pVerts2 = GetEntries<Vertex>( pMODL20->vertexData );
  108. nVertices = pMODL20->vertexData.nEntries / sizeof( Vertex );
  109. }
  110. }
  111. pViews = GetEntries<DIV>( pMODL20->views );
  112. break;
  113. case 23:
  114. pMODL23 = GetEntries<MODL23>(m_pHead->MODL );
  115. if( (pMODL23->flags & 0x20000) != 0 ) { // Has vertices
  116. if( (pMODL23->flags & 0x40000) != 0 ) { // Has extra 4 byte
  117. pVerts1 = GetEntries<VertexExt>( pMODL23->vertexData );
  118. nVertices = pMODL23->vertexData.nEntries/sizeof( VertexExt );
  119. }
  120. else {
  121. pVerts2 = GetEntries<Vertex>( pMODL23->vertexData );
  122. nVertices = pMODL23->vertexData.nEntries/sizeof( Vertex );
  123. }
  124. }
  125. pViews = GetEntries<DIV>( pMODL23->views );
  126. break;
  127. default:
  128. ok = false;
  129. break;
  130. }
  131. // Everything ok, if not throw an exception
  132. if ( !ok ) {
  133. throw DeadlyImportError( "Failed to open file " + pFile + ".");
  134. }
  135. // Get all region data
  136. regions = GetEntries<Region>( pViews->regions );
  137. // Get the face data
  138. faces = GetEntries<uint16>( pViews->faces );
  139. // Convert the vertices
  140. std::vector<aiVector3D> vertices;
  141. vertices.resize( nVertices );
  142. unsigned int offset = 0;
  143. for ( unsigned int i = 0; i < nVertices; i++ ) {
  144. if ( pVerts1 ) {
  145. vertices[ offset ].Set( pVerts1[ i ].pos.x, pVerts1[ i ].pos.y, pVerts1[ i ].pos.z );
  146. ++offset;
  147. }
  148. if ( pVerts2 ) {
  149. vertices[ offset ].Set( pVerts2[ i ].pos.x, pVerts2[ i ].pos.y, pVerts2[ i ].pos.z );
  150. ++offset;
  151. }
  152. }
  153. // Write the UV coordinates
  154. offset = 0;
  155. std::vector<aiVector3D> uvCoords;
  156. uvCoords.resize( nVertices );
  157. for( unsigned int i = 0; i < nVertices; ++i ) {
  158. if( pVerts1 ) {
  159. float u = (float) pVerts1[ i ].uv[ 0 ] / 2048;
  160. float v = (float) pVerts1[ i ].uv[ 1 ] / 2048;
  161. uvCoords[ offset ].Set( u, v, 0.0f );
  162. ++offset;
  163. }
  164. if( pVerts2 ) {
  165. float u = (float) pVerts2[ i ].uv[ 0 ] / 2048;
  166. float v = (float) pVerts2[ i ].uv[ 1 ] / 2048;
  167. uvCoords[ offset ].Set( u, v, 0.0f );
  168. ++offset;
  169. }
  170. }
  171. // Compute the normals
  172. std::vector<aiVector3D> normals;
  173. normals.resize( nVertices );
  174. float w = 0.0f;
  175. Vec3D norm;
  176. offset = 0;
  177. for( unsigned int i = 0; i < nVertices; i++ ) {
  178. w = 0.0f;
  179. if( pVerts1 ) {
  180. norm.x = (float) 2*pVerts1[ i ].normal[ 0 ]/255.0f - 1;
  181. norm.y = (float) 2*pVerts1[ i ].normal[ 1 ]/255.0f - 1;
  182. norm.z = (float) 2*pVerts1[ i ].normal[ 2 ]/255.0f - 1;
  183. w = (float) pVerts1[ i ].normal[ 3 ]/255.0f;
  184. }
  185. if( pVerts2 ) {
  186. norm.x = (float) 2*pVerts2[ i ].normal[ 0 ]/255.0f - 1;
  187. norm.y = (float) 2*pVerts2[ i ].normal[ 1 ]/255.0f - 1;
  188. norm.z = (float) 2*pVerts2[ i ].normal[ 2 ]/255.0f - 1;
  189. w = (float) pVerts2[ i ].normal[ 3 ] / 255.0f;
  190. }
  191. if ( w ) {
  192. const float invW = 1.0f / w;
  193. norm.x = norm.x * invW;
  194. norm.y = norm.y * invW;
  195. norm.z = norm.z * invW;
  196. normals[ offset ].Set( norm.x, norm.y, norm.z );
  197. ++offset;
  198. }
  199. }
  200. // Convert the data into the assimp specific data structures
  201. convertToAssimp( pFile, pScene, pViews, regions, faces, vertices, uvCoords, normals );
  202. }
  203. // ------------------------------------------------------------------------------------------------
  204. //
  205. void M3Importer::convertToAssimp( const std::string& pFile, aiScene* pScene, DIV *pViews,
  206. Region *pRegions, uint16 *pFaces,
  207. const std::vector<aiVector3D> &vertices,
  208. const std::vector<aiVector3D> &uvCoords,
  209. const std::vector<aiVector3D> &normals )
  210. {
  211. std::vector<aiMesh*> MeshArray;
  212. // Create the root node
  213. pScene->mRootNode = createNode( NULL );
  214. // Set the name of the scene
  215. pScene->mRootNode->mName.Set( pFile );
  216. aiNode *pRootNode = pScene->mRootNode;
  217. aiNode *pCurrentNode = NULL;
  218. // Lets create the nodes
  219. pRootNode->mNumChildren = pViews->regions.nEntries;
  220. if ( pRootNode->mNumChildren > 0 ) {
  221. pRootNode->mChildren = new aiNode*[ pRootNode->mNumChildren ];
  222. }
  223. for ( unsigned int i=0; i<pRootNode->mNumChildren; ++i ) {
  224. //pRegions[ i ].
  225. // Create a new node
  226. pCurrentNode = createNode( pRootNode );
  227. std::stringstream stream;
  228. stream << "Node_" << i;
  229. pCurrentNode->mName.Set( stream.str().c_str() );
  230. pRootNode->mChildren[ i ] = pCurrentNode;
  231. // Loop over the faces of the nodes
  232. unsigned int numFaces = ( ( pRegions[ i ].ofsIndices + pRegions[ i ].nIndices ) - pRegions[ i ].ofsIndices ) / 3;
  233. aiMesh *pMesh = new aiMesh;
  234. MeshArray.push_back( pMesh );
  235. pMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  236. pMesh->mNumFaces = numFaces;
  237. pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
  238. aiFace *pCurrentFace = NULL;
  239. unsigned int faceIdx = 0;
  240. for ( unsigned int j = pRegions[ i ].ofsIndices; j < ( pRegions[ i ].ofsIndices + pRegions[ i ].nIndices ); j += 3 ) {
  241. pCurrentFace = &( pMesh->mFaces[ faceIdx ] );
  242. faceIdx++;
  243. pCurrentFace->mNumIndices = 3;
  244. pCurrentFace->mIndices = new unsigned int[ 3 ];
  245. pCurrentFace->mIndices[ 0 ] = pFaces[ j ];
  246. pCurrentFace->mIndices[ 1 ] = pFaces[ j+1 ];
  247. pCurrentFace->mIndices[ 2 ] = pFaces[ j+2 ];
  248. }
  249. // Now we can create the vertex data itself
  250. pCurrentNode->mNumMeshes = 1;
  251. pCurrentNode->mMeshes = new unsigned int[ 1 ];
  252. const unsigned int meshIdx = MeshArray.size() - 1;
  253. pCurrentNode->mMeshes[ 0 ] = meshIdx;
  254. createVertexData( pMesh, vertices, uvCoords, normals );
  255. }
  256. // Copy the meshes into the scene
  257. pScene->mNumMeshes = MeshArray.size();
  258. pScene->mMeshes = new aiMesh*[ MeshArray.size() ];
  259. unsigned int pos = 0;
  260. for ( std::vector<aiMesh*>::iterator it = MeshArray.begin(); it != MeshArray.end(); ++it ) {
  261. pScene->mMeshes[ pos ] = *it;
  262. ++pos;
  263. }
  264. }
  265. // ------------------------------------------------------------------------------------------------
  266. //
  267. void M3Importer::createVertexData( aiMesh *pMesh, const std::vector<aiVector3D> &vertices,
  268. const std::vector<aiVector3D> &uvCoords,
  269. const std::vector<aiVector3D> &normals )
  270. {
  271. pMesh->mNumVertices = pMesh->mNumFaces * 3;
  272. pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
  273. pMesh->mNumUVComponents[ 0 ] = 2;
  274. pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ];
  275. pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
  276. unsigned int pos = 0;
  277. for ( unsigned int currentFace = 0; currentFace < pMesh->mNumFaces; currentFace++ ) {
  278. aiFace *pFace = &( pMesh->mFaces[ currentFace ] );
  279. for ( unsigned int currentIdx=0; currentIdx<pFace->mNumIndices; currentIdx++ ) {
  280. const unsigned int idx = pFace->mIndices[ currentIdx ];
  281. if ( vertices.size() > idx ) {
  282. pMesh->mVertices[ pos ] = vertices[ idx ];
  283. pMesh->mNormals[ pos ] = normals[ idx ];
  284. pMesh->mTextureCoords[ 0 ]->x = uvCoords[ idx ].x;
  285. pMesh->mTextureCoords[ 0 ]->y = uvCoords[ idx ].y;
  286. pFace->mIndices[ currentIdx ] = pos;
  287. pos++;
  288. }
  289. }
  290. }
  291. }
  292. // ------------------------------------------------------------------------------------------------
  293. //
  294. aiNode *M3Importer::createNode( aiNode *pParent )
  295. {
  296. aiNode *pNode = new aiNode;
  297. if ( pParent )
  298. pNode->mParent = pParent;
  299. else
  300. pNode->mParent = NULL;
  301. return pNode;
  302. }
  303. // ------------------------------------------------------------------------------------------------
  304. } // Namespace M3
  305. } // Namespace Assimp
  306. #endif // ASSIMP_BUILD_NO_M3_IMPORTER