B3DImporter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 B3DImporter.cpp
  35. * @brief Implementation of the b3d importer class
  36. */
  37. #include "AssimpPCH.h"
  38. #ifndef ASSIMP_BUILD_NO_B3D_IMPORTER
  39. // internal headers
  40. #include "B3DImporter.h"
  41. #include "TextureTransform.h"
  42. using namespace Assimp;
  43. using namespace std;
  44. // ------------------------------------------------------------------------------------------------
  45. bool B3DImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const{
  46. int pos=pFile.find_last_of( '.' );
  47. if( pos==string::npos ) return false;
  48. string ext=pFile.substr( pos+1 );
  49. if( ext.size()!=3 ) return false;
  50. return (ext[0]=='b' || ext[0]=='B') && (ext[1]=='3') && (ext[2]=='d' || ext[2]=='D');
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. void B3DImporter::GetExtensionList( std::string& append ){
  54. append.append("*.b3d");
  55. }
  56. // ------------------------------------------------------------------------------------------------
  57. void B3DImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler){
  58. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  59. // Check whether we can read from the file
  60. if( file.get() == NULL)
  61. throw new ImportErrorException( "Failed to open B3D file " + pFile + ".");
  62. // check whether the .b3d file is large enough to contain
  63. // at least one chunk.
  64. size_t fileSize = file->FileSize();
  65. if( fileSize<8 ) throw new ImportErrorException( "B3D File is too small.");
  66. _pos=0;
  67. _buf.resize( fileSize );
  68. file->Read( &_buf[0],1,fileSize );
  69. _stack.clear();
  70. _textures.clear();
  71. _materials.size();
  72. _vertices.clear();
  73. _meshes.clear();
  74. ReadBB3D();
  75. //materials
  76. if( _materials.size() ){
  77. aiMaterial **mats=new aiMaterial*[_materials.size()];
  78. for( unsigned i=0;i<_materials.size();++i ){
  79. mats[i]=_materials[i];
  80. }
  81. pScene->mNumMaterials=_materials.size();
  82. pScene->mMaterials=mats;
  83. }
  84. //meshes
  85. if( _meshes.size() ){
  86. aiMesh **meshes=new aiMesh*[_meshes.size()];
  87. for( unsigned i=0;i<_meshes.size();++i ){
  88. meshes[i]=_meshes[i];
  89. }
  90. pScene->mNumMeshes=_meshes.size();
  91. pScene->mMeshes=meshes;
  92. }else{
  93. throw new ImportErrorException( "B3D: No meshes loaded" );
  94. }
  95. //create root node
  96. aiNode *node=new aiNode( "root" );
  97. node->mTransformation=aiMatrix4x4(
  98. 1,0,0,0,
  99. 0,0,1,0,
  100. 0,1,0,0,
  101. 0,0,0,1 );
  102. node->mNumMeshes=_meshes.size();
  103. node->mMeshes=new unsigned[_meshes.size()];
  104. for( unsigned i=0;i<_meshes.size();++i ){
  105. node->mMeshes[i]=i;
  106. }
  107. pScene->mRootNode=node;
  108. }
  109. // ------------------------------------------------------------------------------------------------
  110. int B3DImporter::ReadByte(){
  111. if( _pos<_buf.size() ) return _buf[_pos++];
  112. throw new ImportErrorException( "B3D EOF Error" );
  113. }
  114. // ------------------------------------------------------------------------------------------------
  115. int B3DImporter::ReadInt(){
  116. if( _pos+4<=_buf.size() ){
  117. int n=*(int*)&_buf[_pos];
  118. _pos+=4;
  119. return n;
  120. }
  121. throw new ImportErrorException( "B3D EOF Error" );
  122. }
  123. // ------------------------------------------------------------------------------------------------
  124. float B3DImporter::ReadFloat(){
  125. if( _pos+4<=_buf.size() ){
  126. float n=*(float*)&_buf[_pos];
  127. _pos+=4;
  128. return n;
  129. }
  130. throw new ImportErrorException( "B3D EOF Error" );
  131. }
  132. // ------------------------------------------------------------------------------------------------
  133. B3DImporter::Vec2 B3DImporter::ReadVec2(){
  134. Vec2 t;
  135. t.x=ReadFloat();
  136. t.y=ReadFloat();
  137. return t;
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. B3DImporter::Vec3 B3DImporter::ReadVec3(){
  141. Vec3 t;
  142. t.x=ReadFloat();
  143. t.y=ReadFloat();
  144. t.z=ReadFloat();
  145. return t;
  146. }
  147. // ------------------------------------------------------------------------------------------------
  148. B3DImporter::Vec4 B3DImporter::ReadVec4(){
  149. Vec4 t;
  150. t.x=ReadFloat();
  151. t.y=ReadFloat();
  152. t.z=ReadFloat();
  153. t.w=ReadFloat();
  154. return t;
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. string B3DImporter::ReadString(){
  158. string str;
  159. while( _pos<_buf.size() ){
  160. char c=(char)ReadByte();
  161. if( !c ) return str;
  162. str+=c;
  163. }
  164. throw new ImportErrorException( "B3D EOF Error" );
  165. }
  166. // ------------------------------------------------------------------------------------------------
  167. string B3DImporter::ReadChunk(){
  168. string tag;
  169. for( int i=0;i<4;++i ){
  170. tag+=char( ReadByte() );
  171. }
  172. // cout<<"ReadChunk:"<<tag<<endl;
  173. unsigned sz=(unsigned)ReadInt();
  174. _stack.push_back( _pos+sz );
  175. return tag;
  176. }
  177. // ------------------------------------------------------------------------------------------------
  178. void B3DImporter::ExitChunk(){
  179. _pos=_stack.back();
  180. _stack.pop_back();
  181. }
  182. // ------------------------------------------------------------------------------------------------
  183. unsigned B3DImporter::ChunkSize(){
  184. return _stack.back()-_pos;
  185. }
  186. // ------------------------------------------------------------------------------------------------
  187. void B3DImporter::ReadTEXS(){
  188. while( ChunkSize() ){
  189. string name=ReadString();
  190. int flags=ReadInt();
  191. int blend=ReadInt();
  192. Vec2 pos=ReadVec2();
  193. Vec2 scale=ReadVec2();
  194. float rot=ReadFloat();
  195. Texture tex;
  196. tex.name=name;
  197. _textures.push_back( tex );
  198. }
  199. }
  200. // ------------------------------------------------------------------------------------------------
  201. void B3DImporter::ReadBRUS(){
  202. int n_texs=ReadInt();
  203. while( ChunkSize() ){
  204. string name=ReadString();
  205. Vec4 color=ReadVec4();
  206. float shiny=ReadFloat();
  207. int blend=ReadInt();
  208. int fx=ReadInt();
  209. MaterialHelper *mat=new MaterialHelper;
  210. _materials.push_back( mat );
  211. // Name
  212. aiString ainame( name );
  213. mat->AddProperty( &ainame,AI_MATKEY_NAME );
  214. // Diffuse color
  215. aiColor3D diffcolor( color.x,color.y,color.z );
  216. mat->AddProperty( &diffcolor,1,AI_MATKEY_COLOR_DIFFUSE );
  217. // Opacity
  218. mat->AddProperty( &color.w,1,AI_MATKEY_OPACITY );
  219. // Specular color
  220. aiColor3D speccolor( shiny,shiny,shiny );
  221. mat->AddProperty( &speccolor,1,AI_MATKEY_COLOR_SPECULAR );
  222. // Specular power
  223. float specpow=shiny*128;
  224. mat->AddProperty( &specpow,1,AI_MATKEY_SHININESS );
  225. // Double sided
  226. if( fx & 0x10 ){
  227. int i=1;
  228. mat->AddProperty( &i,1,AI_MATKEY_TWOSIDED );
  229. }
  230. //Textures
  231. for( int i=0;i<n_texs;++i ){
  232. int texid=ReadInt();
  233. if( !i && texid>=0 && texid<(int)_textures.size() ){
  234. //just use tex 0 for now
  235. const Texture &tex=_textures[texid];
  236. aiString texname( tex.name );
  237. mat->AddProperty( &texname,AI_MATKEY_TEXTURE_DIFFUSE(0) );
  238. }
  239. }
  240. }
  241. }
  242. // ------------------------------------------------------------------------------------------------
  243. void B3DImporter::ReadVRTS(){
  244. _vertFlags=ReadInt();
  245. _tcSets=ReadInt();
  246. _tcSize=ReadInt();
  247. if( _tcSets<0 || _tcSets>4 || _tcSize<0 || _tcSize>4 ) throw new ImportErrorException( "B3D Param Error" );
  248. while( ChunkSize() ){
  249. Vertex vert;
  250. vert.position=ReadVec3();
  251. if( _vertFlags & 1 ){
  252. vert.normal=ReadVec3();
  253. }
  254. if( _vertFlags & 2 ){
  255. Vec4 color=ReadVec4();
  256. }
  257. for( int i=0;i<_tcSets;++i ){
  258. float texcoords[4]={0,0,0,0};
  259. for( int j=0;j<_tcSize;++j ){
  260. texcoords[j]=ReadFloat();
  261. }
  262. texcoords[1]=1-texcoords[1];
  263. if( !i ) memcpy( &vert.texcoords.x,texcoords,12 );
  264. }
  265. _vertices.push_back( vert );
  266. }
  267. }
  268. // ------------------------------------------------------------------------------------------------
  269. void B3DImporter::ReadTRIS(){
  270. int matid=ReadInt();
  271. unsigned n_tris=ChunkSize()/12;
  272. unsigned n_verts=n_tris*3;
  273. aiMesh *mesh=new aiMesh;
  274. _meshes.push_back( mesh );
  275. mesh->mMaterialIndex=matid;
  276. mesh->mNumVertices=n_verts;
  277. mesh->mNumFaces=n_tris;
  278. mesh->mPrimitiveTypes=aiPrimitiveType_TRIANGLE;
  279. aiVector3D *mv,*mn=0,*mc=0;
  280. mv=mesh->mVertices=new aiVector3D[n_verts];
  281. if( _vertFlags & 1 ) mn=mesh->mNormals=new aiVector3D[n_verts];
  282. if( _tcSets ) mc=mesh->mTextureCoords[0]=new aiVector3D[n_verts];
  283. aiFace *face=mesh->mFaces=new aiFace[n_tris];
  284. for( unsigned i=0;i<n_verts;i+=3 ){
  285. face->mNumIndices=3;
  286. unsigned *ip=face->mIndices=new unsigned[3];
  287. int v[3];
  288. v[0]=ReadInt();
  289. v[2]=ReadInt();
  290. v[1]=ReadInt();
  291. for( unsigned j=0;j<3;++j ){
  292. int k=v[j];
  293. const Vertex &v=_vertices[k];
  294. memcpy( mv++,&v.position.x,12 );
  295. if( mn ) memcpy( mn++,&v.normal.x,12 );
  296. if( mc ) memcpy( mc++,&v.texcoords.x,12 );
  297. *ip++=i+j;
  298. }
  299. ++face;
  300. }
  301. }
  302. // ------------------------------------------------------------------------------------------------
  303. void B3DImporter::ReadMESH(){
  304. int matid=ReadInt();
  305. _vertices.clear();
  306. while( ChunkSize() ){
  307. string t=ReadChunk();
  308. if( t=="VRTS" ){
  309. ReadVRTS();
  310. }else if( t=="TRIS" ){
  311. ReadTRIS();
  312. }
  313. ExitChunk();
  314. }
  315. _vertices.clear();
  316. }
  317. // ------------------------------------------------------------------------------------------------
  318. void B3DImporter::ReadNODE(){
  319. string name=ReadString();
  320. Vec3 trans=ReadVec3();
  321. Vec3 scale=ReadVec3();
  322. Vec4 rot=ReadVec4();
  323. while( ChunkSize() ){
  324. string t=ReadChunk();
  325. if( t=="MESH" ){
  326. ReadMESH();
  327. }
  328. ExitChunk();
  329. }
  330. }
  331. // ------------------------------------------------------------------------------------------------
  332. void B3DImporter::ReadBB3D(){
  333. string t=ReadChunk();
  334. if( t=="BB3D" ){
  335. int version=ReadInt();
  336. while( ChunkSize() ){
  337. string t=ReadChunk();
  338. if( t=="TEXS" ){
  339. ReadTEXS();
  340. }else if( t=="BRUS" ){
  341. ReadBRUS();
  342. }else if( t=="NODE" ){
  343. ReadNODE();
  344. }
  345. ExitChunk();
  346. }
  347. }
  348. ExitChunk();
  349. }
  350. #endif // !! ASSIMP_BUILD_NO_B3D_IMPORTER