AssbinLoader.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file AssbinLoader.cpp
  35. * @brief Implementation of the .assbin importer class
  36. *
  37. * see assbin_chunks.h
  38. */
  39. #include "AssimpPCH.h"
  40. #ifndef ASSIMP_BUILD_NO_ASSBIN_IMPORTER
  41. // internal headers
  42. #include "AssbinLoader.h"
  43. #include "assbin_chunks.h"
  44. using namespace Assimp;
  45. static const aiImporterDesc desc = {
  46. ".assbin Importer",
  47. "Gargaj / Conspiracy",
  48. "",
  49. "",
  50. aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
  51. 0,
  52. 0,
  53. 0,
  54. 0,
  55. "assbin"
  56. };
  57. const aiImporterDesc* AssbinImporter::GetInfo() const
  58. {
  59. return &desc;
  60. }
  61. bool AssbinImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig ) const
  62. {
  63. IOStream * in = pIOHandler->Open(pFile);
  64. if (!in)
  65. return false;
  66. char s[32];
  67. in->Read( s, sizeof(char), 32 );
  68. pIOHandler->Close(in);
  69. return strncmp( s, "ASSIMP.binary-dump.", 19 ) == 0;
  70. }
  71. template <typename T>
  72. T Read(IOStream * stream)
  73. {
  74. T t;
  75. stream->Read( &t, sizeof(T), 1 );
  76. return t;
  77. }
  78. template <>
  79. aiString Read<aiString>(IOStream * stream)
  80. {
  81. aiString s;
  82. stream->Read(&s.length,4,1);
  83. stream->Read(s.data,s.length,1);
  84. return s;
  85. }
  86. template <>
  87. aiMatrix4x4 Read<aiMatrix4x4>(IOStream * stream)
  88. {
  89. aiMatrix4x4 m;
  90. for (unsigned int i = 0; i < 4;++i) {
  91. for (unsigned int i2 = 0; i2 < 4;++i2) {
  92. m[i][i2] = Read<float>(stream);
  93. }
  94. }
  95. return m;
  96. }
  97. void AssbinImporter::ReadBinaryNode( IOStream * stream, aiNode** node )
  98. {
  99. ai_assert( Read<uint32_t>(stream) == ASSBIN_CHUNK_AINODE);
  100. uint32_t size = Read<uint32_t>(stream);
  101. *node = new aiNode();
  102. (*node)->mName = Read<aiString>(stream);
  103. (*node)->mTransformation = Read<aiMatrix4x4>(stream);
  104. (*node)->mNumChildren = Read<unsigned int>(stream);
  105. (*node)->mNumMeshes = Read<unsigned int>(stream);
  106. if ((*node)->mNumMeshes)
  107. {
  108. (*node)->mMeshes = new unsigned int[(*node)->mNumMeshes];
  109. for (unsigned int i = 0; i < (*node)->mNumMeshes; ++i) {
  110. (*node)->mMeshes[i] = Read<unsigned int>(stream);
  111. }
  112. }
  113. if ((*node)->mNumChildren)
  114. {
  115. (*node)->mChildren = new aiNode*[(*node)->mNumChildren];
  116. for (unsigned int i = 0; i < (*node)->mNumChildren; ++i) {
  117. ReadBinaryNode( stream, &(*node)->mChildren[i] );
  118. }
  119. }
  120. }
  121. void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene )
  122. {
  123. ai_assert( Read<uint32_t>(stream) == ASSBIN_CHUNK_AISCENE);
  124. uint32_t size = Read<uint32_t>(stream);
  125. scene->mFlags = Read<unsigned int>(stream);
  126. scene->mNumMeshes = Read<unsigned int>(stream);
  127. scene->mNumMaterials = Read<unsigned int>(stream);
  128. scene->mNumAnimations = Read<unsigned int>(stream);
  129. scene->mNumTextures = Read<unsigned int>(stream);
  130. scene->mNumLights = Read<unsigned int>(stream);
  131. scene->mNumCameras = Read<unsigned int>(stream);
  132. // Read node graph
  133. scene->mRootNode = new aiNode[1];
  134. ReadBinaryNode( stream, &scene->mRootNode );
  135. /*
  136. // Read all meshes
  137. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  138. const aiMesh* mesh = scene->mMeshes[i];
  139. ReadBinaryMesh( stream,mesh);
  140. }
  141. // Read materials
  142. for (unsigned int i = 0; i< scene->mNumMaterials; ++i) {
  143. const aiMaterial* mat = scene->mMaterials[i];
  144. ReadBinaryMaterial(stream,mat);
  145. }
  146. // Read all animations
  147. for (unsigned int i = 0; i < scene->mNumAnimations;++i) {
  148. const aiAnimation* anim = scene->mAnimations[i];
  149. ReadBinaryAnim(stream,anim);
  150. }
  151. // Read all textures
  152. for (unsigned int i = 0; i < scene->mNumTextures;++i) {
  153. const aiTexture* mesh = scene->mTextures[i];
  154. ReadBinaryTexture(stream,mesh);
  155. }
  156. // Read lights
  157. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  158. const aiLight* l = scene->mLights[i];
  159. ReadBinaryLight(stream,l);
  160. }
  161. // Read cameras
  162. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  163. const aiCamera* cam = scene->mCameras[i];
  164. ReadBinaryCamera(stream,cam);
  165. }
  166. */
  167. }
  168. void AssbinImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler )
  169. {
  170. IOStream * stream = pIOHandler->Open(pFile,"rb");
  171. if (!stream)
  172. return;
  173. stream->Seek( 44, aiOrigin_CUR ); // signature
  174. unsigned int versionMajor = Read<unsigned int>(stream);
  175. unsigned int versionMinor = Read<unsigned int>(stream);
  176. unsigned int versionRevision = Read<unsigned int>(stream);
  177. unsigned int compileFlags = Read<unsigned int>(stream);
  178. shortened = Read<uint16_t>(stream) > 0;
  179. compressed = Read<uint16_t>(stream) > 0;
  180. stream->Seek( 256, aiOrigin_CUR ); // original filename
  181. stream->Seek( 128, aiOrigin_CUR ); // options
  182. stream->Seek( 64, aiOrigin_CUR ); // padding
  183. if (compressed)
  184. {
  185. // TODO
  186. }
  187. else
  188. {
  189. ReadBinaryScene(stream,pScene);
  190. }
  191. pIOHandler->Close(stream);
  192. }
  193. #endif // !! ASSIMP_BUILD_NO_ASSBIN_IMPORTER