AssbinLoader.cpp 23 KB

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