AssbinLoader.cpp 23 KB

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