M3Importer.cpp 12 KB

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