CmFBXImporter.cpp 21 KB

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