AssbinLoader.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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. #ifndef ASSIMP_BUILD_NO_ASSBIN_IMPORTER
  40. // internal headers
  41. #include "Assbin/AssbinLoader.h"
  42. #include "Common/assbin_chunks.h"
  43. #include <assimp/MemoryIOWrapper.h>
  44. #include <assimp/mesh.h>
  45. #include <assimp/anim.h>
  46. #include <assimp/scene.h>
  47. #include <assimp/importerdesc.h>
  48. #include <memory>
  49. #ifdef ASSIMP_BUILD_NO_OWN_ZLIB
  50. # include <zlib.h>
  51. #else
  52. # include <contrib/zlib/zlib.h>
  53. #endif
  54. using namespace Assimp;
  55. static const aiImporterDesc desc = {
  56. "Assimp Binary Importer",
  57. "Gargaj / Conspiracy",
  58. "",
  59. "",
  60. aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
  61. 0,
  62. 0,
  63. 0,
  64. 0,
  65. "assbin"
  66. };
  67. // -----------------------------------------------------------------------------------
  68. const aiImporterDesc* AssbinImporter::GetInfo() const {
  69. return &desc;
  70. }
  71. // -----------------------------------------------------------------------------------
  72. bool AssbinImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool /*checkSig*/ ) const {
  73. IOStream * in = pIOHandler->Open(pFile);
  74. if (nullptr == in) {
  75. return false;
  76. }
  77. char s[32];
  78. in->Read( s, sizeof(char), 32 );
  79. pIOHandler->Close(in);
  80. return strncmp( s, "ASSIMP.binary-dump.", 19 ) == 0;
  81. }
  82. // -----------------------------------------------------------------------------------
  83. template <typename T>
  84. T Read(IOStream * stream) {
  85. T t;
  86. size_t res = stream->Read( &t, sizeof(T), 1 );
  87. if(res != 1)
  88. throw DeadlyImportError("Unexpected EOF");
  89. return t;
  90. }
  91. // -----------------------------------------------------------------------------------
  92. template <>
  93. aiVector3D Read<aiVector3D>(IOStream * stream) {
  94. aiVector3D v;
  95. v.x = Read<float>(stream);
  96. v.y = Read<float>(stream);
  97. v.z = Read<float>(stream);
  98. return v;
  99. }
  100. // -----------------------------------------------------------------------------------
  101. template <>
  102. aiColor4D Read<aiColor4D>(IOStream * stream) {
  103. aiColor4D c;
  104. c.r = Read<float>(stream);
  105. c.g = Read<float>(stream);
  106. c.b = Read<float>(stream);
  107. c.a = Read<float>(stream);
  108. return c;
  109. }
  110. // -----------------------------------------------------------------------------------
  111. template <>
  112. aiQuaternion Read<aiQuaternion>(IOStream * stream) {
  113. aiQuaternion v;
  114. v.w = Read<float>(stream);
  115. v.x = Read<float>(stream);
  116. v.y = Read<float>(stream);
  117. v.z = Read<float>(stream);
  118. return v;
  119. }
  120. // -----------------------------------------------------------------------------------
  121. template <>
  122. aiString Read<aiString>(IOStream * stream) {
  123. aiString s;
  124. stream->Read(&s.length,4,1);
  125. if(s.length)
  126. stream->Read(s.data,s.length,1);
  127. s.data[s.length] = 0;
  128. return s;
  129. }
  130. // -----------------------------------------------------------------------------------
  131. template <>
  132. aiVertexWeight Read<aiVertexWeight>(IOStream * stream) {
  133. aiVertexWeight w;
  134. w.mVertexId = Read<unsigned int>(stream);
  135. w.mWeight = Read<float>(stream);
  136. return w;
  137. }
  138. // -----------------------------------------------------------------------------------
  139. template <>
  140. aiMatrix4x4 Read<aiMatrix4x4>(IOStream * stream) {
  141. aiMatrix4x4 m;
  142. for (unsigned int i = 0; i < 4;++i) {
  143. for (unsigned int i2 = 0; i2 < 4;++i2) {
  144. m[i][i2] = Read<float>(stream);
  145. }
  146. }
  147. return m;
  148. }
  149. // -----------------------------------------------------------------------------------
  150. template <>
  151. aiVectorKey Read<aiVectorKey>(IOStream * stream) {
  152. aiVectorKey v;
  153. v.mTime = Read<double>(stream);
  154. v.mValue = Read<aiVector3D>(stream);
  155. return v;
  156. }
  157. // -----------------------------------------------------------------------------------
  158. template <>
  159. aiQuatKey Read<aiQuatKey>(IOStream * stream) {
  160. aiQuatKey v;
  161. v.mTime = Read<double>(stream);
  162. v.mValue = Read<aiQuaternion>(stream);
  163. return v;
  164. }
  165. // -----------------------------------------------------------------------------------
  166. template <typename T>
  167. void ReadArray( IOStream *stream, T * out, unsigned int size) {
  168. ai_assert( nullptr != stream );
  169. ai_assert( nullptr != out );
  170. for (unsigned int i=0; i<size; i++) {
  171. out[i] = Read<T>(stream);
  172. }
  173. }
  174. // -----------------------------------------------------------------------------------
  175. template <typename T>
  176. void ReadBounds( IOStream * stream, T* /*p*/, unsigned int n ) {
  177. // not sure what to do here, the data isn't really useful.
  178. stream->Seek( sizeof(T) * n, aiOrigin_CUR );
  179. }
  180. // -----------------------------------------------------------------------------------
  181. void AssbinImporter::ReadBinaryNode( IOStream * stream, aiNode** onode, aiNode* parent ) {
  182. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AINODE)
  183. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  184. /*uint32_t size =*/ Read<uint32_t>(stream);
  185. std::unique_ptr<aiNode> node(new aiNode());
  186. node->mName = Read<aiString>(stream);
  187. node->mTransformation = Read<aiMatrix4x4>(stream);
  188. unsigned numChildren = Read<unsigned int>(stream);
  189. unsigned numMeshes = Read<unsigned int>(stream);
  190. unsigned int nb_metadata = Read<unsigned int>(stream);
  191. if(parent) {
  192. node->mParent = parent;
  193. }
  194. if (numMeshes)
  195. {
  196. node->mMeshes = new unsigned int[numMeshes];
  197. for (unsigned int i = 0; i < numMeshes; ++i) {
  198. node->mMeshes[i] = Read<unsigned int>(stream);
  199. node->mNumMeshes++;
  200. }
  201. }
  202. if (numChildren) {
  203. node->mChildren = new aiNode*[numChildren];
  204. for (unsigned int i = 0; i < numChildren; ++i) {
  205. ReadBinaryNode( stream, &node->mChildren[i], node.get() );
  206. node->mNumChildren++;
  207. }
  208. }
  209. if ( nb_metadata > 0 ) {
  210. node->mMetaData = aiMetadata::Alloc(nb_metadata);
  211. for (unsigned int i = 0; i < nb_metadata; ++i) {
  212. node->mMetaData->mKeys[i] = Read<aiString>(stream);
  213. node->mMetaData->mValues[i].mType = (aiMetadataType) Read<uint16_t>(stream);
  214. void* data = nullptr;
  215. switch (node->mMetaData->mValues[i].mType) {
  216. case AI_BOOL:
  217. data = new bool(Read<bool>(stream));
  218. break;
  219. case AI_INT32:
  220. data = new int32_t(Read<int32_t>(stream));
  221. break;
  222. case AI_UINT64:
  223. data = new uint64_t(Read<uint64_t>(stream));
  224. break;
  225. case AI_FLOAT:
  226. data = new float(Read<float>(stream));
  227. break;
  228. case AI_DOUBLE:
  229. data = new double(Read<double>(stream));
  230. break;
  231. case AI_AISTRING:
  232. data = new aiString(Read<aiString>(stream));
  233. break;
  234. case AI_AIVECTOR3D:
  235. data = new aiVector3D(Read<aiVector3D>(stream));
  236. break;
  237. #ifndef SWIG
  238. case FORCE_32BIT:
  239. #endif // SWIG
  240. default:
  241. break;
  242. }
  243. node->mMetaData->mValues[i].mData = data;
  244. }
  245. }
  246. *onode = node.release();
  247. }
  248. // -----------------------------------------------------------------------------------
  249. void AssbinImporter::ReadBinaryBone( IOStream * stream, aiBone* b ) {
  250. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AIBONE)
  251. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  252. /*uint32_t size =*/ Read<uint32_t>(stream);
  253. b->mName = Read<aiString>(stream);
  254. b->mNumWeights = Read<unsigned int>(stream);
  255. b->mOffsetMatrix = Read<aiMatrix4x4>(stream);
  256. // for the moment we write dumb min/max values for the bones, too.
  257. // maybe I'll add a better, hash-like solution later
  258. if (shortened) {
  259. ReadBounds(stream,b->mWeights,b->mNumWeights);
  260. } else {
  261. // else write as usual
  262. b->mWeights = new aiVertexWeight[b->mNumWeights];
  263. ReadArray<aiVertexWeight>(stream,b->mWeights,b->mNumWeights);
  264. }
  265. }
  266. // -----------------------------------------------------------------------------------
  267. static bool fitsIntoUI16(unsigned int mNumVertices) {
  268. return ( mNumVertices < (1u<<16) );
  269. }
  270. // -----------------------------------------------------------------------------------
  271. void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh ) {
  272. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AIMESH)
  273. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  274. /*uint32_t size =*/ Read<uint32_t>(stream);
  275. mesh->mPrimitiveTypes = Read<unsigned int>(stream);
  276. mesh->mNumVertices = Read<unsigned int>(stream);
  277. mesh->mNumFaces = Read<unsigned int>(stream);
  278. mesh->mNumBones = Read<unsigned int>(stream);
  279. mesh->mMaterialIndex = Read<unsigned int>(stream);
  280. // first of all, write bits for all existent vertex components
  281. unsigned int c = Read<unsigned int>(stream);
  282. if (c & ASSBIN_MESH_HAS_POSITIONS) {
  283. if (shortened) {
  284. ReadBounds(stream,mesh->mVertices,mesh->mNumVertices);
  285. } else {
  286. // else write as usual
  287. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  288. ReadArray<aiVector3D>(stream,mesh->mVertices,mesh->mNumVertices);
  289. }
  290. }
  291. if (c & ASSBIN_MESH_HAS_NORMALS) {
  292. if (shortened) {
  293. ReadBounds(stream,mesh->mNormals,mesh->mNumVertices);
  294. } else {
  295. // else write as usual
  296. mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  297. ReadArray<aiVector3D>(stream,mesh->mNormals,mesh->mNumVertices);
  298. }
  299. }
  300. if (c & ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS) {
  301. if (shortened) {
  302. ReadBounds(stream,mesh->mTangents,mesh->mNumVertices);
  303. ReadBounds(stream,mesh->mBitangents,mesh->mNumVertices);
  304. } else {
  305. // else write as usual
  306. mesh->mTangents = new aiVector3D[mesh->mNumVertices];
  307. ReadArray<aiVector3D>(stream,mesh->mTangents,mesh->mNumVertices);
  308. mesh->mBitangents = new aiVector3D[mesh->mNumVertices];
  309. ReadArray<aiVector3D>(stream,mesh->mBitangents,mesh->mNumVertices);
  310. }
  311. }
  312. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) {
  313. if (!(c & ASSBIN_MESH_HAS_COLOR(n))) {
  314. break;
  315. }
  316. if (shortened) {
  317. ReadBounds(stream,mesh->mColors[n],mesh->mNumVertices);
  318. } else {
  319. // else write as usual
  320. mesh->mColors[n] = new aiColor4D[mesh->mNumVertices];
  321. ReadArray<aiColor4D>(stream,mesh->mColors[n],mesh->mNumVertices);
  322. }
  323. }
  324. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) {
  325. if (!(c & ASSBIN_MESH_HAS_TEXCOORD(n))) {
  326. break;
  327. }
  328. // write number of UV components
  329. mesh->mNumUVComponents[n] = Read<unsigned int>(stream);
  330. if (shortened) {
  331. ReadBounds(stream,mesh->mTextureCoords[n],mesh->mNumVertices);
  332. } else {
  333. // else write as usual
  334. mesh->mTextureCoords[n] = new aiVector3D[mesh->mNumVertices];
  335. ReadArray<aiVector3D>(stream,mesh->mTextureCoords[n],mesh->mNumVertices);
  336. }
  337. }
  338. // write faces. There are no floating-point calculations involved
  339. // in these, so we can write a simple hash over the face data
  340. // to the dump file. We generate a single 32 Bit hash for 512 faces
  341. // using Assimp's standard hashing function.
  342. if (shortened) {
  343. Read<unsigned int>(stream);
  344. } else {
  345. // else write as usual
  346. // if there are less than 2^16 vertices, we can simply use 16 bit integers ...
  347. mesh->mFaces = new aiFace[mesh->mNumFaces];
  348. for (unsigned int i = 0; i < mesh->mNumFaces;++i) {
  349. aiFace& f = mesh->mFaces[i];
  350. static_assert(AI_MAX_FACE_INDICES <= 0xffff, "AI_MAX_FACE_INDICES <= 0xffff");
  351. f.mNumIndices = Read<uint16_t>(stream);
  352. f.mIndices = new unsigned int[f.mNumIndices];
  353. for (unsigned int a = 0; a < f.mNumIndices;++a) {
  354. // Check if unsigned short ( 16 bit ) are big enought for the indices
  355. if ( fitsIntoUI16( mesh->mNumVertices ) ) {
  356. f.mIndices[a] = Read<uint16_t>(stream);
  357. } else {
  358. f.mIndices[a] = Read<unsigned int>(stream);
  359. }
  360. }
  361. }
  362. }
  363. // write bones
  364. if (mesh->mNumBones) {
  365. mesh->mBones = new C_STRUCT aiBone*[mesh->mNumBones];
  366. for (unsigned int a = 0; a < mesh->mNumBones;++a) {
  367. mesh->mBones[a] = new aiBone();
  368. ReadBinaryBone(stream,mesh->mBones[a]);
  369. }
  370. }
  371. }
  372. // -----------------------------------------------------------------------------------
  373. void AssbinImporter::ReadBinaryMaterialProperty(IOStream * stream, aiMaterialProperty* prop) {
  374. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AIMATERIALPROPERTY)
  375. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  376. /*uint32_t size =*/ Read<uint32_t>(stream);
  377. prop->mKey = Read<aiString>(stream);
  378. prop->mSemantic = Read<unsigned int>(stream);
  379. prop->mIndex = Read<unsigned int>(stream);
  380. prop->mDataLength = Read<unsigned int>(stream);
  381. prop->mType = (aiPropertyTypeInfo)Read<unsigned int>(stream);
  382. prop->mData = new char [ prop->mDataLength ];
  383. stream->Read(prop->mData,1,prop->mDataLength);
  384. }
  385. // -----------------------------------------------------------------------------------
  386. void AssbinImporter::ReadBinaryMaterial(IOStream * stream, aiMaterial* mat) {
  387. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AIMATERIAL)
  388. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  389. /*uint32_t size =*/ Read<uint32_t>(stream);
  390. mat->mNumAllocated = mat->mNumProperties = Read<unsigned int>(stream);
  391. if (mat->mNumProperties)
  392. {
  393. if (mat->mProperties)
  394. {
  395. delete[] mat->mProperties;
  396. }
  397. mat->mProperties = new aiMaterialProperty*[mat->mNumProperties];
  398. for (unsigned int i = 0; i < mat->mNumProperties;++i) {
  399. mat->mProperties[i] = new aiMaterialProperty();
  400. ReadBinaryMaterialProperty( stream, mat->mProperties[i]);
  401. }
  402. }
  403. }
  404. // -----------------------------------------------------------------------------------
  405. void AssbinImporter::ReadBinaryNodeAnim(IOStream * stream, aiNodeAnim* nd) {
  406. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AINODEANIM)
  407. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  408. /*uint32_t size =*/ Read<uint32_t>(stream);
  409. nd->mNodeName = Read<aiString>(stream);
  410. nd->mNumPositionKeys = Read<unsigned int>(stream);
  411. nd->mNumRotationKeys = Read<unsigned int>(stream);
  412. nd->mNumScalingKeys = Read<unsigned int>(stream);
  413. nd->mPreState = (aiAnimBehaviour)Read<unsigned int>(stream);
  414. nd->mPostState = (aiAnimBehaviour)Read<unsigned int>(stream);
  415. if (nd->mNumPositionKeys) {
  416. if (shortened) {
  417. ReadBounds(stream,nd->mPositionKeys,nd->mNumPositionKeys);
  418. } // else write as usual
  419. else {
  420. nd->mPositionKeys = new aiVectorKey[nd->mNumPositionKeys];
  421. ReadArray<aiVectorKey>(stream,nd->mPositionKeys,nd->mNumPositionKeys);
  422. }
  423. }
  424. if (nd->mNumRotationKeys) {
  425. if (shortened) {
  426. ReadBounds(stream,nd->mRotationKeys,nd->mNumRotationKeys);
  427. } else {
  428. // else write as usual
  429. nd->mRotationKeys = new aiQuatKey[nd->mNumRotationKeys];
  430. ReadArray<aiQuatKey>(stream,nd->mRotationKeys,nd->mNumRotationKeys);
  431. }
  432. }
  433. if (nd->mNumScalingKeys) {
  434. if (shortened) {
  435. ReadBounds(stream,nd->mScalingKeys,nd->mNumScalingKeys);
  436. } else {
  437. // else write as usual
  438. nd->mScalingKeys = new aiVectorKey[nd->mNumScalingKeys];
  439. ReadArray<aiVectorKey>(stream,nd->mScalingKeys,nd->mNumScalingKeys);
  440. }
  441. }
  442. }
  443. // -----------------------------------------------------------------------------------
  444. void AssbinImporter::ReadBinaryAnim( IOStream * stream, aiAnimation* anim ) {
  445. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AIANIMATION)
  446. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  447. /*uint32_t size =*/ Read<uint32_t>(stream);
  448. anim->mName = Read<aiString> (stream);
  449. anim->mDuration = Read<double> (stream);
  450. anim->mTicksPerSecond = Read<double> (stream);
  451. anim->mNumChannels = Read<unsigned int>(stream);
  452. if (anim->mNumChannels) {
  453. anim->mChannels = new aiNodeAnim*[ anim->mNumChannels ];
  454. for (unsigned int a = 0; a < anim->mNumChannels;++a) {
  455. anim->mChannels[a] = new aiNodeAnim();
  456. ReadBinaryNodeAnim(stream,anim->mChannels[a]);
  457. }
  458. }
  459. }
  460. // -----------------------------------------------------------------------------------
  461. void AssbinImporter::ReadBinaryTexture(IOStream * stream, aiTexture* tex) {
  462. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AITEXTURE)
  463. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  464. /*uint32_t size =*/ Read<uint32_t>(stream);
  465. tex->mWidth = Read<unsigned int>(stream);
  466. tex->mHeight = Read<unsigned int>(stream);
  467. stream->Read( tex->achFormatHint, sizeof(char), 4 );
  468. if(!shortened) {
  469. if (!tex->mHeight) {
  470. tex->pcData = new aiTexel[ tex->mWidth ];
  471. stream->Read(tex->pcData,1,tex->mWidth);
  472. } else {
  473. tex->pcData = new aiTexel[ tex->mWidth*tex->mHeight ];
  474. stream->Read(tex->pcData,1,tex->mWidth*tex->mHeight*4);
  475. }
  476. }
  477. }
  478. // -----------------------------------------------------------------------------------
  479. void AssbinImporter::ReadBinaryLight( IOStream * stream, aiLight* l ) {
  480. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AILIGHT)
  481. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  482. /*uint32_t size =*/ Read<uint32_t>(stream);
  483. l->mName = Read<aiString>(stream);
  484. l->mType = (aiLightSourceType)Read<unsigned int>(stream);
  485. if (l->mType != aiLightSource_DIRECTIONAL) {
  486. l->mAttenuationConstant = Read<float>(stream);
  487. l->mAttenuationLinear = Read<float>(stream);
  488. l->mAttenuationQuadratic = Read<float>(stream);
  489. }
  490. l->mColorDiffuse = Read<aiColor3D>(stream);
  491. l->mColorSpecular = Read<aiColor3D>(stream);
  492. l->mColorAmbient = Read<aiColor3D>(stream);
  493. if (l->mType == aiLightSource_SPOT) {
  494. l->mAngleInnerCone = Read<float>(stream);
  495. l->mAngleOuterCone = Read<float>(stream);
  496. }
  497. }
  498. // -----------------------------------------------------------------------------------
  499. void AssbinImporter::ReadBinaryCamera( IOStream * stream, aiCamera* cam ) {
  500. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AICAMERA)
  501. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  502. /*uint32_t size =*/ Read<uint32_t>(stream);
  503. cam->mName = Read<aiString>(stream);
  504. cam->mPosition = Read<aiVector3D>(stream);
  505. cam->mLookAt = Read<aiVector3D>(stream);
  506. cam->mUp = Read<aiVector3D>(stream);
  507. cam->mHorizontalFOV = Read<float>(stream);
  508. cam->mClipPlaneNear = Read<float>(stream);
  509. cam->mClipPlaneFar = Read<float>(stream);
  510. cam->mAspect = Read<float>(stream);
  511. }
  512. // -----------------------------------------------------------------------------------
  513. void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene ) {
  514. if(Read<uint32_t>(stream) != ASSBIN_CHUNK_AISCENE)
  515. throw DeadlyImportError("Magic chunk identifiers are wrong!");
  516. /*uint32_t size =*/ Read<uint32_t>(stream);
  517. scene->mFlags = Read<unsigned int>(stream);
  518. scene->mNumMeshes = Read<unsigned int>(stream);
  519. scene->mNumMaterials = Read<unsigned int>(stream);
  520. scene->mNumAnimations = Read<unsigned int>(stream);
  521. scene->mNumTextures = Read<unsigned int>(stream);
  522. scene->mNumLights = Read<unsigned int>(stream);
  523. scene->mNumCameras = Read<unsigned int>(stream);
  524. // Read node graph
  525. //scene->mRootNode = new aiNode[1];
  526. ReadBinaryNode( stream, &scene->mRootNode, (aiNode*)NULL );
  527. // Read all meshes
  528. if (scene->mNumMeshes) {
  529. scene->mMeshes = new aiMesh*[scene->mNumMeshes];
  530. memset(scene->mMeshes, 0, scene->mNumMeshes*sizeof(aiMesh*));
  531. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  532. scene->mMeshes[i] = new aiMesh();
  533. ReadBinaryMesh( stream,scene->mMeshes[i]);
  534. }
  535. }
  536. // Read materials
  537. if (scene->mNumMaterials) {
  538. scene->mMaterials = new aiMaterial*[scene->mNumMaterials];
  539. memset(scene->mMaterials, 0, scene->mNumMaterials*sizeof(aiMaterial*));
  540. for (unsigned int i = 0; i< scene->mNumMaterials; ++i) {
  541. scene->mMaterials[i] = new aiMaterial();
  542. ReadBinaryMaterial(stream,scene->mMaterials[i]);
  543. }
  544. }
  545. // Read all animations
  546. if (scene->mNumAnimations) {
  547. scene->mAnimations = new aiAnimation*[scene->mNumAnimations];
  548. memset(scene->mAnimations, 0, scene->mNumAnimations*sizeof(aiAnimation*));
  549. for (unsigned int i = 0; i < scene->mNumAnimations;++i) {
  550. scene->mAnimations[i] = new aiAnimation();
  551. ReadBinaryAnim(stream,scene->mAnimations[i]);
  552. }
  553. }
  554. // Read all textures
  555. if (scene->mNumTextures) {
  556. scene->mTextures = new aiTexture*[scene->mNumTextures];
  557. memset(scene->mTextures, 0, scene->mNumTextures*sizeof(aiTexture*));
  558. for (unsigned int i = 0; i < scene->mNumTextures;++i) {
  559. scene->mTextures[i] = new aiTexture();
  560. ReadBinaryTexture(stream,scene->mTextures[i]);
  561. }
  562. }
  563. // Read lights
  564. if (scene->mNumLights) {
  565. scene->mLights = new aiLight*[scene->mNumLights];
  566. memset(scene->mLights, 0, scene->mNumLights*sizeof(aiLight*));
  567. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  568. scene->mLights[i] = new aiLight();
  569. ReadBinaryLight(stream,scene->mLights[i]);
  570. }
  571. }
  572. // Read cameras
  573. if (scene->mNumCameras) {
  574. scene->mCameras = new aiCamera*[scene->mNumCameras];
  575. memset(scene->mCameras, 0, scene->mNumCameras*sizeof(aiCamera*));
  576. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  577. scene->mCameras[i] = new aiCamera();
  578. ReadBinaryCamera(stream,scene->mCameras[i]);
  579. }
  580. }
  581. }
  582. // -----------------------------------------------------------------------------------
  583. void AssbinImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler ) {
  584. IOStream * stream = pIOHandler->Open(pFile,"rb");
  585. if (nullptr == stream) {
  586. return;
  587. }
  588. // signature
  589. stream->Seek( 44, aiOrigin_CUR );
  590. unsigned int versionMajor = Read<unsigned int>(stream);
  591. unsigned int versionMinor = Read<unsigned int>(stream);
  592. if (versionMinor != ASSBIN_VERSION_MINOR || versionMajor != ASSBIN_VERSION_MAJOR) {
  593. throw DeadlyImportError( "Invalid version, data format not compatible!" );
  594. }
  595. /*unsigned int versionRevision =*/ Read<unsigned int>(stream);
  596. /*unsigned int compileFlags =*/ Read<unsigned int>(stream);
  597. shortened = Read<uint16_t>(stream) > 0;
  598. compressed = Read<uint16_t>(stream) > 0;
  599. if (shortened)
  600. throw DeadlyImportError( "Shortened binaries are not supported!" );
  601. stream->Seek( 256, aiOrigin_CUR ); // original filename
  602. stream->Seek( 128, aiOrigin_CUR ); // options
  603. stream->Seek( 64, aiOrigin_CUR ); // padding
  604. if (compressed) {
  605. uLongf uncompressedSize = Read<uint32_t>(stream);
  606. uLongf compressedSize = static_cast<uLongf>(stream->FileSize() - stream->Tell());
  607. unsigned char * compressedData = new unsigned char[ compressedSize ];
  608. size_t len = stream->Read( compressedData, 1, compressedSize );
  609. ai_assert(len == compressedSize);
  610. unsigned char * uncompressedData = new unsigned char[ uncompressedSize ];
  611. int res = uncompress( uncompressedData, &uncompressedSize, compressedData, (uLong) len );
  612. if(res != Z_OK)
  613. {
  614. delete [] uncompressedData;
  615. delete [] compressedData;
  616. pIOHandler->Close(stream);
  617. throw DeadlyImportError("Zlib decompression failed.");
  618. }
  619. MemoryIOStream io( uncompressedData, uncompressedSize );
  620. ReadBinaryScene(&io,pScene);
  621. delete[] uncompressedData;
  622. delete[] compressedData;
  623. } else {
  624. ReadBinaryScene(stream,pScene);
  625. }
  626. pIOHandler->Close(stream);
  627. }
  628. #endif // !! ASSIMP_BUILD_NO_ASSBIN_IMPORTER