CmFBXImporter.cpp 21 KB

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