No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

688 líneas
17 KiB

  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 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. #include "ConvertToLHProcess.h"
  43. using namespace Assimp;
  44. using namespace std;
  45. static const aiImporterDesc desc = {
  46. "BlitzBasic 3D Importer",
  47. "",
  48. "",
  49. "http://www.blitzbasic.com/",
  50. aiImporterFlags_SupportBinaryFlavour,
  51. 0,
  52. 0,
  53. 0,
  54. 0,
  55. "b3d"
  56. };
  57. // (fixme, Aramis) quick workaround to get rid of all those signed to unsigned warnings
  58. #ifdef _MSC_VER
  59. # pragma warning (disable: 4018)
  60. #endif
  61. //#define DEBUG_B3D
  62. // ------------------------------------------------------------------------------------------------
  63. bool B3DImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const{
  64. size_t pos=pFile.find_last_of( '.' );
  65. if( pos==string::npos ) return false;
  66. string ext=pFile.substr( pos+1 );
  67. if( ext.size()!=3 ) return false;
  68. return (ext[0]=='b' || ext[0]=='B') && (ext[1]=='3') && (ext[2]=='d' || ext[2]=='D');
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // Loader meta information
  72. const aiImporterDesc* B3DImporter::GetInfo () const
  73. {
  74. return &desc;
  75. }
  76. #ifdef DEBUG_B3D
  77. extern "C"{ void _stdcall AllocConsole(); }
  78. #endif
  79. // ------------------------------------------------------------------------------------------------
  80. void B3DImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler){
  81. #ifdef DEBUG_B3D
  82. AllocConsole();
  83. freopen( "conin$","r",stdin );
  84. freopen( "conout$","w",stdout );
  85. freopen( "conout$","w",stderr );
  86. cout<<"Hello world from the B3DImporter!"<<endl;
  87. #endif
  88. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  89. // Check whether we can read from the file
  90. if( file.get() == NULL)
  91. throw DeadlyImportError( "Failed to open B3D file " + pFile + ".");
  92. // check whether the .b3d file is large enough to contain
  93. // at least one chunk.
  94. size_t fileSize = file->FileSize();
  95. if( fileSize<8 ) throw DeadlyImportError( "B3D File is too small.");
  96. _pos=0;
  97. _buf.resize( fileSize );
  98. file->Read( &_buf[0],1,fileSize );
  99. _stack.clear();
  100. ReadBB3D( pScene );
  101. }
  102. // ------------------------------------------------------------------------------------------------
  103. void B3DImporter::Oops(){
  104. throw DeadlyImportError( "B3D Importer - INTERNAL ERROR" );
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. void B3DImporter::Fail( string str ){
  108. #ifdef DEBUG_B3D
  109. cout<<"Error in B3D file data: "<<str<<endl;
  110. #endif
  111. throw DeadlyImportError( "B3D Importer - error in B3D file data: "+str );
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. int B3DImporter::ReadByte(){
  115. if( _pos<_buf.size() ) return _buf[_pos++];
  116. Fail( "EOF" );
  117. return 0;
  118. }
  119. // ------------------------------------------------------------------------------------------------
  120. int B3DImporter::ReadInt(){
  121. if( _pos+4<=_buf.size() ){
  122. int n=*(int*)&_buf[_pos];
  123. _pos+=4;
  124. return n;
  125. }
  126. Fail( "EOF" );
  127. return 0;
  128. }
  129. // ------------------------------------------------------------------------------------------------
  130. float B3DImporter::ReadFloat(){
  131. if( _pos+4<=_buf.size() ){
  132. float n=*(float*)&_buf[_pos];
  133. _pos+=4;
  134. return n;
  135. }
  136. Fail( "EOF" );
  137. return 0.0f;
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. aiVector2D B3DImporter::ReadVec2(){
  141. float x=ReadFloat();
  142. float y=ReadFloat();
  143. return aiVector2D( x,y );
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. aiVector3D B3DImporter::ReadVec3(){
  147. float x=ReadFloat();
  148. float y=ReadFloat();
  149. float z=ReadFloat();
  150. return aiVector3D( x,y,z );
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. aiQuaternion B3DImporter::ReadQuat(){
  154. // (aramis_acg) Fix to adapt the loader to changed quat orientation
  155. float w=-ReadFloat();
  156. float x=ReadFloat();
  157. float y=ReadFloat();
  158. float z=ReadFloat();
  159. return aiQuaternion( w,x,y,z );
  160. }
  161. // ------------------------------------------------------------------------------------------------
  162. string B3DImporter::ReadString(){
  163. string str;
  164. while( _pos<_buf.size() ){
  165. char c=(char)ReadByte();
  166. if( !c ) return str;
  167. str+=c;
  168. }
  169. Fail( "EOF" );
  170. return string();
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. string B3DImporter::ReadChunk(){
  174. string tag;
  175. for( int i=0;i<4;++i ){
  176. tag+=char( ReadByte() );
  177. }
  178. #ifdef DEBUG_B3D
  179. // cout<<"ReadChunk:"<<tag<<endl;
  180. #endif
  181. unsigned sz=(unsigned)ReadInt();
  182. _stack.push_back( _pos+sz );
  183. return tag;
  184. }
  185. // ------------------------------------------------------------------------------------------------
  186. void B3DImporter::ExitChunk(){
  187. _pos=_stack.back();
  188. _stack.pop_back();
  189. }
  190. // ------------------------------------------------------------------------------------------------
  191. unsigned B3DImporter::ChunkSize(){
  192. return _stack.back()-_pos;
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. template<class T>
  196. T *B3DImporter::to_array( const vector<T> &v ){
  197. if( !v.size() ) return 0;
  198. T *p=new T[v.size()];
  199. for( size_t i=0;i<v.size();++i ){
  200. p[i]=v[i];
  201. }
  202. return p;
  203. }
  204. // ------------------------------------------------------------------------------------------------
  205. void B3DImporter::ReadTEXS(){
  206. while( ChunkSize() ){
  207. string name=ReadString();
  208. /*int flags=*/ReadInt();
  209. /*int blend=*/ReadInt();
  210. /*aiVector2D pos=*/ReadVec2();
  211. /*aiVector2D scale=*/ReadVec2();
  212. /*float rot=*/ReadFloat();
  213. _textures.push_back( name );
  214. }
  215. }
  216. // ------------------------------------------------------------------------------------------------
  217. void B3DImporter::ReadBRUS(){
  218. int n_texs=ReadInt();
  219. if( n_texs<0 || n_texs>8 ){
  220. Fail( "Bad texture count" );
  221. }
  222. while( ChunkSize() ){
  223. string name=ReadString();
  224. aiVector3D color=ReadVec3();
  225. float alpha=ReadFloat();
  226. float shiny=ReadFloat();
  227. /*int blend=**/ReadInt();
  228. int fx=ReadInt();
  229. aiMaterial *mat=new aiMaterial;
  230. _materials.push_back( mat );
  231. // Name
  232. aiString ainame( name );
  233. mat->AddProperty( &ainame,AI_MATKEY_NAME );
  234. // Diffuse color
  235. mat->AddProperty( &color,1,AI_MATKEY_COLOR_DIFFUSE );
  236. // Opacity
  237. mat->AddProperty( &alpha,1,AI_MATKEY_OPACITY );
  238. // Specular color
  239. aiColor3D speccolor( shiny,shiny,shiny );
  240. mat->AddProperty( &speccolor,1,AI_MATKEY_COLOR_SPECULAR );
  241. // Specular power
  242. float specpow=shiny*128;
  243. mat->AddProperty( &specpow,1,AI_MATKEY_SHININESS );
  244. // Double sided
  245. if( fx & 0x10 ){
  246. int i=1;
  247. mat->AddProperty( &i,1,AI_MATKEY_TWOSIDED );
  248. }
  249. //Textures
  250. for( int i=0;i<n_texs;++i ){
  251. int texid=ReadInt();
  252. if( texid<-1 || (texid>=0 && texid>=static_cast<int>(_textures.size())) ){
  253. Fail( "Bad texture id" );
  254. }
  255. if( i==0 && texid>=0 ){
  256. aiString texname( _textures[texid] );
  257. mat->AddProperty( &texname,AI_MATKEY_TEXTURE_DIFFUSE(0) );
  258. }
  259. }
  260. }
  261. }
  262. // ------------------------------------------------------------------------------------------------
  263. void B3DImporter::ReadVRTS(){
  264. _vflags=ReadInt();
  265. _tcsets=ReadInt();
  266. _tcsize=ReadInt();
  267. if( _tcsets<0 || _tcsets>4 || _tcsize<0 || _tcsize>4 ){
  268. Fail( "Bad texcoord data" );
  269. }
  270. int sz=12+(_vflags&1?12:0)+(_vflags&2?16:0)+(_tcsets*_tcsize*4);
  271. int n_verts=ChunkSize()/sz;
  272. int v0=_vertices.size();
  273. _vertices.resize( v0+n_verts );
  274. for( int i=0;i<n_verts;++i ){
  275. Vertex &v=_vertices[v0+i];
  276. memset( v.bones,0,sizeof(v.bones) );
  277. memset( v.weights,0,sizeof(v.weights) );
  278. v.vertex=ReadVec3();
  279. if( _vflags & 1 ) v.normal=ReadVec3();
  280. if( _vflags & 2 ) ReadQuat(); //skip v 4bytes...
  281. for( int i=0;i<_tcsets;++i ){
  282. float t[4]={0,0,0,0};
  283. for( int j=0;j<_tcsize;++j ){
  284. t[j]=ReadFloat();
  285. }
  286. t[1]=1-t[1];
  287. if( !i ) v.texcoords=aiVector3D( t[0],t[1],t[2] );
  288. }
  289. }
  290. }
  291. // ------------------------------------------------------------------------------------------------
  292. void B3DImporter::ReadTRIS( int v0 ){
  293. int matid=ReadInt();
  294. if( matid==-1 ){
  295. matid=0;
  296. }else if( matid<0 || matid>=(int)_materials.size() ){
  297. #ifdef DEBUG_B3D
  298. cout<<"material id="<<matid<<endl;
  299. #endif
  300. Fail( "Bad material id" );
  301. }
  302. aiMesh *mesh=new aiMesh;
  303. _meshes.push_back( mesh );
  304. mesh->mMaterialIndex=matid;
  305. mesh->mNumFaces=0;
  306. mesh->mPrimitiveTypes=aiPrimitiveType_TRIANGLE;
  307. int n_tris=ChunkSize()/12;
  308. aiFace *face=mesh->mFaces=new aiFace[n_tris];
  309. for( int i=0;i<n_tris;++i ){
  310. int i0=ReadInt()+v0;
  311. int i1=ReadInt()+v0;
  312. int i2=ReadInt()+v0;
  313. if( i0<0 || i0>=(int)_vertices.size() || i1<0 || i1>=(int)_vertices.size() || i2<0 || i2>=(int)_vertices.size() ){
  314. #ifdef DEBUG_B3D
  315. cout<<"Bad triangle index: i0="<<i0<<", i1="<<i1<<", i2="<<i2<<endl;
  316. #endif
  317. Fail( "Bad triangle index" );
  318. continue;
  319. }
  320. face->mNumIndices=3;
  321. face->mIndices=new unsigned[3];
  322. face->mIndices[0]=i0;
  323. face->mIndices[1]=i1;
  324. face->mIndices[2]=i2;
  325. ++mesh->mNumFaces;
  326. ++face;
  327. }
  328. }
  329. // ------------------------------------------------------------------------------------------------
  330. void B3DImporter::ReadMESH(){
  331. /*int matid=*/ReadInt();
  332. int v0=_vertices.size();
  333. while( ChunkSize() ){
  334. string t=ReadChunk();
  335. if( t=="VRTS" ){
  336. ReadVRTS();
  337. }else if( t=="TRIS" ){
  338. ReadTRIS( v0 );
  339. }
  340. ExitChunk();
  341. }
  342. }
  343. // ------------------------------------------------------------------------------------------------
  344. void B3DImporter::ReadBONE( int id ){
  345. while( ChunkSize() ){
  346. int vertex=ReadInt();
  347. float weight=ReadFloat();
  348. if( vertex<0 || vertex>=(int)_vertices.size() ){
  349. Fail( "Bad vertex index" );
  350. }
  351. Vertex &v=_vertices[vertex];
  352. int i;
  353. for( i=0;i<4;++i ){
  354. if( !v.weights[i] ){
  355. v.bones[i]=id;
  356. v.weights[i]=weight;
  357. break;
  358. }
  359. }
  360. #ifdef DEBUG_B3D
  361. if( i==4 ){
  362. cout<<"Too many bone weights"<<endl;
  363. }
  364. #endif
  365. }
  366. }
  367. // ------------------------------------------------------------------------------------------------
  368. void B3DImporter::ReadKEYS( aiNodeAnim *nodeAnim ){
  369. vector<aiVectorKey> trans,scale;
  370. vector<aiQuatKey> rot;
  371. int flags=ReadInt();
  372. while( ChunkSize() ){
  373. int frame=ReadInt();
  374. if( flags & 1 ){
  375. trans.push_back( aiVectorKey( frame,ReadVec3() ) );
  376. }
  377. if( flags & 2 ){
  378. scale.push_back( aiVectorKey( frame,ReadVec3() ) );
  379. }
  380. if( flags & 4 ){
  381. rot.push_back( aiQuatKey( frame,ReadQuat() ) );
  382. }
  383. }
  384. if( flags & 1 ){
  385. nodeAnim->mNumPositionKeys=trans.size();
  386. nodeAnim->mPositionKeys=to_array( trans );
  387. }
  388. if( flags & 2 ){
  389. nodeAnim->mNumScalingKeys=scale.size();
  390. nodeAnim->mScalingKeys=to_array( scale );
  391. }
  392. if( flags & 4 ){
  393. nodeAnim->mNumRotationKeys=rot.size();
  394. nodeAnim->mRotationKeys=to_array( rot );
  395. }
  396. }
  397. // ------------------------------------------------------------------------------------------------
  398. void B3DImporter::ReadANIM(){
  399. /*int flags=*/ReadInt();
  400. int frames=ReadInt();
  401. float fps=ReadFloat();
  402. aiAnimation *anim=new aiAnimation;
  403. _animations.push_back( anim );
  404. anim->mDuration=frames;
  405. anim->mTicksPerSecond=fps;
  406. }
  407. // ------------------------------------------------------------------------------------------------
  408. aiNode *B3DImporter::ReadNODE( aiNode *parent ){
  409. string name=ReadString();
  410. aiVector3D t=ReadVec3();
  411. aiVector3D s=ReadVec3();
  412. aiQuaternion r=ReadQuat();
  413. aiMatrix4x4 trans,scale,rot;
  414. aiMatrix4x4::Translation( t,trans );
  415. aiMatrix4x4::Scaling( s,scale );
  416. rot=aiMatrix4x4( r.GetMatrix() );
  417. aiMatrix4x4 tform=trans * rot * scale;
  418. int nodeid=_nodes.size();
  419. aiNode *node=new aiNode( name );
  420. _nodes.push_back( node );
  421. node->mParent=parent;
  422. node->mTransformation=tform;
  423. aiNodeAnim *nodeAnim=0;
  424. vector<unsigned> meshes;
  425. vector<aiNode*> children;
  426. while( ChunkSize() ){
  427. string t=ReadChunk();
  428. if( t=="MESH" ){
  429. int n=_meshes.size();
  430. ReadMESH();
  431. for( int i=n;i<(int)_meshes.size();++i ){
  432. meshes.push_back( i );
  433. }
  434. }else if( t=="BONE" ){
  435. ReadBONE( nodeid );
  436. }else if( t=="ANIM" ){
  437. ReadANIM();
  438. }else if( t=="KEYS" ){
  439. if( !nodeAnim ){
  440. nodeAnim=new aiNodeAnim;
  441. _nodeAnims.push_back( nodeAnim );
  442. nodeAnim->mNodeName=node->mName;
  443. }
  444. ReadKEYS( nodeAnim );
  445. }else if( t=="NODE" ){
  446. aiNode *child=ReadNODE( node );
  447. children.push_back( child );
  448. }
  449. ExitChunk();
  450. }
  451. node->mNumMeshes=meshes.size();
  452. node->mMeshes=to_array( meshes );
  453. node->mNumChildren=children.size();
  454. node->mChildren=to_array( children );
  455. return node;
  456. }
  457. // ------------------------------------------------------------------------------------------------
  458. void B3DImporter::ReadBB3D( aiScene *scene ){
  459. _textures.clear();
  460. _materials.size();
  461. _vertices.clear();
  462. _meshes.clear();
  463. _nodes.clear();
  464. _nodeAnims.clear();
  465. _animations.clear();
  466. string t=ReadChunk();
  467. if( t=="BB3D" ){
  468. int version=ReadInt();
  469. if (!DefaultLogger::isNullLogger()) {
  470. char dmp[128];
  471. sprintf(dmp,"B3D file format version: %i",version);
  472. DefaultLogger::get()->info(dmp);
  473. }
  474. while( ChunkSize() ){
  475. string t=ReadChunk();
  476. if( t=="TEXS" ){
  477. ReadTEXS();
  478. }else if( t=="BRUS" ){
  479. ReadBRUS();
  480. }else if( t=="NODE" ){
  481. ReadNODE( 0 );
  482. }
  483. ExitChunk();
  484. }
  485. }
  486. ExitChunk();
  487. if( !_nodes.size() ) Fail( "No nodes" );
  488. if( !_meshes.size() ) Fail( "No meshes" );
  489. //Fix nodes/meshes/bones
  490. for(size_t i=0;i<_nodes.size();++i ){
  491. aiNode *node=_nodes[i];
  492. for( size_t j=0;j<node->mNumMeshes;++j ){
  493. aiMesh *mesh=_meshes[node->mMeshes[j]];
  494. int n_tris=mesh->mNumFaces;
  495. int n_verts=mesh->mNumVertices=n_tris * 3;
  496. aiVector3D *mv=mesh->mVertices=new aiVector3D[ n_verts ],*mn=0,*mc=0;
  497. if( _vflags & 1 ) mn=mesh->mNormals=new aiVector3D[ n_verts ];
  498. if( _tcsets ) mc=mesh->mTextureCoords[0]=new aiVector3D[ n_verts ];
  499. aiFace *face=mesh->mFaces;
  500. vector< vector<aiVertexWeight> > vweights( _nodes.size() );
  501. for( int i=0;i<n_verts;i+=3 ){
  502. for( int j=0;j<3;++j ){
  503. Vertex &v=_vertices[face->mIndices[j]];
  504. *mv++=v.vertex;
  505. if( mn ) *mn++=v.normal;
  506. if( mc ) *mc++=v.texcoords;
  507. face->mIndices[j]=i+j;
  508. for( int k=0;k<4;++k ){
  509. if( !v.weights[k] ) break;
  510. int bone=v.bones[k];
  511. float weight=v.weights[k];
  512. vweights[bone].push_back( aiVertexWeight(i+j,weight) );
  513. }
  514. }
  515. ++face;
  516. }
  517. vector<aiBone*> bones;
  518. for(size_t i=0;i<vweights.size();++i ){
  519. vector<aiVertexWeight> &weights=vweights[i];
  520. if( !weights.size() ) continue;
  521. aiBone *bone=new aiBone;
  522. bones.push_back( bone );
  523. aiNode *bnode=_nodes[i];
  524. bone->mName=bnode->mName;
  525. bone->mNumWeights=weights.size();
  526. bone->mWeights=to_array( weights );
  527. aiMatrix4x4 mat=bnode->mTransformation;
  528. while( bnode->mParent ){
  529. bnode=bnode->mParent;
  530. mat=bnode->mTransformation * mat;
  531. }
  532. bone->mOffsetMatrix=mat.Inverse();
  533. }
  534. mesh->mNumBones=bones.size();
  535. mesh->mBones=to_array( bones );
  536. }
  537. }
  538. //nodes
  539. scene->mRootNode=_nodes[0];
  540. //material
  541. if( !_materials.size() ){
  542. _materials.push_back( new aiMaterial );
  543. }
  544. scene->mNumMaterials=_materials.size();
  545. scene->mMaterials=to_array( _materials );
  546. //meshes
  547. scene->mNumMeshes=_meshes.size();
  548. scene->mMeshes=to_array( _meshes );
  549. //animations
  550. if( _animations.size()==1 && _nodeAnims.size() ){
  551. aiAnimation *anim=_animations.back();
  552. anim->mNumChannels=_nodeAnims.size();
  553. anim->mChannels=to_array( _nodeAnims );
  554. scene->mNumAnimations=_animations.size();
  555. scene->mAnimations=to_array( _animations );
  556. }
  557. // convert to RH
  558. MakeLeftHandedProcess makeleft;
  559. makeleft.Execute( scene );
  560. FlipWindingOrderProcess flip;
  561. flip.Execute( scene );
  562. }
  563. #endif // !! ASSIMP_BUILD_NO_B3D_IMPORTER