AssbinLoader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file AssbinLoader.cpp
  35. * @brief Implementation of the .assbin importer class
  36. *
  37. * see assbin_chunks.h
  38. */
  39. #include "AssimpPCH.h"
  40. #ifndef ASSIMP_BUILD_NO_ASSBIN_IMPORTER
  41. // internal headers
  42. #include "AssbinLoader.h"
  43. #include "assbin_chunks.h"
  44. using namespace Assimp;
  45. static const aiImporterDesc desc = {
  46. ".assbin Importer",
  47. "Gargaj / Conspiracy",
  48. "",
  49. "",
  50. aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
  51. 0,
  52. 0,
  53. 0,
  54. 0,
  55. "assbin"
  56. };
  57. const aiImporterDesc* AssbinImporter::GetInfo() const
  58. {
  59. return &desc;
  60. }
  61. bool AssbinImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig ) const
  62. {
  63. IOStream * in = pIOHandler->Open(pFile);
  64. if (!in)
  65. return false;
  66. char s[32];
  67. in->Read( s, sizeof(char), 32 );
  68. pIOHandler->Close(in);
  69. return strncmp( s, "ASSIMP.binary-dump.", 19 ) == 0;
  70. }
  71. template <typename T>
  72. T Read(IOStream * stream)
  73. {
  74. T t;
  75. stream->Read( &t, sizeof(T), 1 );
  76. return t;
  77. }
  78. template <>
  79. aiString Read<aiString>(IOStream * stream)
  80. {
  81. aiString s;
  82. stream->Read(&s.length,4,1);
  83. stream->Read(s.data,s.length,1);
  84. return s;
  85. }
  86. template <>
  87. aiMatrix4x4 Read<aiMatrix4x4>(IOStream * stream)
  88. {
  89. aiMatrix4x4 m;
  90. for (unsigned int i = 0; i < 4;++i) {
  91. for (unsigned int i2 = 0; i2 < 4;++i2) {
  92. m[i][i2] = Read<float>(stream);
  93. }
  94. }
  95. return m;
  96. }
  97. template <typename T> void ReadBounds( IOStream * stream, T* p, unsigned int n )
  98. {
  99. // not sure what to do here, the data isn't really useful.
  100. stream->Seek( sizeof(T) * n, aiOrigin_CUR );
  101. }
  102. void AssbinImporter::ReadBinaryNode( IOStream * stream, aiNode** node )
  103. {
  104. ai_assert( Read<uint32_t>(stream) == ASSBIN_CHUNK_AINODE);
  105. uint32_t size = Read<uint32_t>(stream);
  106. *node = new aiNode();
  107. (*node)->mName = Read<aiString>(stream);
  108. (*node)->mTransformation = Read<aiMatrix4x4>(stream);
  109. (*node)->mNumChildren = Read<unsigned int>(stream);
  110. (*node)->mNumMeshes = Read<unsigned int>(stream);
  111. if ((*node)->mNumMeshes)
  112. {
  113. (*node)->mMeshes = new unsigned int[(*node)->mNumMeshes];
  114. for (unsigned int i = 0; i < (*node)->mNumMeshes; ++i) {
  115. (*node)->mMeshes[i] = Read<unsigned int>(stream);
  116. }
  117. }
  118. if ((*node)->mNumChildren)
  119. {
  120. (*node)->mChildren = new aiNode*[(*node)->mNumChildren];
  121. for (unsigned int i = 0; i < (*node)->mNumChildren; ++i) {
  122. ReadBinaryNode( stream, &(*node)->mChildren[i] );
  123. }
  124. }
  125. }
  126. // -----------------------------------------------------------------------------------
  127. void AssbinImporter::ReadBinaryBone( IOStream * stream, aiBone* b )
  128. {
  129. ai_assert( Read<uint32_t>(stream) == ASSBIN_CHUNK_AIBONE );
  130. uint32_t size = Read<uint32_t>(stream);
  131. b->mName = Read<aiString>(stream);
  132. b->mNumWeights = Read<unsigned int>(stream);
  133. b->mOffsetMatrix = Read<aiMatrix4x4>(stream);
  134. // for the moment we write dumb min/max values for the bones, too.
  135. // maybe I'll add a better, hash-like solution later
  136. if (shortened)
  137. {
  138. ReadBounds(stream,b->mWeights,b->mNumWeights);
  139. } // else write as usual
  140. else
  141. {
  142. b->mWeights = new aiVertexWeight[b->mNumWeights];
  143. stream->Read(b->mWeights,1,b->mNumWeights*sizeof(aiVertexWeight));
  144. }
  145. }
  146. void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh )
  147. {
  148. ai_assert( Read<uint32_t>(stream) == ASSBIN_CHUNK_AIMESH);
  149. uint32_t size = Read<uint32_t>(stream);
  150. mesh->mPrimitiveTypes = Read<unsigned int>(stream);
  151. mesh->mNumVertices = Read<unsigned int>(stream);
  152. mesh->mNumFaces = Read<unsigned int>(stream);
  153. mesh->mNumBones = Read<unsigned int>(stream);
  154. mesh->mMaterialIndex = Read<unsigned int>(stream);
  155. // first of all, write bits for all existent vertex components
  156. unsigned int c = Read<unsigned int>(stream);
  157. if (c & ASSBIN_MESH_HAS_POSITIONS)
  158. {
  159. if (shortened) {
  160. ReadBounds(stream,mesh->mVertices,mesh->mNumVertices);
  161. } // else write as usual
  162. else
  163. {
  164. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  165. stream->Read(mesh->mVertices,1,12*mesh->mNumVertices);
  166. }
  167. }
  168. if (c & ASSBIN_MESH_HAS_NORMALS)
  169. {
  170. if (shortened) {
  171. ReadBounds(stream,mesh->mNormals,mesh->mNumVertices);
  172. } // else write as usual
  173. else
  174. {
  175. mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  176. stream->Read(mesh->mNormals,1,12*mesh->mNumVertices);
  177. }
  178. }
  179. if (c & ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS)
  180. {
  181. if (shortened) {
  182. ReadBounds(stream,mesh->mTangents,mesh->mNumVertices);
  183. ReadBounds(stream,mesh->mBitangents,mesh->mNumVertices);
  184. } // else write as usual
  185. else {
  186. mesh->mTangents = new aiVector3D[mesh->mNumVertices];
  187. stream->Read(mesh->mTangents,1,12*mesh->mNumVertices);
  188. mesh->mBitangents = new aiVector3D[mesh->mNumVertices];
  189. stream->Read(mesh->mBitangents,1,12*mesh->mNumVertices);
  190. }
  191. }
  192. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n)
  193. {
  194. if (!(c & ASSBIN_MESH_HAS_COLOR(n)))
  195. break;
  196. if (shortened)
  197. {
  198. ReadBounds(stream,mesh->mColors[n],mesh->mNumVertices);
  199. } // else write as usual
  200. else
  201. {
  202. mesh->mColors[n] = new aiColor4D[mesh->mNumVertices];
  203. stream->Read(mesh->mColors[n],16*mesh->mNumVertices,1);
  204. }
  205. }
  206. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n)
  207. {
  208. if (!(c & ASSBIN_MESH_HAS_TEXCOORD(n)))
  209. break;
  210. // write number of UV components
  211. mesh->mNumUVComponents[n] = Read<unsigned int>(stream);
  212. if (shortened) {
  213. ReadBounds(stream,mesh->mTextureCoords[n],mesh->mNumVertices);
  214. } // else write as usual
  215. else
  216. {
  217. mesh->mTextureCoords[n] = new aiVector3D[mesh->mNumVertices];
  218. stream->Read(mesh->mTextureCoords[n],12*mesh->mNumVertices,1);
  219. }
  220. }
  221. // write faces. There are no floating-point calculations involved
  222. // in these, so we can write a simple hash over the face data
  223. // to the dump file. We generate a single 32 Bit hash for 512 faces
  224. // using Assimp's standard hashing function.
  225. if (shortened) {
  226. Read<unsigned int>(stream);
  227. }
  228. else // else write as usual
  229. {
  230. // if there are less than 2^16 vertices, we can simply use 16 bit integers ...
  231. mesh->mFaces = new aiFace[mesh->mNumFaces];
  232. for (unsigned int i = 0; i < mesh->mNumFaces;++i) {
  233. aiFace& f = mesh->mFaces[i];
  234. BOOST_STATIC_ASSERT(AI_MAX_FACE_INDICES <= 0xffff);
  235. f.mNumIndices = Read<uint16_t>(stream);
  236. f.mIndices = new unsigned int[f.mNumIndices];
  237. for (unsigned int a = 0; a < f.mNumIndices;++a) {
  238. if (mesh->mNumVertices < (1u<<16))
  239. {
  240. f.mIndices[a] = Read<uint16_t>(stream);
  241. }
  242. else
  243. {
  244. f.mIndices[a] = Read<unsigned int>(stream);
  245. }
  246. }
  247. }
  248. }
  249. // write bones
  250. if (mesh->mNumBones) {
  251. mesh->mBones = new C_STRUCT aiBone*[mesh->mNumBones];
  252. for (unsigned int a = 0; a < mesh->mNumBones;++a) {
  253. mesh->mBones[a] = new aiBone();
  254. ReadBinaryBone(stream,mesh->mBones[a]);
  255. }
  256. }
  257. }
  258. void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene )
  259. {
  260. ai_assert( Read<uint32_t>(stream) == ASSBIN_CHUNK_AISCENE);
  261. uint32_t size = Read<uint32_t>(stream);
  262. scene->mFlags = Read<unsigned int>(stream);
  263. scene->mNumMeshes = Read<unsigned int>(stream);
  264. scene->mNumMaterials = Read<unsigned int>(stream);
  265. scene->mNumAnimations = Read<unsigned int>(stream);
  266. scene->mNumTextures = Read<unsigned int>(stream);
  267. scene->mNumLights = Read<unsigned int>(stream);
  268. scene->mNumCameras = Read<unsigned int>(stream);
  269. // Read node graph
  270. scene->mRootNode = new aiNode[1];
  271. ReadBinaryNode( stream, &scene->mRootNode );
  272. // Read all meshes
  273. if (scene->mNumMeshes)
  274. {
  275. scene->mMeshes = new aiMesh*[scene->mNumMeshes];
  276. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  277. scene->mMeshes[i] = new aiMesh();
  278. ReadBinaryMesh( stream,scene->mMeshes[i]);
  279. }
  280. }
  281. /*
  282. // Read materials
  283. for (unsigned int i = 0; i< scene->mNumMaterials; ++i) {
  284. const aiMaterial* mat = scene->mMaterials[i];
  285. ReadBinaryMaterial(stream,mat);
  286. }
  287. // Read all animations
  288. for (unsigned int i = 0; i < scene->mNumAnimations;++i) {
  289. const aiAnimation* anim = scene->mAnimations[i];
  290. ReadBinaryAnim(stream,anim);
  291. }
  292. // Read all textures
  293. for (unsigned int i = 0; i < scene->mNumTextures;++i) {
  294. const aiTexture* mesh = scene->mTextures[i];
  295. ReadBinaryTexture(stream,mesh);
  296. }
  297. // Read lights
  298. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  299. const aiLight* l = scene->mLights[i];
  300. ReadBinaryLight(stream,l);
  301. }
  302. // Read cameras
  303. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  304. const aiCamera* cam = scene->mCameras[i];
  305. ReadBinaryCamera(stream,cam);
  306. }
  307. */
  308. }
  309. void AssbinImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler )
  310. {
  311. IOStream * stream = pIOHandler->Open(pFile,"rb");
  312. if (!stream)
  313. return;
  314. stream->Seek( 44, aiOrigin_CUR ); // signature
  315. unsigned int versionMajor = Read<unsigned int>(stream);
  316. unsigned int versionMinor = Read<unsigned int>(stream);
  317. unsigned int versionRevision = Read<unsigned int>(stream);
  318. unsigned int compileFlags = Read<unsigned int>(stream);
  319. shortened = Read<uint16_t>(stream) > 0;
  320. compressed = Read<uint16_t>(stream) > 0;
  321. stream->Seek( 256, aiOrigin_CUR ); // original filename
  322. stream->Seek( 128, aiOrigin_CUR ); // options
  323. stream->Seek( 64, aiOrigin_CUR ); // padding
  324. if (compressed)
  325. {
  326. // TODO
  327. }
  328. else
  329. {
  330. ReadBinaryScene(stream,pScene);
  331. }
  332. pIOHandler->Close(stream);
  333. }
  334. #endif // !! ASSIMP_BUILD_NO_ASSBIN_IMPORTER