2
0

AssbinLoader.cpp 26 KB

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