BlenderLoader.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file BlenderLoader.cpp
  34. * @brief Implementation of the Blender3D importer class.
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
  38. #include "BlenderLoader.h"
  39. #include "BlenderDNA.h"
  40. #include "BlenderScene.h"
  41. #include "BlenderSceneGen.h"
  42. #include "StreamReader.h"
  43. #include "TinyFormatter.h"
  44. //#include <boost/make_shared.hpp>
  45. using namespace Assimp;
  46. using namespace Assimp::Blender;
  47. using namespace Assimp::Formatter;
  48. #define for_each(x,y) BOOST_FOREACH(x,y)
  49. static const aiLoaderDesc blenderDesc = {
  50. "Blender 3D Importer \nhttp://www.blender3d.org",
  51. "Alexander Gessler <[email protected]>",
  52. "",
  53. "",
  54. aiLoaderFlags_SupportBinaryFlavour | aiLoaderFlags_Experimental,
  55. 0,
  56. 0,
  57. 2,
  58. 50
  59. };
  60. namespace Assimp {
  61. namespace Blender {
  62. /** Mini smart-array to avoid pulling in even more boost stuff. usable with vector and deque */
  63. template <template <typename,typename> class TCLASS, typename T>
  64. struct TempArray {
  65. TempArray() {
  66. }
  67. ~TempArray () {
  68. for_each(T* elem, arr) {
  69. delete elem;
  70. }
  71. }
  72. void dismiss() {
  73. arr.clear();
  74. }
  75. TCLASS< T*,std::allocator<T*> >* operator -> () {
  76. return &arr;
  77. }
  78. operator TCLASS< T*,std::allocator<T*> > () {
  79. return arr;
  80. }
  81. TCLASS< T*,std::allocator<T*> >& get () {
  82. return arr;
  83. }
  84. const TCLASS< T*,std::allocator<T*> >& get () const {
  85. return arr;
  86. }
  87. T* operator[] (size_t idx) const {
  88. return arr[idx];
  89. }
  90. private:
  91. // no copy semantics
  92. void operator= (const TempArray&) {
  93. }
  94. TempArray(const TempArray& arr) {
  95. }
  96. private:
  97. TCLASS< T*,std::allocator<T*> > arr;
  98. };
  99. /** ConversionData acts as intermediate storage location for
  100. * the various ConvertXXX routines in BlenderImporter.*/
  101. struct ConversionData {
  102. std::set<const Object*> objects;
  103. TempArray <std::vector, aiMesh> meshes;
  104. TempArray <std::vector, aiCamera> cameras;
  105. TempArray <std::vector, aiLight> lights;
  106. TempArray <std::vector, aiMaterial> materials;
  107. // set of all materials referenced by at least one mesh in the scene
  108. std::deque< boost::shared_ptr< Material > > materials_raw;
  109. };
  110. }
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. // Constructor to be privately used by Importer
  114. BlenderImporter::BlenderImporter()
  115. {}
  116. // ------------------------------------------------------------------------------------------------
  117. // Destructor, private as well
  118. BlenderImporter::~BlenderImporter()
  119. {}
  120. // ------------------------------------------------------------------------------------------------
  121. // Returns whether the class can handle the format of the given file.
  122. bool BlenderImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  123. {
  124. const std::string& extension = GetExtension(pFile);
  125. if (extension == "blend") {
  126. return true;
  127. }
  128. else if ((!extension.length() || checkSig) && pIOHandler) {
  129. const char* tokens[] = {"BLENDER"};
  130. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  131. }
  132. return false;
  133. }
  134. // ------------------------------------------------------------------------------------------------
  135. // List all extensions handled by this loader
  136. void BlenderImporter::GetExtensionList(std::set<std::string>& app)
  137. {
  138. app.insert("blend");
  139. }
  140. // ------------------------------------------------------------------------------------------------
  141. // Loader registry entry
  142. const aiLoaderDesc& BlenderImporter::GetInfo () const
  143. {
  144. return blenderDesc;
  145. }
  146. // ------------------------------------------------------------------------------------------------
  147. // Setup configuration properties for the loader
  148. void BlenderImporter::SetupProperties(const Importer* pImp)
  149. {
  150. // nothing to be done for the moment
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. // Imports the given file into the given scene structure.
  154. void BlenderImporter::InternReadFile( const std::string& pFile,
  155. aiScene* pScene, IOSystem* pIOHandler)
  156. {
  157. FileDatabase file;
  158. boost::shared_ptr<IOStream> stream(pIOHandler->Open(pFile,"rb"));
  159. if (!stream) {
  160. ThrowException("Could not open file for reading");
  161. }
  162. char magic[8] = {0};
  163. stream->Read(magic,7,1);
  164. if (strcmp(magic,"BLENDER")) {
  165. ThrowException("BLENDER magic bytes are missing");
  166. }
  167. file.i64bit = (stream->Read(magic,1,1),magic[0]=='-');
  168. file.little = (stream->Read(magic,1,1),magic[0]=='v');
  169. stream->Read(magic,3,1);
  170. magic[3] = '\0';
  171. LogInfo((format(),"Blender version is ",magic[0],".",magic+1,
  172. " (64bit: ",file.i64bit?"true":"false",
  173. ", little endian: ",file.little?"true":"false",")"
  174. ));
  175. ParseBlendFile(file,stream);
  176. Scene scene;
  177. ExtractScene(scene,file);
  178. ConvertBlendFile(pScene,scene);
  179. }
  180. // ------------------------------------------------------------------------------------------------
  181. void BlenderImporter::ParseBlendFile(FileDatabase& out, boost::shared_ptr<IOStream> stream)
  182. {
  183. out.reader = boost::shared_ptr<StreamReaderAny>(new StreamReaderAny(stream,out.little));
  184. DNAParser dna_reader(out);
  185. const DNA* dna = NULL;
  186. out.entries.reserve(128); { // even small BLEND files tend to consist of many file blocks
  187. SectionParser parser(*out.reader.get(),out.i64bit);
  188. // first parse the file in search for the DNA and insert all other sections into the database
  189. while ((parser.Next(),1)) {
  190. const FileBlockHead& head = parser.GetCurrent();
  191. if (head.id == "ENDB") {
  192. break; // only valid end of the file
  193. }
  194. else if (head.id == "DNA1") {
  195. dna_reader.Parse();
  196. dna = &dna_reader.GetDNA();
  197. continue;
  198. }
  199. out.entries.push_back(head);
  200. }
  201. }
  202. if (!dna) {
  203. ThrowException("SDNA not found");
  204. }
  205. std::sort(out.entries.begin(),out.entries.end());
  206. }
  207. // ------------------------------------------------------------------------------------------------
  208. void BlenderImporter::ExtractScene(Scene& out, const FileDatabase& file)
  209. {
  210. const FileBlockHead* block = NULL;
  211. std::map<std::string,size_t>::const_iterator it = file.dna.indices.find("Scene");
  212. if (it == file.dna.indices.end()) {
  213. ThrowException("There is no `Scene` structure record");
  214. }
  215. const Structure& ss = file.dna.structures[(*it).second];
  216. // we need a scene somewhere to start with.
  217. for_each(const FileBlockHead& bl,file.entries) {
  218. if (bl.id == "SC") {
  219. block = &bl;
  220. break;
  221. }
  222. }
  223. if (!block) {
  224. ThrowException("There is not a single `Scene` record to load");
  225. }
  226. file.reader->SetCurrentPos(block->start);
  227. ss.Convert(out,file);
  228. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  229. DefaultLogger::get()->info((format(),
  230. "(Stats) Fields read: " ,file.stats().fields_read,
  231. ", pointers resolved: " ,file.stats().pointers_resolved,
  232. ", cache hits: " ,file.stats().cache_hits,
  233. ", cached objects: " ,file.stats().cached_objects
  234. ));
  235. #endif
  236. }
  237. // ------------------------------------------------------------------------------------------------
  238. void BlenderImporter::ConvertBlendFile(aiScene* out, const Scene& in)
  239. {
  240. ConversionData conv;
  241. // FIXME it must be possible to take the hierarchy directly from
  242. // the file. This is terrible. Here, we're first looking for
  243. // all objects which don't have parent objects at all -
  244. std::deque<const Object*> no_parents;
  245. for (boost::shared_ptr<Base> cur = boost::static_pointer_cast<Base> ( in.base.first ); cur; cur = cur->next) {
  246. if (cur->object) {
  247. if(!cur->object->parent) {
  248. no_parents.push_back(cur->object.get());
  249. }
  250. else conv.objects.insert(cur->object.get());
  251. }
  252. }
  253. for (boost::shared_ptr<Base> cur = in.basact; cur; cur = cur->next) {
  254. if (cur->object) {
  255. if(cur->object->parent) {
  256. conv.objects.insert(cur->object.get());
  257. }
  258. }
  259. }
  260. if (no_parents.empty()) {
  261. ThrowException("Expected at least one object with no parent");
  262. }
  263. aiNode* root = out->mRootNode = new aiNode("<BlenderRoot>");
  264. root->mNumChildren = static_cast<unsigned int>(no_parents.size());
  265. root->mChildren = new aiNode*[root->mNumChildren]();
  266. for (unsigned int i = 0; i < root->mNumChildren; ++i) {
  267. root->mChildren[i] = ConvertNode(in, no_parents[i], conv);
  268. root->mChildren[i]->mParent = root;
  269. }
  270. BuildMaterials(conv);
  271. if (conv.meshes->size()) {
  272. out->mMeshes = new aiMesh*[out->mNumMeshes = static_cast<unsigned int>( conv.meshes->size() )];
  273. std::copy(conv.meshes->begin(),conv.meshes->end(),out->mMeshes);
  274. conv.meshes.dismiss();
  275. }
  276. if (conv.lights->size()) {
  277. out->mLights = new aiLight*[out->mNumLights = static_cast<unsigned int>( conv.lights->size() )];
  278. std::copy(conv.lights->begin(),conv.lights->end(),out->mLights);
  279. conv.lights.dismiss();
  280. }
  281. if (conv.cameras->size()) {
  282. out->mCameras = new aiCamera*[out->mNumCameras = static_cast<unsigned int>( conv.cameras->size() )];
  283. std::copy(conv.cameras->begin(),conv.cameras->end(),out->mCameras);
  284. conv.cameras.dismiss();
  285. }
  286. if (conv.materials->size()) {
  287. out->mMaterials = new aiMaterial*[out->mNumMaterials = static_cast<unsigned int>( conv.materials->size() )];
  288. std::copy(conv.materials->begin(),conv.materials->end(),out->mMaterials);
  289. conv.materials.dismiss();
  290. }
  291. // acknowledge that the scene might come out incomplete
  292. // by Assimps definition of `complete`: blender scenes
  293. // can consist of thousands of cameras or lights with
  294. // not a single mesh between them.
  295. if (!out->mNumMeshes) {
  296. out->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
  297. }
  298. }
  299. // ------------------------------------------------------------------------------------------------
  300. void BlenderImporter::BuildMaterials(ConversionData& conv_data)
  301. {
  302. conv_data.materials->reserve(conv_data.materials_raw.size());
  303. // add a default material if necessary
  304. unsigned int index = static_cast<unsigned int>( -1 );
  305. for_each( aiMesh* mesh, conv_data.meshes.get() ) {
  306. if (mesh->mMaterialIndex == static_cast<unsigned int>( -1 )) {
  307. if (index == static_cast<unsigned int>( -1 )) {
  308. // ok, we need to add a dedicated default material for some poor material-less meshes
  309. boost::shared_ptr<Material> p(new Material());
  310. strcpy( p->id.name, "$AssimpDefault" );
  311. p->r = p->g = p->b = 0.6f;
  312. p->specr = p->specg = p->specb = 0.6f;
  313. p->ambir = p->ambig = p->ambib = 0.0f;
  314. p->emit = 0.f;
  315. p->alpha = 0.f;
  316. // XXX add more / or add default c'tor to Material
  317. index = static_cast<unsigned int>( conv_data.materials_raw.size() );
  318. conv_data.materials_raw.push_back(p);
  319. LogInfo("Adding default material ...");
  320. }
  321. mesh->mMaterialIndex = index;
  322. }
  323. }
  324. for_each(boost::shared_ptr<Material> mat, conv_data.materials_raw) {
  325. MaterialHelper* mout = new MaterialHelper();
  326. conv_data.materials->push_back(mout);
  327. // set material name
  328. aiString name = aiString(mat->id.name);
  329. mout->AddProperty(&name,AI_MATKEY_NAME);
  330. // basic material colors
  331. aiColor3D col(mat->r,mat->g,mat->b);
  332. mout->AddProperty(&col,1,AI_MATKEY_COLOR_DIFFUSE);
  333. col = aiColor3D(mat->specr,mat->specg,mat->specb);
  334. mout->AddProperty(&col,1,AI_MATKEY_COLOR_SPECULAR);
  335. col = aiColor3D(mat->ambir,mat->ambig,mat->ambib);
  336. mout->AddProperty(&col,1,AI_MATKEY_COLOR_AMBIENT);
  337. }
  338. }
  339. // ------------------------------------------------------------------------------------------------
  340. void BlenderImporter::CheckActualType(const ElemBase* dt, const char* check)
  341. {
  342. ai_assert(dt);
  343. if (strcmp(dt->dna_type,check)) {
  344. ThrowException((format(),
  345. "Expected object at ",std::hex,dt," to be of type `",check,
  346. "`, but it claims to be a `",dt->dna_type,"`instead"
  347. ));
  348. }
  349. }
  350. // ------------------------------------------------------------------------------------------------
  351. void BlenderImporter::NotSupportedObjectType(const Object* obj, const char* type)
  352. {
  353. LogWarn((format(), "Object `",obj->id.name,"` - type is unsupported: `",type, "`, skipping" ));
  354. }
  355. // ------------------------------------------------------------------------------------------------
  356. void BlenderImporter::ConvertMesh(const Scene& in, const Object* obj, const Mesh* mesh,
  357. ConversionData& conv_data, TempArray<std::vector,aiMesh>& temp
  358. )
  359. {
  360. typedef std::pair<const int,size_t> MyPair;
  361. if (!mesh->totface || !mesh->totvert) {
  362. return;
  363. }
  364. // some sanity checks
  365. if (mesh->totface > mesh->mface.size() ){
  366. ThrowException("Number of faces is larger than the corresponding array");
  367. }
  368. if (mesh->totvert > mesh->mvert.size()) {
  369. ThrowException("Number of vertices is larger than the corresponding array");
  370. }
  371. // collect per-submesh numbers
  372. std::map<int,size_t> per_mat;
  373. for (int i = 0; i < mesh->totface; ++i) {
  374. const MFace& mf = mesh->mface[i];
  375. per_mat[ mf.mat_nr ]++;
  376. }
  377. // ... and allocate the corresponding meshes
  378. const size_t old = temp->size();
  379. temp->reserve(temp->size() + per_mat.size());
  380. std::map<size_t,size_t> mat_num_to_mesh_idx;
  381. for_each(MyPair& it, per_mat) {
  382. mat_num_to_mesh_idx[it.first] = temp->size();
  383. temp->push_back(new aiMesh());
  384. aiMesh* out = temp->back();
  385. out->mVertices = new aiVector3D[it.second*4];
  386. out->mNormals = new aiVector3D[it.second*4];
  387. //out->mNumFaces = 0
  388. //out->mNumVertices = 0
  389. out->mFaces = new aiFace[it.second]();
  390. // all submeshes created from this mesh are named equally. this allows
  391. // curious users to recover the original adjacency.
  392. out->mName = aiString(mesh->id.name);
  393. // resolve the material reference and add this material to the set of
  394. // output materials. The (temporary) material index is the index
  395. // of the material entry within the list of resolved materials.
  396. if (mesh->mat) {
  397. if (it.first >= mesh->mat.size() ) {
  398. ThrowException("Material index is out of range");
  399. }
  400. boost::shared_ptr<Material> mat = mesh->mat[it.first];
  401. const std::deque< boost::shared_ptr<Material> >::iterator has = std::find(
  402. conv_data.materials_raw.begin(),
  403. conv_data.materials_raw.end(),mat
  404. );
  405. if (has != conv_data.materials_raw.end()) {
  406. out->mMaterialIndex = static_cast<unsigned int>( std::distance(conv_data.materials_raw.begin(),has));
  407. }
  408. else {
  409. out->mMaterialIndex = static_cast<unsigned int>( conv_data.materials_raw.size() );
  410. conv_data.materials_raw.push_back(mat);
  411. }
  412. }
  413. else out->mMaterialIndex = static_cast<unsigned int>( -1 );
  414. }
  415. for (int i = 0; i < mesh->totface; ++i) {
  416. const MFace& mf = mesh->mface[i];
  417. aiMesh* const out = temp[ mat_num_to_mesh_idx[ mf.mat_nr ] ];
  418. aiFace& f = out->mFaces[out->mNumFaces++];
  419. f.mIndices = new unsigned int[ f.mNumIndices = mf.v4?4:3 ];
  420. aiVector3D* vo = out->mVertices + out->mNumVertices;
  421. aiVector3D* vn = out->mNormals + out->mNumVertices;
  422. // XXX we can't fold this easily, because we are restricted
  423. // to the member names from the BLEND file (v1,v2,v3,v4) ..
  424. if (mf.v1 >= mesh->totvert) {
  425. ThrowException("Vertex index v1 out of range");
  426. }
  427. const MVert* v = &mesh->mvert[mf.v1];
  428. vo->x = v->co[0];
  429. vo->y = v->co[1];
  430. vo->z = v->co[2];
  431. vn->x = v->no[0];
  432. vn->y = v->no[1];
  433. vn->z = v->no[2];
  434. f.mIndices[0] = out->mNumVertices++;
  435. ++vo;
  436. ++vn;
  437. // if (f.mNumIndices >= 2) {
  438. if (mf.v2 >= mesh->totvert) {
  439. ThrowException("Vertex index v2 out of range");
  440. }
  441. v = &mesh->mvert[mf.v2];
  442. vo->x = v->co[0];
  443. vo->y = v->co[1];
  444. vo->z = v->co[2];
  445. vn->x = v->no[0];
  446. vn->y = v->no[1];
  447. vn->z = v->no[2];
  448. f.mIndices[1] = out->mNumVertices++;
  449. ++vo;
  450. ++vn;
  451. if (mf.v3 >= mesh->totvert) {
  452. ThrowException("Vertex index v3 out of range");
  453. }
  454. // if (f.mNumIndices >= 3) {
  455. v = &mesh->mvert[mf.v3];
  456. vo->x = v->co[0];
  457. vo->y = v->co[1];
  458. vo->z = v->co[2];
  459. vn->x = v->no[0];
  460. vn->y = v->no[1];
  461. vn->z = v->no[2];
  462. f.mIndices[2] = out->mNumVertices++;
  463. ++vo;
  464. ++vn;
  465. if (mf.v4 >= mesh->totvert) {
  466. ThrowException("Vertex index v4 out of range");
  467. }
  468. // if (f.mNumIndices >= 4) {
  469. if (mf.v4) {
  470. v = &mesh->mvert[mf.v4];
  471. vo->x = v->co[0];
  472. vo->y = v->co[1];
  473. vo->z = v->co[2];
  474. vn->x = v->no[0];
  475. vn->y = v->no[1];
  476. vn->z = v->no[2];
  477. f.mIndices[3] = out->mNumVertices++;
  478. ++vo;
  479. ++vn;
  480. out->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  481. }
  482. else out->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  483. // }
  484. // }
  485. // }
  486. }
  487. // collect texture coordinates, they're stored in a separate per-face buffer
  488. if (mesh->mtface) {
  489. if (mesh->totface > mesh->mtface.size()) {
  490. ThrowException("Number of UV faces is larger than the corresponding UV face array (#1)");
  491. }
  492. for (std::vector<aiMesh*>::iterator it = temp->begin()+old; it != temp->end(); ++it) {
  493. ai_assert((*it)->mNumVertices && (*it)->mNumFaces);
  494. (*it)->mTextureCoords[0] = new aiVector3D[(*it)->mNumVertices];
  495. (*it)->mNumFaces = (*it)->mNumVertices = 0;
  496. }
  497. for (int i = 0; i < mesh->totface; ++i) {
  498. const MTFace* v = &mesh->mtface[i];
  499. aiMesh* const out = temp[ mat_num_to_mesh_idx[ mesh->mface[i].mat_nr ] ];
  500. const aiFace& f = out->mFaces[out->mNumFaces++];
  501. aiVector3D* vo = &out->mTextureCoords[0][out->mNumVertices];
  502. for (unsigned int i = 0; i < f.mNumIndices; ++i,++vo,++out->mNumVertices) {
  503. vo->x = v->uv[i][0];
  504. vo->y = v->uv[i][1];
  505. }
  506. }
  507. }
  508. // collect texture coordinates, old-style (marked as deprecated in current blender sources)
  509. if (mesh->tface) {
  510. if (mesh->totface > mesh->mtface.size()) {
  511. ThrowException("Number of faces is larger than the corresponding UV face array (#2)");
  512. }
  513. for (std::vector<aiMesh*>::iterator it = temp->begin()+old; it != temp->end(); ++it) {
  514. ai_assert((*it)->mNumVertices && (*it)->mNumFaces);
  515. (*it)->mTextureCoords[0] = new aiVector3D[(*it)->mNumVertices];
  516. (*it)->mNumFaces = (*it)->mNumVertices = 0;
  517. }
  518. for (int i = 0; i < mesh->totface; ++i) {
  519. const TFace* v = &mesh->tface[i];
  520. aiMesh* const out = temp[ mat_num_to_mesh_idx[ mesh->mface[i].mat_nr ] ];
  521. const aiFace& f = out->mFaces[out->mNumFaces++];
  522. aiVector3D* vo = &out->mTextureCoords[0][out->mNumVertices];
  523. for (unsigned int i = 0; i < f.mNumIndices; ++i,++vo,++out->mNumVertices) {
  524. vo->x = v->uv[i][0];
  525. vo->y = v->uv[i][1];
  526. }
  527. }
  528. }
  529. // collect vertex colors, stored separately as well
  530. if (mesh->mcol) {
  531. if (mesh->totface > (mesh->mcol.size()/4)) {
  532. ThrowException("Number of faces is larger than the corresponding color face array");
  533. }
  534. for (std::vector<aiMesh*>::iterator it = temp->begin()+old; it != temp->end(); ++it) {
  535. ai_assert((*it)->mNumVertices && (*it)->mNumFaces);
  536. (*it)->mColors[0] = new aiColor4D[(*it)->mNumVertices];
  537. (*it)->mNumFaces = (*it)->mNumVertices = 0;
  538. }
  539. for (int i = 0; i < mesh->totface; ++i) {
  540. aiMesh* const out = temp[ mat_num_to_mesh_idx[ mesh->mface[i].mat_nr ] ];
  541. const aiFace& f = out->mFaces[out->mNumFaces++];
  542. aiColor4D* vo = &out->mColors[0][out->mNumVertices];
  543. for (unsigned int n = 0; n < f.mNumIndices; ++n, ++vo,++out->mNumVertices) {
  544. const MCol* col = &mesh->mcol[(i<<2)+n];
  545. vo->r = col->r;
  546. vo->g = col->g;
  547. vo->b = col->b;
  548. vo->a = col->a;
  549. }
  550. for (unsigned int n = f.mNumIndices; n < 4; ++n);
  551. }
  552. }
  553. return;
  554. }
  555. // ------------------------------------------------------------------------------------------------
  556. aiCamera* BlenderImporter::ConvertCamera(const Scene& in, const Object* obj, const Camera* mesh, ConversionData& conv_data)
  557. {
  558. ScopeGuard<aiCamera> out(new aiCamera());
  559. return NULL ; //out.dismiss();
  560. }
  561. // ------------------------------------------------------------------------------------------------
  562. aiLight* BlenderImporter::ConvertLight(const Scene& in, const Object* obj, const Lamp* mesh, ConversionData& conv_data)
  563. {
  564. ScopeGuard<aiLight> out(new aiLight());
  565. return NULL ; //out.dismiss();
  566. }
  567. // ------------------------------------------------------------------------------------------------
  568. aiNode* BlenderImporter::ConvertNode(const Scene& in, const Object* obj, ConversionData& conv_data)
  569. {
  570. std::deque<const Object*> children;
  571. for(std::set<const Object*>::iterator it = conv_data.objects.begin(); it != conv_data.objects.end() ;) {
  572. const Object* object = *it;
  573. if (object->parent.get() == obj) {
  574. children.push_back(object);
  575. conv_data.objects.erase(it++);
  576. continue;
  577. }
  578. ++it;
  579. }
  580. ScopeGuard<aiNode> node(new aiNode(obj->id.name));
  581. if (obj->data) {
  582. switch (obj->type)
  583. {
  584. case Object :: Type_EMPTY:
  585. break; // do nothing
  586. // supported object types
  587. case Object :: Type_MESH: {
  588. const size_t old = conv_data.meshes->size();
  589. CheckActualType(obj->data.get(),"Mesh");
  590. ConvertMesh(in,obj,static_cast<const Mesh*>(obj->data.get()),conv_data,conv_data.meshes);
  591. if (conv_data.meshes->size() > old) {
  592. node->mMeshes = new unsigned int[node->mNumMeshes = static_cast<unsigned int>(conv_data.meshes->size()-old)];
  593. for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
  594. node->mMeshes[i] = i + old;
  595. }
  596. }}
  597. break;
  598. case Object :: Type_LAMP: {
  599. CheckActualType(obj->data.get(),"Lamp");
  600. aiLight* mesh = ConvertLight(in,obj,static_cast<const Lamp*>(
  601. obj->data.get()),conv_data);
  602. if (mesh) {
  603. conv_data.lights->push_back(mesh);
  604. }}
  605. break;
  606. case Object :: Type_CAMERA: {
  607. CheckActualType(obj->data.get(),"Camera");
  608. aiCamera* mesh = ConvertCamera(in,obj,static_cast<const Camera*>(
  609. obj->data.get()),conv_data);
  610. if (mesh) {
  611. conv_data.cameras->push_back(mesh);
  612. }}
  613. break;
  614. // unsupported object types / log, but do not break
  615. case Object :: Type_CURVE:
  616. NotSupportedObjectType(obj,"Curve");
  617. break;
  618. case Object :: Type_SURF:
  619. NotSupportedObjectType(obj,"Surface");
  620. break;
  621. case Object :: Type_FONT:
  622. NotSupportedObjectType(obj,"Font");
  623. break;
  624. case Object :: Type_MBALL:
  625. NotSupportedObjectType(obj,"MetaBall");
  626. break;
  627. case Object :: Type_WAVE:
  628. NotSupportedObjectType(obj,"Wave");
  629. break;
  630. case Object :: Type_LATTICE:
  631. NotSupportedObjectType(obj,"Lattice");
  632. break;
  633. // invalid or unknown type
  634. default:
  635. break;
  636. }
  637. }
  638. for(unsigned int x = 0; x < 4; ++x) {
  639. for(unsigned int y = 0; y < 4; ++y) {
  640. node->mTransformation[y][x] = obj->parentinv[x][y];
  641. }
  642. }
  643. aiMatrix4x4 m;
  644. for(unsigned int x = 0; x < 4; ++x) {
  645. for(unsigned int y = 0; y < 4; ++y) {
  646. m[y][x] = obj->obmat[x][y];
  647. }
  648. }
  649. node->mTransformation = m*node->mTransformation;
  650. if (children.size()) {
  651. node->mNumChildren = static_cast<unsigned int>(children.size());
  652. aiNode** nd = node->mChildren = new aiNode*[node->mNumChildren]();
  653. for_each (const Object* nobj,children) {
  654. *nd = ConvertNode(in,nobj,conv_data);
  655. (*nd++)->mParent = node;
  656. }
  657. }
  658. return node.dismiss();
  659. }
  660. // ------------------------------------------------------------------------------------------------
  661. /*static*/ void BlenderImporter::ThrowException(const std::string& msg)
  662. {
  663. throw DeadlyImportError("BLEND: "+msg);
  664. }
  665. // ------------------------------------------------------------------------------------------------
  666. /*static*/ void BlenderImporter::LogWarn(const Formatter::format& message) {
  667. DefaultLogger::get()->warn(std::string("BLEND: ")+=message);
  668. }
  669. // ------------------------------------------------------------------------------------------------
  670. /*static*/ void BlenderImporter::LogError(const Formatter::format& message) {
  671. DefaultLogger::get()->error(std::string("BLEND: ")+=message);
  672. }
  673. // ------------------------------------------------------------------------------------------------
  674. /*static*/ void BlenderImporter::LogInfo(const Formatter::format& message) {
  675. DefaultLogger::get()->info(std::string("BLEND: ")+=message);
  676. }
  677. // ------------------------------------------------------------------------------------------------
  678. /*static*/ void BlenderImporter::LogDebug(const Formatter::format& message) {
  679. DefaultLogger::get()->debug(std::string("BLEND: ")+=message);
  680. }
  681. #endif