M3Importer.cpp 12 KB

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