B3DImporter.cpp 21 KB

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