FBXConverter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp 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 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 FBXConverter.cpp
  34. * @brief Implementation of the FBX DOM -> aiScene converter
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include "FBXParser.h"
  39. #include "FBXConverter.h"
  40. #include "FBXDocument.h"
  41. #include "FBXUtil.h"
  42. #include "FBXProperties.h"
  43. #include "FBXImporter.h"
  44. namespace Assimp {
  45. namespace FBX {
  46. using namespace Util;
  47. // XXX vc9's debugger won't step into anonymous namespaces
  48. //namespace {
  49. /** Dummy class to encapsulate the conversion process */
  50. class Converter
  51. {
  52. public:
  53. Converter(aiScene* out, const Document& doc)
  54. : out(out)
  55. , doc(doc)
  56. {
  57. ConvertRootNode();
  58. if(doc.Settings().readAllMaterials) {
  59. // unfortunately this means we have to evaluate all objects
  60. BOOST_FOREACH(const ObjectMap::value_type& v,doc.Objects()) {
  61. const Object* ob = v.second->Get();
  62. if(!ob) {
  63. continue;
  64. }
  65. const Material* mat = dynamic_cast<const Material*>(ob);
  66. if(mat) {
  67. if (materials_converted.find(mat) == materials_converted.end()) {
  68. ConvertMaterial(*mat);
  69. }
  70. }
  71. }
  72. }
  73. // dummy root node
  74. out->mRootNode = new aiNode();
  75. out->mRootNode->mNumMeshes = static_cast<unsigned int>(meshes.size());
  76. out->mRootNode->mMeshes = new unsigned int[meshes.size()];
  77. for(unsigned int i = 0; i < out->mRootNode->mNumMeshes; ++i) {
  78. out->mRootNode->mMeshes[i] = i;
  79. }
  80. TransferDataToScene();
  81. }
  82. ~Converter()
  83. {
  84. std::for_each(meshes.begin(),meshes.end(),Util::delete_fun<aiMesh>());
  85. std::for_each(materials.begin(),materials.end(),Util::delete_fun<aiMaterial>());
  86. }
  87. private:
  88. // ------------------------------------------------------------------------------------------------
  89. // find scene root and trigger recursive scene conversion
  90. void ConvertRootNode()
  91. {
  92. out->mRootNode = new aiNode();
  93. out->mRootNode->mName.Set("Model::RootNode");
  94. // root has ID 0
  95. ConvertNodes(0L, *out->mRootNode);
  96. }
  97. // ------------------------------------------------------------------------------------------------
  98. // collect and assign child nodes
  99. void ConvertNodes(uint64_t id, aiNode& parent)
  100. {
  101. const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(id);
  102. std::vector<aiNode*> nodes;
  103. nodes.reserve(conns.size());
  104. BOOST_FOREACH(const Connection* con, conns) {
  105. // ignore object-property links
  106. if(con->PropertyName().length()) {
  107. continue;
  108. }
  109. const Object* const object = con->SourceObject();
  110. if(!object) {
  111. FBXImporter::LogWarn("failed to convert source object for node link");
  112. continue;
  113. }
  114. const Model* const model = dynamic_cast<const Model*>(object);
  115. if(model) {
  116. aiNode* nd = new aiNode();
  117. nd->mName.Set(model->Name());
  118. nd->mParent = &parent;
  119. // XXX handle transformation
  120. ConvertModel(*model, *nd);
  121. ConvertNodes(model->ID(), *nd);
  122. }
  123. }
  124. if(nodes.size()) {
  125. parent.mChildren = new aiNode*[nodes.size()]();
  126. parent.mNumChildren = static_cast<unsigned int>(nodes.size());
  127. std::swap_ranges(nodes.begin(),nodes.end(),parent.mChildren);
  128. }
  129. }
  130. // ------------------------------------------------------------------------------------------------
  131. void ConvertModel(const Model& model, aiNode& nd)
  132. {
  133. const std::vector<const Geometry*>& geos = model.GetGeometry();
  134. std::vector<unsigned int> meshes;
  135. meshes.reserve(geos.size());
  136. BOOST_FOREACH(const Geometry* geo, geos) {
  137. const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*>(geo);
  138. if(mesh) {
  139. const unsigned int index = ConvertMesh(*mesh, model);
  140. if(index == 0) {
  141. continue;
  142. }
  143. meshes.push_back(index-1);
  144. }
  145. else {
  146. FBXImporter::LogWarn("ignoring unrecognized geometry: " + geo->Name());
  147. }
  148. }
  149. if(meshes.size()) {
  150. nd.mMeshes = new unsigned int[meshes.size()]();
  151. nd.mNumMeshes = static_cast<unsigned int>(meshes.size());
  152. std::swap_ranges(meshes.begin(),meshes.end(),nd.mMeshes);
  153. }
  154. }
  155. // ------------------------------------------------------------------------------------------------
  156. // MeshGeometry -> aiMesh, return mesh index + 1 or 0 if the conversion failed
  157. unsigned int ConvertMesh(const MeshGeometry& mesh, const Model& model)
  158. {
  159. MeshMap::const_iterator it = meshes_converted.find(&mesh);
  160. if (it != meshes_converted.end()) {
  161. return (*it).second + 1;
  162. }
  163. const std::vector<aiVector3D>& vertices = mesh.GetVertices();
  164. const std::vector<unsigned int>& faces = mesh.GetFaceIndexCounts();
  165. if(vertices.empty() || faces.empty()) {
  166. FBXImporter::LogWarn("ignoring empty geometry: " + mesh.Name());
  167. return 0;
  168. }
  169. aiMesh* out_mesh = new aiMesh();
  170. meshes.push_back(out_mesh);
  171. meshes_converted[&mesh] = static_cast<unsigned int>(meshes.size()-1);
  172. // copy vertices
  173. out_mesh->mNumVertices = static_cast<size_t>(vertices.size());
  174. out_mesh->mVertices = new aiVector3D[vertices.size()];
  175. std::copy(vertices.begin(),vertices.end(),out_mesh->mVertices);
  176. // generate dummy faces
  177. out_mesh->mNumFaces = static_cast<size_t>(faces.size());
  178. aiFace* fac = out_mesh->mFaces = new aiFace[faces.size()]();
  179. unsigned int cursor = 0;
  180. BOOST_FOREACH(unsigned int pcount, faces) {
  181. aiFace& f = *fac++;
  182. f.mNumIndices = pcount;
  183. f.mIndices = new unsigned int[pcount];
  184. switch(pcount)
  185. {
  186. case 1:
  187. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  188. break;
  189. case 2:
  190. out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  191. break;
  192. case 3:
  193. out_mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  194. break;
  195. default:
  196. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  197. break;
  198. }
  199. for (unsigned int i = 0; i < pcount; ++i) {
  200. f.mIndices[i] = cursor++;
  201. }
  202. }
  203. // copy normals
  204. const std::vector<aiVector3D>& normals = mesh.GetNormals();
  205. if(normals.size()) {
  206. ai_assert(normals.size() == vertices.size());
  207. out_mesh->mNormals = new aiVector3D[vertices.size()];
  208. std::copy(normals.begin(),normals.end(),out_mesh->mNormals);
  209. }
  210. // copy tangents - assimp requires both tangents and bitangents (binormals)
  211. // to be present, or neither of them. Compute binormals from normals
  212. // and tangents if needed.
  213. const std::vector<aiVector3D>& tangents = mesh.GetTangents();
  214. const std::vector<aiVector3D>* binormals = &mesh.GetBinormals();
  215. if(tangents.size()) {
  216. std::vector<aiVector3D> tempBinormals;
  217. if (!binormals->size()) {
  218. if (normals.size()) {
  219. tempBinormals.resize(normals.size());
  220. for (unsigned int i = 0; i < tangents.size(); ++i) {
  221. tempBinormals[i] = normals[i] ^ tangents[i];
  222. }
  223. binormals = &tempBinormals;
  224. }
  225. else {
  226. binormals = NULL;
  227. }
  228. }
  229. if(binormals) {
  230. ai_assert(tangents.size() == vertices.size() && binormals->size() == vertices.size());
  231. out_mesh->mTangents = new aiVector3D[vertices.size()];
  232. std::copy(tangents.begin(),tangents.end(),out_mesh->mTangents);
  233. out_mesh->mBitangents = new aiVector3D[vertices.size()];
  234. std::copy(binormals->begin(),binormals->end(),out_mesh->mBitangents);
  235. }
  236. }
  237. // copy texture coords
  238. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  239. const std::vector<aiVector2D>& uvs = mesh.GetTextureCoords(i);
  240. if(uvs.empty()) {
  241. break;
  242. }
  243. aiVector3D* out_uv = out_mesh->mTextureCoords[i] = new aiVector3D[vertices.size()];
  244. BOOST_FOREACH(const aiVector2D& v, uvs) {
  245. *out_uv++ = aiVector3D(v.x,v.y,0.0f);
  246. }
  247. out_mesh->mNumUVComponents[i] = 2;
  248. }
  249. // copy vertex colors
  250. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
  251. const std::vector<aiColor4D>& colors = mesh.GetVertexColors(i);
  252. if(colors.empty()) {
  253. break;
  254. }
  255. out_mesh->mColors[i] = new aiColor4D[vertices.size()];
  256. std::copy(colors.begin(),colors.end(),out_mesh->mColors[i]);
  257. }
  258. const std::vector<unsigned int>& mindices = mesh.GetMaterialIndices();
  259. ConvertMaterialForMesh(out_mesh,model,mesh,mindices.size() ? mindices[0] : 0);
  260. return static_cast<unsigned int>(meshes.size());
  261. }
  262. // ------------------------------------------------------------------------------------------------
  263. void ConvertMaterialForMesh(aiMesh* out, const Model& model, const MeshGeometry& geo, unsigned int materialIndex)
  264. {
  265. // locate source materials for this mesh
  266. const std::vector<const Material*>& mats = model.GetMaterials();
  267. if (materialIndex >= mats.size()) {
  268. FBXImporter::LogError("material index out of bounds, ignoring");
  269. out->mMaterialIndex = GetDefaultMaterial();
  270. return;
  271. }
  272. const Material* const mat = mats[materialIndex];
  273. MaterialMap::const_iterator it = materials_converted.find(mat);
  274. if (it != materials_converted.end()) {
  275. out->mMaterialIndex = (*it).second;
  276. return;
  277. }
  278. out->mMaterialIndex = ConvertMaterial(*mat);
  279. materials_converted[mat] = out->mMaterialIndex;
  280. }
  281. // ------------------------------------------------------------------------------------------------
  282. unsigned int GetDefaultMaterial()
  283. {
  284. if (defaultMaterialIndex) {
  285. return defaultMaterialIndex - 1;
  286. }
  287. aiMaterial* out_mat = new aiMaterial();
  288. materials.push_back(out_mat);
  289. const aiColor3D diffuse = aiColor3D(0.8f,0.8f,0.8f);
  290. out_mat->AddProperty(&diffuse,1,AI_MATKEY_COLOR_DIFFUSE);
  291. aiString s;
  292. s.Set(AI_DEFAULT_MATERIAL_NAME);
  293. out_mat->AddProperty(&s,AI_MATKEY_NAME);
  294. defaultMaterialIndex = static_cast<unsigned int>(materials.size());
  295. return defaultMaterialIndex - 1;
  296. }
  297. // ------------------------------------------------------------------------------------------------
  298. // Material -> aiMaterial
  299. unsigned int ConvertMaterial(const Material& material)
  300. {
  301. const PropertyTable& props = material.Props();
  302. // generate empty output material
  303. aiMaterial* out_mat = new aiMaterial();
  304. materials_converted[&material] = static_cast<unsigned int>(materials.size());
  305. materials.push_back(out_mat);
  306. aiString str;
  307. // set material name
  308. str.Set(material.Name());
  309. out_mat->AddProperty(&str,AI_MATKEY_NAME);
  310. // shading stuff and colors
  311. SetShadingPropertiesCommon(out_mat,props);
  312. // texture assignments
  313. SetTextureProperties(out_mat,material.Textures());
  314. return static_cast<unsigned int>(materials.size() - 1);
  315. }
  316. // ------------------------------------------------------------------------------------------------
  317. void TrySetTextureProperties(aiMaterial* out_mat, const TextureMap& textures, const std::string& propName, aiTextureType target)
  318. {
  319. TextureMap::const_iterator it = textures.find(propName);
  320. if(it == textures.end()) {
  321. return;
  322. }
  323. const Texture* const tex = (*it).second;
  324. aiString path;
  325. path.Set(tex->RelativeFilename());
  326. out_mat->AddProperty(&path,_AI_MATKEY_TEXTURE_BASE,target,0);
  327. aiUVTransform uvTrafo;
  328. // XXX handle all kinds of UV transformations
  329. uvTrafo.mScaling = tex->UVScaling();
  330. uvTrafo.mTranslation = tex->UVTranslation();
  331. out_mat->AddProperty(&uvTrafo,1,_AI_MATKEY_UVTRANSFORM_BASE,target,0);
  332. const PropertyTable& props = tex->Props();
  333. int uvIndex = 0;
  334. bool ok;
  335. const std::string& uvSet = PropertyGet<std::string>(props,"UVSet",ok);
  336. if(ok) {
  337. // "default" is the name which usually appears in the FbxFileTexture template
  338. if(uvSet != "default" && uvSet.length()) {
  339. // this is a bit awkward - we need to find a mesh that uses this
  340. // material and scan its UV channels for the given UV name because
  341. // assimp references UV channels by index, not by name.
  342. // XXX: the case that UV channels may appear in different orders
  343. // in meshes is unhandled. A possible solution would be to sort
  344. // the UV channels alphabetically, but this would have the side
  345. // effect that the primary (first) UV channel would sometimes
  346. // be moved, causing trouble when users read only the first
  347. // UV channel and ignore UV channel assignments altogether.
  348. const unsigned int matIndex = std::distance(materials.begin(),
  349. std::find(materials.begin(),materials.end(),out_mat)
  350. );
  351. uvIndex = -1;
  352. BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) {
  353. const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*> (v.first);
  354. if(!mesh) {
  355. continue;
  356. }
  357. const std::vector<unsigned int>& mats = mesh->GetMaterialIndices();
  358. if(std::find(mats.begin(),mats.end(),matIndex) == mats.end()) {
  359. continue;
  360. }
  361. int index = -1;
  362. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  363. if(mesh->GetTextureCoords(i).empty()) {
  364. break;
  365. }
  366. const std::string& name = mesh->GetTextureCoordChannelName(i);
  367. if(name == uvSet) {
  368. index = static_cast<int>(i);
  369. break;
  370. }
  371. }
  372. if(index == -1) {
  373. FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
  374. continue;
  375. }
  376. if(uvIndex == -1) {
  377. uvIndex = index;
  378. }
  379. else {
  380. FBXImporter::LogWarn("the UV channel named " + uvSet +
  381. " appears at different positions in meshes, results will be wrong");
  382. }
  383. }
  384. if(uvIndex == -1) {
  385. FBXImporter::LogWarn("failed to resolve UV channel " + uvSet + ", using first UV channel");
  386. uvIndex = 0;
  387. }
  388. }
  389. }
  390. out_mat->AddProperty(&uvIndex,1,_AI_MATKEY_UVWSRC_BASE,target,0);
  391. }
  392. // ------------------------------------------------------------------------------------------------
  393. void SetTextureProperties(aiMaterial* out_mat, const TextureMap& textures)
  394. {
  395. TrySetTextureProperties(out_mat, textures, "DiffuseColor", aiTextureType_DIFFUSE);
  396. TrySetTextureProperties(out_mat, textures, "AmbientColor", aiTextureType_AMBIENT);
  397. TrySetTextureProperties(out_mat, textures, "EmissiveColor", aiTextureType_EMISSIVE);
  398. TrySetTextureProperties(out_mat, textures, "SpecularColor", aiTextureType_SPECULAR);
  399. TrySetTextureProperties(out_mat, textures, "TransparentColor", aiTextureType_OPACITY);
  400. TrySetTextureProperties(out_mat, textures, "ReflectionColor", aiTextureType_REFLECTION);
  401. TrySetTextureProperties(out_mat, textures, "DisplacementColor", aiTextureType_DISPLACEMENT);
  402. TrySetTextureProperties(out_mat, textures, "NormalMap", aiTextureType_NORMALS);
  403. TrySetTextureProperties(out_mat, textures, "Bump", aiTextureType_HEIGHT);
  404. }
  405. // ------------------------------------------------------------------------------------------------
  406. aiColor3D GetColorPropertyFromMaterial(const PropertyTable& props,const std::string& baseName, bool& result)
  407. {
  408. result = true;
  409. bool ok;
  410. const aiVector3D& Diffuse = PropertyGet<aiVector3D>(props,baseName,ok);
  411. if(ok) {
  412. return aiColor3D(Diffuse.x,Diffuse.y,Diffuse.z);
  413. }
  414. else {
  415. aiVector3D DiffuseColor = PropertyGet<aiVector3D>(props,baseName + "Color",ok);
  416. if(ok) {
  417. float DiffuseFactor = PropertyGet<float>(props,baseName + "Factor",ok);
  418. if(ok) {
  419. DiffuseColor *= DiffuseFactor;
  420. }
  421. return aiColor3D(DiffuseColor.x,DiffuseColor.y,DiffuseColor.z);
  422. }
  423. }
  424. result = false;
  425. return aiColor3D(0.0f,0.0f,0.0f);
  426. }
  427. // ------------------------------------------------------------------------------------------------
  428. void SetShadingPropertiesCommon(aiMaterial* out_mat, const PropertyTable& props)
  429. {
  430. // set shading properties. There are various, redundant ways in which FBX materials
  431. // specify their shading settings (depending on shading models, prop
  432. // template etc.). No idea which one is right in a particular context.
  433. // Just try to make sense of it - there's no spec to verify this against,
  434. // so why should we.
  435. bool ok;
  436. const aiColor3D& Diffuse = GetColorPropertyFromMaterial(props,"Diffuse",ok);
  437. if(ok) {
  438. out_mat->AddProperty(&Diffuse,1,AI_MATKEY_COLOR_DIFFUSE);
  439. }
  440. const aiColor3D& Emissive = GetColorPropertyFromMaterial(props,"Emissive",ok);
  441. if(ok) {
  442. out_mat->AddProperty(&Emissive,1,AI_MATKEY_COLOR_EMISSIVE);
  443. }
  444. const aiColor3D& Ambient = GetColorPropertyFromMaterial(props,"Ambient",ok);
  445. if(ok) {
  446. out_mat->AddProperty(&Ambient,1,AI_MATKEY_COLOR_AMBIENT);
  447. }
  448. const aiColor3D& Specular = GetColorPropertyFromMaterial(props,"Specular",ok);
  449. if(ok) {
  450. out_mat->AddProperty(&Specular,1,AI_MATKEY_COLOR_SPECULAR);
  451. }
  452. const float Opacity = PropertyGet<float>(props,"Opacity",ok);
  453. if(ok) {
  454. out_mat->AddProperty(&Opacity,1,AI_MATKEY_OPACITY);
  455. }
  456. const float Reflectivity = PropertyGet<float>(props,"Reflectivity",ok);
  457. if(ok) {
  458. out_mat->AddProperty(&Reflectivity,1,AI_MATKEY_REFLECTIVITY);
  459. }
  460. const float Shininess = PropertyGet<float>(props,"Shininess",ok);
  461. if(ok) {
  462. out_mat->AddProperty(&Shininess,1,AI_MATKEY_SHININESS_STRENGTH);
  463. }
  464. const float ShininessExponent = PropertyGet<float>(props,"ShininessExponent",ok);
  465. if(ok) {
  466. out_mat->AddProperty(&ShininessExponent,1,AI_MATKEY_SHININESS);
  467. }
  468. }
  469. // ------------------------------------------------------------------------------------------------
  470. // copy generated meshes, animations, lights, cameras and textures to the output scene
  471. void TransferDataToScene()
  472. {
  473. ai_assert(!out->mMeshes && !out->mNumMeshes);
  474. // note: the trailing () ensures initialization with NULL - not
  475. // many C++ users seem to know this, so pointing it out to avoid
  476. // confusion why this code works.
  477. out->mMeshes = new aiMesh*[meshes.size()]();
  478. out->mNumMeshes = static_cast<unsigned int>(meshes.size());
  479. std::swap_ranges(meshes.begin(),meshes.end(),out->mMeshes);
  480. if(materials.size()) {
  481. out->mMaterials = new aiMaterial*[materials.size()]();
  482. out->mNumMaterials = static_cast<unsigned int>(materials.size());
  483. std::swap_ranges(materials.begin(),materials.end(),out->mMaterials);
  484. }
  485. }
  486. private:
  487. // 0: not assigned yet, others: index is value - 1
  488. unsigned int defaultMaterialIndex;
  489. std::vector<aiMesh*> meshes;
  490. std::vector<aiMaterial*> materials;
  491. typedef std::map<const Material*, unsigned int> MaterialMap;
  492. MaterialMap materials_converted;
  493. typedef std::map<const Geometry*, unsigned int> MeshMap;
  494. MeshMap meshes_converted;
  495. aiScene* const out;
  496. const FBX::Document& doc;
  497. };
  498. //} // !anon
  499. // ------------------------------------------------------------------------------------------------
  500. void ConvertToAssimpScene(aiScene* out, const Document& doc)
  501. {
  502. Converter converter(out,doc);
  503. }
  504. } // !FBX
  505. } // !Assimp
  506. #endif