FBXReader.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. #include "FBXReader.h"
  2. #include "BFApp.h"
  3. #include "gfx/RenderDevice.h"
  4. #include "gfx/DrawLayer.h"
  5. #include "gfx/ModelInstance.h"
  6. #include "FileStream.h"
  7. #ifndef BF_NO_FBX
  8. #include "boost/unordered_map.hpp"
  9. #pragma warning(disable:4996)
  10. USING_NS_BF;
  11. namespace boost
  12. {
  13. static inline std::size_t hash_value(const FBXVertexData& vtxData)
  14. {
  15. uint32* data = (uint32*) &vtxData.mCoords;
  16. return (data[0] ^ data[1] ^ data[2]);
  17. }
  18. }
  19. #pragma comment(lib, "libfbxsdk.lib")
  20. #ifdef IOS_REF
  21. #undef IOS_REF
  22. #define IOS_REF (*(pManager->GetIOSettings()))
  23. #endif
  24. bool FBXIsValidMatrix(const FbxAMatrix& mat)
  25. {
  26. // Check that scales aren't zero and quaternions don't have //1.#QNAN0 values
  27. if (mat.GetQ()[0] != mat.GetQ()[0] ||
  28. mat.GetS()[0] == 0)
  29. {
  30. return false;
  31. }
  32. return true;
  33. }
  34. FbxAMatrix FBXCorrectMatrix(const FbxAMatrix& mat)
  35. {
  36. if (!FBXIsValidMatrix(mat))
  37. {
  38. /*FxOgreFBXLog("Detected invalid matrix:\n");
  39. logMatrix(mat);
  40. FxOgreFBXLog("Returning identity matrix:\n");*/
  41. return FbxAMatrix();
  42. }
  43. return mat;
  44. }
  45. static void InitializeSdkObjects(FbxManager*& pManager, FbxScene*& pScene)
  46. {
  47. //The first thing to do is to create the FBX Manager which is the object allocator for almost all the classes in the SDK
  48. pManager = FbxManager::Create();
  49. if (!pManager)
  50. {
  51. FBXSDK_printf("Error: Unable to create FBX Manager!\n");
  52. exit(1);
  53. }
  54. else FBXSDK_printf("Autodesk FBX SDK version %s\n", pManager->GetVersion());
  55. //Create an IOSettings object. This object holds all import/export settings.
  56. FbxIOSettings* ios = FbxIOSettings::Create(pManager, IOSROOT);
  57. pManager->SetIOSettings(ios);
  58. //Load plugins from the executable directory (optional)
  59. FbxString lPath = FbxGetApplicationDirectory();
  60. pManager->LoadPluginsDirectory(lPath.Buffer());
  61. //Create an FBX scene. This object holds most objects imported/exported from/to files.
  62. pScene = FbxScene::Create(pManager, "My Scene");
  63. if (!pScene)
  64. {
  65. FBXSDK_printf("Error: Unable to create FBX scene!\n");
  66. exit(1);
  67. }
  68. }
  69. static void DestroySdkObjects(FbxManager* pManager, bool pExitStatus)
  70. {
  71. //Delete the FBX Manager. All the objects that have been allocated using the FBX Manager and that haven't been explicitly destroyed are also automatically destroyed.
  72. if (pManager) pManager->Destroy();
  73. if (pExitStatus) FBXSDK_printf("Program Success!\n");
  74. }
  75. static bool LoadScene(FbxManager* pManager, FbxDocument* pScene, const char* pFilename, FbxImporter* pImporter)
  76. {
  77. int lFileMajor, lFileMinor, lFileRevision;
  78. int lSDKMajor, lSDKMinor, lSDKRevision;
  79. //int lFileFormat = -1;
  80. int i, lAnimStackCount;
  81. bool lStatus;
  82. char lPassword[1024];
  83. // Get the file version number generate by the FBX SDK.
  84. FbxManager::GetFileFormatVersion(lSDKMajor, lSDKMinor, lSDKRevision);
  85. // Create an importer.
  86. //FbxImporter* pImporter = FbxImporter::Create(pManager, "");
  87. // Initialize the importer by providing a filename.
  88. const bool lImportStatus = pImporter->Initialize(pFilename, -1, pManager->GetIOSettings());
  89. pImporter->GetFileVersion(lFileMajor, lFileMinor, lFileRevision);
  90. if (!lImportStatus)
  91. {
  92. FbxString error = pImporter->GetStatus().GetErrorString();
  93. FBXSDK_printf("Call to FbxImporter::Initialize() failed.\n");
  94. FBXSDK_printf("Error returned: %s\n\n", error.Buffer());
  95. if (pImporter->GetStatus().GetCode() == FbxStatus::eInvalidFileVersion)
  96. {
  97. FBXSDK_printf("FBX file format version for this FBX SDK is %d.%d.%d\n", lSDKMajor, lSDKMinor, lSDKRevision);
  98. FBXSDK_printf("FBX file format version for file '%s' is %d.%d.%d\n\n", pFilename, lFileMajor, lFileMinor, lFileRevision);
  99. }
  100. return false;
  101. }
  102. FBXSDK_printf("FBX file format version for this FBX SDK is %d.%d.%d\n", lSDKMajor, lSDKMinor, lSDKRevision);
  103. if (pImporter->IsFBX())
  104. {
  105. FBXSDK_printf("FBX file format version for file '%s' is %d.%d.%d\n\n", pFilename, lFileMajor, lFileMinor, lFileRevision);
  106. // From this point, it is possible to access animation stack information without
  107. // the expense of loading the entire file.
  108. FBXSDK_printf("Animation Stack Information\n");
  109. lAnimStackCount = pImporter->GetAnimStackCount();
  110. FBXSDK_printf(" Number of Animation Stacks: %d\n", lAnimStackCount);
  111. FBXSDK_printf(" Current Animation Stack: \"%s\"\n", pImporter->GetActiveAnimStackName().Buffer());
  112. FBXSDK_printf("\n");
  113. for (i = 0; i < lAnimStackCount; i++)
  114. {
  115. FbxTakeInfo* lTakeInfo = pImporter->GetTakeInfo(i);
  116. FBXSDK_printf(" Animation Stack %d\n", i);
  117. FBXSDK_printf(" Name: \"%s\"\n", lTakeInfo->mName.Buffer());
  118. FBXSDK_printf(" Description: \"%s\"\n", lTakeInfo->mDescription.Buffer());
  119. // Change the value of the import name if the animation stack should be imported
  120. // under a different name.
  121. FBXSDK_printf(" Import Name: \"%s\"\n", lTakeInfo->mImportName.Buffer());
  122. // Set the value of the import state to false if the animation stack should be not
  123. // be imported.
  124. FBXSDK_printf(" Import State: %s\n", lTakeInfo->mSelect ? "true" : "false");
  125. FBXSDK_printf("\n");
  126. }
  127. // Set the import states. By default, the import states are always set to
  128. // true. The code below shows how to change these states.
  129. IOS_REF.SetBoolProp(IMP_FBX_MATERIAL, true);
  130. IOS_REF.SetBoolProp(IMP_FBX_TEXTURE, true);
  131. IOS_REF.SetBoolProp(IMP_FBX_LINK, true);
  132. IOS_REF.SetBoolProp(IMP_FBX_SHAPE, true);
  133. IOS_REF.SetBoolProp(IMP_FBX_GOBO, true);
  134. IOS_REF.SetBoolProp(IMP_FBX_ANIMATION, true);
  135. IOS_REF.SetBoolProp(IMP_FBX_GLOBAL_SETTINGS, true);
  136. }
  137. // Import the scene.
  138. lStatus = pImporter->Import(pScene);
  139. if (lStatus == false && pImporter->GetStatus().GetCode() == FbxStatus::ePasswordError)
  140. {
  141. FBXSDK_printf("Please enter password: ");
  142. lPassword[0] = '\0';
  143. FBXSDK_CRT_SECURE_NO_WARNING_BEGIN
  144. scanf("%s", lPassword);
  145. FBXSDK_CRT_SECURE_NO_WARNING_END
  146. FbxString lString(lPassword);
  147. IOS_REF.SetStringProp(IMP_FBX_PASSWORD, lString);
  148. IOS_REF.SetBoolProp(IMP_FBX_PASSWORD_ENABLE, true);
  149. lStatus = pImporter->Import(pScene);
  150. if (lStatus == false && pImporter->GetStatus().GetCode() == FbxStatus::ePasswordError)
  151. {
  152. FBXSDK_printf("\nPassword is wrong, import aborted.\n");
  153. }
  154. }
  155. // Destroy the importer.
  156. //pImporter->Destroy();
  157. return lStatus;
  158. }
  159. FBXReader::FBXReader(ModelDef* modelDef)
  160. {
  161. mModelDef = modelDef;
  162. mParamLum = 1.0f;
  163. mParamBindframe = 0;
  164. }
  165. FBXReader::~FBXReader()
  166. {
  167. while (mMeshes.size() > 0)
  168. {
  169. delete mMeshes.back();
  170. mMeshes.pop_back();
  171. }
  172. }
  173. //void FBXReader::TranslateNode
  174. FBXMesh* FBXReader::LoadMesh(FbxNode* fbxNode, FbxMesh* fbxMesh)
  175. {
  176. FBXMesh* mesh = new FBXMesh();
  177. mesh->mName = fbxNode->GetName();
  178. FbxAMatrix nodeTransform = GetBindPose(fbxNode, fbxMesh);
  179. nodeTransform = FBXCorrectMatrix(nodeTransform);
  180. int numSrcVertices = fbxMesh->GetControlPointsCount();
  181. int numFaces = fbxMesh->GetPolygonCount();
  182. int numVertices = numFaces * 3;
  183. int elementMatCount = fbxMesh->GetElementMaterialCount();
  184. int numMaterials = fbxNode->GetMaterialCount();
  185. FBXMaterial* material = &mesh->mMaterial;
  186. for (int matIdx = 0; matIdx < numMaterials; matIdx++)
  187. {
  188. FbxSurfaceMaterial* fbxMaterial = fbxNode->GetMaterial(matIdx);
  189. const char* matName = fbxMaterial->GetName();
  190. for (int channelIdx = 0; true; channelIdx++)
  191. {
  192. FbxProperty prop = fbxMaterial->FindProperty(FbxLayerElement::sTextureChannelNames[channelIdx]);
  193. if (!prop.IsValid())
  194. break;
  195. int texCount = prop.GetSrcObjectCount(FbxTexture::ClassId);
  196. for (int texIdx = 0; texIdx < texCount; texIdx++)
  197. {
  198. FbxTexture* texture = FbxCast<FbxTexture>(prop.GetSrcObject(FbxTexture::ClassId, texIdx));
  199. if (texture != NULL)
  200. {
  201. FbxFileTexture* fileTexture = FbxCast<FbxFileTexture>(texture);
  202. if (fileTexture != NULL)
  203. {
  204. String mediaName = fileTexture->GetMediaName();
  205. String fileExt = "";
  206. int parenPos = (int)mediaName.find(" (");
  207. String origFileName = fileTexture->GetFileName();
  208. int dotPos = (int)origFileName.rfind('.');
  209. if (dotPos != -1)
  210. fileExt = origFileName.substr(dotPos);
  211. //material->mTexFileName = fileName;
  212. /*if (parenPos != -1)
  213. {
  214. String suffix = mediaName.substr(parenPos + 1);
  215. String fileName = mediaName.substr(0, parenPos);
  216. fileName += fileExt;
  217. if (suffix == "(Map)")
  218. material->mTexFileName = fileName;
  219. if (suffix == "(Normal Map Translator)")
  220. material->mBumpFileName = fileName;
  221. }*/
  222. String fileName = fileTexture->GetFileName();
  223. if (material->mTexFileName.length() == 0)
  224. {
  225. int slashPos = std::max((int)fileName.rfind('\\'), (int)fileName.rfind('/'));
  226. if (slashPos > 0)
  227. fileName = fileName.substr(slashPos + 1);
  228. material->mTexFileName = fileName;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. //material->GetTex
  235. int b = 0;
  236. }
  237. /*for (int matIdx = 0; matIdx < matCount; matIdx++)
  238. {
  239. fbxMesh->GetElementMaterial(matIdx);
  240. }*/
  241. std::vector<FBXVertexData> unpackedVtxData;
  242. unpackedVtxData.resize(numVertices);
  243. FbxVector4* controlPoints = fbxMesh->GetControlPoints();
  244. /*std::vector<VertexData> srcVtxData;
  245. srcVtxData.resize(numSrcVertices);
  246. FbxVector4* controlPoints = fbxMesh->GetControlPoints();
  247. for (int i = 0; i < numSrcVertices; i++)
  248. {
  249. VertexData* vtxData = &srcVtxData[i];
  250. FbxVector4 controlPoint = controlPoints[i];
  251. vtxData->mCoords = Vector3((float)controlPoint[0], (float)controlPoint[1], (float)controlPoint[2]);
  252. }*/
  253. for (int uvSetIdx = 0; uvSetIdx < fbxMesh->GetElementUVCount(); uvSetIdx++)
  254. {
  255. FbxGeometryElementUV* elementUV = fbxMesh->GetElementUV(uvSetIdx);
  256. FbxLayerElement::EMappingMode mappingMode = elementUV->GetMappingMode();
  257. if (mappingMode == FbxLayerElement::eByControlPoint)
  258. {
  259. auto directArray = elementUV->GetDirectArray();
  260. int uvLen = directArray.GetCount();
  261. int maxIdx = 0;
  262. /*for (int i = 0; i < uvLen; i++)
  263. {
  264. FBXVertexData* vtxData = &unpackedVtxData[i];
  265. auto texCoords = directArray.GetAt(i);
  266. vtxData->mTexCoords.push_back(TexCoords((float) texCoords[0], (float) texCoords[1]));
  267. }*/
  268. int vtxIdx = 0;
  269. for (int faceIdx = 0; faceIdx < numFaces; faceIdx++)
  270. {
  271. for (int faceVtxIdx = 0; faceVtxIdx < 3; faceVtxIdx++)
  272. {
  273. int controlIdx = fbxMesh->GetPolygonVertex(faceIdx, faceVtxIdx);
  274. FBXVertexData* vtxData = &unpackedVtxData[vtxIdx];
  275. auto texCoords = directArray.GetAt(controlIdx);
  276. vtxData->mTexCoords.push_back(TexCoords((float) texCoords[0], (float) texCoords[1]));
  277. vtxIdx++;
  278. }
  279. }
  280. OutputDebugStrF("Max: %d\n", maxIdx);
  281. }
  282. else if (mappingMode == FbxLayerElement::eByPolygonVertex)
  283. {
  284. auto idxArray = elementUV->GetIndexArray();
  285. int idxLen = idxArray.GetCount();
  286. auto directArray = elementUV->GetDirectArray();
  287. int uvLen = directArray.GetCount();
  288. int maxIdx = 0;
  289. for (int i = 0; i < idxLen; i++)
  290. {
  291. FBXVertexData* vtxData = &unpackedVtxData[i];
  292. int directIdx = idxArray.GetAt(i);
  293. maxIdx = std::max(maxIdx, directIdx);
  294. auto texCoords = directArray.GetAt(directIdx);
  295. vtxData->mTexCoords.push_back(TexCoords((float)texCoords[0], (float)texCoords[1]));
  296. }
  297. OutputDebugStrF("Max: %d\n", maxIdx);
  298. }
  299. else
  300. {
  301. BF_ASSERT("Unsupported UV" == 0);
  302. }
  303. }
  304. if (fbxMesh->GetLayer(0)->GetNormals() == NULL)
  305. {
  306. fbxMesh->InitNormals();
  307. fbxMesh->ComputeVertexNormals();
  308. }
  309. //fbxMesh->InitTangents();
  310. int layerCount = fbxMesh->GetLayerCount();
  311. //auto tangents = fbxMesh->GetLayer(0)->GetTangents();
  312. //auto bionarmals = fbxMesh->GetLayer(0)->GetBinormals();
  313. FbxLayerElementArrayTemplate<FbxVector4>* tangentArray = NULL;
  314. fbxMesh->GetTangents(&tangentArray);
  315. FbxLayerElementArrayTemplate<int>* tangentIndexArray = NULL;
  316. fbxMesh->GetTangentsIndices(&tangentIndexArray);
  317. tangentIndexArray[0];
  318. std::vector<BoneWeightVector> boneWeightsVector;
  319. boneWeightsVector.resize(numSrcVertices);
  320. GetVertexBoneWeights(fbxNode, fbxMesh, boneWeightsVector);
  321. int vtxIdx = 0;
  322. for (int faceIdx = 0; faceIdx < numFaces; faceIdx++)
  323. {
  324. for (int faceVtxIdx = 0; faceVtxIdx < 3; faceVtxIdx++)
  325. {
  326. FbxVector4 normal;
  327. fbxMesh->GetPolygonVertexNormal(faceIdx, faceVtxIdx, normal);
  328. //TODO: Rotate to the bind pose
  329. int controlIdx = fbxMesh->GetPolygonVertex(faceIdx, faceVtxIdx);
  330. FbxVector4 controlPt = controlPoints[controlIdx];
  331. controlPt = nodeTransform.MultT(controlPt);
  332. FBXVertexData* vtxData = &unpackedVtxData[vtxIdx];
  333. vtxData->mNormal = Vector3((float)normal[0], (float)normal[1], (float)normal[2]);
  334. //vtxData->mCoords = Vector3((float)controlPt[0] * 50 + 50, (float)controlPt[1] * 50 + 50, (float)controlPt[2] * 50 + 50);
  335. vtxData->mCoords = Vector3((float)controlPt[0], (float)controlPt[1], (float)controlPt[2]);
  336. vtxData->mBoneWeights = boneWeightsVector[controlIdx];
  337. vtxIdx++;
  338. }
  339. }
  340. mesh->mVertexData.reserve(numVertices);
  341. typedef boost::unordered_map<FBXVertexData, int> VertexDataMap;
  342. VertexDataMap usedVertexData;
  343. int vertexDataSize = numVertices;
  344. for (int vtxIdx = 0; vtxIdx < vertexDataSize; vtxIdx++)
  345. {
  346. //VertexData* vtxData = hash
  347. FBXVertexData* vtxData = &unpackedVtxData[vtxIdx];
  348. /*bool hasValidIdx = false;
  349. for (int boneIdx = 0; boneIdx < (int)vtxData->mBoneWeights.size(); boneIdx++)
  350. {
  351. BoneWeight boneWeight = vtxData->mBoneWeights[boneIdx];
  352. hasValidIdx |= boneWeight.mBoneIdx <= 45;
  353. }
  354. if (!hasValidIdx)
  355. {
  356. vtxData->mCoords.mX = 0;
  357. vtxData->mCoords.mY = 0;
  358. vtxData->mCoords.mZ = 0;
  359. }*/
  360. auto itr = usedVertexData.find(*vtxData);
  361. if (itr != usedVertexData.end())
  362. {
  363. mesh->mIndexData.push_back(itr->second);
  364. }
  365. else
  366. {
  367. int idx = (int)mesh->mVertexData.size();
  368. usedVertexData.insert(VertexDataMap::value_type(*vtxData, idx));
  369. mesh->mVertexData.push_back(*vtxData);
  370. mesh->mIndexData.push_back(idx);
  371. }
  372. }
  373. return mesh;
  374. }
  375. static bool IsNodeVisible(FbxNode *pNode)
  376. {
  377. bool bIsVisible = false;
  378. FbxNode* pParentNode = pNode;
  379. while (pParentNode != NULL)
  380. {
  381. bIsVisible = pParentNode->GetVisibility();
  382. if (!bIsVisible)
  383. {
  384. break;
  385. }
  386. pParentNode = pParentNode->GetParent();
  387. }
  388. return bIsVisible;
  389. }
  390. void FBXReader::TranslateNode(FbxNode* fbxNode)
  391. {
  392. if (fbxNode == NULL)
  393. return;
  394. switch (fbxNode->GetNodeAttribute()->GetAttributeType())
  395. {
  396. case FbxNodeAttribute::eMesh:
  397. if (IsNodeVisible(fbxNode))
  398. {
  399. FbxGeometryConverter geometryConverter(mFBXManager);
  400. for (int attrIdx = 0; attrIdx < fbxNode->GetNodeAttributeCount(); attrIdx++)
  401. {
  402. if (fbxNode->GetNodeAttributeByIndex(attrIdx)->GetAttributeType() == FbxNodeAttribute::eMesh)
  403. {
  404. FbxMesh* fbxMesh = (FbxMesh*) fbxNode->GetNodeAttributeByIndex(attrIdx);
  405. FbxMesh* triMesh = geometryConverter.TriangulateMesh(fbxMesh);
  406. FBXMesh* mesh = LoadMesh(fbxNode, triMesh);
  407. mMeshes.push_back(mesh);
  408. }
  409. }
  410. }
  411. }
  412. for (int childIdx = 0; childIdx < fbxNode->GetChildCount(); childIdx++)
  413. {
  414. // Call recursive translateNode once for each child of the root node
  415. FbxNode* childNode = fbxNode->GetChild(childIdx);
  416. TranslateNode(childNode);
  417. }
  418. }
  419. void FBXReader::FindJoints(FbxNode* fbxNode)
  420. {
  421. if (fbxNode == NULL)
  422. return;
  423. if (fbxNode->GetParent() != NULL)
  424. {
  425. switch (fbxNode->GetNodeAttribute()->GetAttributeType())
  426. {
  427. case FbxNodeAttribute::eSkeleton:
  428. if (IsNodeVisible(fbxNode))
  429. {
  430. String jointName = fbxNode->GetName();
  431. int idx = mJointIndexMap[jointName];
  432. mFBXJoints[idx].pNode = fbxNode;
  433. }
  434. }
  435. }
  436. for (int childIdx = 0; childIdx < fbxNode->GetChildCount(); childIdx++)
  437. {
  438. // Call recursive translateNode once for each child of the root node
  439. FbxNode* childNode = fbxNode->GetChild(childIdx);
  440. FindJoints(childNode);
  441. }
  442. }
  443. static FbxAMatrix FBXConvertMatrix(const FbxMatrix& mat)
  444. {
  445. FbxVector4 trans, shear, scale;
  446. FbxQuaternion rot;
  447. double sign;
  448. mat.GetElements(trans, rot, shear, scale, sign);
  449. FbxAMatrix ret;
  450. ret.SetT(trans);
  451. ret.SetQ(rot);
  452. ret.SetS(scale);
  453. return ret;
  454. }
  455. int FBXReader::FBXGetJointIndex(FbxNode* pNode)
  456. {
  457. if (pNode)
  458. {
  459. if (mJointIndexMap.find(pNode->GetName()) != mJointIndexMap.end())
  460. {
  461. return mJointIndexMap[pNode->GetName()];
  462. }
  463. }
  464. return -1;
  465. }
  466. FbxAMatrix FBXReader::GetBindPose(FbxNode *pNode, FbxMesh *pMesh)
  467. {
  468. //if (!params.useanimframebind)
  469. {
  470. int lSkinCount = pMesh->GetDeformerCount(FbxDeformer::eSkin);
  471. for (int i = 0; i != lSkinCount; ++i)
  472. {
  473. int lClusterCount = ((FbxSkin *) pMesh->GetDeformer(i, FbxDeformer::eSkin))->GetClusterCount();
  474. for (int j = 0; j < lClusterCount; ++j)
  475. {
  476. FbxCluster *pCluster = ((FbxSkin *) pMesh->GetDeformer(i, FbxDeformer::eSkin))->GetCluster(j);
  477. FbxAMatrix geometryBind;
  478. pCluster->GetTransformMatrix(geometryBind);
  479. if (FBXIsValidMatrix(geometryBind))
  480. return geometryBind;
  481. }
  482. }
  483. // It can be difficult to calculate the correct bind position for the mesh because
  484. // the FBX Global evaluate functions do not seem to take the inheritance type into account.
  485. // eInherit_Rrs nodes can mess things up.
  486. //FxOgreFBXLog("Warning: can not find bind pose for mesh %s. Attempting to calculate it\n", pNode->GetName());
  487. }
  488. FbxAMatrix globalTransform;
  489. //if (getSkeleton())
  490. {
  491. globalTransform = CalculateGlobalTransformWithBind(pNode, FbxTime(mParamBindframe));
  492. //globalTransform = getSkeleton()->CalculateGlobalTransformWithBind(pNode, FbxTime(params.bindframe), params);
  493. }
  494. //else
  495. {
  496. //globalTransform = CalculateGlobalTransform(pNode, FbxTime(params.bindframe));
  497. }
  498. return globalTransform;
  499. }
  500. bool FBXReader::FBXLoadJoint(FbxNode* pNode, FbxAMatrix globalBindPose)
  501. {
  502. if (!pNode)
  503. {
  504. //FxOgreFBXLog("Failed to load joint.\n");
  505. return false;
  506. }
  507. if (mJointIndexMap.find(pNode->GetName()) != mJointIndexMap.end())
  508. {
  509. // We have already exported this joint.
  510. return false;
  511. }
  512. if (!pNode->GetParent())
  513. {
  514. // Ignore the FBX root node here.
  515. return false;
  516. }
  517. // Protect against nodes that have parents with the same name (to avoid infinite recursion)
  518. String nodename(pNode->GetName());
  519. FbxNode *pParent = pNode->GetParent();
  520. String parentName;
  521. if (pParent)
  522. parentName = pParent->GetName();
  523. while (pParent)
  524. {
  525. String parentname = pParent->GetName();
  526. if (parentname.compare(nodename) == 0)
  527. {
  528. //FxOgreFBXLog("Warning! %s joint has a parent by the same name. Joint names must be unique! Output file may render incorrectly.", pNode->GetName());
  529. pNode = pParent;
  530. break;
  531. }
  532. pParent = pParent->GetParent();
  533. }
  534. globalBindPose = FBXCorrectMatrix(globalBindPose);
  535. FBXJoint newJoint;
  536. newJoint.mBoneLength = 0;
  537. newJoint.parentIndex = -1;
  538. mFBXJoints.push_back(newJoint);
  539. int index = mFBXJoints.size() - 1;
  540. mJointIndexMap[pNode->GetName()] = index;
  541. mFBXJoints[index].pNode = pNode;
  542. mFBXJoints[index].name = pNode->GetName();
  543. mFBXJoints[index].id = index;
  544. mFBXJoints[index].parentName = parentName;
  545. //FxOgreFBXLog("%s joint added at index %i with global transform:", pNode->GetName(), index);
  546. //logMatrix(globalBindPose);
  547. mFBXJoints[index].globalBindPose = globalBindPose;
  548. return true;
  549. }
  550. static FbxAMatrix CalculateGlobalTransform(FbxNode* pNode, FbxTime time)
  551. {
  552. // There seems to be some variance between calculating the global transform and
  553. // using the FBX calculate it with EvaluateGlobalTransform.
  554. /*
  555. FbxAMatrix lTM = pNode->EvaluateLocalTransform(time);
  556. if( pNode->GetParent() )
  557. {
  558. FbxAMatrix parentMatrix = CalculateGlobalTransform(pNode->GetParent(), time);
  559. return parentMatrix * lTM;
  560. }
  561. // root node
  562. // params.exportWorldCoords?
  563. return lTM;
  564. */
  565. return pNode->EvaluateGlobalTransform(time);
  566. }
  567. FbxAMatrix FBXReader::CalculateGlobalTransformWithBind(FbxNode* pNode, FbxTime time)
  568. {
  569. /*if (params.useanimframebind)
  570. {
  571. return CalculateGlobalTransform(pNode, time);
  572. }
  573. else*/
  574. {
  575. FbxAMatrix lTM = pNode->EvaluateLocalTransform(time);
  576. if (pNode->GetParent())
  577. {
  578. FbxAMatrix parentMatrix;
  579. if (mJointIndexMap.find(pNode->GetParent()->GetName()) == mJointIndexMap.end())
  580. {
  581. parentMatrix = CalculateGlobalTransform(pNode->GetParent(), time);
  582. }
  583. else
  584. {
  585. int index = mJointIndexMap[pNode->GetParent()->GetName()];
  586. parentMatrix = mFBXJoints[index].globalBindPose;
  587. }
  588. return parentMatrix * lTM;
  589. }
  590. return lTM;
  591. }
  592. }
  593. #define PRECISION 0.00000001f
  594. static int gKeyFrameCount = 0;
  595. FBXSkeletonKeyframe FBXReader::FBXLoadKeyframe(FBXJoint& j, float time)
  596. {
  597. gKeyFrameCount++;
  598. FbxTime FbxTime;
  599. FbxTime.SetSecondDouble(time);
  600. //create keyframe
  601. FBXSkeletonKeyframe key;
  602. key.time = time;
  603. FbxAMatrix localTMOrig = j.localBindPose;
  604. FbxAMatrix localTMAtTime = localTMOrig;
  605. FbxAMatrix nodeTM, parentTM;
  606. if (j.pNode)
  607. {
  608. //nodeTM = CalculateGlobalTransformWithBind(j.pNode, FbxTime);
  609. /*if (params.skelBB)
  610. {
  611. // Bounding box info is output to the MESH file, so this will not impact the
  612. // mesh if called from AddFBXAnimationToExisting.
  613. m_bbox.merge(Point3(nodeTM.GetT()[0], nodeTM.GetT()[1], nodeTM.GetT()[2]));
  614. }
  615. else*/
  616. {
  617. localTMAtTime = j.pNode->EvaluateLocalTransform(FbxTime);
  618. }
  619. }
  620. //FbxAMatrix deltaMat = localTMOrig.Inverse() * localTMAtTime;
  621. //FbxVector4 diff = localTMAtTime.GetT() - localTMOrig.GetT();
  622. //FbxAMatrix deltaMat = localTMAtTime * localTMOrig.Inverse();
  623. //FbxAMatrix deltaGlobalMat = nodeTM * j.bindPose.Inverse();
  624. FbxAMatrix deltaMat = localTMAtTime;
  625. FbxVector4 diff = localTMAtTime.GetT();
  626. //FbxVector4 diff = deltaGlobalMat.GetT();
  627. // @todo why does calculating the diff vector like below break UTRef?
  628. // FbxVector4 diff = deltaMat.GetT();
  629. Vector3 translation((float)diff[0], (float)diff[1], (float)diff[2]);
  630. if (fabs(translation.mX) < PRECISION)
  631. translation.mX = 0;
  632. if (fabs(translation.mY) < PRECISION)
  633. translation.mY = 0;
  634. if (fabs(translation.mZ) < PRECISION)
  635. translation.mZ = 0;
  636. Vector3 scale((float)deltaMat.GetS()[0], (float)deltaMat.GetS()[1], (float)deltaMat.GetS()[2]);
  637. if (fabs(scale.mX) < PRECISION)
  638. scale.mX = 0;
  639. if (fabs(scale.mY) < PRECISION)
  640. scale.mY = 0;
  641. if (fabs(scale.mZ) < PRECISION)
  642. scale.mZ = 0;
  643. key.tx = translation.mX * mParamLum;
  644. key.ty = translation.mY * mParamLum;
  645. key.tz = translation.mZ * mParamLum;
  646. FbxQuaternion quat = deltaMat.GetQ();
  647. //FbxQuaternion quat = deltaGlobalMat.GetQ();
  648. key.quat_x = quat[0];
  649. key.quat_y = quat[1];
  650. key.quat_z = quat[2];
  651. key.quat_w = quat[3];
  652. Quaternion quatTest((float)quat[0], (float)quat[1], (float)quat[2], (float)quat[3]);
  653. Vector3 vecOut = Vector3::Transform(Vector3(0, 1, 0), quatTest);
  654. key.sx = static_cast<float>(scale.mX);
  655. key.sy = static_cast<float>(scale.mY);
  656. key.sz = static_cast<float>(scale.mZ);
  657. return key;
  658. }
  659. bool FBXReader::FBXLoadClipAnim(String clipName, float start, float stop, float rate, FBXAnimation& a)
  660. {
  661. size_t i, j;
  662. std::vector<float> times;
  663. times.clear();
  664. if (rate <= 0)
  665. {
  666. //FxOgreFBXLog("invalid sample rate for the clip (must be >0), we skip it\n");
  667. return false;
  668. }
  669. else
  670. {
  671. for (float t = start; t < stop - 0.00001f; t += rate)
  672. times.push_back(t);
  673. //times.push_back(stop);
  674. }
  675. // get animation length
  676. float length = 0;
  677. if (times.size() >= 0)
  678. length = times[times.size() - 1] - times[0];
  679. if (length < 0)
  680. {
  681. //FxOgreFBXLog("invalid time range for the clip, we skip it\n");
  682. return false;
  683. }
  684. a.mName = clipName.c_str();
  685. a.mTracks.clear();
  686. a.mLength = length;
  687. // create a track for current clip for all joints
  688. std::vector<FBXTrack> animTracks;
  689. for (i = 0; i < mFBXJoints.size(); i++)
  690. {
  691. FBXTrack t;
  692. t.mType = TT_SKELETON;
  693. t.mBone = mFBXJoints[i].name;
  694. t.mSkeletonKeyframes.clear();
  695. animTracks.push_back(t);
  696. }
  697. // evaluate animation curves at selected times
  698. for (i = 0; i < times.size(); i++)
  699. {
  700. //load a keyframe for every joint at current time
  701. for (j = 0; j < mFBXJoints.size(); j++)
  702. {
  703. FBXSkeletonKeyframe key = FBXLoadKeyframe(mFBXJoints[j], times[i]);
  704. key.time = key.time - times[0];
  705. //add keyframe to joint track
  706. animTracks[j].mSkeletonKeyframes.push_back(key);
  707. }
  708. }
  709. // add created tracks to current clip
  710. for (i = 0; i < animTracks.size(); i++)
  711. {
  712. a.mTracks.push_back(animTracks[i]);
  713. }
  714. if (animTracks.size() > 0)
  715. {
  716. // display info
  717. //FxOgreFBXLog("length: %f\n", a.m_length);
  718. //FxOgreFBXLog("num keyframes: %d\n", animTracks[0].m_skeletonKeyframes.size());
  719. }
  720. return true;
  721. }
  722. bool FBXReader::FBXLoadClip(String clipName, float start, float stop, float rate)
  723. {
  724. FBXAnimation a;
  725. mAnimations.push_back(a);
  726. bool stat = FBXLoadClipAnim(clipName, start, stop, rate, mAnimations[mAnimations.size() - 1]);
  727. if (!stat)
  728. {
  729. mAnimations.pop_back();
  730. return false;
  731. }
  732. return true;
  733. }
  734. void FBXReader::GetAnimationBounds()
  735. {
  736. mAnimStart = FLT_MAX;
  737. mAnimStop = -FLT_MAX;
  738. FbxTime kStop = FBXSDK_TIME_MINUS_INFINITE;
  739. FbxTime kStart = FBXSDK_TIME_INFINITE;
  740. // Iterate through all curves to find the start and end time of the animation. Works
  741. // for bones and morph targets.
  742. for (int i = 0; i < mFBXScene->GetSrcObjectCount(FbxAnimCurve::ClassId); ++i)
  743. {
  744. FbxAnimCurve* pCurve = (FbxAnimCurve*) mFBXScene->GetSrcObject(FbxAnimCurve::ClassId, i);
  745. if (pCurve)
  746. {
  747. int numKeys = pCurve->KeyGetCount();
  748. if (numKeys > 0)
  749. {
  750. float first = (float) pCurve->KeyGet(0).GetTime().GetSecondDouble();
  751. float last = first;
  752. if (numKeys > 1)
  753. {
  754. last = (float) pCurve->KeyGet(numKeys - 1).GetTime().GetSecondDouble();
  755. }
  756. if (first < mAnimStart)
  757. mAnimStart = first;
  758. if (last > mAnimStop)
  759. mAnimStop = last;
  760. }
  761. }
  762. }
  763. }
  764. void FBXReader::ComputeBindPoseBoundingBox()
  765. {
  766. // Make sure the bind
  767. for (size_t i = 0; i < mFBXJoints.size(); ++i)
  768. {
  769. //m_bbox.merge(Point3(mJoints[i].globalBindPose.GetT()[0], mJoints[i].globalBindPose.GetT()[1], mJoints[i].globalBindPose.GetT()[2]));
  770. }
  771. }
  772. void FBXReader::SetParentIndexes()
  773. {
  774. // Set the parent indexes, so we know what to sort.
  775. for (size_t i = 0; i < mFBXJoints.size(); ++i)
  776. {
  777. FbxNode *pNode = mFBXJoints[i].pNode;
  778. int parentIndex = -1;
  779. if (pNode->GetParent())
  780. {
  781. if (mJointIndexMap.find(pNode->GetParent()->GetName()) != mJointIndexMap.end())
  782. {
  783. parentIndex = mJointIndexMap[pNode->GetParent()->GetName()];
  784. }
  785. }
  786. mFBXJoints[i].parentIndex = parentIndex;
  787. }
  788. }
  789. void FBXReader::AddParentsOfExistingJoints()
  790. {
  791. // If the parent for this node doesn't exist, add it here.
  792. for (size_t i = 0; i < mFBXJoints.size(); ++i)
  793. {
  794. FbxNode *pNode = mFBXJoints[i].pNode;
  795. if (pNode->GetParent())
  796. {
  797. if (mJointIndexMap.find(pNode->GetParent()->GetName()) == mJointIndexMap.end())
  798. {
  799. FbxAMatrix global = CalculateGlobalTransformWithBind(pNode->GetParent(), FbxTime(mParamBindframe));
  800. bool bLoaded = FBXLoadJoint(pNode->GetParent(), global);
  801. //FxOgreFBXLog("Warning joint %s created with no bind pose information.\n", pNode->GetParent()->GetName());
  802. //logMatrix(global);
  803. }
  804. }
  805. }
  806. }
  807. // Ensure that parents are before their children in mJoints
  808. void FBXReader::SortAndPruneJoints()
  809. {
  810. // Set the indexes to sort by
  811. SetParentIndexes();
  812. std::map<String, int> sortedJointIndexMap;
  813. std::vector<FBXJoint> sorted_joints;
  814. for (size_t i = 0; i < mFBXJoints.size(); ++i)
  815. {
  816. SortJoint(mFBXJoints[i], sortedJointIndexMap, sorted_joints);
  817. }
  818. // Update the joint list.
  819. mFBXJoints.clear();
  820. mFBXJoints = sorted_joints;
  821. mJointIndexMap.clear();
  822. mJointIndexMap = sortedJointIndexMap;
  823. }
  824. void FBXReader::SortJoint(FBXJoint j, std::map<String, int> &sortedJointIndexMap, std::vector<FBXJoint>& sorted_joints)
  825. {
  826. // Only export if we haven't already.
  827. if (sortedJointIndexMap.find(j.name) == sortedJointIndexMap.end())
  828. {
  829. int newParentIndex = j.parentIndex;
  830. if (j.parentIndex != -1)
  831. {
  832. // Export parents first.
  833. SortJoint(mFBXJoints[j.parentIndex], sortedJointIndexMap, sorted_joints);
  834. newParentIndex = sortedJointIndexMap[j.pNode->GetParent()->GetName()];
  835. }
  836. if (sorted_joints.size() >= BF_MAX_NUM_BONES)
  837. {
  838. //FxOgreFBXLog("Warning: pruned joint - %s. Too many bones!\n", j.name.c_str());
  839. }
  840. else
  841. {
  842. int id = sorted_joints.size();
  843. sorted_joints.push_back(j);
  844. sortedJointIndexMap[j.name] = id;
  845. sorted_joints[id].id = id;
  846. sorted_joints[id].parentIndex = newParentIndex;
  847. //FxOgreFBXLog("%i - %s - parent index: %i.\n", sorted_joints[id].id, sorted_joints[id].name.c_str(), sorted_joints[id].parentIndex);
  848. }
  849. }
  850. }
  851. void FBXReader::CalculateLocalTransforms(FbxNode* pRootNode)
  852. {
  853. //FxOgreFBXLog("Calculating local transforms for skeleton bones.\n");
  854. // Calculate global transforms.
  855. for (size_t i = 0; i < mFBXJoints.size(); ++i)
  856. {
  857. FbxNode* pNode = mFBXJoints[i].pNode;
  858. FbxAMatrix localTM, parentTM;
  859. localTM = mFBXJoints[i].globalBindPose;
  860. Quaternion quatOrig(
  861. (float)localTM.GetQ()[0],
  862. (float)localTM.GetQ()[1],
  863. (float)localTM.GetQ()[2],
  864. (float)localTM.GetQ()[3]);
  865. /*if (params.useanimframebind)
  866. {
  867. // We could calculate the local transforms from the global ones, but
  868. // in some circumstances (blake), this produces incorrect values, possibly as
  869. // a result of gimble lock. With useanimframebind, we are relying on the
  870. // FBX SDK to calculate everything, so we rely on the EvaluateLocalTransform
  871. // function as opposed to calculating our own values.
  872. localTM = mJoints[i].pNode->EvaluateLocalTransform(FbxTime((params.bindframe)));
  873. }
  874. else*/ if (mFBXJoints[i].parentIndex != -1)
  875. {
  876. parentTM = mFBXJoints[mFBXJoints[i].parentIndex].globalBindPose;
  877. localTM = parentTM.Inverse() * localTM;
  878. //localTM = localTM * parentTM.Inverse();
  879. }
  880. mFBXJoints[i].localBindPose = localTM;
  881. mFBXJoints[i].bindPose = localTM;
  882. Quaternion quat(
  883. (float)localTM.GetQ()[0],
  884. (float)localTM.GetQ()[1],
  885. (float)localTM.GetQ()[2],
  886. (float)localTM.GetQ()[3]);
  887. Matrix4 quatOrigMat = quatOrig.ToMatrix();
  888. Matrix4 quatMat = quat.ToMatrix();
  889. Vector3 translation((float)localTM.GetT()[0], (float)localTM.GetT()[1], (float)localTM.GetT()[2]);
  890. if (fabs(translation.mX) < PRECISION)
  891. translation.mX = 0;
  892. if (fabs(translation.mY) < PRECISION)
  893. translation.mY = 0;
  894. if (fabs(translation.mZ) < PRECISION)
  895. translation.mZ = 0;
  896. Vector3 scale((float)localTM.GetS()[0], (float)localTM.GetS()[1], (float)localTM.GetS()[2]);
  897. if (fabs(scale.mX) < PRECISION)
  898. scale.mX = 0;
  899. if (fabs(scale.mY) < PRECISION)
  900. scale.mY = 0;
  901. if (fabs(scale.mZ) < PRECISION)
  902. scale.mZ = 0;
  903. mFBXJoints[i].posx = translation.mX * mParamLum;
  904. mFBXJoints[i].posy = translation.mY * mParamLum;
  905. mFBXJoints[i].posz = translation.mZ * mParamLum;
  906. mFBXJoints[i].quatx = localTM.GetQ()[0];
  907. mFBXJoints[i].quaty = localTM.GetQ()[1];
  908. mFBXJoints[i].quatz = localTM.GetQ()[2];
  909. mFBXJoints[i].quatw = localTM.GetQ()[3];
  910. mFBXJoints[i].scalex = static_cast<float>(scale.mX);
  911. mFBXJoints[i].scaley = static_cast<float>(scale.mY);
  912. mFBXJoints[i].scalez = static_cast<float>(scale.mZ);
  913. mFBXJoints[i].bInheritScale = true;
  914. if (mFBXJoints[i].parentIndex >= 0)
  915. {
  916. FBXJoint* parentJoint = &mFBXJoints[mFBXJoints[i].parentIndex];
  917. mFBXJoints[i].bindPose = parentJoint->bindPose * mFBXJoints[i].bindPose;
  918. float dx = (float)(mFBXJoints[i].posx - parentJoint->posx);
  919. float dy = (float)(mFBXJoints[i].posy - parentJoint->posy);
  920. float dz = (float)(mFBXJoints[i].posz - parentJoint->posz);
  921. float len = sqrt(dx*dx + dy*dy + dz*dz);
  922. mFBXJoints[i].mBoneLength = len;
  923. }
  924. //FxOgreFBXLog("%s - Local Trans:( %f,%f,%f) Quat( %f,%f,%f,%f), Scale(%f,%f,%f).\n", pNode->GetName(), translation.mX, translation.mY, translation.mZ, localTM.GetQ()[3], localTM.GetQ()[0], localTM.GetQ()[1], localTM.GetQ()[2], scale.mX, scale.mY, scale.mZ);
  925. }
  926. ComputeBindPoseBoundingBox();
  927. }
  928. void FBXReader::LoadBindPose()
  929. {
  930. //if (!params.useanimframebind)
  931. {
  932. int poseCount = mFBXScene->GetPoseCount();
  933. for (int poseIdx = 0; poseIdx < poseCount; poseIdx++)
  934. {
  935. FbxPose* pPose = mFBXScene->GetPose(poseIdx);
  936. if (pPose->IsBindPose())
  937. {
  938. for (int j = 0; j < pPose->GetCount(); ++j)
  939. {
  940. FbxNode *pNode = pPose->GetNode(j);
  941. if (!IsNodeVisible(pNode))
  942. {
  943. continue;
  944. }
  945. // Only export actual joints here. Otherwise meshes will create joints unnecessarily.
  946. if (pNode->GetNodeAttribute()->GetAttributeType() == FbxNodeAttribute::eSkeleton)
  947. {
  948. FbxAMatrix globalBindPose;
  949. globalBindPose = FBXConvertMatrix(pPose->GetMatrix(j));
  950. int jointIndex = FBXGetJointIndex(pNode);
  951. if (-1 == jointIndex)
  952. {
  953. if (FBXIsValidMatrix(globalBindPose))
  954. {
  955. FBXLoadJoint(pNode, globalBindPose);
  956. }
  957. }
  958. else if (globalBindPose != mFBXJoints[jointIndex].globalBindPose)
  959. {
  960. //FxOgreFBXLog("Warning: Multiple bind poses found for joint %s. Setting bind pose to frame 0 and ignoring all stored bind poses.\n", pNode->GetName());
  961. //params.useanimframebind = true;
  962. break;
  963. }
  964. }
  965. }
  966. }
  967. }
  968. }
  969. // Nodes to export using bind pose information calculated from the animation.
  970. std::vector<FbxNode*> time0skeletonNodes;
  971. // Iterate through geometry and look for bones that were not incuded in the bind pose
  972. // but the mesh is weighted to.
  973. for (int iGeom = 0; iGeom < mFBXScene->GetGeometryCount(); ++iGeom)
  974. {
  975. FbxGeometry *pGeom = mFBXScene->GetGeometry(iGeom);
  976. if (!IsNodeVisible(pGeom->GetNode()))
  977. {
  978. continue;
  979. }
  980. bool bIsMeshWeighted = false;
  981. int lSkinCount = pGeom->GetDeformerCount(FbxDeformer::eSkin);
  982. for (int i = 0; i != lSkinCount; ++i)
  983. {
  984. int lClusterCount = ((FbxSkin *) pGeom->GetDeformer(i, FbxDeformer::eSkin))->GetClusterCount();
  985. for (int j = 0; j < lClusterCount; ++j)
  986. {
  987. FbxCluster *pCluster = ((FbxSkin *) pGeom->GetDeformer(i, FbxDeformer::eSkin))->GetCluster(j);
  988. if (pCluster)
  989. {
  990. bIsMeshWeighted = true;
  991. FbxNode* pNode = pCluster->GetLink();
  992. FbxAMatrix globalMatrix;
  993. int jointIndex = FBXGetJointIndex(pNode);
  994. // Add this joint to the sk
  995. time0skeletonNodes.push_back(pNode);
  996. //if (!params.useanimframebind)
  997. {
  998. pCluster->GetTransformLinkMatrix(globalMatrix);
  999. if (-1 == jointIndex)
  1000. {
  1001. //If a bind pose was properly set in the FBX file, this is not needed, but just in case.
  1002. FBXLoadJoint(pNode, globalMatrix);
  1003. }
  1004. else if (globalMatrix != mFBXJoints[jointIndex].globalBindPose)
  1005. {
  1006. //FxOgreFBXLog("Warning: Multiple bind poses found for joint %s on mesh %s. Setting bind pose to frame 0 and ignoring all stored bind poses.\n", pNode->GetName(), pGeom->GetName());
  1007. //params.useanimframebind = true;
  1008. }
  1009. }
  1010. }
  1011. }
  1012. }
  1013. }
  1014. /*if (params.useanimframebind)
  1015. {
  1016. if (m_joints.size() > 0)
  1017. {
  1018. FxOgreFBXLog("Recalculating transforms for joints based on bindframe.\n");
  1019. }
  1020. // Delete bind pose information stored thus far.
  1021. clear();
  1022. for (size_t i = 0; i < time0skeletonNodes.size(); ++i)
  1023. {
  1024. FbxAMatrix bindTransform = CalculateGlobalTransform(time0skeletonNodes[i], FbxTime(params.bindframe));
  1025. loadJoint(time0skeletonNodes[i], params, bindTransform);
  1026. }
  1027. }*/
  1028. // loop through the geometry again and make sure that parents of unweighted meshes are treated as bones.
  1029. for (int iGeom = 0; iGeom < mFBXScene->GetGeometryCount(); ++iGeom)
  1030. {
  1031. FbxGeometry *pGeom = mFBXScene->GetGeometry(iGeom);
  1032. if (!IsNodeVisible(pGeom->GetNode()))
  1033. {
  1034. continue;
  1035. }
  1036. bool bIsMeshWeighted = false;
  1037. int lSkinCount = pGeom->GetDeformerCount(FbxDeformer::eSkin);
  1038. for (int i = 0; i != lSkinCount; ++i)
  1039. {
  1040. int lClusterCount = ((FbxSkin*)pGeom->GetDeformer(i, FbxDeformer::eSkin))->GetClusterCount();
  1041. for (int j = 0; j < lClusterCount; ++j)
  1042. {
  1043. FbxCluster *pCluster = ((FbxSkin*)pGeom->GetDeformer(i, FbxDeformer::eSkin))->GetCluster(j);
  1044. if (pCluster)
  1045. {
  1046. bIsMeshWeighted = true;
  1047. }
  1048. }
  1049. }
  1050. // If the mesh is unweighted, export it's parent as a bone.
  1051. if (!bIsMeshWeighted)
  1052. {
  1053. FbxNode *pNode = pGeom->GetNode();
  1054. if (pNode && pNode->GetParent())
  1055. {
  1056. // If the mesh is unweighted, export its parent as a bone so we can rigidly skin the mesh.
  1057. FbxAMatrix bindTransform = CalculateGlobalTransformWithBind(pNode, FbxTime(mParamBindframe));
  1058. if (FBXLoadJoint(pNode->GetParent(), bindTransform))
  1059. {
  1060. //FxOgreFBXLog("Unskinned mesh %s found. Added parent %s to the skeleton.\n", pNode->GetName(), pNode->GetParent()->GetName());
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }
  1066. bool FBXReader::GetVertexBoneWeights(FbxNode* pNode, FbxMesh *pMesh, std::vector<BoneWeightVector>& boneWeightsVector)
  1067. {
  1068. int lSkinCount = pMesh->GetDeformerCount(FbxDeformer::eSkin);
  1069. for (int i = 0; i != lSkinCount; ++i)
  1070. {
  1071. int lClusterCount = ((FbxSkin*)pMesh->GetDeformer(i, FbxDeformer::eSkin))->GetClusterCount();
  1072. for (int j = 0; j < lClusterCount; ++j)
  1073. {
  1074. FbxCluster *pCluster = ((FbxSkin*)pMesh->GetDeformer(i, FbxDeformer::eSkin))->GetCluster(j);
  1075. if (pCluster /*&& m_pSkeleton*/)
  1076. {
  1077. int boneIndex = FBXGetJointIndex(pCluster->GetLink());
  1078. int k, lIndexCount = pCluster->GetControlPointIndicesCount();
  1079. int* lIndices = pCluster->GetControlPointIndices();
  1080. double* lWeights = pCluster->GetControlPointWeights();
  1081. for (k = 0; k < lIndexCount; k++)
  1082. {
  1083. if (boneIndex >= 0)
  1084. {
  1085. FBXBoneWeight boneWeight;
  1086. boneWeight.mBoneIdx = boneIndex;
  1087. boneWeight.mBoneWeight = (float)lWeights[k];
  1088. boneWeightsVector[lIndices[k]].push_back(boneWeight);
  1089. //newweights[lIndices[k]].push_back(static_cast<float>(lWeights[k]));
  1090. //newjointIds[lIndices[k]].push_back(boneIndex);
  1091. }
  1092. }
  1093. }
  1094. }
  1095. }
  1096. return true;
  1097. }
  1098. bool FBXReader::ReadFile(const StringImpl& fileName, bool loadAnims)
  1099. {
  1100. bool loadDefData = true;
  1101. mModelDef->mLoadDir = GetFileDir(fileName);
  1102. String bfModelFileName = fileName + ".bfmodel";
  1103. if (ReadBFFile(bfModelFileName))
  1104. return true;
  1105. String checkFileName2;
  1106. int atPos = (int)fileName.find('@');
  1107. if (atPos != -1)
  1108. {
  1109. loadDefData = false;
  1110. checkFileName2 = fileName.substr(0, atPos) + ".fbx";
  1111. if (!ReadFile(checkFileName2, false))
  1112. return false;
  1113. for (int jointIdx = 0; jointIdx < (int) mFBXJoints.size(); jointIdx++)
  1114. {
  1115. FBXJoint* fbxJoint = &mFBXJoints[jointIdx];
  1116. fbxJoint->pNode = NULL;
  1117. }
  1118. }
  1119. FbxManager* sdkManager = NULL;
  1120. FbxScene* scene = NULL;
  1121. // Prepare the FBX SDK.
  1122. InitializeSdkObjects(sdkManager, scene);
  1123. mFBXManager = sdkManager;
  1124. mFBXScene = scene;
  1125. FbxImporter* lImporter = FbxImporter::Create(sdkManager, "");
  1126. if (!LoadScene(sdkManager, scene, fileName.c_str(), lImporter))
  1127. {
  1128. lImporter->Destroy();
  1129. return false;
  1130. }
  1131. FbxGlobalSettings& globalSettings = scene->GetGlobalSettings();
  1132. FbxTime::EMode timeMode = globalSettings.GetTimeMode();
  1133. mFrameRate = (float) FbxTime::GetFrameRate(timeMode);
  1134. if (!loadDefData)
  1135. {
  1136. // Hook joints back up
  1137. FindJoints(scene->GetRootNode());
  1138. }
  1139. /*clipInfo clip;
  1140. clip.name = clipName;
  1141. clip.start = 0;
  1142. clip.stop = 0;
  1143. clip.rate = 1;
  1144. m_params.skelClipList.push_back(clip);
  1145. return true;*/
  1146. FbxGlobalSettings& lGlobalSettings = mFBXScene->GetGlobalSettings();
  1147. FbxTime::EMode lTimeMode = lGlobalSettings.GetTimeMode();
  1148. mFPS = (float) FbxTime::GetFrameRate(lTimeMode);
  1149. GetAnimationBounds();
  1150. // Loading anims
  1151. //bool stat;
  1152. //size_t i;
  1153. // clear animations list
  1154. mAnimations.clear();
  1155. LoadBindPose();
  1156. AddParentsOfExistingJoints();
  1157. SortAndPruneJoints();
  1158. CalculateLocalTransforms(mFBXScene->GetRootNode());
  1159. //AddFBXAnimationToExisting("Test", mAnimStart, mAnimStop);
  1160. //stat = FBXLoadClip("Test", 0, 10000, mFPS);
  1161. //FBXLoadClip("Test", mAnimStart, mAnimStop, 1.0f/mFPS);
  1162. if (loadAnims)
  1163. {
  1164. int lAnimStackCount = lImporter->GetAnimStackCount();
  1165. for (int i = 0; i < lAnimStackCount; i++)
  1166. {
  1167. FbxTakeInfo* lTakeInfo = lImporter->GetTakeInfo(i);
  1168. FbxTime startTime = lTakeInfo->mLocalTimeSpan.GetStart();
  1169. FbxTime endTime = lTakeInfo->mLocalTimeSpan.GetStop();
  1170. FbxAnimStack* lAnimStack = scene->GetSrcObject<FbxAnimStack>(i);
  1171. scene->SetCurrentAnimationStack(lAnimStack);
  1172. //scene->SetCurrentTake();
  1173. FBXLoadClip(lTakeInfo->mName.Buffer(), (float) startTime.GetSecondDouble(), (float) endTime.GetSecondDouble(), 1.0f / mFPS);
  1174. }
  1175. }
  1176. //FxOgreFBXLog("Loading skeleton animations...\n");
  1177. // load skeleton animation clips for the whole skeleton
  1178. //for (i = 0; i < mParamsSkelClipList.size(); i++)
  1179. {
  1180. //FxOgreFBXLog("Loading clip %s.\n", params.skelClipList[i].name.c_str());
  1181. /*stat = FBXLoadClip(mParamsSkelClipList[i].name, params.skelClipList[i].start,
  1182. params.skelClipList[i].stop, params.skelClipList[i].rate);*/
  1183. /*if (stat == true)
  1184. {
  1185. FxOgreFBXLog("Clip successfully loaded\n");
  1186. }
  1187. else
  1188. {
  1189. FxOgreFBXLog("Failed loading clip\n");
  1190. }*/
  1191. }
  1192. FbxNode* rootNode = scene->GetRootNode();
  1193. if (rootNode != NULL)
  1194. {
  1195. for (int childIdx = 0; childIdx < rootNode->GetChildCount(); childIdx++)
  1196. {
  1197. // Call recursive translateNode once for each child of the root node
  1198. FbxNode* childNode = rootNode->GetChild(childIdx);
  1199. TranslateNode(childNode);
  1200. }
  1201. }
  1202. mModelDef->mFrameRate = mFPS;
  1203. if (loadDefData)
  1204. {
  1205. mModelDef->mMeshes.resize(mMeshes.size());
  1206. mModelDef->mJoints.resize(mFBXJoints.size());
  1207. for (int meshIdx = 0; meshIdx < (int) mMeshes.size(); meshIdx++)
  1208. {
  1209. FBXMesh* fbxMesh = mMeshes[meshIdx];
  1210. ModelMesh* mesh = &mModelDef->mMeshes[meshIdx];
  1211. mesh->mName = fbxMesh->mName;
  1212. mesh->mTexFileName = fbxMesh->mMaterial.mTexFileName;
  1213. mesh->mBumpFileName = fbxMesh->mMaterial.mBumpFileName;
  1214. mesh->mIndices.resize(fbxMesh->mIndexData.size());
  1215. for (int idxIdx = 0; idxIdx < (int) fbxMesh->mIndexData.size(); idxIdx++)
  1216. mesh->mIndices[idxIdx] = (uint16) fbxMesh->mIndexData[idxIdx];
  1217. BF_ASSERT(fbxMesh->mVertexData.size() < 0x10000);
  1218. mesh->mVertices.resize(fbxMesh->mVertexData.size());
  1219. for (int vtxIdx = 0; vtxIdx < (int) fbxMesh->mVertexData.size(); vtxIdx++)
  1220. {
  1221. FBXVertexData* fbxVertex = &fbxMesh->mVertexData[vtxIdx];
  1222. ModelVertex* vertex = &mesh->mVertices[vtxIdx];
  1223. vertex->mPosition = fbxVertex->mCoords;
  1224. vertex->mColor = fbxVertex->mColor;
  1225. if (fbxVertex->mTexCoords.size() != 0)
  1226. vertex->mTexCoords = TexCoords::FlipV(fbxVertex->mTexCoords[0]);
  1227. if (fbxVertex->mBumpTexCoords.size() != 0)
  1228. vertex->mBumpTexCoords = TexCoords::FlipV(fbxVertex->mBumpTexCoords[0]);
  1229. vertex->mNormal = fbxVertex->mNormal;
  1230. vertex->mTangent = fbxVertex->mTangent;
  1231. vertex->mNumBoneWeights = (int) fbxVertex->mBoneWeights.size();
  1232. BF_ASSERT(vertex->mNumBoneWeights <= MODEL_MAX_BONE_WEIGHTS);
  1233. for (int boneWeightIdx = 0; boneWeightIdx < vertex->mNumBoneWeights; boneWeightIdx++)
  1234. {
  1235. vertex->mBoneIndices[boneWeightIdx] = fbxVertex->mBoneWeights[boneWeightIdx].mBoneIdx;
  1236. vertex->mBoneWeights[boneWeightIdx] = fbxVertex->mBoneWeights[boneWeightIdx].mBoneWeight;
  1237. }
  1238. }
  1239. }
  1240. for (int jointIdx = 0; jointIdx < (int) mFBXJoints.size(); jointIdx++)
  1241. {
  1242. FBXJoint* fbxJoint = &mFBXJoints[jointIdx];
  1243. ModelJoint* joint = &mModelDef->mJoints[jointIdx];
  1244. joint->mName = fbxJoint->name;
  1245. joint->mParentIdx = fbxJoint->parentIndex;
  1246. auto invGlobalMtx = fbxJoint->globalBindPose.Inverse();
  1247. for (int row = 0; row < 4; row++)
  1248. for (int col = 0; col < 4; col++)
  1249. joint->mPoseInvMatrix.mMat[row][col] = (float) invGlobalMtx.Get(col, row);
  1250. }
  1251. }
  1252. mModelDef->mAnims.resize(mAnimations.size());
  1253. for (int animIdx = 0; animIdx < (int)mAnimations.size(); animIdx++)
  1254. {
  1255. FBXAnimation* fbxAnimation = &mAnimations[animIdx];
  1256. ModelAnimation* animation = &mModelDef->mAnims[animIdx];
  1257. animation->mName = fbxAnimation->mName;
  1258. animation->mFrames.resize((int)fbxAnimation->mTracks[0].mSkeletonKeyframes.size());
  1259. for (int frameIdx = 0; frameIdx < (int)animation->mFrames.size(); frameIdx++)
  1260. animation->mFrames[frameIdx].mJointTranslations.resize(mFBXJoints.size());
  1261. for (int trackIdx = 0; trackIdx < (int)fbxAnimation->mTracks.size(); trackIdx++)
  1262. {
  1263. FBXTrack* fbxTrack = &fbxAnimation->mTracks[trackIdx];
  1264. for (int frameIdx = 0; frameIdx < (int)fbxTrack->mSkeletonKeyframes.size(); frameIdx++)
  1265. {
  1266. FBXSkeletonKeyframe* fbxSkeletonKeyframe = &fbxTrack->mSkeletonKeyframes[frameIdx];
  1267. ModelAnimationFrame* animFrame = &animation->mFrames[frameIdx];
  1268. ModelJointTranslation* jointPosition = &animFrame->mJointTranslations[trackIdx];
  1269. jointPosition->mQuat = Quaternion((float)fbxSkeletonKeyframe->quat_x, (float)fbxSkeletonKeyframe->quat_y, (float)fbxSkeletonKeyframe->quat_z, (float)fbxSkeletonKeyframe->quat_w);
  1270. jointPosition->mScale = Vector3((float)fbxSkeletonKeyframe->sx, (float)fbxSkeletonKeyframe->sy, (float)fbxSkeletonKeyframe->sz);
  1271. jointPosition->mTrans = Vector3((float)fbxSkeletonKeyframe->tx, (float)fbxSkeletonKeyframe->ty, (float)fbxSkeletonKeyframe->tz);
  1272. }
  1273. }
  1274. }
  1275. //mModelDef->mAnims
  1276. lImporter->Destroy();
  1277. if (loadAnims)
  1278. WriteBFFile(bfModelFileName, fileName, checkFileName2);
  1279. return true;
  1280. }
  1281. bool Beefy::FBXReader::WriteBFFile(const StringImpl& fileName, const StringImpl& checkFile, const StringImpl& checkFile2)
  1282. {
  1283. FILE* fp = fopen(fileName.c_str(), "wb");
  1284. if (fp == NULL)
  1285. return false;
  1286. FileStream fs;
  1287. fs.mFP = fp;
  1288. String fileDir = GetFileDir(checkFile);
  1289. fs.Write(BF_MODEL_VERSION);
  1290. fs.Write(GetFileName(checkFile));
  1291. fs.Write(GetFileTimeWrite(checkFile));
  1292. fs.Write(GetFileName(checkFile2));
  1293. fs.Write(GetFileTimeWrite(checkFile2));
  1294. fs.Write(mModelDef->mFrameRate);
  1295. fs.Write((int)mModelDef->mMeshes.size());
  1296. for (int meshIdx = 0; meshIdx < (int)mModelDef->mMeshes.size(); meshIdx++)
  1297. {
  1298. ModelMesh* modelMesh = &mModelDef->mMeshes[meshIdx];
  1299. fs.Write(modelMesh->mName);
  1300. fs.Write((int)modelMesh->mIndices.size());
  1301. fs.Write((void*)&modelMesh->mIndices[0], modelMesh->mIndices.size() * sizeof(modelMesh->mIndices[0]));
  1302. fs.Write((int) modelMesh->mVertices.size());
  1303. fs.Write((void*)&modelMesh->mVertices[0], modelMesh->mVertices.size() * sizeof(modelMesh->mVertices[0]));
  1304. fs.Write(modelMesh->mTexFileName);
  1305. fs.Write(modelMesh->mBumpFileName);
  1306. }
  1307. fs.Write((int)mModelDef->mJoints.size());
  1308. for (int jointIdx = 0; jointIdx < (int)mModelDef->mJoints.size(); jointIdx++)
  1309. {
  1310. ModelJoint* modelJoint = &mModelDef->mJoints[jointIdx];
  1311. fs.Write(modelJoint->mName);
  1312. fs.Write(modelJoint->mParentIdx);
  1313. fs.WriteT(modelJoint->mPoseInvMatrix);
  1314. }
  1315. fs.Write((int)mModelDef->mAnims.size());
  1316. for (int animIdx = 0; animIdx < (int)mModelDef->mAnims.size(); animIdx++)
  1317. {
  1318. ModelAnimation* modelAnim = &mModelDef->mAnims[animIdx];
  1319. fs.Write(modelAnim->mName);
  1320. fs.Write((int)modelAnim->mFrames.size());
  1321. for (int frameIdx = 0; frameIdx < (int)modelAnim->mFrames.size(); frameIdx++)
  1322. {
  1323. ModelAnimationFrame* frame = &modelAnim->mFrames[frameIdx];
  1324. BF_ASSERT(mModelDef->mJoints.size() == frame->mJointTranslations.size());
  1325. fs.Write((void*)&frame->mJointTranslations[0], (int)frame->mJointTranslations.size() * sizeof(frame->mJointTranslations[0]));
  1326. }
  1327. }
  1328. return true;
  1329. }
  1330. bool Beefy::FBXReader::ReadBFFile(const StringImpl& fileName)
  1331. {
  1332. FILE* fp = fopen(fileName.c_str(), "rb");
  1333. if (fp == NULL)
  1334. return false;
  1335. FileStream fs;
  1336. fs.mFP = fp;
  1337. int version = fs.ReadInt32();
  1338. if (version != BF_MODEL_VERSION)
  1339. return false;
  1340. String fileDir = GetFileDir(fileName);
  1341. String checkFileName = fs.ReadAscii32SizedString();
  1342. int64 storedTime = fs.ReadInt64();
  1343. int64 localTime = GetFileTimeWrite(fileDir + checkFileName);
  1344. if (storedTime != localTime)
  1345. return false;
  1346. checkFileName = fs.ReadAscii32SizedString();
  1347. storedTime = fs.ReadInt64();
  1348. if (checkFileName.length() != 0)
  1349. {
  1350. localTime = GetFileTimeWrite(fileDir + checkFileName);
  1351. if (storedTime != localTime)
  1352. return false;
  1353. }
  1354. mModelDef->mFrameRate = fs.ReadFloat();
  1355. mModelDef->mMeshes.resize(fs.ReadInt32());
  1356. for (int meshIdx = 0; meshIdx < (int) mModelDef->mMeshes.size(); meshIdx++)
  1357. {
  1358. ModelMesh* modelMesh = &mModelDef->mMeshes[meshIdx];
  1359. modelMesh->mName = fs.ReadAscii32SizedString();
  1360. modelMesh->mIndices.resize(fs.ReadInt32());
  1361. fs.Read((void*)&modelMesh->mIndices[0], modelMesh->mIndices.size() * sizeof(modelMesh->mIndices[0]));
  1362. modelMesh->mVertices.resize(fs.ReadInt32());
  1363. fs.Read((void*)&modelMesh->mVertices[0], modelMesh->mVertices.size() * sizeof(modelMesh->mVertices[0]));
  1364. modelMesh->mTexFileName = fs.ReadAscii32SizedString();
  1365. modelMesh->mBumpFileName = fs.ReadAscii32SizedString();
  1366. }
  1367. mModelDef->mJoints.resize(fs.ReadInt32());
  1368. for (int jointIdx = 0; jointIdx < (int)mModelDef->mJoints.size(); jointIdx++)
  1369. {
  1370. ModelJoint* modelJoint = &mModelDef->mJoints[jointIdx];
  1371. modelJoint->mName = fs.ReadAscii32SizedString();
  1372. modelJoint->mParentIdx = fs.ReadInt32();
  1373. fs.ReadT(modelJoint->mPoseInvMatrix);
  1374. }
  1375. mModelDef->mAnims.resize(fs.ReadInt32());
  1376. for (int animIdx = 0; animIdx < (int)mModelDef->mAnims.size(); animIdx++)
  1377. {
  1378. ModelAnimation* modelAnim = &mModelDef->mAnims[animIdx];
  1379. modelAnim->mName = fs.ReadAscii32SizedString();
  1380. modelAnim->mFrames.resize(fs.ReadInt32());
  1381. for (int frameIdx = 0; frameIdx < (int)modelAnim->mFrames.size(); frameIdx++)
  1382. {
  1383. ModelAnimationFrame* frame = &modelAnim->mFrames[frameIdx];
  1384. frame->mJointTranslations.resize(mModelDef->mJoints.size());
  1385. fs.Read((void*)&frame->mJointTranslations[0], (int)frame->mJointTranslations.size() * sizeof(frame->mJointTranslations[0]));
  1386. }
  1387. }
  1388. return true;
  1389. }
  1390. ////
  1391. BF_EXPORT ModelDef* BF_CALLTYPE Res_OpenFBX(const char* fileName, VertexDefinition* vertexDefinition)
  1392. {
  1393. ModelDef* modelDef = new ModelDef();
  1394. FBXReader fbxReader(modelDef);
  1395. if (!fbxReader.ReadFile(fileName))
  1396. {
  1397. delete modelDef;
  1398. return NULL;
  1399. }
  1400. return modelDef;
  1401. }
  1402. #else
  1403. BF_EXPORT void* BF_CALLTYPE Res_OpenFBX(const char* fileName, void* vertexDefinition)
  1404. {
  1405. return NULL;
  1406. }
  1407. #endif