IQMImporter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2021, 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. #ifndef ASSIMP_BUILD_NO_IQM_IMPORTER
  34. #include <assimp/DefaultIOSystem.h>
  35. #include <assimp/IOStreamBuffer.h>
  36. #include <assimp/ai_assert.h>
  37. #include <assimp/importerdesc.h>
  38. #include <assimp/scene.h>
  39. #include <assimp/DefaultLogger.hpp>
  40. #include <assimp/Importer.hpp>
  41. #include <assimp/ByteSwapper.h>
  42. #include <memory>
  43. #include <numeric>
  44. #include "IQMImporter.h"
  45. #include "iqm.h"
  46. // RESOURCES:
  47. // http://sauerbraten.org/iqm/
  48. // https://github.com/lsalzman/iqm
  49. inline void swap_block( uint32_t *block, size_t size ){
  50. (void)block; // suppress 'unreferenced formal parameter' MSVC warning
  51. size >>= 2;
  52. for ( size_t i = 0; i < size; ++i )
  53. AI_SWAP4( block[ i ] );
  54. }
  55. static constexpr aiImporterDesc desc = {
  56. "Inter-Quake Model Importer",
  57. "",
  58. "",
  59. "",
  60. aiImporterFlags_SupportBinaryFlavour,
  61. 0,
  62. 0,
  63. 0,
  64. 0,
  65. "iqm"
  66. };
  67. namespace Assimp {
  68. // ------------------------------------------------------------------------------------------------
  69. // Default constructor
  70. IQMImporter::IQMImporter() :
  71. mScene(nullptr) {
  72. // empty
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. // Returns true, if file is a binary Inter-Quake Model file.
  76. bool IQMImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
  77. const std::string extension = GetExtension(pFile);
  78. if (extension == "iqm")
  79. return true;
  80. else if (!extension.length() || checkSig) {
  81. if (!pIOHandler) {
  82. return true;
  83. }
  84. std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile, "rb"));
  85. unsigned char data[15];
  86. if (!pStream || 15 != pStream->Read(data, 1, 15)) {
  87. return false;
  88. }
  89. return !memcmp(data, "INTERQUAKEMODEL", 15);
  90. }
  91. return false;
  92. }
  93. // ------------------------------------------------------------------------------------------------
  94. const aiImporterDesc *IQMImporter::GetInfo() const {
  95. return &desc;
  96. }
  97. // ------------------------------------------------------------------------------------------------
  98. // Model 3D import implementation
  99. void IQMImporter::InternReadFile(const std::string &file, aiScene *pScene, IOSystem *pIOHandler) {
  100. // Read file into memory
  101. std::unique_ptr<IOStream> pStream(pIOHandler->Open(file, "rb"));
  102. if (!pStream) {
  103. throw DeadlyImportError("Failed to open file ", file, ".");
  104. }
  105. // Get the file-size and validate it, throwing an exception when fails
  106. const size_t fileSize = pStream->FileSize();
  107. if (fileSize < sizeof( iqmheader )) {
  108. throw DeadlyImportError("IQM-file ", file, " is too small.");
  109. }
  110. std::vector<unsigned char> buffer(fileSize);
  111. unsigned char *data = buffer.data();
  112. if (fileSize != pStream->Read(data, 1, fileSize)) {
  113. throw DeadlyImportError("Failed to read the file ", file, ".");
  114. }
  115. // get header
  116. iqmheader &hdr = reinterpret_cast<iqmheader&>( *data );
  117. swap_block( &hdr.version, sizeof( iqmheader ) - sizeof( iqmheader::magic ) );
  118. // extra check for header
  119. if (memcmp(data, IQM_MAGIC, sizeof( IQM_MAGIC ) )
  120. || hdr.version != IQM_VERSION
  121. || hdr.filesize != fileSize) {
  122. throw DeadlyImportError("Bad binary header in file ", file, ".");
  123. }
  124. ASSIMP_LOG_DEBUG("IQM: loading ", file);
  125. // create the root node
  126. pScene->mRootNode = new aiNode( "<IQMRoot>" );
  127. // Now rotate the whole scene 90 degrees around the x axis to convert to internal coordinate system
  128. pScene->mRootNode->mTransformation = aiMatrix4x4(
  129. 1.f, 0.f, 0.f, 0.f,
  130. 0.f, 0.f, 1.f, 0.f,
  131. 0.f, -1.f, 0.f, 0.f,
  132. 0.f, 0.f, 0.f, 1.f);
  133. pScene->mRootNode->mNumMeshes = hdr.num_meshes;
  134. pScene->mRootNode->mMeshes = new unsigned int[hdr.num_meshes];
  135. std::iota( pScene->mRootNode->mMeshes, pScene->mRootNode->mMeshes + pScene->mRootNode->mNumMeshes, 0 );
  136. mScene = pScene;
  137. // Allocate output storage
  138. pScene->mNumMeshes = 0;
  139. pScene->mMeshes = new aiMesh *[hdr.num_meshes](); // Set arrays to zero to ensue proper destruction if an exception is raised
  140. pScene->mNumMaterials = 0;
  141. pScene->mMaterials = new aiMaterial *[hdr.num_meshes]();
  142. // swap vertex arrays beforehand...
  143. for( auto array = reinterpret_cast<iqmvertexarray*>( data + hdr.ofs_vertexarrays ), end = array + hdr.num_vertexarrays; array != end; ++array )
  144. {
  145. swap_block( &array->type, sizeof( iqmvertexarray ) );
  146. }
  147. // Read all surfaces from the file
  148. for( auto imesh = reinterpret_cast<iqmmesh*>( data + hdr.ofs_meshes ), end_ = imesh + hdr.num_meshes; imesh != end_; ++imesh )
  149. {
  150. swap_block( &imesh->name, sizeof( iqmmesh ) );
  151. // Allocate output mesh & material
  152. auto mesh = pScene->mMeshes[pScene->mNumMeshes++] = new aiMesh();
  153. mesh->mMaterialIndex = pScene->mNumMaterials;
  154. auto mat = pScene->mMaterials[pScene->mNumMaterials++] = new aiMaterial();
  155. {
  156. auto text = reinterpret_cast<char*>( data + hdr.ofs_text );
  157. aiString name( text + imesh->material );
  158. mat->AddProperty( &name, AI_MATKEY_NAME );
  159. mat->AddProperty( &name, AI_MATKEY_TEXTURE_DIFFUSE(0) );
  160. }
  161. // Fill mesh information
  162. mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  163. mesh->mNumFaces = 0;
  164. mesh->mFaces = new aiFace[imesh->num_triangles];
  165. // Fill in all triangles
  166. for( auto tri = reinterpret_cast<iqmtriangle*>( data + hdr.ofs_triangles ) + imesh->first_triangle, end = tri + imesh->num_triangles; tri != end; ++tri )
  167. {
  168. swap_block( tri->vertex, sizeof( tri->vertex ) );
  169. auto& face = mesh->mFaces[mesh->mNumFaces++];
  170. face.mNumIndices = 3;
  171. face.mIndices = new unsigned int[3]{ tri->vertex[0] - imesh->first_vertex,
  172. tri->vertex[2] - imesh->first_vertex,
  173. tri->vertex[1] - imesh->first_vertex };
  174. }
  175. // Fill in all vertices
  176. for( auto array = reinterpret_cast<const iqmvertexarray*>( data + hdr.ofs_vertexarrays ), end__ = array + hdr.num_vertexarrays; array != end__; ++array )
  177. {
  178. const unsigned int nVerts = imesh->num_vertexes;
  179. const unsigned int step = array->size;
  180. switch ( array->type )
  181. {
  182. case IQM_POSITION:
  183. if( array->format == IQM_FLOAT && step >= 3 ){
  184. mesh->mNumVertices = nVerts;
  185. auto v = mesh->mVertices = new aiVector3D[nVerts];
  186. for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
  187. end = f + nVerts * step; f != end; f += step, ++v )
  188. {
  189. *v = { AI_BE( f[0] ),
  190. AI_BE( f[1] ),
  191. AI_BE( f[2] ) };
  192. }
  193. }
  194. break;
  195. case IQM_TEXCOORD:
  196. if( array->format == IQM_FLOAT && step >= 2)
  197. {
  198. auto v = mesh->mTextureCoords[0] = new aiVector3D[nVerts];
  199. mesh->mNumUVComponents[0] = 2;
  200. for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
  201. end = f + nVerts * step; f != end; f += step, ++v )
  202. {
  203. *v = { AI_BE( f[0] ),
  204. 1 - AI_BE( f[1] ), 0 };
  205. }
  206. }
  207. break;
  208. case IQM_NORMAL:
  209. if (array->format == IQM_FLOAT && step >= 3)
  210. {
  211. auto v = mesh->mNormals = new aiVector3D[nVerts];
  212. for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
  213. end = f + nVerts * step; f != end; f += step, ++v )
  214. {
  215. *v = { AI_BE( f[0] ),
  216. AI_BE( f[1] ),
  217. AI_BE( f[2] ) };
  218. }
  219. }
  220. break;
  221. case IQM_COLOR:
  222. if (array->format == IQM_UBYTE && step >= 3)
  223. {
  224. auto v = mesh->mColors[0] = new aiColor4D[nVerts];
  225. for( auto f = ( data + array->offset ) + imesh->first_vertex * step,
  226. end = f + nVerts * step; f != end; f += step, ++v )
  227. {
  228. *v = { ( f[0] ) / 255.f,
  229. ( f[1] ) / 255.f,
  230. ( f[2] ) / 255.f,
  231. step == 3? 1 : ( f[3] ) / 255.f };
  232. }
  233. }
  234. else if (array->format == IQM_FLOAT && step >= 3)
  235. {
  236. auto v = mesh->mColors[0] = new aiColor4D[nVerts];
  237. for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
  238. end = f + nVerts * step; f != end; f += step, ++v )
  239. {
  240. *v = { AI_BE( f[0] ),
  241. AI_BE( f[1] ),
  242. AI_BE( f[2] ),
  243. step == 3? 1 : AI_BE( f[3] ) };
  244. }
  245. }
  246. break;
  247. case IQM_TANGENT:
  248. #if 0
  249. if (array->format == IQM_FLOAT && step >= 3)
  250. {
  251. auto v = mesh->mTangents = new aiVector3D[nVerts];
  252. for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
  253. end = f + nVerts * step; f != end; f += step, ++v )
  254. {
  255. *v = { AI_BE( f[0] ),
  256. AI_BE( f[1] ),
  257. AI_BE( f[2] ) };
  258. }
  259. }
  260. #endif
  261. break;
  262. case IQM_BLENDINDEXES:
  263. case IQM_BLENDWEIGHTS:
  264. case IQM_CUSTOM:
  265. break; // these attributes are not relevant.
  266. default:
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. // ------------------------------------------------------------------------------------------------
  273. } // Namespace Assimp
  274. #endif // !! ASSIMP_BUILD_NO_IQM_IMPORTER