CmFBXImporter.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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.synchronize();
  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<UINT32> colors;
  300. if(hasColor)
  301. colors = meshData->getDWORDDataIter(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. UINT32 color32 = curColorValue.getAsRGBA();
  370. colors.addValue(color32);
  371. }
  372. // Save the normal.
  373. if (hasNormal)
  374. {
  375. int lNormalIndex = lIndex;
  376. if (lNormalElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  377. lNormalIndex = lNormalElement->GetIndexArray().GetAt(lIndex);
  378. lCurrentNormal = lNormalElement->GetDirectArray().GetAt(lNormalIndex);
  379. Vector3 curNormalValue;
  380. curNormalValue[0] = static_cast<float>(lCurrentNormal[0]);
  381. curNormalValue[1] = static_cast<float>(lCurrentNormal[1]);
  382. curNormalValue[2] = static_cast<float>(lCurrentNormal[2]);
  383. normals.addValue(curNormalValue);
  384. }
  385. // Save the tangent.
  386. if (hasTangent)
  387. {
  388. int lTangentIndex = lIndex;
  389. if (lTangentElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  390. lTangentIndex = lTangentElement->GetIndexArray().GetAt(lIndex);
  391. FbxVector4 lCurrentTangent = lTangentElement->GetDirectArray().GetAt(lTangentIndex);
  392. Vector3 curTangentValue;
  393. curTangentValue[0] = static_cast<float>(lCurrentTangent[0]);
  394. curTangentValue[1] = static_cast<float>(lCurrentTangent[1]);
  395. curTangentValue[2] = static_cast<float>(lCurrentTangent[2]);
  396. tangents.addValue(curTangentValue);
  397. }
  398. // Save the bitangent.
  399. if (hasBitangent)
  400. {
  401. int lBitangentIndex = lIndex;
  402. if (lBitangentElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  403. lBitangentIndex = lBitangentElement->GetIndexArray().GetAt(lIndex);
  404. FbxVector4 lCurrentBitangent = lBitangentElement->GetDirectArray().GetAt(lBitangentIndex);
  405. Vector3 curBitangentValue;
  406. curBitangentValue[0] = static_cast<float>(lCurrentBitangent[0]);
  407. curBitangentValue[1] = static_cast<float>(lCurrentBitangent[1]);
  408. curBitangentValue[2] = static_cast<float>(lCurrentBitangent[2]);
  409. bitangents.addValue(curBitangentValue);
  410. }
  411. // Save the UV.
  412. if (hasUV0)
  413. {
  414. int lUVIndex = lIndex;
  415. if (lUVElement0->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  416. lUVIndex = lUVElement0->GetIndexArray().GetAt(lIndex);
  417. lCurrentUV = lUVElement0->GetDirectArray().GetAt(lUVIndex);
  418. Vector2 curUV0Value;
  419. curUV0Value[0] = static_cast<float>(lCurrentUV[0]);
  420. curUV0Value[1] = static_cast<float>(lCurrentUV[1]);
  421. uv0.addValue(curUV0Value);
  422. }
  423. if (hasUV1)
  424. {
  425. int lUVIndex = lIndex;
  426. if (lUVElement1->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  427. lUVIndex = lUVElement1->GetIndexArray().GetAt(lIndex);
  428. lCurrentUV = lUVElement1->GetDirectArray().GetAt(lUVIndex);
  429. Vector2 curUV1Value;
  430. curUV1Value[0] = static_cast<float>(lCurrentUV[0]);
  431. curUV1Value[1] = static_cast<float>(lCurrentUV[1]);
  432. uv1.addValue(curUV1Value);
  433. }
  434. }
  435. }
  436. Vector<UINT32>::type indexOffsetPerSubmesh;
  437. indexOffsetPerSubmesh.resize(subMeshes.size(), 0);
  438. Vector<UINT32*>::type indices;
  439. indices.resize(subMeshes.size());
  440. for(UINT32 i = 0; i < (UINT32)indices.size(); i++)
  441. {
  442. indices[i] = meshData->getIndices32(i);
  443. }
  444. UINT32 lVertexCount = 0;
  445. for (int lPolygonIndex = 0; lPolygonIndex < lPolygonCount; ++lPolygonIndex)
  446. {
  447. // The material for current face.
  448. int lMaterialIndex = 0;
  449. if (lMaterialIndice && lMaterialMappingMode == FbxGeometryElement::eByPolygon)
  450. {
  451. lMaterialIndex = lMaterialIndice->GetAt(lPolygonIndex);
  452. }
  453. // Where should I save the vertex attribute index, according to the material
  454. int lIndexOffset = subMeshes[lMaterialIndex].indexOffset + indexOffsetPerSubmesh[lMaterialIndex];
  455. for (int lVerticeIndex = 0; lVerticeIndex < 3; ++lVerticeIndex)
  456. {
  457. const int lControlPointIndex = mesh->GetPolygonVertex(lPolygonIndex, lVerticeIndex);
  458. if (allByControlPoint)
  459. {
  460. indices[lMaterialIndex][lIndexOffset + lVerticeIndex] = static_cast<unsigned int>(lControlPointIndex);
  461. }
  462. // Populate the array with vertex attribute, if by polygon vertex.
  463. else
  464. {
  465. indices[lMaterialIndex][lIndexOffset + lVerticeIndex] = static_cast<unsigned int>(lVertexCount);
  466. lCurrentVertex = lControlPoints[lControlPointIndex];
  467. Vector3 curPosValue;
  468. curPosValue[0] = static_cast<float>(lCurrentVertex[0]);
  469. curPosValue[1] = static_cast<float>(lCurrentVertex[1]);
  470. curPosValue[2] = static_cast<float>(lCurrentVertex[2]);
  471. positions.addValue(curPosValue);
  472. if(hasColor)
  473. {
  474. int lColorIndex = lIndexOffset + lVerticeIndex; // TODO - Is this right?
  475. if (lColorElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  476. lColorIndex = lColorElement->GetIndexArray().GetAt(lColorIndex);
  477. FbxColor lCurrentColor = lColorElement->GetDirectArray().GetAt(lColorIndex);
  478. Color curColorValue;
  479. curColorValue[0] = static_cast<float>(lCurrentColor[0]);
  480. curColorValue[1] = static_cast<float>(lCurrentColor[1]);
  481. curColorValue[2] = static_cast<float>(lCurrentColor[2]);
  482. curColorValue[3] = static_cast<float>(lCurrentColor[3]);
  483. UINT32 color32 = curColorValue.getAsRGBA();
  484. colors.addValue(color32);
  485. }
  486. if (hasNormal)
  487. {
  488. mesh->GetPolygonVertexNormal(lPolygonIndex, lVerticeIndex, lCurrentNormal);
  489. Vector3 curNormalValue;
  490. curNormalValue[0] = static_cast<float>(lCurrentNormal[0]);
  491. curNormalValue[1] = static_cast<float>(lCurrentNormal[1]);
  492. curNormalValue[2] = static_cast<float>(lCurrentNormal[2]);
  493. normals.addValue(curNormalValue);
  494. }
  495. if (hasTangent)
  496. {
  497. int lTangentIndex = lIndexOffset + lVerticeIndex; // TODO - Is this right?
  498. if (lTangentElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  499. lTangentIndex = lTangentElement->GetIndexArray().GetAt(lTangentIndex);
  500. FbxVector4 lCurrentTangent = lTangentElement->GetDirectArray().GetAt(lTangentIndex);
  501. Vector3 curTangentValue;
  502. curTangentValue[0] = static_cast<float>(lCurrentTangent[0]);
  503. curTangentValue[1] = static_cast<float>(lCurrentTangent[1]);
  504. curTangentValue[2] = static_cast<float>(lCurrentTangent[2]);
  505. tangents.addValue(curTangentValue);
  506. }
  507. if (hasBitangent)
  508. {
  509. int lBitangentIndex = lIndexOffset + lVerticeIndex; // TODO - Is this right?
  510. if (lBitangentElement->GetReferenceMode() == FbxLayerElement::eIndexToDirect)
  511. lBitangentIndex = lBitangentElement->GetIndexArray().GetAt(lBitangentIndex);
  512. FbxVector4 lCurrentBitangent = lBitangentElement->GetDirectArray().GetAt(lBitangentIndex);
  513. Vector3 curBitangentValue;
  514. curBitangentValue[0] = static_cast<float>(lCurrentBitangent[0]);
  515. curBitangentValue[1] = static_cast<float>(lCurrentBitangent[1]);
  516. curBitangentValue[2] = static_cast<float>(lCurrentBitangent[2]);
  517. bitangents.addValue(curBitangentValue);
  518. }
  519. if (hasUV0)
  520. {
  521. mesh->GetPolygonVertexUV(lPolygonIndex, lVerticeIndex, lUVName0, lCurrentUV);
  522. Vector2 curUV0Value;
  523. curUV0Value[0] = static_cast<float>(lCurrentUV[0]);
  524. curUV0Value[1] = static_cast<float>(lCurrentUV[1]);
  525. uv0.addValue(curUV0Value);
  526. }
  527. if (hasUV1)
  528. {
  529. mesh->GetPolygonVertexUV(lPolygonIndex, lVerticeIndex, lUVName1, lCurrentUV);
  530. Vector2 curUV1Value;
  531. curUV1Value[0] = static_cast<float>(lCurrentUV[0]);
  532. curUV1Value[1] = static_cast<float>(lCurrentUV[1]);
  533. uv1.addValue(curUV1Value);
  534. }
  535. }
  536. ++lVertexCount;
  537. }
  538. indexOffsetPerSubmesh[lMaterialIndex] += 3;
  539. }
  540. return meshData;
  541. }
  542. }