CmFBXImporter.cpp 21 KB

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