FBXConverter.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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 <boost/tuple/tuple.hpp>
  39. #include "FBXParser.h"
  40. #include "FBXConverter.h"
  41. #include "FBXDocument.h"
  42. #include "FBXUtil.h"
  43. #include "FBXProperties.h"
  44. #include "FBXImporter.h"
  45. namespace Assimp {
  46. namespace FBX {
  47. using namespace Util;
  48. // XXX vc9's debugger won't step into anonymous namespaces
  49. //namespace {
  50. /** Dummy class to encapsulate the conversion process */
  51. class Converter
  52. {
  53. public:
  54. Converter(aiScene* out, const Document& doc)
  55. : out(out)
  56. , doc(doc)
  57. {
  58. ConvertRootNode();
  59. ConvertAnimations();
  60. if(doc.Settings().readAllMaterials) {
  61. // unfortunately this means we have to evaluate all objects
  62. BOOST_FOREACH(const ObjectMap::value_type& v,doc.Objects()) {
  63. const Object* ob = v.second->Get();
  64. if(!ob) {
  65. continue;
  66. }
  67. const Material* mat = dynamic_cast<const Material*>(ob);
  68. if(mat) {
  69. if (materials_converted.find(mat) == materials_converted.end()) {
  70. ConvertMaterial(*mat);
  71. }
  72. }
  73. }
  74. }
  75. TransferDataToScene();
  76. }
  77. ~Converter()
  78. {
  79. std::for_each(meshes.begin(),meshes.end(),Util::delete_fun<aiMesh>());
  80. std::for_each(materials.begin(),materials.end(),Util::delete_fun<aiMaterial>());
  81. }
  82. private:
  83. // ------------------------------------------------------------------------------------------------
  84. // find scene root and trigger recursive scene conversion
  85. void ConvertRootNode()
  86. {
  87. out->mRootNode = new aiNode();
  88. out->mRootNode->mName.Set("RootNode");
  89. // root has ID 0
  90. ConvertNodes(0L, *out->mRootNode);
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. // collect and assign child nodes
  94. void ConvertNodes(uint64_t id, aiNode& parent)
  95. {
  96. const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(id);
  97. std::vector<aiNode*> nodes;
  98. nodes.reserve(conns.size());
  99. BOOST_FOREACH(const Connection* con, conns) {
  100. // ignore object-property links
  101. if(con->PropertyName().length()) {
  102. continue;
  103. }
  104. const Object* const object = con->SourceObject();
  105. if(!object) {
  106. FBXImporter::LogWarn("failed to convert source object for node link");
  107. continue;
  108. }
  109. const Model* const model = dynamic_cast<const Model*>(object);
  110. if(model) {
  111. aiNode* nd = new aiNode();
  112. nodes.push_back(nd);
  113. // strip Model:: prefix
  114. std::string name = model->Name();
  115. if(name.substr(0,7) == "Model::") {
  116. name = name.substr(7);
  117. }
  118. nd->mName.Set(name);
  119. nd->mParent = &parent;
  120. ConvertTransformation(*model,*nd);
  121. ConvertModel(*model, *nd);
  122. ConvertNodes(model->ID(), *nd);
  123. }
  124. }
  125. if(nodes.size()) {
  126. parent.mChildren = new aiNode*[nodes.size()]();
  127. parent.mNumChildren = static_cast<unsigned int>(nodes.size());
  128. std::swap_ranges(nodes.begin(),nodes.end(),parent.mChildren);
  129. }
  130. }
  131. // ------------------------------------------------------------------------------------------------
  132. void ConvertTransformation(const Model& model, aiNode& nd)
  133. {
  134. const PropertyTable& props = model.Props();
  135. bool ok;
  136. aiVector3D Translation = PropertyGet<aiVector3D>(props,"Lcl Translation",ok);
  137. if(!ok) {
  138. Translation = aiVector3D(0.0f,0.0f,0.0f);
  139. }
  140. aiVector3D Scaling = PropertyGet<aiVector3D>(props,"Lcl Scaling",ok);
  141. if(!ok) {
  142. Scaling = aiVector3D(1.0f,1.0f,1.0f);
  143. }
  144. // XXX euler angles, radians, xyz order?
  145. aiVector3D Rotation = PropertyGet<aiVector3D>(props,"Lcl Rotation",ok);
  146. if(!ok) {
  147. Rotation = aiVector3D(0.0f,0.0f,0.0f);
  148. }
  149. aiMatrix4x4 temp;
  150. nd.mTransformation = aiMatrix4x4::Scaling(Scaling,temp);
  151. if(fabs(Rotation.x) > 1e-6f) {
  152. nd.mTransformation *= aiMatrix4x4::RotationX(Rotation.x,temp);
  153. }
  154. if(fabs(Rotation.y) > 1e-6f) {
  155. nd.mTransformation *= aiMatrix4x4::RotationY(Rotation.y,temp);
  156. }
  157. if(fabs(Rotation.z) > 1e-6f) {
  158. nd.mTransformation *= aiMatrix4x4::RotationZ(Rotation.z,temp);
  159. }
  160. nd.mTransformation.a4 = Translation.x;
  161. nd.mTransformation.b4 = Translation.y;
  162. nd.mTransformation.c4 = Translation.z;
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. void ConvertModel(const Model& model, aiNode& nd)
  166. {
  167. const std::vector<const Geometry*>& geos = model.GetGeometry();
  168. std::vector<unsigned int> meshes;
  169. meshes.reserve(geos.size());
  170. BOOST_FOREACH(const Geometry* geo, geos) {
  171. const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*>(geo);
  172. if(mesh) {
  173. const std::vector<unsigned int>& indices = ConvertMesh(*mesh, model);
  174. std::copy(indices.begin(),indices.end(),std::back_inserter(meshes) );
  175. }
  176. else {
  177. FBXImporter::LogWarn("ignoring unrecognized geometry: " + geo->Name());
  178. }
  179. }
  180. if(meshes.size()) {
  181. nd.mMeshes = new unsigned int[meshes.size()]();
  182. nd.mNumMeshes = static_cast<unsigned int>(meshes.size());
  183. std::swap_ranges(meshes.begin(),meshes.end(),nd.mMeshes);
  184. }
  185. }
  186. // ------------------------------------------------------------------------------------------------
  187. // MeshGeometry -> aiMesh, return mesh index + 1 or 0 if the conversion failed
  188. std::vector<unsigned int> ConvertMesh(const MeshGeometry& mesh, const Model& model)
  189. {
  190. std::vector<unsigned int> temp;
  191. MeshMap::const_iterator it = meshes_converted.find(&mesh);
  192. if (it != meshes_converted.end()) {
  193. std::copy((*it).second.begin(),(*it).second.end(),std::back_inserter(temp));
  194. return temp;
  195. }
  196. const std::vector<aiVector3D>& vertices = mesh.GetVertices();
  197. const std::vector<unsigned int>& faces = mesh.GetFaceIndexCounts();
  198. if(vertices.empty() || faces.empty()) {
  199. FBXImporter::LogWarn("ignoring empty geometry: " + mesh.Name());
  200. return temp;
  201. }
  202. // one material per mesh maps easily to aiMesh. Multiple material
  203. // meshes need to be split.
  204. const std::vector<unsigned int>& mindices = mesh.GetMaterialIndices();
  205. if (doc.Settings().readMaterials && !mindices.empty()) {
  206. const unsigned int base = mindices[0];
  207. BOOST_FOREACH(unsigned int index, mindices) {
  208. if(index != base) {
  209. return ConvertMeshMultiMaterial(mesh, model);
  210. }
  211. }
  212. }
  213. // faster codepath, just copy the data
  214. temp.push_back(ConvertMeshSingleMaterial(mesh, model));
  215. return temp;
  216. }
  217. // ------------------------------------------------------------------------------------------------
  218. aiMesh* SetupEmptyMesh(const MeshGeometry& mesh, unsigned int material_index)
  219. {
  220. aiMesh* const out_mesh = new aiMesh();
  221. meshes.push_back(out_mesh);
  222. meshes_converted[&mesh].push_back(static_cast<unsigned int>(meshes.size()-1));
  223. // set name
  224. std::string name = mesh.Name();
  225. if (name.substr(0,10) == "Geometry::") {
  226. name = name.substr(10);
  227. }
  228. if(name.length()) {
  229. out_mesh->mName.Set(name);
  230. }
  231. return out_mesh;
  232. }
  233. // ------------------------------------------------------------------------------------------------
  234. unsigned int ConvertMeshSingleMaterial(const MeshGeometry& mesh, const Model& model)
  235. {
  236. const std::vector<unsigned int>& mindices = mesh.GetMaterialIndices();
  237. aiMesh* const out_mesh = SetupEmptyMesh(mesh,mindices.size() ? mindices[0] : static_cast<unsigned int>(-1));
  238. const std::vector<aiVector3D>& vertices = mesh.GetVertices();
  239. const std::vector<unsigned int>& faces = mesh.GetFaceIndexCounts();
  240. // copy vertices
  241. out_mesh->mNumVertices = static_cast<unsigned int>(vertices.size());
  242. out_mesh->mVertices = new aiVector3D[vertices.size()];
  243. std::copy(vertices.begin(),vertices.end(),out_mesh->mVertices);
  244. // generate dummy faces
  245. out_mesh->mNumFaces = static_cast<unsigned int>(faces.size());
  246. aiFace* fac = out_mesh->mFaces = new aiFace[faces.size()]();
  247. unsigned int cursor = 0;
  248. BOOST_FOREACH(unsigned int pcount, faces) {
  249. aiFace& f = *fac++;
  250. f.mNumIndices = pcount;
  251. f.mIndices = new unsigned int[pcount];
  252. switch(pcount)
  253. {
  254. case 1:
  255. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  256. break;
  257. case 2:
  258. out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  259. break;
  260. case 3:
  261. out_mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  262. break;
  263. default:
  264. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  265. break;
  266. }
  267. for (unsigned int i = 0; i < pcount; ++i) {
  268. f.mIndices[i] = cursor++;
  269. }
  270. }
  271. // copy normals
  272. const std::vector<aiVector3D>& normals = mesh.GetNormals();
  273. if(normals.size()) {
  274. ai_assert(normals.size() == vertices.size());
  275. out_mesh->mNormals = new aiVector3D[vertices.size()];
  276. std::copy(normals.begin(),normals.end(),out_mesh->mNormals);
  277. }
  278. // copy tangents - assimp requires both tangents and bitangents (binormals)
  279. // to be present, or neither of them. Compute binormals from normals
  280. // and tangents if needed.
  281. const std::vector<aiVector3D>& tangents = mesh.GetTangents();
  282. const std::vector<aiVector3D>* binormals = &mesh.GetBinormals();
  283. if(tangents.size()) {
  284. std::vector<aiVector3D> tempBinormals;
  285. if (!binormals->size()) {
  286. if (normals.size()) {
  287. tempBinormals.resize(normals.size());
  288. for (unsigned int i = 0; i < tangents.size(); ++i) {
  289. tempBinormals[i] = normals[i] ^ tangents[i];
  290. }
  291. binormals = &tempBinormals;
  292. }
  293. else {
  294. binormals = NULL;
  295. }
  296. }
  297. if(binormals) {
  298. ai_assert(tangents.size() == vertices.size() && binormals->size() == vertices.size());
  299. out_mesh->mTangents = new aiVector3D[vertices.size()];
  300. std::copy(tangents.begin(),tangents.end(),out_mesh->mTangents);
  301. out_mesh->mBitangents = new aiVector3D[vertices.size()];
  302. std::copy(binormals->begin(),binormals->end(),out_mesh->mBitangents);
  303. }
  304. }
  305. // copy texture coords
  306. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  307. const std::vector<aiVector2D>& uvs = mesh.GetTextureCoords(i);
  308. if(uvs.empty()) {
  309. break;
  310. }
  311. aiVector3D* out_uv = out_mesh->mTextureCoords[i] = new aiVector3D[vertices.size()];
  312. BOOST_FOREACH(const aiVector2D& v, uvs) {
  313. *out_uv++ = aiVector3D(v.x,v.y,0.0f);
  314. }
  315. out_mesh->mNumUVComponents[i] = 2;
  316. }
  317. // copy vertex colors
  318. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
  319. const std::vector<aiColor4D>& colors = mesh.GetVertexColors(i);
  320. if(colors.empty()) {
  321. break;
  322. }
  323. out_mesh->mColors[i] = new aiColor4D[vertices.size()];
  324. std::copy(colors.begin(),colors.end(),out_mesh->mColors[i]);
  325. }
  326. if(!doc.Settings().readMaterials || mindices.empty()) {
  327. FBXImporter::LogError("no material assigned to mesh, setting default material");
  328. out_mesh->mMaterialIndex = GetDefaultMaterial();
  329. }
  330. else {
  331. ConvertMaterialForMesh(out_mesh,model,mesh,mindices[0]);
  332. }
  333. return static_cast<unsigned int>(meshes.size() - 1);
  334. }
  335. // ------------------------------------------------------------------------------------------------
  336. std::vector<unsigned int> ConvertMeshMultiMaterial(const MeshGeometry& mesh, const Model& model)
  337. {
  338. const std::vector<unsigned int>& mindices = mesh.GetMaterialIndices();
  339. ai_assert(mindices.size());
  340. std::set<unsigned int> had;
  341. std::vector<unsigned int> indices;
  342. BOOST_FOREACH(unsigned int index, mindices) {
  343. if(had.find(index) == had.end()) {
  344. indices.push_back(ConvertMeshMultiMaterial(mesh, model, index));
  345. had.insert(index);
  346. }
  347. }
  348. return indices;
  349. }
  350. // ------------------------------------------------------------------------------------------------
  351. unsigned int ConvertMeshMultiMaterial(const MeshGeometry& mesh, const Model& model, unsigned int index)
  352. {
  353. aiMesh* const out_mesh = SetupEmptyMesh(mesh, index);
  354. const std::vector<unsigned int>& mindices = mesh.GetMaterialIndices();
  355. const std::vector<aiVector3D>& vertices = mesh.GetVertices();
  356. const std::vector<unsigned int>& faces = mesh.GetFaceIndexCounts();
  357. unsigned int count_faces = 0;
  358. unsigned int count_vertices = 0;
  359. // count faces
  360. for(std::vector<unsigned int>::const_iterator it = mindices.begin(),
  361. end = mindices.end(), itf = faces.begin(); it != end; ++it, ++itf)
  362. {
  363. if ((*it) != index) {
  364. continue;
  365. }
  366. ++count_faces;
  367. count_vertices += *itf;
  368. }
  369. ai_assert(count_faces);
  370. // allocate output data arrays, but don't fill them yet
  371. out_mesh->mNumVertices = count_vertices;
  372. out_mesh->mVertices = new aiVector3D[count_vertices];
  373. out_mesh->mNumFaces = count_faces;
  374. aiFace* fac = out_mesh->mFaces = new aiFace[count_faces]();
  375. // allocate normals
  376. const std::vector<aiVector3D>& normals = mesh.GetNormals();
  377. if(normals.size()) {
  378. ai_assert(normals.size() == vertices.size());
  379. out_mesh->mNormals = new aiVector3D[vertices.size()];
  380. }
  381. // allocate tangents, binormals.
  382. const std::vector<aiVector3D>& tangents = mesh.GetTangents();
  383. const std::vector<aiVector3D>* binormals = &mesh.GetBinormals();
  384. if(tangents.size()) {
  385. std::vector<aiVector3D> tempBinormals;
  386. if (!binormals->size()) {
  387. if (normals.size()) {
  388. // XXX this computes the binormals for the entire mesh, not only
  389. // the part for which we need them.
  390. tempBinormals.resize(normals.size());
  391. for (unsigned int i = 0; i < tangents.size(); ++i) {
  392. tempBinormals[i] = normals[i] ^ tangents[i];
  393. }
  394. binormals = &tempBinormals;
  395. }
  396. else {
  397. binormals = NULL;
  398. }
  399. }
  400. if(binormals) {
  401. ai_assert(tangents.size() == vertices.size() && binormals->size() == vertices.size());
  402. out_mesh->mTangents = new aiVector3D[vertices.size()];
  403. out_mesh->mBitangents = new aiVector3D[vertices.size()];
  404. }
  405. }
  406. // allocate texture coords
  407. unsigned int num_uvs = 0;
  408. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i, ++num_uvs) {
  409. const std::vector<aiVector2D>& uvs = mesh.GetTextureCoords(i);
  410. if(uvs.empty()) {
  411. break;
  412. }
  413. out_mesh->mTextureCoords[i] = new aiVector3D[vertices.size()];
  414. out_mesh->mNumUVComponents[i] = 2;
  415. }
  416. // allocate vertex colors
  417. unsigned int num_vcs = 0;
  418. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i, ++num_vcs) {
  419. const std::vector<aiColor4D>& colors = mesh.GetVertexColors(i);
  420. if(colors.empty()) {
  421. break;
  422. }
  423. out_mesh->mColors[i] = new aiColor4D[vertices.size()];
  424. }
  425. unsigned int cursor = 0, in_cursor = 0;
  426. for(std::vector<unsigned int>::const_iterator it = mindices.begin(),
  427. end = mindices.end(), itf = faces.begin(); it != end; ++it, ++itf)
  428. {
  429. const unsigned int pcount = *itf;
  430. if ((*it) != index) {
  431. in_cursor += pcount;
  432. continue;
  433. }
  434. aiFace& f = *fac++;
  435. f.mNumIndices = pcount;
  436. f.mIndices = new unsigned int[pcount];
  437. switch(pcount)
  438. {
  439. case 1:
  440. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  441. break;
  442. case 2:
  443. out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  444. break;
  445. case 3:
  446. out_mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  447. break;
  448. default:
  449. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  450. break;
  451. }
  452. for (unsigned int i = 0; i < pcount; ++i, ++cursor, ++in_cursor) {
  453. f.mIndices[i] = cursor;
  454. out_mesh->mVertices[cursor] = vertices[in_cursor];
  455. if(out_mesh->mNormals) {
  456. out_mesh->mNormals[cursor] = normals[in_cursor];
  457. }
  458. if(out_mesh->mTangents) {
  459. out_mesh->mTangents[cursor] = tangents[in_cursor];
  460. out_mesh->mBitangents[cursor] = (*binormals)[in_cursor];
  461. }
  462. for (unsigned int i = 0; i < num_uvs; ++i) {
  463. const std::vector<aiVector2D>& uvs = mesh.GetTextureCoords(i);
  464. out_mesh->mTextureCoords[i][cursor] = aiVector3D(uvs[in_cursor].x,uvs[in_cursor].y, 0.0f);
  465. }
  466. for (unsigned int i = 0; i < num_vcs; ++i) {
  467. const std::vector<aiColor4D>& cols = mesh.GetVertexColors(i);
  468. out_mesh->mColors[i][cursor] = cols[in_cursor];
  469. }
  470. }
  471. }
  472. ConvertMaterialForMesh(out_mesh,model,mesh,index);
  473. return static_cast<unsigned int>(meshes.size() - 1);
  474. }
  475. // ------------------------------------------------------------------------------------------------
  476. void ConvertMaterialForMesh(aiMesh* out, const Model& model, const MeshGeometry& geo, unsigned int materialIndex)
  477. {
  478. // locate source materials for this mesh
  479. const std::vector<const Material*>& mats = model.GetMaterials();
  480. if (materialIndex >= mats.size()) {
  481. FBXImporter::LogError("material index out of bounds, setting default material");
  482. out->mMaterialIndex = GetDefaultMaterial();
  483. return;
  484. }
  485. const Material* const mat = mats[materialIndex];
  486. MaterialMap::const_iterator it = materials_converted.find(mat);
  487. if (it != materials_converted.end()) {
  488. out->mMaterialIndex = (*it).second;
  489. return;
  490. }
  491. out->mMaterialIndex = ConvertMaterial(*mat);
  492. materials_converted[mat] = out->mMaterialIndex;
  493. }
  494. // ------------------------------------------------------------------------------------------------
  495. unsigned int GetDefaultMaterial()
  496. {
  497. if (defaultMaterialIndex) {
  498. return defaultMaterialIndex - 1;
  499. }
  500. aiMaterial* out_mat = new aiMaterial();
  501. materials.push_back(out_mat);
  502. const aiColor3D diffuse = aiColor3D(0.8f,0.8f,0.8f);
  503. out_mat->AddProperty(&diffuse,1,AI_MATKEY_COLOR_DIFFUSE);
  504. aiString s;
  505. s.Set(AI_DEFAULT_MATERIAL_NAME);
  506. out_mat->AddProperty(&s,AI_MATKEY_NAME);
  507. defaultMaterialIndex = static_cast<unsigned int>(materials.size());
  508. return defaultMaterialIndex - 1;
  509. }
  510. // ------------------------------------------------------------------------------------------------
  511. // Material -> aiMaterial
  512. unsigned int ConvertMaterial(const Material& material)
  513. {
  514. const PropertyTable& props = material.Props();
  515. // generate empty output material
  516. aiMaterial* out_mat = new aiMaterial();
  517. materials_converted[&material] = static_cast<unsigned int>(materials.size());
  518. materials.push_back(out_mat);
  519. aiString str;
  520. // stip Material:: prefix
  521. std::string name = material.Name();
  522. if(name.substr(0,10) == "Material::") {
  523. name = name.substr(10);
  524. }
  525. // set material name if not empty - this could happen
  526. // and there should be no key for it in this case.
  527. if(name.length()) {
  528. str.Set(name);
  529. out_mat->AddProperty(&str,AI_MATKEY_NAME);
  530. }
  531. // shading stuff and colors
  532. SetShadingPropertiesCommon(out_mat,props);
  533. // texture assignments
  534. SetTextureProperties(out_mat,material.Textures());
  535. return static_cast<unsigned int>(materials.size() - 1);
  536. }
  537. // ------------------------------------------------------------------------------------------------
  538. void TrySetTextureProperties(aiMaterial* out_mat, const TextureMap& textures, const std::string& propName, aiTextureType target)
  539. {
  540. TextureMap::const_iterator it = textures.find(propName);
  541. if(it == textures.end()) {
  542. return;
  543. }
  544. const Texture* const tex = (*it).second;
  545. aiString path;
  546. path.Set(tex->RelativeFilename());
  547. out_mat->AddProperty(&path,_AI_MATKEY_TEXTURE_BASE,target,0);
  548. aiUVTransform uvTrafo;
  549. // XXX handle all kinds of UV transformations
  550. uvTrafo.mScaling = tex->UVScaling();
  551. uvTrafo.mTranslation = tex->UVTranslation();
  552. out_mat->AddProperty(&uvTrafo,1,_AI_MATKEY_UVTRANSFORM_BASE,target,0);
  553. const PropertyTable& props = tex->Props();
  554. int uvIndex = 0;
  555. bool ok;
  556. const std::string& uvSet = PropertyGet<std::string>(props,"UVSet",ok);
  557. if(ok) {
  558. // "default" is the name which usually appears in the FbxFileTexture template
  559. if(uvSet != "default" && uvSet.length()) {
  560. // this is a bit awkward - we need to find a mesh that uses this
  561. // material and scan its UV channels for the given UV name because
  562. // assimp references UV channels by index, not by name.
  563. // XXX: the case that UV channels may appear in different orders
  564. // in meshes is unhandled. A possible solution would be to sort
  565. // the UV channels alphabetically, but this would have the side
  566. // effect that the primary (first) UV channel would sometimes
  567. // be moved, causing trouble when users read only the first
  568. // UV channel and ignore UV channel assignments altogether.
  569. const unsigned int matIndex = std::distance(materials.begin(),
  570. std::find(materials.begin(),materials.end(),out_mat)
  571. );
  572. uvIndex = -1;
  573. BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) {
  574. const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*> (v.first);
  575. if(!mesh) {
  576. continue;
  577. }
  578. const std::vector<unsigned int>& mats = mesh->GetMaterialIndices();
  579. if(std::find(mats.begin(),mats.end(),matIndex) == mats.end()) {
  580. continue;
  581. }
  582. int index = -1;
  583. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  584. if(mesh->GetTextureCoords(i).empty()) {
  585. break;
  586. }
  587. const std::string& name = mesh->GetTextureCoordChannelName(i);
  588. if(name == uvSet) {
  589. index = static_cast<int>(i);
  590. break;
  591. }
  592. }
  593. if(index == -1) {
  594. FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
  595. continue;
  596. }
  597. if(uvIndex == -1) {
  598. uvIndex = index;
  599. }
  600. else {
  601. FBXImporter::LogWarn("the UV channel named " + uvSet +
  602. " appears at different positions in meshes, results will be wrong");
  603. }
  604. }
  605. if(uvIndex == -1) {
  606. FBXImporter::LogWarn("failed to resolve UV channel " + uvSet + ", using first UV channel");
  607. uvIndex = 0;
  608. }
  609. }
  610. }
  611. out_mat->AddProperty(&uvIndex,1,_AI_MATKEY_UVWSRC_BASE,target,0);
  612. }
  613. // ------------------------------------------------------------------------------------------------
  614. void SetTextureProperties(aiMaterial* out_mat, const TextureMap& textures)
  615. {
  616. TrySetTextureProperties(out_mat, textures, "DiffuseColor", aiTextureType_DIFFUSE);
  617. TrySetTextureProperties(out_mat, textures, "AmbientColor", aiTextureType_AMBIENT);
  618. TrySetTextureProperties(out_mat, textures, "EmissiveColor", aiTextureType_EMISSIVE);
  619. TrySetTextureProperties(out_mat, textures, "SpecularColor", aiTextureType_SPECULAR);
  620. TrySetTextureProperties(out_mat, textures, "TransparentColor", aiTextureType_OPACITY);
  621. TrySetTextureProperties(out_mat, textures, "ReflectionColor", aiTextureType_REFLECTION);
  622. TrySetTextureProperties(out_mat, textures, "DisplacementColor", aiTextureType_DISPLACEMENT);
  623. TrySetTextureProperties(out_mat, textures, "NormalMap", aiTextureType_NORMALS);
  624. TrySetTextureProperties(out_mat, textures, "Bump", aiTextureType_HEIGHT);
  625. }
  626. // ------------------------------------------------------------------------------------------------
  627. aiColor3D GetColorPropertyFromMaterial(const PropertyTable& props,const std::string& baseName, bool& result)
  628. {
  629. result = true;
  630. bool ok;
  631. const aiVector3D& Diffuse = PropertyGet<aiVector3D>(props,baseName,ok);
  632. if(ok) {
  633. return aiColor3D(Diffuse.x,Diffuse.y,Diffuse.z);
  634. }
  635. else {
  636. aiVector3D DiffuseColor = PropertyGet<aiVector3D>(props,baseName + "Color",ok);
  637. if(ok) {
  638. float DiffuseFactor = PropertyGet<float>(props,baseName + "Factor",ok);
  639. if(ok) {
  640. DiffuseColor *= DiffuseFactor;
  641. }
  642. return aiColor3D(DiffuseColor.x,DiffuseColor.y,DiffuseColor.z);
  643. }
  644. }
  645. result = false;
  646. return aiColor3D(0.0f,0.0f,0.0f);
  647. }
  648. // ------------------------------------------------------------------------------------------------
  649. void SetShadingPropertiesCommon(aiMaterial* out_mat, const PropertyTable& props)
  650. {
  651. // set shading properties. There are various, redundant ways in which FBX materials
  652. // specify their shading settings (depending on shading models, prop
  653. // template etc.). No idea which one is right in a particular context.
  654. // Just try to make sense of it - there's no spec to verify this against,
  655. // so why should we.
  656. bool ok;
  657. const aiColor3D& Diffuse = GetColorPropertyFromMaterial(props,"Diffuse",ok);
  658. if(ok) {
  659. out_mat->AddProperty(&Diffuse,1,AI_MATKEY_COLOR_DIFFUSE);
  660. }
  661. const aiColor3D& Emissive = GetColorPropertyFromMaterial(props,"Emissive",ok);
  662. if(ok) {
  663. out_mat->AddProperty(&Emissive,1,AI_MATKEY_COLOR_EMISSIVE);
  664. }
  665. const aiColor3D& Ambient = GetColorPropertyFromMaterial(props,"Ambient",ok);
  666. if(ok) {
  667. out_mat->AddProperty(&Ambient,1,AI_MATKEY_COLOR_AMBIENT);
  668. }
  669. const aiColor3D& Specular = GetColorPropertyFromMaterial(props,"Specular",ok);
  670. if(ok) {
  671. out_mat->AddProperty(&Specular,1,AI_MATKEY_COLOR_SPECULAR);
  672. }
  673. const float Opacity = PropertyGet<float>(props,"Opacity",ok);
  674. if(ok) {
  675. out_mat->AddProperty(&Opacity,1,AI_MATKEY_OPACITY);
  676. }
  677. const float Reflectivity = PropertyGet<float>(props,"Reflectivity",ok);
  678. if(ok) {
  679. out_mat->AddProperty(&Reflectivity,1,AI_MATKEY_REFLECTIVITY);
  680. }
  681. const float Shininess = PropertyGet<float>(props,"Shininess",ok);
  682. if(ok) {
  683. out_mat->AddProperty(&Shininess,1,AI_MATKEY_SHININESS_STRENGTH);
  684. }
  685. const float ShininessExponent = PropertyGet<float>(props,"ShininessExponent",ok);
  686. if(ok) {
  687. out_mat->AddProperty(&ShininessExponent,1,AI_MATKEY_SHININESS);
  688. }
  689. }
  690. // ------------------------------------------------------------------------------------------------
  691. // convert animation data to aiAnimation et al
  692. void ConvertAnimations()
  693. {
  694. const std::vector<const AnimationStack*>& animations = doc.AnimationStacks();
  695. BOOST_FOREACH(const AnimationStack* stack, animations) {
  696. ConvertAnimationStack(*stack);
  697. }
  698. }
  699. // ------------------------------------------------------------------------------------------------
  700. std::string FixNodeName(const std::string& name)
  701. {
  702. // XXX handle prefix
  703. return name;
  704. }
  705. typedef std::map<const AnimationCurveNode*, const AnimationLayer*> LayerMap;
  706. // ------------------------------------------------------------------------------------------------
  707. void ConvertAnimationStack(const AnimationStack& st)
  708. {
  709. aiAnimation* const anim = new aiAnimation();
  710. animations.push_back(anim);
  711. // strip AnimationStack:: prefix
  712. std::string name = st.Name();
  713. if(name.substr(0,16) == "AnimationStack::") {
  714. name = name.substr(16);
  715. }
  716. anim->mName.Set(name);
  717. const AnimationLayerList& layers = st.Layers();
  718. // need to find all nodes for which we need to generate node animations -
  719. // it may happen that we need to merge multiple layers, though.
  720. // XXX: better use multi_map ..
  721. typedef std::map<std::string, std::vector<const AnimationCurveNode*> > NodeMap;
  722. NodeMap node_map;
  723. // reverse mapping from curves to layers, much faster than querying
  724. // the FBX DOM for it.
  725. LayerMap layer_map;
  726. BOOST_FOREACH(const AnimationLayer* layer, layers) {
  727. ai_assert(layer);
  728. const AnimationCurveNodeList& nodes = layer->Nodes();
  729. BOOST_FOREACH(const AnimationCurveNode* node, nodes) {
  730. ai_assert(node);
  731. const Model* model = node->TargetNode();
  732. ai_assert(model);
  733. const std::string& name = FixNodeName(model->Name());
  734. node_map[name].push_back(node);
  735. layer_map[node] = layer;
  736. }
  737. }
  738. // generate node animations
  739. std::vector<aiNodeAnim*> node_anims;
  740. try {
  741. NodeMap node_property_map;
  742. BOOST_FOREACH(const NodeMap::value_type& kv, node_map) {
  743. node_property_map.clear();
  744. BOOST_FOREACH(const AnimationCurveNode* node, kv.second) {
  745. ai_assert(node);
  746. if (node->TargetProperty().empty()) {
  747. FBXImporter::LogWarn("target property for animation curve not set");
  748. continue;
  749. }
  750. node_property_map[node->TargetProperty()].push_back(node);
  751. }
  752. const NodeMap::const_iterator itScale = node_property_map.find("Lcl Scaling");
  753. const NodeMap::const_iterator itRotation = node_property_map.find("Lcl Rotation");
  754. const NodeMap::const_iterator itTranslation = node_property_map.find("Lcl Translation");
  755. const bool hasScale = !!(*itScale).second.size();
  756. const bool hasRotation = !!(*itRotation).second.size();
  757. const bool hasTranslation = !!(*itTranslation).second.size();
  758. if (!hasScale && !hasRotation && !hasTranslation) {
  759. FBXImporter::LogWarn("ignoring node animation, did not find transformation key frames");
  760. continue;
  761. }
  762. aiNodeAnim* const na = new aiNodeAnim();
  763. node_anims.push_back(na);
  764. if(hasScale) {
  765. ConvertScaleKeys(na, (*itScale).second, layer_map);
  766. }
  767. if(hasRotation) {
  768. ConvertRotationKeys(na, (*itRotation).second, layer_map);
  769. }
  770. if(hasTranslation) {
  771. ConvertTranslationKeys(na, (*itTranslation).second, layer_map);
  772. }
  773. }
  774. }
  775. catch(std::exception&) {
  776. std::for_each(node_anims.begin(), node_anims.end(), Util::delete_fun<aiNodeAnim>());
  777. }
  778. if(node_anims.size()) {
  779. anim->mChannels = new aiNodeAnim*[node_anims.size()]();
  780. anim->mNumChannels = static_cast<unsigned int>(node_anims.size());
  781. std::swap_ranges(node_anims.begin(),node_anims.end(),anim->mChannels);
  782. }
  783. }
  784. // key (time), value, mapto (component index)
  785. typedef boost::tuple< std::vector<float>*, std::vector<float>*, unsigned int > KeyFrameList;
  786. typedef std::vector<KeyFrameList> KeyFrameListList;
  787. typedef std::vector<float> KeyTimeList;
  788. // ------------------------------------------------------------------------------------------------
  789. KeyFrameListList GetKeyframeList(const std::vector<const AnimationCurveNode*>& nodes)
  790. {
  791. KeyFrameListList inputs;
  792. inputs.reserve(nodes.size()*3);
  793. BOOST_FOREACH(const AnimationCurveNode* node, nodes) {
  794. ai_assert(node);
  795. const AnimationCurveMap& curves = node->Curves();
  796. BOOST_FOREACH(const AnimationCurveMap::value_type& kv, curves) {
  797. unsigned int mapto;
  798. if (kv.first == "d|X") {
  799. mapto = 0;
  800. }
  801. else if (kv.first == "d|Y") {
  802. mapto = 1;
  803. }
  804. else if (kv.first == "d|Z") {
  805. mapto = 2;
  806. }
  807. else {
  808. FBXImporter::LogWarn("ignoring scale animation curve, did not recognize target component");
  809. continue;
  810. }
  811. const AnimationCurve* const curve = kv.second;
  812. ai_assert(curve->GetKeys().size() == curve->GetValues().size() && curve->GetKeys().size());
  813. inputs.push_back(boost::make_tuple(&curve->GetKeys(), &curve->GetValues(), mapto));
  814. }
  815. }
  816. return inputs; // pray for NRVO :-)
  817. }
  818. // ------------------------------------------------------------------------------------------------
  819. std::vector<float> GetKeyTimeList(const KeyFrameListList& inputs)
  820. {
  821. // reserve some space upfront - it is likely that the keyframe lists
  822. // have matching time values, so max(of all keyframe lists) should
  823. // be a good estimate.
  824. std::vector<float> keys;
  825. size_t estimate = 0;
  826. BOOST_FOREACH(const KeyFrameList& kfl, inputs) {
  827. estimate = std::max(estimate, kfl.get<0>()->size());
  828. }
  829. keys.reserve(estimate);
  830. std::vector<unsigned int> next_pos;
  831. next_pos.resize(inputs.size(),0);
  832. const size_t count = inputs.size();
  833. while(true) {
  834. float min_tick = 1e10f;
  835. for (size_t i = 0; i < count; ++i) {
  836. const KeyFrameList& kfl = inputs[i];
  837. if (kfl.get<0>()->size() > next_pos[i] && kfl.get<0>()->at(next_pos[i]) < min_tick) {
  838. min_tick = kfl.get<0>()->at(next_pos[i]);
  839. }
  840. }
  841. if (min_tick > 1e9f) {
  842. break;
  843. }
  844. keys.push_back(min_tick);
  845. for (size_t i = 0; i < count; ++i) {
  846. const KeyFrameList& kfl = inputs[i];
  847. const float time_epsilon = 1e-4f;
  848. while(kfl.get<0>()->size() > next_pos[i] && fabs(kfl.get<0>()->at(next_pos[i]) - min_tick) < time_epsilon) {
  849. ++next_pos[i];
  850. }
  851. }
  852. }
  853. return keys;
  854. }
  855. // ------------------------------------------------------------------------------------------------
  856. void InterpolateKeys(aiVectorKey* valOut,const KeyTimeList& keys, const KeyFrameListList& inputs, const bool geom = false)
  857. {
  858. ai_assert(keys.size());
  859. ai_assert(valOut);
  860. std::vector<unsigned int> next_pos;
  861. const size_t count = inputs.size();
  862. next_pos.resize(inputs.size(),0);
  863. BOOST_FOREACH(float time, keys) {
  864. float result[3] = {0.0f, 0.0f, 0.0f};
  865. if(geom) {
  866. result[0] = result[1] = result[2] = 1.0f;
  867. }
  868. for (size_t i = 0; i < count; ++i) {
  869. const KeyFrameList& kfl = inputs[i];
  870. const float time_epsilon = 1e-4f;
  871. if (kfl.get<0>()->size() > next_pos[i] && fabs(kfl.get<0>()->at(next_pos[i]) - time) < time_epsilon) {
  872. ++next_pos[i];
  873. }
  874. // use lerp for interpolation
  875. const float valueA = kfl.get<1>()->at(next_pos[i]>0 ? next_pos[i]-1 : 0);
  876. const float valueB = kfl.get<1>()->at(next_pos[i]);
  877. const float timeA = kfl.get<0>()->at(next_pos[i]>0 ? next_pos[i]-1 : 0);
  878. const float timeB = kfl.get<0>()->at(next_pos[i]);
  879. const float factor = (time - timeA) / (timeB - timeA);
  880. const float interpValue = valueA + (valueB - valueA) * factor;
  881. if(geom) {
  882. result[kfl.get<2>()] *= interpValue;
  883. }
  884. else {
  885. result[kfl.get<2>()] += interpValue;
  886. }
  887. }
  888. valOut->mTime = time;
  889. valOut->mValue.x = result[0];
  890. valOut->mValue.y = result[1];
  891. valOut->mValue.z = result[2];
  892. ++valOut;
  893. }
  894. }
  895. // ------------------------------------------------------------------------------------------------
  896. void InterpolateKeys(aiQuatKey* valOut,const KeyTimeList& keys, const KeyFrameListList& inputs, const bool geom = false)
  897. {
  898. ai_assert(keys.size());
  899. ai_assert(valOut);
  900. boost::scoped_array<aiVectorKey> temp(new aiVectorKey[keys.size()]);
  901. InterpolateKeys(temp.get(),keys,inputs,geom);
  902. for (size_t i = 0, c = keys.size(); i < c; ++i) {
  903. const aiVector3D rot = temp[i].mValue;
  904. aiMatrix4x4 m, mtemp;
  905. if(fabs(rot.x) > 1e-6f) {
  906. m *= aiMatrix4x4::RotationX(rot.x,mtemp);
  907. }
  908. if(fabs(rot.y) > 1e-6f) {
  909. m *= aiMatrix4x4::RotationY(rot.y,mtemp);
  910. }
  911. if(fabs(rot.z) > 1e-6f) {
  912. m *= aiMatrix4x4::RotationZ(rot.z,mtemp);
  913. }
  914. valOut[i].mTime = temp[i].mTime;
  915. valOut[i].mValue = aiQuaternion(aiMatrix3x3(m));
  916. }
  917. }
  918. // ------------------------------------------------------------------------------------------------
  919. void ConvertScaleKeys(aiNodeAnim* na, const std::vector<const AnimationCurveNode*>& nodes, const LayerMap& layers)
  920. {
  921. ai_assert(nodes.size());
  922. // XXX for now, assume scale should be blended geometrically (i.e. two
  923. // layers should be multiplied with each other). There is a FBX
  924. // property in the layer to specify the behaviour, though.
  925. const KeyFrameListList& inputs = GetKeyframeList(nodes);
  926. const KeyTimeList& keys = GetKeyTimeList(inputs);
  927. na->mNumScalingKeys = static_cast<unsigned int>(keys.size());
  928. na->mScalingKeys = new aiVectorKey[keys.size()];
  929. InterpolateKeys(na->mScalingKeys, keys, inputs, true);
  930. }
  931. // ------------------------------------------------------------------------------------------------
  932. void ConvertTranslationKeys(aiNodeAnim* na, const std::vector<const AnimationCurveNode*>& nodes, const LayerMap& layers)
  933. {
  934. ai_assert(nodes.size());
  935. // XXX see notes in ConvertScaleKeys()
  936. const KeyFrameListList& inputs = GetKeyframeList(nodes);
  937. const KeyTimeList& keys = GetKeyTimeList(inputs);
  938. na->mNumPositionKeys = static_cast<unsigned int>(keys.size());
  939. na->mPositionKeys = new aiVectorKey[keys.size()];
  940. InterpolateKeys(na->mPositionKeys, keys, inputs, false);
  941. }
  942. // ------------------------------------------------------------------------------------------------
  943. void ConvertRotationKeys(aiNodeAnim* na, const std::vector<const AnimationCurveNode*>& nodes, const LayerMap& layers)
  944. {
  945. ai_assert(nodes.size());
  946. // XXX see notes in ConvertScaleKeys()
  947. const std::vector< KeyFrameList >& inputs = GetKeyframeList(nodes);
  948. const std::vector<float>& keys = GetKeyTimeList(inputs);
  949. na->mNumRotationKeys = static_cast<unsigned int>(keys.size());
  950. na->mRotationKeys = new aiQuatKey[keys.size()];
  951. InterpolateKeys(na->mRotationKeys, keys, inputs, false);
  952. }
  953. // ------------------------------------------------------------------------------------------------
  954. // copy generated meshes, animations, lights, cameras and textures to the output scene
  955. void TransferDataToScene()
  956. {
  957. ai_assert(!out->mMeshes && !out->mNumMeshes);
  958. // note: the trailing () ensures initialization with NULL - not
  959. // many C++ users seem to know this, so pointing it out to avoid
  960. // confusion why this code works.
  961. out->mMeshes = new aiMesh*[meshes.size()]();
  962. out->mNumMeshes = static_cast<unsigned int>(meshes.size());
  963. std::swap_ranges(meshes.begin(),meshes.end(),out->mMeshes);
  964. if(materials.size()) {
  965. out->mMaterials = new aiMaterial*[materials.size()]();
  966. out->mNumMaterials = static_cast<unsigned int>(materials.size());
  967. std::swap_ranges(materials.begin(),materials.end(),out->mMaterials);
  968. }
  969. if(animations.size()) {
  970. out->mAnimations = new aiAnimation*[animations.size()]();
  971. out->mNumAnimations = static_cast<unsigned int>(animations.size());
  972. std::swap_ranges(animations.begin(),animations.end(),out->mAnimations);
  973. }
  974. }
  975. private:
  976. // 0: not assigned yet, others: index is value - 1
  977. unsigned int defaultMaterialIndex;
  978. std::vector<aiMesh*> meshes;
  979. std::vector<aiMaterial*> materials;
  980. std::vector<aiAnimation*> animations;
  981. typedef std::map<const Material*, unsigned int> MaterialMap;
  982. MaterialMap materials_converted;
  983. typedef std::map<const Geometry*, std::vector<unsigned int> > MeshMap;
  984. MeshMap meshes_converted;
  985. aiScene* const out;
  986. const FBX::Document& doc;
  987. };
  988. //} // !anon
  989. // ------------------------------------------------------------------------------------------------
  990. void ConvertToAssimpScene(aiScene* out, const Document& doc)
  991. {
  992. Converter converter(out,doc);
  993. }
  994. } // !FBX
  995. } // !Assimp
  996. #endif