BsFBXImporter.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. #include "BsFBXImporter.h"
  2. #include "BsResource.h"
  3. #include "BsCoreApplication.h"
  4. #include "BsDebug.h"
  5. #include "BsDataStream.h"
  6. #include "BsPath.h"
  7. #include "BsMeshData.h"
  8. #include "BsMesh.h"
  9. #include "BsVector2.h"
  10. #include "BsVector3.h"
  11. #include "BsVector4.h"
  12. #include "BsVertexDataDesc.h"
  13. namespace BansheeEngine
  14. {
  15. FBXImporter::FBXImporter()
  16. :SpecificImporter()
  17. {
  18. mExtensions.push_back(L"fbx");
  19. }
  20. FBXImporter::~FBXImporter()
  21. {
  22. }
  23. bool FBXImporter::isExtensionSupported(const WString& ext) const
  24. {
  25. WString lowerCaseExt = ext;
  26. StringUtil::toLowerCase(lowerCaseExt);
  27. return find(mExtensions.begin(), mExtensions.end(), lowerCaseExt) != mExtensions.end();
  28. }
  29. bool FBXImporter::isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const
  30. {
  31. return true; // FBX files can be plain-text so I don't even check for magic number
  32. }
  33. ResourcePtr FBXImporter::import(const Path& filePath, ConstImportOptionsPtr importOptions)
  34. {
  35. FbxManager* fbxManager = nullptr;
  36. FbxScene* fbxScene = nullptr;
  37. startUpSdk(fbxManager, fbxScene);
  38. loadScene(fbxManager, fbxScene, filePath);
  39. Vector<SubMesh> subMeshes;
  40. MeshDataPtr meshData = parseScene(fbxManager, fbxScene, subMeshes);
  41. shutDownSdk(fbxManager);
  42. MeshPtr mesh = Mesh::_createPtr(meshData, subMeshes);
  43. WString fileName = filePath.getWFilename(false);
  44. mesh->setName(toString(fileName));
  45. return mesh;
  46. }
  47. void FBXImporter::startUpSdk(FbxManager*& manager, FbxScene*& scene)
  48. {
  49. // TODO Low priority - Initialize allocator methods for FBX. It calls a lot of heap allocs (200 000 calls for a simple 2k poly mesh) which slows down the import.
  50. // Custom allocator would help a lot.
  51. manager = FbxManager::Create();
  52. if(manager == nullptr)
  53. BS_EXCEPT(InternalErrorException, "FBX SDK failed to initialize. FbxManager::Create() failed.");
  54. FbxIOSettings* ios = FbxIOSettings::Create(manager, IOSROOT);
  55. manager->SetIOSettings(ios);
  56. scene = FbxScene::Create(manager, "Import Scene");
  57. if(scene == nullptr)
  58. BS_EXCEPT(InternalErrorException, "Failed to create FBX scene.");
  59. }
  60. void FBXImporter::shutDownSdk(FbxManager* manager)
  61. {
  62. manager->Destroy();
  63. }
  64. void FBXImporter::loadScene(FbxManager* manager, FbxScene* scene, const Path& filePath)
  65. {
  66. int lFileMajor, lFileMinor, lFileRevision;
  67. int lSDKMajor, lSDKMinor, lSDKRevision;
  68. FbxManager::GetFileFormatVersion(lSDKMajor, lSDKMinor, lSDKRevision);
  69. FbxImporter* importer = FbxImporter::Create(manager, "");
  70. bool importStatus = importer->Initialize(filePath.toString().c_str(), -1, manager->GetIOSettings());
  71. importer->GetFileVersion(lFileMajor, lFileMinor, lFileRevision);
  72. if(!importStatus)
  73. {
  74. BS_EXCEPT(InternalErrorException, "Call to FbxImporter::Initialize() failed.\n" +
  75. String("Error returned: %s\n\n") + String(importer->GetStatus().GetErrorString()));
  76. }
  77. manager->GetIOSettings()->SetBoolProp(IMP_FBX_ANIMATION, false);
  78. manager->GetIOSettings()->SetBoolProp(IMP_FBX_TEXTURE, false);
  79. manager->GetIOSettings()->SetBoolProp(IMP_FBX_LINK, false);
  80. manager->GetIOSettings()->SetBoolProp(IMP_FBX_GOBO, false);
  81. manager->GetIOSettings()->SetBoolProp(IMP_FBX_SHAPE, false);
  82. // TODO - Parse animations
  83. // TODO - Parse blend shapes
  84. importStatus = importer->Import(scene);
  85. if(!importStatus)
  86. {
  87. importer->Destroy();
  88. BS_EXCEPT(InternalErrorException, "Call to FbxImporter::Initialize() failed.\n" +
  89. String("Error returned: %s\n\n") + String(importer->GetStatus().GetErrorString()));
  90. }
  91. importer->Destroy();
  92. }
  93. MeshDataPtr FBXImporter::parseScene(FbxManager* manager, FbxScene* scene, Vector<SubMesh>& subMeshes)
  94. {
  95. Stack<FbxNode*> todo;
  96. todo.push(scene->GetRootNode());
  97. Vector<MeshDataPtr> allMeshes;
  98. Vector<Vector<SubMesh>> allSubMeshes;
  99. while(!todo.empty())
  100. {
  101. FbxNode* curNode = todo.top();
  102. todo.pop();
  103. const char* name = curNode->GetName();
  104. FbxNodeAttribute* attrib = curNode->GetNodeAttribute();
  105. if(attrib != nullptr)
  106. {
  107. FbxNodeAttribute::EType attribType = attrib->GetAttributeType();
  108. switch(attribType)
  109. {
  110. case FbxNodeAttribute::eMesh:
  111. {
  112. FbxMesh* mesh = static_cast<FbxMesh*>(attrib);
  113. if(!mesh->IsTriangleMesh())
  114. {
  115. FbxGeometryConverter geomConverter(manager);
  116. geomConverter.Triangulate(mesh, true);
  117. attrib = curNode->GetNodeAttribute();
  118. mesh = static_cast<FbxMesh*>(attrib);
  119. }
  120. allSubMeshes.push_back(Vector<SubMesh>());
  121. MeshDataPtr meshData = parseMesh(mesh, allSubMeshes.back());
  122. allMeshes.push_back(meshData);
  123. // TODO - Transform meshes based on node transform
  124. }
  125. break;
  126. case FbxNodeAttribute::eSkeleton:
  127. break; // TODO - I should probably implement skeleton parsing
  128. }
  129. }
  130. for(int i = 0; i < curNode->GetChildCount(); i++)
  131. todo.push(curNode->GetChild(i));
  132. }
  133. if(allMeshes.size() == 0)
  134. return nullptr;
  135. else if(allMeshes.size() == 1)
  136. {
  137. subMeshes = allSubMeshes[0];
  138. return allMeshes[0];
  139. }
  140. else
  141. {
  142. MeshDataPtr combinedMeshData = MeshData::combine(allMeshes, allSubMeshes, subMeshes);
  143. return combinedMeshData;
  144. }
  145. }
  146. MeshDataPtr FBXImporter::parseMesh(FbxMesh* mesh, Vector<SubMesh>& subMeshes, bool createTangentsIfMissing)
  147. {
  148. if (!mesh->GetNode())
  149. {
  150. VertexDataDescPtr tmpVertDesc = bs_shared_ptr<VertexDataDesc>();
  151. return bs_shared_ptr<MeshData, ScratchAlloc>(0, 0, tmpVertDesc);
  152. }
  153. // Find out which vertex attributes exist
  154. bool allByControlPoint = true;
  155. bool hasColor = mesh->GetElementVertexColorCount() > 0;
  156. FbxGeometryElement::EMappingMode lColorMappingMode = FbxGeometryElement::eNone;
  157. if(hasColor)
  158. {
  159. lColorMappingMode = mesh->GetElementVertexColor(0)->GetMappingMode();
  160. if (lColorMappingMode == FbxGeometryElement::eNone)
  161. {
  162. hasColor = false;
  163. }
  164. if (hasColor && lColorMappingMode != FbxGeometryElement::eByControlPoint)
  165. {
  166. allByControlPoint = false;
  167. }
  168. }
  169. bool hasNormal = mesh->GetElementNormalCount() > 0;
  170. FbxGeometryElement::EMappingMode lNormalMappingMode = FbxGeometryElement::eNone;
  171. if (hasNormal)
  172. {
  173. lNormalMappingMode = mesh->GetElementNormal(0)->GetMappingMode();
  174. if (lNormalMappingMode == FbxGeometryElement::eNone)
  175. {
  176. hasNormal = false;
  177. }
  178. if (hasNormal && lNormalMappingMode != FbxGeometryElement::eByControlPoint)
  179. {
  180. allByControlPoint = false;
  181. }
  182. }
  183. bool hasTangent = mesh->GetElementTangentCount() > 0;
  184. FbxGeometryElement::EMappingMode lTangentMappingMode = FbxGeometryElement::eNone;
  185. if (hasTangent)
  186. {
  187. lTangentMappingMode = mesh->GetElementTangent(0)->GetMappingMode();
  188. if (lTangentMappingMode == FbxGeometryElement::eNone)
  189. {
  190. hasTangent = false;
  191. }
  192. if (hasTangent && lTangentMappingMode != FbxGeometryElement::eByControlPoint)
  193. {
  194. allByControlPoint = false;
  195. }
  196. }
  197. bool hasBitangent = mesh->GetElementBinormalCount() > 0;
  198. FbxGeometryElement::EMappingMode lBitangentMappingMode = FbxGeometryElement::eNone;
  199. if (hasBitangent)
  200. {
  201. lBitangentMappingMode = mesh->GetElementBinormal(0)->GetMappingMode();
  202. if (lBitangentMappingMode == FbxGeometryElement::eNone)
  203. {
  204. hasBitangent = false;
  205. }
  206. if (hasBitangent && lBitangentMappingMode != FbxGeometryElement::eByControlPoint)
  207. {
  208. allByControlPoint = false;
  209. }
  210. }
  211. bool hasUV0 = mesh->GetElementUVCount() > 0;
  212. FbxGeometryElement::EMappingMode lUVMappingMode0 = FbxGeometryElement::eNone;
  213. if (hasUV0)
  214. {
  215. lUVMappingMode0 = mesh->GetElementUV(0)->GetMappingMode();
  216. if (lUVMappingMode0 == FbxGeometryElement::eNone)
  217. hasUV0 = false;
  218. if (hasUV0 && lUVMappingMode0 != FbxGeometryElement::eByControlPoint)
  219. allByControlPoint = false;
  220. }
  221. bool hasUV1 = mesh->GetElementUVCount() > 1;
  222. FbxGeometryElement::EMappingMode lUVMappingMode1 = FbxGeometryElement::eNone;
  223. if (hasUV1)
  224. {
  225. lUVMappingMode1 = mesh->GetElementUV(1)->GetMappingMode();
  226. if (lUVMappingMode1 == FbxGeometryElement::eNone)
  227. hasUV1 = false;
  228. if (hasUV1 && lUVMappingMode1 != FbxGeometryElement::eByControlPoint)
  229. allByControlPoint = false;
  230. }
  231. // Create tangents if needed
  232. if(createTangentsIfMissing && mesh->GetElementUVCount() > 0)
  233. mesh->GenerateTangentsData(0, false);
  234. // Calculate number of vertices and indexes
  235. const int lPolygonCount = mesh->GetPolygonCount();
  236. int lPolygonVertexCount = mesh->GetControlPointsCount();
  237. if (!allByControlPoint)
  238. lPolygonVertexCount = lPolygonCount * 3;
  239. UINT32 vertexCount = lPolygonVertexCount;
  240. VertexDataDescPtr vertexDesc = bs_shared_ptr<VertexDataDesc>();
  241. vertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  242. if(hasColor)
  243. vertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  244. if(hasNormal)
  245. vertexDesc->addVertElem(VET_FLOAT3, VES_NORMAL);
  246. if(hasTangent)
  247. vertexDesc->addVertElem(VET_FLOAT3, VES_TANGENT);
  248. if(hasBitangent)
  249. vertexDesc->addVertElem(VET_FLOAT3, VES_BITANGENT);
  250. FbxStringList lUVNames;
  251. mesh->GetUVSetNames(lUVNames);
  252. const char * lUVName0 = NULL;
  253. if (hasUV0 && lUVNames.GetCount() > 0)
  254. {
  255. vertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD, 0);
  256. lUVName0 = lUVNames[0];
  257. }
  258. const char * lUVName1 = NULL;
  259. if (hasUV1 && lUVNames.GetCount() > 1)
  260. {
  261. vertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD, 1);
  262. lUVName1 = lUVNames[1];
  263. }
  264. // Count the polygon count of each material
  265. FbxLayerElementArrayTemplate<int>* lMaterialIndice = NULL;
  266. FbxGeometryElement::EMappingMode lMaterialMappingMode = FbxGeometryElement::eNone;
  267. UINT32 numIndices = 0;
  268. if (mesh->GetElementMaterial())
  269. {
  270. lMaterialIndice = &mesh->GetElementMaterial()->GetIndexArray();
  271. lMaterialMappingMode = mesh->GetElementMaterial()->GetMappingMode();
  272. if (lMaterialIndice && lMaterialMappingMode == FbxGeometryElement::eByPolygon)
  273. {
  274. FBX_ASSERT(lMaterialIndice->GetCount() == lPolygonCount);
  275. if (lMaterialIndice->GetCount() == lPolygonCount)
  276. {
  277. // Count the faces of each material
  278. for (int lPolygonIndex = 0; lPolygonIndex < lPolygonCount; ++lPolygonIndex)
  279. {
  280. const UINT32 lMaterialIndex = (UINT32)lMaterialIndice->GetAt(lPolygonIndex);
  281. if (subMeshes.size() < lMaterialIndex + 1)
  282. {
  283. subMeshes.resize(lMaterialIndex + 1);
  284. }
  285. subMeshes[lMaterialIndex].indexCount += 3;
  286. }
  287. // Record the offsets and allocate index arrays
  288. const int lMaterialCount = (const int)subMeshes.size();
  289. int lOffset = 0;
  290. for (int lIndex = 0; lIndex < lMaterialCount; ++lIndex)
  291. {
  292. subMeshes[lIndex].indexOffset = lOffset;
  293. lOffset += subMeshes[lIndex].indexCount;
  294. numIndices += subMeshes[lIndex].indexCount;
  295. }
  296. FBX_ASSERT(lOffset == lPolygonCount * 3);
  297. }
  298. }
  299. }
  300. // All faces will use the same material.
  301. if (subMeshes.size() == 0)
  302. {
  303. numIndices = lPolygonCount * 3;
  304. subMeshes.resize(1);
  305. subMeshes[0].indexCount = lPolygonCount * 3;
  306. }
  307. MeshDataPtr meshData = bs_shared_ptr<MeshData, ScratchAlloc>(vertexCount, numIndices, vertexDesc);
  308. // Allocate the array memory, by control point or by polygon vertex.
  309. VertexElemIter<Vector3> positions = meshData->getVec3DataIter(VES_POSITION);
  310. VertexElemIter<UINT32> colors;
  311. if(hasColor)
  312. colors = meshData->getDWORDDataIter(VES_COLOR);
  313. VertexElemIter<Vector3> normals;
  314. if (hasNormal)
  315. normals = meshData->getVec3DataIter(VES_NORMAL);
  316. VertexElemIter<Vector3> tangents;
  317. if (hasTangent)
  318. tangents = meshData->getVec3DataIter(VES_TANGENT);
  319. VertexElemIter<Vector3> bitangents;
  320. if (hasBitangent)
  321. bitangents = meshData->getVec3DataIter(VES_BITANGENT);
  322. VertexElemIter<Vector2> uv0;
  323. if (hasUV0 && lUVNames.GetCount() > 0)
  324. {
  325. uv0 = meshData->getVec2DataIter(VES_TEXCOORD, 0);
  326. lUVName0 = lUVNames[0];
  327. }
  328. VertexElemIter<Vector2> uv1;
  329. if (hasUV1 && lUVNames.GetCount() > 1)
  330. {
  331. uv1 = meshData->getVec2DataIter(VES_TEXCOORD, 1);
  332. lUVName1 = lUVNames[1];
  333. }
  334. // Populate the array with vertex attribute, if by control point.
  335. const FbxVector4 * lControlPoints = mesh->GetControlPoints();
  336. FbxVector4 lCurrentVertex;
  337. FbxVector4 lCurrentNormal;
  338. FbxVector2 lCurrentUV;
  339. const FbxGeometryElementVertexColor * lColorElement = NULL;
  340. if (hasColor)
  341. lColorElement = mesh->GetElementVertexColor(0);
  342. const FbxGeometryElementTangent * lTangentElement = NULL;
  343. if (hasTangent)
  344. lTangentElement = mesh->GetElementTangent(0);
  345. const FbxGeometryElementBinormal * lBitangentElement = NULL;
  346. if (hasBitangent)
  347. lBitangentElement = mesh->GetElementBinormal(0);
  348. if (allByControlPoint)
  349. {
  350. const FbxGeometryElementNormal * lNormalElement = NULL;
  351. if (hasNormal)
  352. lNormalElement = mesh->GetElementNormal(0);
  353. const FbxGeometryElementUV * lUVElement0 = NULL;
  354. if (hasUV0)
  355. lUVElement0 = mesh->GetElementUV(0);
  356. const FbxGeometryElementUV * lUVElement1 = NULL;
  357. if (hasUV1)
  358. lUVElement1 = mesh->GetElementUV(1);
  359. for (int lIndex = 0; lIndex < lPolygonVertexCount; ++lIndex)
  360. {
  361. // Save the vertex position.
  362. lCurrentVertex = lControlPoints[lIndex];
  363. Vector3 curPosValue;
  364. curPosValue[0] = static_cast<float>(lCurrentVertex[0]);
  365. curPosValue[1] = static_cast<float>(lCurrentVertex[1]);
  366. curPosValue[2] = static_cast<float>(lCurrentVertex[2]);
  367. positions.addValue(curPosValue);
  368. // Save vertex color
  369. if(hasColor)
  370. {
  371. int lColorIndex = lIndex;
  372. if (lColorElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  373. lColorIndex = lColorElement->GetIndexArray().GetAt(lIndex);
  374. FbxColor lCurrentColor = lColorElement->GetDirectArray().GetAt(lColorIndex);
  375. Color curColorValue;
  376. curColorValue[0] = static_cast<float>(lCurrentColor[0]);
  377. curColorValue[1] = static_cast<float>(lCurrentColor[1]);
  378. curColorValue[2] = static_cast<float>(lCurrentColor[2]);
  379. curColorValue[3] = static_cast<float>(lCurrentColor[3]);
  380. UINT32 color32 = curColorValue.getAsRGBA();
  381. colors.addValue(color32);
  382. }
  383. // Save the normal.
  384. if (hasNormal)
  385. {
  386. int lNormalIndex = lIndex;
  387. if (lNormalElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  388. lNormalIndex = lNormalElement->GetIndexArray().GetAt(lIndex);
  389. lCurrentNormal = lNormalElement->GetDirectArray().GetAt(lNormalIndex);
  390. Vector3 curNormalValue;
  391. curNormalValue[0] = static_cast<float>(lCurrentNormal[0]);
  392. curNormalValue[1] = static_cast<float>(lCurrentNormal[1]);
  393. curNormalValue[2] = static_cast<float>(lCurrentNormal[2]);
  394. normals.addValue(curNormalValue);
  395. }
  396. // Save the tangent.
  397. if (hasTangent)
  398. {
  399. int lTangentIndex = lIndex;
  400. if (lTangentElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  401. lTangentIndex = lTangentElement->GetIndexArray().GetAt(lIndex);
  402. FbxVector4 lCurrentTangent = lTangentElement->GetDirectArray().GetAt(lTangentIndex);
  403. Vector3 curTangentValue;
  404. curTangentValue[0] = static_cast<float>(lCurrentTangent[0]);
  405. curTangentValue[1] = static_cast<float>(lCurrentTangent[1]);
  406. curTangentValue[2] = static_cast<float>(lCurrentTangent[2]);
  407. tangents.addValue(curTangentValue);
  408. }
  409. // Save the bitangent.
  410. if (hasBitangent)
  411. {
  412. int lBitangentIndex = lIndex;
  413. if (lBitangentElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  414. lBitangentIndex = lBitangentElement->GetIndexArray().GetAt(lIndex);
  415. FbxVector4 lCurrentBitangent = lBitangentElement->GetDirectArray().GetAt(lBitangentIndex);
  416. Vector3 curBitangentValue;
  417. curBitangentValue[0] = static_cast<float>(lCurrentBitangent[0]);
  418. curBitangentValue[1] = static_cast<float>(lCurrentBitangent[1]);
  419. curBitangentValue[2] = static_cast<float>(lCurrentBitangent[2]);
  420. bitangents.addValue(curBitangentValue);
  421. }
  422. // Save the UV.
  423. if (hasUV0)
  424. {
  425. int lUVIndex = lIndex;
  426. if (lUVElement0->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  427. lUVIndex = lUVElement0->GetIndexArray().GetAt(lIndex);
  428. lCurrentUV = lUVElement0->GetDirectArray().GetAt(lUVIndex);
  429. Vector2 curUV0Value;
  430. curUV0Value[0] = static_cast<float>(lCurrentUV[0]);
  431. curUV0Value[1] = static_cast<float>(lCurrentUV[1]);
  432. uv0.addValue(curUV0Value);
  433. }
  434. if (hasUV1)
  435. {
  436. int lUVIndex = lIndex;
  437. if (lUVElement1->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  438. lUVIndex = lUVElement1->GetIndexArray().GetAt(lIndex);
  439. lCurrentUV = lUVElement1->GetDirectArray().GetAt(lUVIndex);
  440. Vector2 curUV1Value;
  441. curUV1Value[0] = static_cast<float>(lCurrentUV[0]);
  442. curUV1Value[1] = static_cast<float>(lCurrentUV[1]);
  443. uv1.addValue(curUV1Value);
  444. }
  445. }
  446. }
  447. Vector<UINT32> indexOffsetPerSubmesh;
  448. indexOffsetPerSubmesh.resize(subMeshes.size(), 0);
  449. Vector<UINT32*> indices;
  450. indices.resize(subMeshes.size());
  451. for(UINT32 i = 0; i < (UINT32)indices.size(); i++)
  452. {
  453. indices[i] = meshData->getIndices32() + subMeshes[i].indexOffset;
  454. }
  455. UINT32 lVertexCount = 0;
  456. for (int lPolygonIndex = 0; lPolygonIndex < lPolygonCount; ++lPolygonIndex)
  457. {
  458. // The material for current face.
  459. int lMaterialIndex = 0;
  460. if (lMaterialIndice && lMaterialMappingMode == FbxGeometryElement::eByPolygon)
  461. {
  462. lMaterialIndex = lMaterialIndice->GetAt(lPolygonIndex);
  463. }
  464. // Where should I save the vertex attribute index, according to the material
  465. int lIndexOffset = subMeshes[lMaterialIndex].indexOffset + indexOffsetPerSubmesh[lMaterialIndex];
  466. for (int lVerticeIndex = 0; lVerticeIndex < 3; ++lVerticeIndex)
  467. {
  468. const int lControlPointIndex = mesh->GetPolygonVertex(lPolygonIndex, lVerticeIndex);
  469. if (allByControlPoint)
  470. {
  471. indices[lMaterialIndex][lIndexOffset + lVerticeIndex] = static_cast<unsigned int>(lControlPointIndex);
  472. }
  473. // Populate the array with vertex attribute, if by polygon vertex.
  474. else
  475. {
  476. indices[lMaterialIndex][lIndexOffset + lVerticeIndex] = static_cast<unsigned int>(lVertexCount);
  477. lCurrentVertex = lControlPoints[lControlPointIndex];
  478. Vector3 curPosValue;
  479. curPosValue[0] = static_cast<float>(lCurrentVertex[0]);
  480. curPosValue[1] = static_cast<float>(lCurrentVertex[1]);
  481. curPosValue[2] = static_cast<float>(lCurrentVertex[2]);
  482. positions.addValue(curPosValue);
  483. if(hasColor)
  484. {
  485. int lColorIndex = lIndexOffset + lVerticeIndex; // TODO - Is this right?
  486. if (lColorElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  487. lColorIndex = lColorElement->GetIndexArray().GetAt(lColorIndex);
  488. FbxColor lCurrentColor = lColorElement->GetDirectArray().GetAt(lColorIndex);
  489. Color curColorValue;
  490. curColorValue[0] = static_cast<float>(lCurrentColor[0]);
  491. curColorValue[1] = static_cast<float>(lCurrentColor[1]);
  492. curColorValue[2] = static_cast<float>(lCurrentColor[2]);
  493. curColorValue[3] = static_cast<float>(lCurrentColor[3]);
  494. UINT32 color32 = curColorValue.getAsRGBA();
  495. colors.addValue(color32);
  496. }
  497. if (hasNormal)
  498. {
  499. mesh->GetPolygonVertexNormal(lPolygonIndex, lVerticeIndex, lCurrentNormal);
  500. Vector3 curNormalValue;
  501. curNormalValue[0] = static_cast<float>(lCurrentNormal[0]);
  502. curNormalValue[1] = static_cast<float>(lCurrentNormal[1]);
  503. curNormalValue[2] = static_cast<float>(lCurrentNormal[2]);
  504. normals.addValue(curNormalValue);
  505. }
  506. if (hasTangent)
  507. {
  508. int lTangentIndex = lIndexOffset + lVerticeIndex; // TODO - Is this right?
  509. if (lTangentElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  510. lTangentIndex = lTangentElement->GetIndexArray().GetAt(lTangentIndex);
  511. FbxVector4 lCurrentTangent = lTangentElement->GetDirectArray().GetAt(lTangentIndex);
  512. Vector3 curTangentValue;
  513. curTangentValue[0] = static_cast<float>(lCurrentTangent[0]);
  514. curTangentValue[1] = static_cast<float>(lCurrentTangent[1]);
  515. curTangentValue[2] = static_cast<float>(lCurrentTangent[2]);
  516. tangents.addValue(curTangentValue);
  517. }
  518. if (hasBitangent)
  519. {
  520. int lBitangentIndex = lIndexOffset + lVerticeIndex; // TODO - Is this right?
  521. if (lBitangentElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  522. lBitangentIndex = lBitangentElement->GetIndexArray().GetAt(lBitangentIndex);
  523. FbxVector4 lCurrentBitangent = lBitangentElement->GetDirectArray().GetAt(lBitangentIndex);
  524. Vector3 curBitangentValue;
  525. curBitangentValue[0] = static_cast<float>(lCurrentBitangent[0]);
  526. curBitangentValue[1] = static_cast<float>(lCurrentBitangent[1]);
  527. curBitangentValue[2] = static_cast<float>(lCurrentBitangent[2]);
  528. bitangents.addValue(curBitangentValue);
  529. }
  530. if (hasUV0)
  531. {
  532. bool unmapped = false;
  533. mesh->GetPolygonVertexUV(lPolygonIndex, lVerticeIndex, lUVName0, lCurrentUV, unmapped);
  534. Vector2 curUV0Value;
  535. curUV0Value[0] = static_cast<float>(lCurrentUV[0]);
  536. curUV0Value[1] = static_cast<float>(lCurrentUV[1]);
  537. uv0.addValue(curUV0Value);
  538. }
  539. if (hasUV1)
  540. {
  541. bool unmapped = false;
  542. mesh->GetPolygonVertexUV(lPolygonIndex, lVerticeIndex, lUVName1, lCurrentUV, unmapped);
  543. Vector2 curUV1Value;
  544. curUV1Value[0] = static_cast<float>(lCurrentUV[0]);
  545. curUV1Value[1] = static_cast<float>(lCurrentUV[1]);
  546. uv1.addValue(curUV1Value);
  547. }
  548. }
  549. ++lVertexCount;
  550. }
  551. indexOffsetPerSubmesh[lMaterialIndex] += 3;
  552. }
  553. return meshData;
  554. }
  555. }