FBXSceneEncoder.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. #ifdef USE_FBX
  2. #include <algorithm>
  3. #include <string>
  4. #include "FBXSceneEncoder.h"
  5. #include "EncoderArguments.h"
  6. using namespace gameplay;
  7. /**
  8. * Returns the aspect ratio from the given camera.
  9. *
  10. * @param fbxCamera The FBX camera to get the aspect ratio from.
  11. *
  12. * @return The aspect ratio from the camera.
  13. */
  14. static float getAspectRatio(FbxCamera* fbxCamera);
  15. /**
  16. * Returns the field of view Y from the given camera.
  17. *
  18. * @param fbxCamera The camera to get the fiew of view from.
  19. *
  20. * @return The field of view Y.
  21. */
  22. static float getFieldOfView(FbxCamera* fbxCamera);
  23. /**
  24. * Loads the texture coordinates from given mesh's polygon part into the vertex.
  25. *
  26. * @param fbxMesh The mesh to get the polygon from.
  27. * @param uvs The UV list to load tex coords from.
  28. * @param uvSetIndex The UV set index of the uvs.
  29. * @param polyIndex The index of the polygon in the mesh.
  30. * @param posInPoly The position of the vertex in the polygon.
  31. * @param meshVertexIndex The index of the vertex in the mesh.
  32. * @param vertex The vertex to copy the texture coordinates to.
  33. */
  34. static void loadTextureCoords(FbxMesh* fbxMesh, const FbxGeometryElementUV* uvs, int uvSetIndex, int polyIndex, int posInPoly, int meshVertexIndex, Vertex* vertex);
  35. /**
  36. * Loads the normal from the mesh and adds it to the given vertex.
  37. *
  38. * @param fbxMesh The mesh to get the polygon from.
  39. * @param vertexIndex The vertex index in the mesh.
  40. * @param controlPointIndex The control point index.
  41. * @param vertex The vertex to copy to.
  42. */
  43. static void loadNormal(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex);
  44. /**
  45. * Loads the tangent from the mesh and adds it to the given vertex.
  46. *
  47. * @param fbxMesh The mesh to load from.
  48. * @param vertexIndex The index of the vertex within fbxMesh.
  49. * @param controlPointIndex The control point index.
  50. * @param vertex The vertex to copy to.
  51. */
  52. static void loadTangent(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex);
  53. /**
  54. * Loads the binormal from the mesh and adds it to the given vertex.
  55. *
  56. * @param fbxMesh The mesh to load from.
  57. * @param vertexIndex The index of the vertex within fbxMesh.
  58. * @param controlPointIndex The control point index.
  59. * @param vertex The vertex to copy to.
  60. */
  61. static void loadBinormal(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex);
  62. /**
  63. * Loads the vertex diffuse color from the mesh and adds it to the given vertex.
  64. *
  65. * @param fbxMesh The mesh to load from.
  66. * @param vertexIndex The index of the vertex within fbxMesh.
  67. * @param controlPointIndex The control point index.
  68. * @param vertex The vertex to copy to.
  69. */
  70. static void loadVertexColor(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex);
  71. /**
  72. * Loads the blend weight and blend indices data into the vertex.
  73. *
  74. * @param vertexWeights List of vertex weights. The x member contains the blendIndices. The y member contains the blendWeights.
  75. * @param vertex The vertex to copy the blend data to.
  76. */
  77. static void loadBlendData(const std::vector<Vector2>& vertexWeights, Vertex* vertex);
  78. /**
  79. * Loads the blend weights and blend indices from the given mesh.
  80. *
  81. * Each element of weights is a list of Vector2s where "x" is the blend index and "y" is the blend weight.
  82. *
  83. * @param fbxMesh The mesh to load from.
  84. * @param weights List of blend weights and blend indices for each vertex.
  85. *
  86. * @return True if this mesh has a mesh skin, false otherwise.
  87. */
  88. static bool loadBlendWeights(FbxMesh* fbxMesh, std::vector<std::vector<Vector2> >& weights);
  89. /**
  90. * Copies from an FBX matrix to a float[16] array.
  91. */
  92. static void copyMatrix(const FbxMatrix& fbxMatrix, float* matrix);
  93. /**
  94. * Copies from an FBX matrix to a gameplay matrix.
  95. */
  96. static void copyMatrix(const FbxMatrix& fbxMatrix, Matrix& matrix);
  97. /**
  98. * Finds the min and max start time and stop time of the given animation curve.
  99. *
  100. * startTime is updated if the animation curve contains a start time that is less than startTime.
  101. * stopTime is updated if the animation curve contains a stop time that is greater than stopTime.
  102. * frameRate is updated if the animation curve contains a frame rate that is greater than frameRate.
  103. *
  104. * @param animCurve The animation curve to read from.
  105. * @param startTime The min start time. (in/out)
  106. * @param stopTime The max stop time. (in/out)
  107. * @param frameRate The frame rate. (in/out)
  108. */
  109. static void findMinMaxTime(FbxAnimCurve* animCurve, float* startTime, float* stopTime, float* frameRate);
  110. /**
  111. * Appends key frame data to the given node for the specified animation target attribute.
  112. *
  113. * @param fbxNode The node to get the matrix transform from.
  114. * @param channel The aniamtion channel to write values into.
  115. * @param time The time of the keyframe.
  116. * @param scale The evaluated scale for the keyframe.
  117. * @param rotation The evalulated rotation for the keyframe.
  118. * @param translation The evalulated translation for the keyframe.
  119. */
  120. static void appendKeyFrame(FbxNode* fbxNode, AnimationChannel* channel, float time, const Vector3& scale, const Quaternion& rotation, const Vector3& translation);
  121. /**
  122. * Decomposes the given node's matrix transform at the given time and copies to scale, rotation and translation.
  123. *
  124. * @param fbxNode The node to get the matrix transform from.
  125. * @param time The time to get the matrix transform from.
  126. * @param scale The scale to copy to.
  127. * @param rotation The rotation to copy to.
  128. * @param translation The translation to copy to.
  129. */
  130. static void decompose(FbxNode* fbxNode, float time, Vector3* scale, Quaternion* rotation, Vector3* translation);
  131. /**
  132. * Creates an animation channel that targets the given node and target attribute using the given key times and key values.
  133. *
  134. * @param fbxNode The node to target.
  135. * @param targetAttrib The attribute type to target.
  136. * @param keyTimes The key times for the animation channel.
  137. * @param keyValues The key values for the animation channel.
  138. *
  139. * @return The newly created animation channel.
  140. */
  141. static AnimationChannel* createAnimationChannel(FbxNode* fbxNode, unsigned int targetAttrib, const std::vector<float>& keyTimes, const std::vector<float>& keyValues);
  142. void addScaleChannel(Animation* animation, FbxNode* fbxNode, float startTime, float stopTime);
  143. void addTranslateChannel(Animation* animation, FbxNode* fbxNode, float startTime, float stopTime);
  144. /**
  145. * Determines if it is possible to automatically group animations for mesh skins.
  146. *
  147. * @param fbxScene The FBX scene to search.
  148. *
  149. * @return True if there is at least one mesh skin that has animations that can be grouped.
  150. */
  151. bool isGroupAnimationPossible(FbxScene* fbxScene);
  152. bool isGroupAnimationPossible(FbxNode* fbxNode);
  153. bool isGroupAnimationPossible(FbxMesh* fbxMesh);
  154. /**
  155. * Recursively generates the tangents and binormals for all nodes that were specified in the command line arguments.
  156. */
  157. void generateTangentsAndBinormals(FbxNode* fbxNode, const EncoderArguments& arguments);
  158. FbxAnimCurve* getCurve(FbxPropertyT<FbxDouble3>& prop, FbxAnimLayer* animLayer, const char* pChannel)
  159. {
  160. #if FBXSDK_VERSION_MAJOR == 2013 && FBXSDK_VERSION_MINOR == 1
  161. return prop.GetCurve<FbxAnimCurve>(animLayer, pChannel);
  162. #else
  163. return prop.GetCurve(animLayer, pChannel);
  164. #endif
  165. }
  166. ////////////////////////////////////
  167. // Member Functions
  168. ////////////////////////////////////
  169. FBXSceneEncoder::FBXSceneEncoder()
  170. : _groupAnimation(NULL), _autoGroupAnimations(false)
  171. {
  172. }
  173. FBXSceneEncoder::~FBXSceneEncoder()
  174. {
  175. }
  176. void FBXSceneEncoder::write(const std::string& filepath, const EncoderArguments& arguments)
  177. {
  178. FbxManager* sdkManager = FbxManager::Create();
  179. FbxIOSettings *ios = FbxIOSettings::Create(sdkManager, IOSROOT);
  180. sdkManager->SetIOSettings(ios);
  181. FbxImporter* importer = FbxImporter::Create(sdkManager,"");
  182. if (!importer->Initialize(filepath.c_str(), -1, sdkManager->GetIOSettings()))
  183. {
  184. LOG(1, "Call to FbxImporter::Initialize() failed.\n");
  185. LOG(1, "Error returned: %s\n\n", importer->GetLastErrorString());
  186. exit(-1);
  187. }
  188. FbxScene* fbxScene = FbxScene::Create(sdkManager,"__FBX_SCENE__");
  189. print("Loading FBX file.");
  190. importer->Import(fbxScene);
  191. importer->Destroy();
  192. // Determine if animations should be grouped.
  193. if (arguments.getGroupAnimationAnimationId().empty() && isGroupAnimationPossible(fbxScene))
  194. {
  195. if (promptUserGroupAnimations())
  196. {
  197. _autoGroupAnimations = true;
  198. }
  199. }
  200. if (arguments.tangentBinormalIdCount() > 0)
  201. generateTangentsAndBinormals(fbxScene->GetRootNode(), arguments);
  202. print("Loading Scene.");
  203. loadScene(fbxScene);
  204. print("Loading animations.");
  205. loadAnimations(fbxScene, arguments);
  206. sdkManager->Destroy();
  207. print("Optimizing GamePlay Binary.");
  208. _gamePlayFile.adjust();
  209. if (_autoGroupAnimations)
  210. {
  211. _gamePlayFile.groupMeshSkinAnimations();
  212. }
  213. std::string outputFilePath = arguments.getOutputFilePath();
  214. if (arguments.textOutputEnabled())
  215. {
  216. int pos = outputFilePath.find_last_of('.');
  217. if (pos > 2)
  218. {
  219. std::string path = outputFilePath.substr(0, pos);
  220. path.append(".xml");
  221. LOG(1, "Saving debug file: %s\n", path.c_str());
  222. if (!_gamePlayFile.saveText(path))
  223. {
  224. LOG(1, "Error writing text file: %s\n", path.c_str());
  225. }
  226. }
  227. }
  228. else
  229. {
  230. LOG(1, "Saving binary file: %s\n", outputFilePath.c_str());
  231. if (!_gamePlayFile.saveBinary(outputFilePath))
  232. {
  233. LOG(1, "Error writing binary file: %s\n", outputFilePath.c_str());
  234. }
  235. }
  236. }
  237. void FBXSceneEncoder::loadScene(FbxScene* fbxScene)
  238. {
  239. Scene* scene = new Scene();
  240. scene->setId(fbxScene->GetName());
  241. if (scene->getId().length() == 0)
  242. {
  243. scene->setId("__SCENE__");
  244. }
  245. // Load all of the nodes and their contents.
  246. FbxNode* rootNode = fbxScene->GetRootNode();
  247. if (rootNode)
  248. {
  249. print("Triangulate.");
  250. triangulateRecursive(rootNode);
  251. print("Load nodes.");
  252. // Don't include the FBX root node in the GPB.
  253. const int childCount = rootNode->GetChildCount();
  254. for (int i = 0; i < childCount; ++i)
  255. {
  256. Node* node = loadNode(rootNode->GetChild(i));
  257. if (node)
  258. {
  259. scene->add(node);
  260. }
  261. }
  262. }
  263. // Load the MeshSkin information from the scene's poses.
  264. loadBindShapes(fbxScene);
  265. // Find the ambient light of the scene
  266. FbxColor ambientColor = fbxScene->GetGlobalSettings().GetAmbientColor();
  267. scene->setAmbientColor((float)ambientColor.mRed, (float)ambientColor.mGreen, (float)ambientColor.mBlue);
  268. // Assign the first camera node (if there is one) in the scene as the active camera
  269. // This ensures that if there's a camera in the scene that it is assigned as the
  270. // active camera.
  271. // TODO: add logic to find the "active" camera node in the fbxScene
  272. scene->setActiveCameraNode(scene->getFirstCameraNode());
  273. _gamePlayFile.addScene(scene);
  274. }
  275. void FBXSceneEncoder::loadAnimationChannels(FbxAnimLayer* animLayer, FbxNode* fbxNode, Animation* animation)
  276. {
  277. const char* name = fbxNode->GetName();
  278. //Node* node = _gamePlayFile.getNode(name);
  279. // Determine which properties are animated on this node
  280. // Find the transform at each key frame
  281. // TODO: Ignore properties that are not animated (scale, rotation, translation)
  282. // This should result in only one animation channel per animated node.
  283. float startTime = FLT_MAX, stopTime = -1.0f, frameRate = -FLT_MAX;
  284. bool tx = false, ty = false, tz = false, rx = false, ry = false, rz = false, sx = false, sy = false, sz = false;
  285. FbxAnimCurve* animCurve = NULL;
  286. animCurve = getCurve(fbxNode->LclTranslation, animLayer, FBXSDK_CURVENODE_COMPONENT_X);
  287. if (animCurve)
  288. {
  289. tx = true;
  290. findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate);
  291. }
  292. animCurve = getCurve(fbxNode->LclTranslation, animLayer, FBXSDK_CURVENODE_COMPONENT_Y);
  293. if (animCurve)
  294. {
  295. ty = true;
  296. findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate);
  297. }
  298. animCurve = getCurve(fbxNode->LclTranslation, animLayer, FBXSDK_CURVENODE_COMPONENT_Z);
  299. if (animCurve)
  300. {
  301. tz = true;
  302. findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate);
  303. }
  304. animCurve = getCurve(fbxNode->LclRotation, animLayer, FBXSDK_CURVENODE_COMPONENT_X);
  305. if (animCurve)
  306. {
  307. rx = true;
  308. findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate);
  309. }
  310. animCurve = getCurve(fbxNode->LclRotation, animLayer, FBXSDK_CURVENODE_COMPONENT_Y);
  311. if (animCurve)
  312. {
  313. ry = true;
  314. findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate);
  315. }
  316. animCurve = getCurve(fbxNode->LclRotation, animLayer, FBXSDK_CURVENODE_COMPONENT_Z);
  317. if (animCurve)
  318. {
  319. rz = true;
  320. findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate);
  321. }
  322. animCurve = getCurve(fbxNode->LclScaling, animLayer, FBXSDK_CURVENODE_COMPONENT_X);
  323. if (animCurve)
  324. {
  325. sx = true;
  326. findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate);
  327. }
  328. animCurve = getCurve(fbxNode->LclScaling, animLayer, FBXSDK_CURVENODE_COMPONENT_Y);
  329. if (animCurve)
  330. {
  331. sy = true;
  332. findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate);
  333. }
  334. animCurve = getCurve(fbxNode->LclScaling, animLayer, FBXSDK_CURVENODE_COMPONENT_Z);
  335. if (animCurve)
  336. {
  337. sz = true;
  338. findMinMaxTime(animCurve, &startTime, &stopTime, &frameRate);
  339. }
  340. if (!(sx || sy || sz || rx || ry || rz || tx || ty || tz))
  341. return; // no animation channels
  342. assert(startTime != FLT_MAX);
  343. assert(stopTime >= 0.0f);
  344. // Determine which animation channels to create
  345. std::vector<unsigned int> channelAttribs;
  346. if (sx && sy && sz)
  347. {
  348. if (rx || ry || rz)
  349. {
  350. if (tx && ty && tz)
  351. {
  352. channelAttribs.push_back(Transform::ANIMATE_SCALE_ROTATE_TRANSLATE);
  353. }
  354. else
  355. {
  356. channelAttribs.push_back(Transform::ANIMATE_SCALE_ROTATE);
  357. if (tx)
  358. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_X);
  359. if (ty)
  360. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Y);
  361. if (tz)
  362. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Z);
  363. }
  364. }
  365. else
  366. {
  367. if (tx && ty && tz)
  368. {
  369. channelAttribs.push_back(Transform::ANIMATE_SCALE_TRANSLATE);
  370. }
  371. else
  372. {
  373. channelAttribs.push_back(Transform::ANIMATE_SCALE);
  374. if (tx)
  375. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_X);
  376. if (ty)
  377. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Y);
  378. if (tz)
  379. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Z);
  380. }
  381. }
  382. }
  383. else
  384. {
  385. if (rx || ry || rz)
  386. {
  387. if (tx && ty && tz)
  388. {
  389. channelAttribs.push_back(Transform::ANIMATE_ROTATE_TRANSLATE);
  390. }
  391. else
  392. {
  393. channelAttribs.push_back(Transform::ANIMATE_ROTATE);
  394. if (tx)
  395. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_X);
  396. if (ty)
  397. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Y);
  398. if (tz)
  399. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Z);
  400. }
  401. }
  402. else
  403. {
  404. if (tx && ty && tz)
  405. {
  406. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE);
  407. }
  408. else
  409. {
  410. if (tx)
  411. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_X);
  412. if (ty)
  413. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Y);
  414. if (tz)
  415. channelAttribs.push_back(Transform::ANIMATE_TRANSLATE_Z);
  416. }
  417. }
  418. if (sx)
  419. channelAttribs.push_back(Transform::ANIMATE_SCALE_X);
  420. if (sy)
  421. channelAttribs.push_back(Transform::ANIMATE_SCALE_Y);
  422. if (sz)
  423. channelAttribs.push_back(Transform::ANIMATE_SCALE_Z);
  424. }
  425. unsigned int channelCount = channelAttribs.size();
  426. assert(channelCount > 0);
  427. // Allocate channel list
  428. int channelStart = animation->getAnimationChannelCount();
  429. for (unsigned int i = 0; i < channelCount; ++i)
  430. {
  431. AnimationChannel* channel = new AnimationChannel();
  432. channel->setTargetId(name);
  433. channel->setInterpolation(AnimationChannel::LINEAR);
  434. channel->setTargetAttribute(channelAttribs[i]);
  435. animation->add(channel);
  436. }
  437. // Evaulate animation curve in increments of frameRate and populate channel data.
  438. FbxAMatrix fbxMatrix;
  439. Matrix matrix;
  440. double increment = 1000.0 / (double)frameRate;
  441. int frameCount = (int)ceil((double)(stopTime - startTime) / increment) + 1; // +1 because stop time is inclusive
  442. for (int frame = 0; frame < frameCount; ++frame)
  443. {
  444. double time = startTime + (frame * (double)increment);
  445. // Note: We used to clamp time to stop time, but FBX sdk does not always produce
  446. // and accurate stopTime (sometimes it is rounded down for some reason), so I'm
  447. // disabling this clamping for now as it seems more accurate under normal circumstances.
  448. //time = std::min(time, (double)stopTime);
  449. // Evalulate the animation at this time
  450. FbxTime kTime;
  451. kTime.SetMilliSeconds((FbxLongLong)time);
  452. fbxMatrix = fbxNode->EvaluateLocalTransform(kTime);
  453. copyMatrix(fbxMatrix, matrix);
  454. // Decompose the evalulated transformation matrix into separate
  455. // scale, rotation and translation.
  456. Vector3 scale;
  457. Quaternion rotation;
  458. Vector3 translation;
  459. matrix.decompose(&scale, &rotation, &translation);
  460. rotation.normalize();
  461. // Append keyframe data to all channels
  462. for (unsigned int i = channelStart, channelEnd = channelStart + channelCount; i < channelEnd; ++i)
  463. {
  464. appendKeyFrame(fbxNode, animation->getAnimationChannel(i), time, scale, rotation, translation);
  465. }
  466. }
  467. if (_groupAnimation != animation)
  468. {
  469. // TODO explain
  470. _gamePlayFile.addAnimation(animation);
  471. }
  472. }
  473. void FBXSceneEncoder::loadAnimationLayer(FbxAnimLayer* fbxAnimLayer, FbxNode* fbxNode, const EncoderArguments& arguments)
  474. {
  475. bool animationGroupId = false;
  476. const char* name = fbxNode->GetName();
  477. // Check if this node's animations are supposed to be grouped
  478. if (name && arguments.containsGroupNodeId(name))
  479. {
  480. animationGroupId = true;
  481. _groupAnimation = new Animation();
  482. _groupAnimation->setId(arguments.getAnimationId(name));
  483. }
  484. Animation* animation = _groupAnimation;
  485. if (!animation)
  486. {
  487. animation = new Animation();
  488. animation->setId(name);
  489. }
  490. loadAnimationChannels(fbxAnimLayer, fbxNode, animation);
  491. const int childCount = fbxNode->GetChildCount();
  492. for (int modelCount = 0; modelCount < childCount; ++modelCount)
  493. {
  494. loadAnimationLayer(fbxAnimLayer, fbxNode->GetChild(modelCount), arguments);
  495. }
  496. if (animationGroupId)
  497. {
  498. _gamePlayFile.addAnimation(_groupAnimation);
  499. _groupAnimation = NULL;
  500. }
  501. }
  502. void FBXSceneEncoder::loadAnimations(FbxScene* fbxScene, const EncoderArguments& arguments)
  503. {
  504. FbxAnimEvaluator* evaluator = fbxScene->GetEvaluator();
  505. if (!evaluator)
  506. return;
  507. FbxAnimStack* animStack = evaluator->GetContext();
  508. if (!animStack)
  509. return;
  510. for (int i = 0; i < fbxScene->GetSrcObjectCount(FBX_TYPE(FbxAnimStack)); ++i)
  511. {
  512. FbxAnimStack* animStack = FbxCast<FbxAnimStack>(fbxScene->GetSrcObject(FBX_TYPE(FbxAnimStack), i));
  513. int nbAnimLayers = animStack->GetMemberCount(FBX_TYPE(FbxAnimLayer));
  514. for (int l = 0; l < nbAnimLayers; ++l)
  515. {
  516. FbxAnimLayer* animLayer = animStack->GetMember(FBX_TYPE(FbxAnimLayer), l);
  517. loadAnimationLayer(animLayer, fbxScene->GetRootNode(), arguments);
  518. }
  519. }
  520. }
  521. Node* FBXSceneEncoder::loadNode(FbxNode* fbxNode)
  522. {
  523. Node* node = NULL;
  524. // Check if this node has already been loaded
  525. const char* id = fbxNode->GetName();
  526. if (id && strlen(id) > 0)
  527. {
  528. node = _gamePlayFile.getNode(fbxNode->GetName());
  529. if (node)
  530. {
  531. return node;
  532. }
  533. }
  534. node = new Node();
  535. if (id)
  536. {
  537. node->setId(id);
  538. }
  539. _gamePlayFile.addNode(node);
  540. transformNode(fbxNode, node);
  541. loadCamera(fbxNode, node);
  542. loadLight(fbxNode, node);
  543. loadModel(fbxNode, node);
  544. if (fbxNode->GetSkeleton())
  545. {
  546. // Indicate that this is a joint node for the purpose of debugging.
  547. // The XML debug output will print that this node is a joint.
  548. node->setIsJoint(true);
  549. }
  550. // Load child nodes
  551. const int childCount = fbxNode->GetChildCount();
  552. for (int i = 0; i < childCount; ++i)
  553. {
  554. Node* child = loadNode(fbxNode->GetChild(i));
  555. if (child)
  556. {
  557. node->addChild(child);
  558. }
  559. }
  560. return node;
  561. }
  562. Mesh* FBXSceneEncoder::getMesh(FbxUInt64 meshId)
  563. {
  564. // Check if this mesh was already loaded.
  565. std::map<FbxUInt64, Mesh*>::iterator it = _meshes.find(meshId);
  566. if (it != _meshes.end())
  567. {
  568. return it->second;
  569. }
  570. return NULL;
  571. }
  572. void FBXSceneEncoder::saveMesh(FbxUInt64 meshId, Mesh* mesh)
  573. {
  574. assert(mesh);
  575. if (!getMesh(meshId))
  576. {
  577. _meshes[meshId] = mesh;
  578. }
  579. }
  580. void FBXSceneEncoder::print(const char* str)
  581. {
  582. LOG(1, "%s\n", str);
  583. }
  584. void FBXSceneEncoder::transformNode(FbxNode* fbxNode, Node* node)
  585. {
  586. FbxAMatrix matrix;
  587. if (fbxNode->GetCamera() || fbxNode->GetLight())
  588. {
  589. // TODO: Why is this necessary for Camera and Light?
  590. matrix.SetTRS(fbxNode->LclTranslation.Get(), fbxNode->LclRotation.Get(), fbxNode->LclScaling.Get());
  591. }
  592. else
  593. {
  594. matrix = fbxNode->EvaluateLocalTransform();
  595. }
  596. float m[16];
  597. copyMatrix(matrix, m);
  598. node->setTransformMatrix(m);
  599. }
  600. void FBXSceneEncoder::loadBindShapes(FbxScene* fbxScene)
  601. {
  602. float m[16];
  603. const int poseCount = fbxScene->GetPoseCount();
  604. for (int i = 0; i < poseCount; ++i)
  605. {
  606. FbxPose* pose = fbxScene->GetPose(i);
  607. assert(pose);
  608. if (pose->IsBindPose() && pose->GetCount() > 0)
  609. {
  610. FbxNode* fbxNode = pose->GetNode(0);
  611. if (fbxNode->GetMesh() != NULL)
  612. {
  613. Node* node = _gamePlayFile.getNode(fbxNode->GetName());
  614. assert(node && node->getModel());
  615. Model* model = node->getModel();
  616. if (model && model->getSkin())
  617. {
  618. MeshSkin* skin = model->getSkin();
  619. copyMatrix(pose->GetMatrix(0), m);
  620. skin->setBindShape(m);
  621. }
  622. }
  623. }
  624. }
  625. }
  626. void FBXSceneEncoder::loadCamera(FbxNode* fbxNode, Node* node)
  627. {
  628. FbxCamera* fbxCamera = fbxNode->GetCamera();
  629. if (!fbxCamera)
  630. {
  631. return;
  632. }
  633. Camera* camera = new Camera();
  634. const char* name = fbxNode->GetName();
  635. if (name)
  636. {
  637. std::string id(name);
  638. id.append("_Camera");
  639. camera->setId(id);
  640. }
  641. camera->setAspectRatio(getAspectRatio(fbxCamera));
  642. camera->setNearPlane((float)fbxCamera->NearPlane.Get());
  643. camera->setFarPlane((float)fbxCamera->FarPlane.Get());
  644. if (fbxCamera->ProjectionType.Get() == FbxCamera::eOrthogonal)
  645. {
  646. camera->setOrthographic();
  647. camera->setViewportWidth((float)fbxCamera->GetApertureWidth());
  648. camera->setViewportWidth((float)fbxCamera->GetApertureHeight());
  649. // xmag in FBX can be calculated from: OrthoZoom * 30.0 / 2.0
  650. camera->setViewportWidth((float)fbxCamera->OrthoZoom.Get() * 15.0f);
  651. }
  652. else if (fbxCamera->ProjectionType.Get() == FbxCamera::ePerspective)
  653. {
  654. camera->setPerspective();
  655. camera->setFieldOfView(getFieldOfView(fbxCamera));
  656. }
  657. else
  658. {
  659. LOG(2, "Warning: Unknown camera type in node.\n");
  660. return;
  661. }
  662. _gamePlayFile.addCamera(camera);
  663. node->setCamera(camera);
  664. }
  665. void FBXSceneEncoder::loadLight(FbxNode* fbxNode, Node* node)
  666. {
  667. FbxLight* fbxLight = fbxNode->GetLight();
  668. if (!fbxLight)
  669. {
  670. return;
  671. }
  672. Light* light = new Light();
  673. const char* name = fbxNode->GetName();
  674. if (name)
  675. {
  676. std::string id(name);
  677. id.append("_Light");
  678. light->setId(id);
  679. }
  680. FbxDouble3 color = fbxLight->Color.Get();
  681. light->setColor((float)color[0], (float)color[1], (float)color[2]);
  682. switch (fbxLight->LightType.Get())
  683. {
  684. case FbxLight::ePoint:
  685. {
  686. FbxLight::EDecayType decayType = fbxLight->DecayType.Get();
  687. switch (decayType)
  688. {
  689. case FbxLight::eNone:
  690. // No decay. Can assume we have an ambient light, because ambient lights in the scene are
  691. // converted to point lights with no decay when exporting to FBX.
  692. light->setAmbientLight();
  693. break;
  694. case FbxLight::eLinear:
  695. light->setPointLight();
  696. light->setLinearAttenuation((float)fbxLight->DecayStart.Get());
  697. break;
  698. case FbxLight::eQuadratic:
  699. light->setPointLight();
  700. light->setQuadraticAttenuation((float)fbxLight->DecayStart.Get());
  701. break;
  702. case FbxLight::eCubic:
  703. default:
  704. // Not supported..
  705. break;
  706. }
  707. break;
  708. }
  709. case FbxLight::eDirectional:
  710. {
  711. light->setDirectionalLight();
  712. break;
  713. }
  714. case FbxLight::eSpot:
  715. {
  716. light->setSpotLight();
  717. FbxLight::EDecayType decayType = fbxLight->DecayType.Get();
  718. switch (decayType)
  719. {
  720. case FbxLight::eNone:
  721. // No decay.
  722. break;
  723. case FbxLight::eLinear:
  724. light->setLinearAttenuation((float)fbxLight->DecayStart.Get());
  725. break;
  726. case FbxLight::eQuadratic:
  727. light->setQuadraticAttenuation((float)fbxLight->DecayStart.Get());
  728. break;
  729. case FbxLight::eCubic:
  730. // Not supported..
  731. break;
  732. }
  733. light->setFalloffAngle(MATH_DEG_TO_RAD((float)fbxLight->OuterAngle.Get())); // fall off angle
  734. break;
  735. }
  736. default:
  737. {
  738. LOG(2, "Warning: Unknown light type in node.\n");
  739. return;
  740. }
  741. }
  742. _gamePlayFile.addLight(light);
  743. node->setLight(light);
  744. }
  745. void FBXSceneEncoder::loadModel(FbxNode* fbxNode, Node* node)
  746. {
  747. FbxMesh* fbxMesh = fbxNode->GetMesh();
  748. if (!fbxMesh)
  749. {
  750. return;
  751. }
  752. if (fbxMesh->IsTriangleMesh())
  753. {
  754. Mesh* mesh = loadMesh(fbxMesh);
  755. Model* model = new Model();
  756. model->setMesh(mesh);
  757. node->setModel(model);
  758. loadSkin(fbxMesh, model);
  759. if (model->getSkin())
  760. {
  761. // TODO: explain
  762. node->resetTransformMatrix();
  763. }
  764. }
  765. }
  766. void FBXSceneEncoder::loadSkin(FbxMesh* fbxMesh, Model* model)
  767. {
  768. const int deformerCount = fbxMesh->GetDeformerCount();
  769. for (int i = 0; i < deformerCount; ++i)
  770. {
  771. FbxDeformer* deformer = fbxMesh->GetDeformer(i);
  772. if (deformer->GetDeformerType() == FbxDeformer::eSkin)
  773. {
  774. FbxSkin* fbxSkin = static_cast<FbxSkin*>(deformer);
  775. MeshSkin* skin = new MeshSkin();
  776. std::vector<std::string> jointNames;
  777. std::vector<Node*> joints;
  778. std::vector<Matrix> bindPoses;
  779. const int clusterCount = fbxSkin->GetClusterCount();
  780. for (int j = 0; j < clusterCount; ++j)
  781. {
  782. FbxCluster* cluster = fbxSkin->GetCluster(j);
  783. assert(cluster);
  784. FbxNode* linkedNode = cluster->GetLink();
  785. if (linkedNode && linkedNode->GetSkeleton())
  786. {
  787. const char* jointName = linkedNode->GetName();
  788. assert(jointName);
  789. jointNames.push_back(jointName);
  790. Node* joint = loadNode(linkedNode);
  791. assert(joint);
  792. joints.push_back(joint);
  793. FbxAMatrix matrix;
  794. cluster->GetTransformLinkMatrix(matrix);
  795. Matrix m;
  796. copyMatrix(matrix.Inverse(), m);
  797. bindPoses.push_back(m);
  798. }
  799. }
  800. skin->setJointNames(jointNames);
  801. skin->setJoints(joints);
  802. skin->setBindPoses(bindPoses);
  803. model->setSkin(skin);
  804. break;
  805. }
  806. }
  807. }
  808. Mesh* FBXSceneEncoder::loadMesh(FbxMesh* fbxMesh)
  809. {
  810. // Check if this mesh has already been loaded.
  811. Mesh* mesh = getMesh(fbxMesh->GetUniqueID());
  812. if (mesh)
  813. {
  814. return mesh;
  815. }
  816. mesh = new Mesh();
  817. // GamePlay requires that a mesh have a unique ID but FbxMesh doesn't have a string ID.
  818. const char* name = fbxMesh->GetNode()->GetName();
  819. if (name)
  820. {
  821. std::string id(name);
  822. id.append("_Mesh");
  823. mesh->setId(id);
  824. }
  825. // The number of mesh parts is equal to the number of materials that affect this mesh.
  826. // There is always at least one mesh part.
  827. std::vector<MeshPart*> meshParts;
  828. const int materialCount = fbxMesh->GetNode()->GetMaterialCount();
  829. int meshPartSize = (materialCount > 0) ? materialCount : 1;
  830. for (int i = 0; i < meshPartSize; ++i)
  831. {
  832. meshParts.push_back(new MeshPart());
  833. }
  834. // Find the blend weights and blend indices if this mesh is skinned.
  835. std::vector<std::vector<Vector2> > weights;
  836. bool hasSkin = loadBlendWeights(fbxMesh, weights);
  837. // Get list of uv sets for mesh
  838. FbxStringList uvSetNameList;
  839. fbxMesh->GetUVSetNames(uvSetNameList);
  840. const int uvSetCount = uvSetNameList.GetCount();
  841. int vertexIndex = 0;
  842. FbxVector4* controlPoints = fbxMesh->GetControlPoints();
  843. const int polygonCount = fbxMesh->GetPolygonCount();
  844. for (int polyIndex = 0; polyIndex < polygonCount; ++polyIndex)
  845. {
  846. const int polygonSize = fbxMesh->GetPolygonSize(polyIndex);
  847. for (int posInPoly = 0; posInPoly < polygonSize; ++posInPoly)
  848. {
  849. int controlPointIndex = fbxMesh->GetPolygonVertex(polyIndex, posInPoly);
  850. Vertex vertex;
  851. FbxVector4& position = controlPoints[controlPointIndex];
  852. vertex.position.x = (float)position[0];
  853. vertex.position.y = (float)position[1];
  854. vertex.position.z = (float)position[2];
  855. // Load tex coords for all uv sets
  856. for (int uvSetIndex = 0; uvSetIndex < uvSetCount; ++uvSetIndex)
  857. {
  858. const FbxGeometryElementUV* uvElement = fbxMesh->GetElementUV(uvSetNameList.GetStringAt(uvSetIndex));
  859. if (uvElement)
  860. loadTextureCoords(fbxMesh, uvElement, uvSetIndex, polyIndex, posInPoly, vertexIndex, &vertex);
  861. }
  862. // Load other data
  863. loadNormal(fbxMesh, vertexIndex, controlPointIndex, &vertex);
  864. loadTangent(fbxMesh, vertexIndex, controlPointIndex, &vertex);
  865. loadBinormal(fbxMesh, vertexIndex, controlPointIndex, &vertex);
  866. loadVertexColor(fbxMesh, vertexIndex, controlPointIndex, &vertex);
  867. if (hasSkin)
  868. {
  869. loadBlendData(weights[controlPointIndex], &vertex);
  870. }
  871. // Determine which mesh part this vertex index should be added to based on the material that affects it.
  872. int meshPartIndex = 0;
  873. const int elementMatrialCount = fbxMesh->GetElementMaterialCount();
  874. for (int k = 0; k < elementMatrialCount; ++k)
  875. {
  876. FbxGeometryElementMaterial* elementMaterial = fbxMesh->GetElementMaterial(k);
  877. meshPartIndex = elementMaterial->GetIndexArray().GetAt(polyIndex);
  878. }
  879. // Add the vertex to the mesh if it hasn't already been added and find the vertex index.
  880. unsigned int index;
  881. if (mesh->contains(vertex))
  882. {
  883. index = mesh->getVertexIndex(vertex);
  884. }
  885. else
  886. {
  887. index = mesh->addVertex(vertex);
  888. }
  889. meshParts[meshPartIndex]->addIndex(index);
  890. vertexIndex++;
  891. }
  892. }
  893. const size_t meshpartsSize = meshParts.size();
  894. for (size_t i = 0; i < meshpartsSize; ++i)
  895. {
  896. mesh->addMeshPart(meshParts[i]);
  897. }
  898. // The order that the vertex elements are add to the list matters.
  899. // It should be the same order as how the Vertex data is written.
  900. // Position
  901. mesh->addVetexAttribute(POSITION, Vertex::POSITION_COUNT);
  902. const Vertex& vertex = mesh->vertices[0];
  903. // Normals
  904. if (vertex.hasNormal)
  905. {
  906. mesh->addVetexAttribute(NORMAL, Vertex::NORMAL_COUNT);
  907. }
  908. // Tangents
  909. if (vertex.hasTangent)
  910. {
  911. mesh->addVetexAttribute(TANGENT, Vertex::TANGENT_COUNT);
  912. }
  913. // Binormals
  914. if (vertex.hasBinormal)
  915. {
  916. mesh->addVetexAttribute(BINORMAL, Vertex::BINORMAL_COUNT);
  917. }
  918. // Texture Coordinates
  919. for (unsigned int i = 0; i < MAX_UV_SETS; ++i)
  920. {
  921. if (vertex.hasTexCoord[i])
  922. {
  923. mesh->addVetexAttribute(TEXCOORD0 + i, Vertex::TEXCOORD_COUNT);
  924. }
  925. }
  926. // Diffuse Color
  927. if (vertex.hasDiffuse)
  928. {
  929. mesh->addVetexAttribute(COLOR, Vertex::DIFFUSE_COUNT);
  930. }
  931. // Skinning BlendWeights BlendIndices
  932. if (vertex.hasWeights)
  933. {
  934. mesh->addVetexAttribute(BLENDWEIGHTS, Vertex::BLEND_WEIGHTS_COUNT);
  935. mesh->addVetexAttribute(BLENDINDICES, Vertex::BLEND_INDICES_COUNT);
  936. }
  937. _gamePlayFile.addMesh(mesh);
  938. saveMesh(fbxMesh->GetUniqueID(), mesh);
  939. return mesh;
  940. }
  941. void FBXSceneEncoder::triangulateRecursive(FbxNode* fbxNode)
  942. {
  943. // Triangulate all NURBS, patch and mesh under this node recursively.
  944. FbxNodeAttribute* nodeAttribute = fbxNode->GetNodeAttribute();
  945. if (nodeAttribute)
  946. {
  947. if (nodeAttribute->GetAttributeType() == FbxNodeAttribute::eMesh ||
  948. nodeAttribute->GetAttributeType() == FbxNodeAttribute::eNurbs ||
  949. nodeAttribute->GetAttributeType() == FbxNodeAttribute::eNurbsSurface ||
  950. nodeAttribute->GetAttributeType() == FbxNodeAttribute::ePatch)
  951. {
  952. FbxGeometryConverter converter(fbxNode->GetFbxManager());
  953. converter.TriangulateInPlace(fbxNode);
  954. }
  955. }
  956. const int childCount = fbxNode->GetChildCount();
  957. for (int childIndex = 0; childIndex < childCount; ++childIndex)
  958. {
  959. triangulateRecursive(fbxNode->GetChild(childIndex));
  960. }
  961. }
  962. ////////////////////////////////////
  963. // Functions
  964. ////////////////////////////////////
  965. float getAspectRatio(FbxCamera* fbxCamera)
  966. {
  967. return (float)fbxCamera->FilmAspectRatio.Get();
  968. /*
  969. FbxCamera::ECameraAspectRatioMode camAspectRatioMode = fbxCamera->GetAspectRatioMode();
  970. double aspectX = fbxCamera->AspectWidth.Get();
  971. double aspectY = fbxCamera->AspectHeight.Get();
  972. double aspectRatio = 1.333333;
  973. switch ( camAspectRatioMode)
  974. {
  975. case FbxCamera::eWINDOW_SIZE:
  976. aspectRatio = aspectX / aspectY;
  977. break;
  978. case FbxCamera::eFIXED_RATIO:
  979. aspectRatio = aspectX;
  980. break;
  981. case FbxCamera::eFIXED_RESOLUTION:
  982. aspectRatio = aspectX / aspectY * fbxCamera->GetPixelRatio();
  983. break;
  984. case FbxCamera::eFIXED_WIDTH:
  985. aspectRatio = fbxCamera->GetPixelRatio() / aspectY;
  986. break;
  987. case FbxCamera::eFIXED_HEIGHT:
  988. aspectRatio = fbxCamera->GetPixelRatio() * aspectX;
  989. break;
  990. default:
  991. break;
  992. }
  993. return (float)aspectRatio;
  994. */
  995. }
  996. inline double vfov(double hfov, double aspect)
  997. {
  998. static const double MATH_PI_180 = 0.01745329251994329576923690768489;
  999. static const double MATH_180_PI = 57.295779513082320876798154814105;
  1000. return (2.0 * atan((aspect) * tan( (hfov * MATH_PI_180) * 0.5)) * MATH_180_PI);
  1001. }
  1002. float getFieldOfView(FbxCamera* fbxCamera)
  1003. {
  1004. double fieldOfViewX = 0.0;
  1005. double fieldOfViewY = 0.0;
  1006. double filmHeight = fbxCamera->GetApertureHeight();
  1007. double filmWidth = fbxCamera->GetApertureWidth() * fbxCamera->GetSqueezeRatio();
  1008. double apertureRatio = filmHeight / filmWidth;
  1009. if ( fbxCamera->GetApertureMode() == FbxCamera::eVertical)
  1010. {
  1011. fieldOfViewY = fbxCamera->FieldOfView.Get();
  1012. }
  1013. else if (fbxCamera->GetApertureMode() == FbxCamera::eHorizontal)
  1014. {
  1015. fieldOfViewX = fbxCamera->FieldOfView.Get();
  1016. fieldOfViewY = vfov( fieldOfViewX, apertureRatio);
  1017. }
  1018. else if (fbxCamera->GetApertureMode() == FbxCamera::eFocalLength)
  1019. {
  1020. fieldOfViewX = fbxCamera->ComputeFieldOfView(fbxCamera->FocalLength.Get());
  1021. fieldOfViewY = vfov( fieldOfViewX, apertureRatio);
  1022. }
  1023. else if (fbxCamera->GetApertureMode() == FbxCamera::eHorizAndVert)
  1024. {
  1025. fieldOfViewY = fbxCamera->FieldOfViewY.Get();
  1026. }
  1027. else
  1028. {
  1029. fieldOfViewY = 45.0;
  1030. }
  1031. return (float)fieldOfViewY;
  1032. }
  1033. void loadTextureCoords(FbxMesh* fbxMesh, const FbxGeometryElementUV* uvs, int uvSetIndex, int polyIndex, int posInPoly, int meshVertexIndex, Vertex* vertex)
  1034. {
  1035. assert(fbxMesh && polyIndex >=0 && posInPoly >= 0);
  1036. const bool useIndex = uvs->GetReferenceMode() != FbxGeometryElement::eDirect;
  1037. const int indexCount = useIndex ? uvs->GetIndexArray().GetCount() : 0;
  1038. int uvIndex = -1;
  1039. switch (uvs->GetMappingMode())
  1040. {
  1041. case FbxGeometryElement::eByControlPoint:
  1042. {
  1043. // Get the index of the current vertex in control points array
  1044. int polyVertIndex = fbxMesh->GetPolygonVertex(polyIndex, posInPoly);
  1045. // The UV index depends on the reference mode
  1046. uvIndex = useIndex ? uvs->GetIndexArray().GetAt(polyVertIndex) : polyVertIndex;
  1047. }
  1048. break;
  1049. case FbxGeometryElement::eByPolygonVertex:
  1050. if (meshVertexIndex < indexCount)
  1051. {
  1052. uvIndex = useIndex ? uvs->GetIndexArray().GetAt(meshVertexIndex) : meshVertexIndex;
  1053. }
  1054. break;
  1055. default:
  1056. // Only support eByPolygonVertex and eByControlPoint mappings
  1057. break;
  1058. }
  1059. vertex->hasTexCoord[uvSetIndex] = true;
  1060. // Store UV information in vertex
  1061. if (uvIndex != -1)
  1062. {
  1063. FbxVector2 uvValue = uvs->GetDirectArray().GetAt(uvIndex);
  1064. vertex->texCoord[uvSetIndex].x = (float)uvValue[0];
  1065. vertex->texCoord[uvSetIndex].y = (float)uvValue[1];
  1066. }
  1067. }
  1068. void loadNormal(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex)
  1069. {
  1070. if (fbxMesh->GetElementNormalCount() > 0)
  1071. {
  1072. // Get only the first
  1073. FbxGeometryElementNormal* normal = fbxMesh->GetElementNormal(0);
  1074. FbxGeometryElement::EMappingMode mappingMode = normal->GetMappingMode();
  1075. if (mappingMode == FbxGeometryElement::eByControlPoint)
  1076. {
  1077. switch (normal->GetReferenceMode())
  1078. {
  1079. case FbxGeometryElement::eDirect:
  1080. {
  1081. FbxVector4 vec4 = normal->GetDirectArray().GetAt(controlPointIndex);
  1082. vertex->hasNormal = true;
  1083. vertex->normal.x = (float)vec4[0];
  1084. vertex->normal.y = (float)vec4[1];
  1085. vertex->normal.z = (float)vec4[2];
  1086. }
  1087. break;
  1088. case FbxGeometryElement::eIndexToDirect:
  1089. {
  1090. int id = normal->GetIndexArray().GetAt(controlPointIndex);
  1091. FbxVector4 vec4 = normal->GetDirectArray().GetAt(id);
  1092. vertex->hasNormal = true;
  1093. vertex->normal.x = (float)vec4[0];
  1094. vertex->normal.y = (float)vec4[1];
  1095. vertex->normal.z = (float)vec4[2];
  1096. }
  1097. break;
  1098. default:
  1099. break;
  1100. }
  1101. }
  1102. else if (mappingMode == FbxGeometryElement::eByPolygonVertex)
  1103. {
  1104. switch (normal->GetReferenceMode())
  1105. {
  1106. case FbxGeometryElement::eDirect:
  1107. {
  1108. FbxVector4 vec4 = normal->GetDirectArray().GetAt(vertexIndex);
  1109. vertex->hasNormal = true;
  1110. vertex->normal.x = (float)vec4[0];
  1111. vertex->normal.y = (float)vec4[1];
  1112. vertex->normal.z = (float)vec4[2];
  1113. }
  1114. break;
  1115. case FbxGeometryElement::eIndexToDirect:
  1116. {
  1117. int id = normal->GetIndexArray().GetAt(vertexIndex);
  1118. FbxVector4 vec4 = normal->GetDirectArray().GetAt(id);
  1119. vertex->hasNormal = true;
  1120. vertex->normal.x = (float)vec4[0];
  1121. vertex->normal.y = (float)vec4[1];
  1122. vertex->normal.z = (float)vec4[2];
  1123. }
  1124. break;
  1125. default:
  1126. break;
  1127. }
  1128. }
  1129. }
  1130. }
  1131. void loadTangent(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex)
  1132. {
  1133. if (fbxMesh->GetElementTangentCount() > 0)
  1134. {
  1135. // Get only the first tangent
  1136. FbxGeometryElementTangent* tangent = fbxMesh->GetElementTangent(0);
  1137. FbxGeometryElement::EMappingMode mappingMode = tangent->GetMappingMode();
  1138. if (mappingMode == FbxGeometryElement::eByControlPoint)
  1139. {
  1140. switch (tangent->GetReferenceMode())
  1141. {
  1142. case FbxGeometryElement::eDirect:
  1143. {
  1144. FbxVector4 vec4 = tangent->GetDirectArray().GetAt(controlPointIndex);
  1145. vertex->hasTangent = true;
  1146. vertex->tangent.x = (float)vec4[0];
  1147. vertex->tangent.y = (float)vec4[1];
  1148. vertex->tangent.z = (float)vec4[2];
  1149. }
  1150. break;
  1151. case FbxGeometryElement::eIndexToDirect:
  1152. {
  1153. int id = tangent->GetIndexArray().GetAt(controlPointIndex);
  1154. FbxVector4 vec4 = tangent->GetDirectArray().GetAt(id);
  1155. vertex->hasTangent = true;
  1156. vertex->tangent.x = (float)vec4[0];
  1157. vertex->tangent.y = (float)vec4[1];
  1158. vertex->tangent.z = (float)vec4[2];
  1159. }
  1160. break;
  1161. default:
  1162. break;
  1163. }
  1164. }
  1165. else if (mappingMode == FbxGeometryElement::eByPolygonVertex)
  1166. {
  1167. switch (tangent->GetReferenceMode())
  1168. {
  1169. case FbxGeometryElement::eDirect:
  1170. {
  1171. FbxVector4 vec4 = tangent->GetDirectArray().GetAt(vertexIndex);
  1172. vertex->hasTangent = true;
  1173. vertex->tangent.x = (float)vec4[0];
  1174. vertex->tangent.y = (float)vec4[1];
  1175. vertex->tangent.z = (float)vec4[2];
  1176. }
  1177. break;
  1178. case FbxGeometryElement::eIndexToDirect:
  1179. {
  1180. int id = tangent->GetIndexArray().GetAt(vertexIndex);
  1181. FbxVector4 vec4 = tangent->GetDirectArray().GetAt(id);
  1182. vertex->hasTangent = true;
  1183. vertex->tangent.x = (float)vec4[0];
  1184. vertex->tangent.y = (float)vec4[1];
  1185. vertex->tangent.z = (float)vec4[2];
  1186. }
  1187. break;
  1188. default:
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. }
  1194. void loadBinormal(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex)
  1195. {
  1196. if (fbxMesh->GetElementBinormalCount() > 0)
  1197. {
  1198. // Get only the first binormal.
  1199. FbxGeometryElementBinormal* binormal = fbxMesh->GetElementBinormal(0);
  1200. FbxGeometryElement::EMappingMode mappingMode = binormal->GetMappingMode();
  1201. if (mappingMode == FbxGeometryElement::eByControlPoint)
  1202. {
  1203. switch (binormal->GetReferenceMode())
  1204. {
  1205. case FbxGeometryElement::eDirect:
  1206. {
  1207. FbxVector4 vec4 = binormal->GetDirectArray().GetAt(controlPointIndex);
  1208. vertex->hasBinormal = true;
  1209. vertex->binormal.x = (float)vec4[0];
  1210. vertex->binormal.y = (float)vec4[1];
  1211. vertex->binormal.z = (float)vec4[2];
  1212. }
  1213. break;
  1214. case FbxGeometryElement::eIndexToDirect:
  1215. {
  1216. int id = binormal->GetIndexArray().GetAt(controlPointIndex);
  1217. FbxVector4 vec4 = binormal->GetDirectArray().GetAt(id);
  1218. vertex->hasBinormal = true;
  1219. vertex->binormal.x = (float)vec4[0];
  1220. vertex->binormal.y = (float)vec4[1];
  1221. vertex->binormal.z = (float)vec4[2];
  1222. }
  1223. break;
  1224. default:
  1225. break;
  1226. }
  1227. }
  1228. else if (mappingMode == FbxGeometryElement::eByPolygonVertex)
  1229. {
  1230. switch (binormal->GetReferenceMode())
  1231. {
  1232. case FbxGeometryElement::eDirect:
  1233. {
  1234. FbxVector4 vec4 = binormal->GetDirectArray().GetAt(vertexIndex);
  1235. vertex->hasBinormal = true;
  1236. vertex->binormal.x = (float)vec4[0];
  1237. vertex->binormal.y = (float)vec4[1];
  1238. vertex->binormal.z = (float)vec4[2];
  1239. }
  1240. break;
  1241. case FbxGeometryElement::eIndexToDirect:
  1242. {
  1243. int id = binormal->GetIndexArray().GetAt(vertexIndex);
  1244. FbxVector4 vec4 = binormal->GetDirectArray().GetAt(id);
  1245. vertex->hasBinormal = true;
  1246. vertex->binormal.x = (float)vec4[0];
  1247. vertex->binormal.y = (float)vec4[1];
  1248. vertex->binormal.z = (float)vec4[2];
  1249. }
  1250. break;
  1251. default:
  1252. break;
  1253. }
  1254. }
  1255. }
  1256. }
  1257. void loadVertexColor(FbxMesh* fbxMesh, int vertexIndex, int controlPointIndex, Vertex* vertex)
  1258. {
  1259. if (fbxMesh->GetElementVertexColorCount() > 0)
  1260. {
  1261. // Get only the first vertex color.
  1262. FbxGeometryElementVertexColor* vertexColor = fbxMesh->GetElementVertexColor(0);
  1263. FbxGeometryElement::EMappingMode mappingMode = vertexColor->GetMappingMode();
  1264. if (mappingMode == FbxGeometryElement::eByControlPoint)
  1265. {
  1266. switch (vertexColor->GetReferenceMode())
  1267. {
  1268. case FbxGeometryElement::eDirect:
  1269. {
  1270. FbxColor color = vertexColor->GetDirectArray().GetAt(controlPointIndex);
  1271. vertex->hasDiffuse = true;
  1272. vertex->diffuse.x = (float)color.mRed;
  1273. vertex->diffuse.y = (float)color.mGreen;
  1274. vertex->diffuse.z = (float)color.mBlue;
  1275. vertex->diffuse.w = (float)color.mAlpha;
  1276. }
  1277. break;
  1278. case FbxGeometryElement::eIndexToDirect:
  1279. {
  1280. int id = vertexColor->GetIndexArray().GetAt(controlPointIndex);
  1281. FbxColor color = vertexColor->GetDirectArray().GetAt(id);
  1282. vertex->hasDiffuse = true;
  1283. vertex->diffuse.x = (float)color.mRed;
  1284. vertex->diffuse.y = (float)color.mGreen;
  1285. vertex->diffuse.z = (float)color.mBlue;
  1286. vertex->diffuse.w = (float)color.mAlpha;
  1287. }
  1288. break;
  1289. default:
  1290. break;
  1291. }
  1292. }
  1293. else if (mappingMode == FbxGeometryElement::eByPolygonVertex)
  1294. {
  1295. switch (vertexColor->GetReferenceMode())
  1296. {
  1297. case FbxGeometryElement::eDirect:
  1298. {
  1299. FbxColor color = vertexColor->GetDirectArray().GetAt(vertexIndex);
  1300. vertex->hasDiffuse = true;
  1301. vertex->diffuse.x = (float)color.mRed;
  1302. vertex->diffuse.y = (float)color.mGreen;
  1303. vertex->diffuse.z = (float)color.mBlue;
  1304. vertex->diffuse.w = (float)color.mAlpha;
  1305. }
  1306. break;
  1307. case FbxGeometryElement::eIndexToDirect:
  1308. {
  1309. int id = vertexColor->GetIndexArray().GetAt(vertexIndex);
  1310. FbxColor color = vertexColor->GetDirectArray().GetAt(id);
  1311. vertex->hasDiffuse = true;
  1312. vertex->diffuse.x = (float)color.mRed;
  1313. vertex->diffuse.y = (float)color.mGreen;
  1314. vertex->diffuse.z = (float)color.mBlue;
  1315. vertex->diffuse.w = (float)color.mAlpha;
  1316. }
  1317. break;
  1318. default:
  1319. break;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. void loadBlendData(const std::vector<Vector2>& vertexWeights, Vertex* vertex)
  1325. {
  1326. size_t size = vertexWeights.size();
  1327. if (size >= 1)
  1328. {
  1329. vertex->hasWeights= true;
  1330. vertex->blendIndices.x = vertexWeights[0].x;
  1331. vertex->blendWeights.x = vertexWeights[0].y;
  1332. }
  1333. if (size >= 2)
  1334. {
  1335. vertex->blendIndices.y = vertexWeights[1].x;
  1336. vertex->blendWeights.y = vertexWeights[1].y;
  1337. }
  1338. if (size >= 3)
  1339. {
  1340. vertex->blendIndices.z = vertexWeights[2].x;
  1341. vertex->blendWeights.z = vertexWeights[2].y;
  1342. }
  1343. if (size >= 4)
  1344. {
  1345. vertex->blendIndices.w = vertexWeights[3].x;
  1346. vertex->blendWeights.w = vertexWeights[3].y;
  1347. }
  1348. //vertex->normalizeBlendWeight();
  1349. }
  1350. bool loadBlendWeights(FbxMesh* fbxMesh, std::vector<std::vector<Vector2> >& weights)
  1351. {
  1352. assert(fbxMesh);
  1353. const int vertexCount = fbxMesh->GetControlPointsCount();
  1354. FbxSkin* fbxSkin = NULL;
  1355. const int deformerCount = fbxMesh->GetDeformerCount();
  1356. for (int i = 0; i < deformerCount; ++i)
  1357. {
  1358. FbxDeformer* deformer = fbxMesh->GetDeformer(i);
  1359. if (deformer->GetDeformerType() == FbxDeformer::eSkin)
  1360. {
  1361. fbxSkin = static_cast<FbxSkin*>(deformer);
  1362. weights.resize(vertexCount);
  1363. const int clusterCount = fbxSkin->GetClusterCount();
  1364. for (int j = 0; j < clusterCount; ++j)
  1365. {
  1366. FbxCluster* cluster = fbxSkin->GetCluster(j);
  1367. assert(cluster);
  1368. const int vertexIndexCount = cluster->GetControlPointIndicesCount();
  1369. for (int k = 0; k < vertexIndexCount; ++k)
  1370. {
  1371. int index = cluster->GetControlPointIndices()[k];
  1372. if (index >= vertexCount)
  1373. {
  1374. continue;
  1375. }
  1376. double weight = cluster->GetControlPointWeights()[k];
  1377. if (weight == 0.0)
  1378. {
  1379. continue;
  1380. }
  1381. weights[index].push_back(Vector2((float)j, (float)weight));
  1382. }
  1383. }
  1384. // Only the first skin deformer will be loaded.
  1385. // There probably won't be more than one.
  1386. break;
  1387. }
  1388. }
  1389. return fbxSkin != NULL;
  1390. }
  1391. void findMinMaxTime(FbxAnimCurve* animCurve, float* startTime, float* stopTime, float* frameRate)
  1392. {
  1393. FbxTime start, stop;
  1394. FbxTimeSpan timeSpan;
  1395. animCurve->GetTimeInterval(timeSpan);
  1396. start = timeSpan.GetStart();
  1397. stop = timeSpan.GetStop();
  1398. *startTime = std::min(*startTime, (float)start.GetMilliSeconds());
  1399. *stopTime = std::max(*stopTime, (float)stop.GetMilliSeconds());
  1400. *frameRate = std::max(*frameRate, (float)stop.GetFrameRate(FbxTime::eDefaultMode));
  1401. }
  1402. void appendKeyFrame(FbxNode* fbxNode, AnimationChannel* channel, float time, const Vector3& scale, const Quaternion& rotation, const Vector3& translation)
  1403. {
  1404. // Write key time
  1405. channel->getKeyTimes().push_back(time);
  1406. // Write key values
  1407. std::vector<float>& keyValues = channel->getKeyValues();
  1408. switch (channel->getTargetAttribute())
  1409. {
  1410. case Transform::ANIMATE_SCALE:
  1411. {
  1412. keyValues.push_back(scale.x);
  1413. keyValues.push_back(scale.y);
  1414. keyValues.push_back(scale.z);
  1415. }
  1416. break;
  1417. case Transform::ANIMATE_SCALE_X:
  1418. {
  1419. keyValues.push_back(scale.x);
  1420. }
  1421. break;
  1422. case Transform::ANIMATE_SCALE_Y:
  1423. {
  1424. keyValues.push_back(scale.y);
  1425. }
  1426. break;
  1427. case Transform::ANIMATE_SCALE_Z:
  1428. {
  1429. keyValues.push_back(scale.z);
  1430. }
  1431. break;
  1432. case Transform::ANIMATE_ROTATE:
  1433. {
  1434. keyValues.push_back(rotation.x);
  1435. keyValues.push_back(rotation.y);
  1436. keyValues.push_back(rotation.z);
  1437. keyValues.push_back(rotation.w);
  1438. }
  1439. break;
  1440. case Transform::ANIMATE_TRANSLATE:
  1441. {
  1442. keyValues.push_back(translation.x);
  1443. keyValues.push_back(translation.y);
  1444. keyValues.push_back(translation.z);
  1445. }
  1446. break;
  1447. case Transform::ANIMATE_TRANSLATE_X:
  1448. {
  1449. keyValues.push_back(translation.x);
  1450. }
  1451. break;
  1452. case Transform::ANIMATE_TRANSLATE_Y:
  1453. {
  1454. keyValues.push_back(translation.y);
  1455. }
  1456. break;
  1457. case Transform::ANIMATE_TRANSLATE_Z:
  1458. {
  1459. keyValues.push_back(translation.z);
  1460. }
  1461. break;
  1462. case Transform::ANIMATE_ROTATE_TRANSLATE:
  1463. {
  1464. keyValues.push_back(rotation.x);
  1465. keyValues.push_back(rotation.y);
  1466. keyValues.push_back(rotation.z);
  1467. keyValues.push_back(rotation.w);
  1468. keyValues.push_back(translation.x);
  1469. keyValues.push_back(translation.y);
  1470. keyValues.push_back(translation.z);
  1471. }
  1472. break;
  1473. case Transform::ANIMATE_SCALE_ROTATE_TRANSLATE:
  1474. {
  1475. keyValues.push_back(scale.x);
  1476. keyValues.push_back(scale.y);
  1477. keyValues.push_back(scale.z);
  1478. keyValues.push_back(rotation.x);
  1479. keyValues.push_back(rotation.y);
  1480. keyValues.push_back(rotation.z);
  1481. keyValues.push_back(rotation.w);
  1482. keyValues.push_back(translation.x);
  1483. keyValues.push_back(translation.y);
  1484. keyValues.push_back(translation.z);
  1485. }
  1486. break;
  1487. case Transform::ANIMATE_SCALE_TRANSLATE:
  1488. {
  1489. keyValues.push_back(scale.x);
  1490. keyValues.push_back(scale.y);
  1491. keyValues.push_back(scale.z);
  1492. keyValues.push_back(translation.x);
  1493. keyValues.push_back(translation.y);
  1494. keyValues.push_back(translation.z);
  1495. }
  1496. break;
  1497. case Transform::ANIMATE_SCALE_ROTATE:
  1498. {
  1499. keyValues.push_back(scale.x);
  1500. keyValues.push_back(scale.y);
  1501. keyValues.push_back(scale.z);
  1502. keyValues.push_back(rotation.x);
  1503. keyValues.push_back(rotation.y);
  1504. keyValues.push_back(rotation.z);
  1505. keyValues.push_back(rotation.w);
  1506. }
  1507. break;
  1508. default:
  1509. {
  1510. LOG(1, "Warning: Invalid animatoin target (%d) attribute for node: %s.\n", channel->getTargetAttribute(), fbxNode->GetName());
  1511. }
  1512. return;
  1513. }
  1514. }
  1515. void decompose(FbxNode* fbxNode, float time, Vector3* scale, Quaternion* rotation, Vector3* translation)
  1516. {
  1517. FbxAMatrix fbxMatrix;
  1518. Matrix matrix;
  1519. FbxTime kTime;
  1520. kTime.SetMilliSeconds((FbxLongLong)time);
  1521. fbxMatrix = fbxNode->EvaluateLocalTransform(kTime);
  1522. copyMatrix(fbxMatrix, matrix);
  1523. matrix.decompose(scale, rotation, translation);
  1524. }
  1525. AnimationChannel* createAnimationChannel(FbxNode* fbxNode, unsigned int targetAttrib, const std::vector<float>& keyTimes, const std::vector<float>& keyValues)
  1526. {
  1527. AnimationChannel* channel = new AnimationChannel();
  1528. channel->setTargetId(fbxNode->GetName());
  1529. channel->setKeyTimes(keyTimes);
  1530. channel->setKeyValues(keyValues);
  1531. channel->setInterpolation(AnimationChannel::LINEAR);
  1532. channel->setTargetAttribute(targetAttrib);
  1533. return channel;
  1534. }
  1535. void addScaleChannel(Animation* animation, FbxNode* fbxNode, float startTime, float stopTime)
  1536. {
  1537. std::vector<float> keyTimes;
  1538. std::vector<float> keyValues;
  1539. Vector3 scale;
  1540. Quaternion rotation;
  1541. Vector3 translation;
  1542. decompose(fbxNode, startTime, &scale, &rotation, &translation);
  1543. keyTimes.push_back(startTime);
  1544. keyValues.push_back(scale.x);
  1545. keyValues.push_back(scale.y);
  1546. keyValues.push_back(scale.z);
  1547. decompose(fbxNode, stopTime, &scale, &rotation, &translation);
  1548. keyTimes.push_back(stopTime);
  1549. keyValues.push_back(scale.x);
  1550. keyValues.push_back(scale.y);
  1551. keyValues.push_back(scale.z);
  1552. AnimationChannel* channel = createAnimationChannel(fbxNode, Transform::ANIMATE_SCALE, keyTimes, keyValues);
  1553. animation->add(channel);
  1554. }
  1555. void addTranslateChannel(Animation* animation, FbxNode* fbxNode, float startTime, float stopTime)
  1556. {
  1557. std::vector<float> keyTimes;
  1558. std::vector<float> keyValues;
  1559. Vector3 scale;
  1560. Quaternion rotation;
  1561. Vector3 translation;
  1562. decompose(fbxNode, startTime, &scale, &rotation, &translation);
  1563. keyTimes.push_back(startTime);
  1564. keyValues.push_back(translation.x);
  1565. keyValues.push_back(translation.y);
  1566. keyValues.push_back(translation.z);
  1567. decompose(fbxNode, stopTime, &scale, &rotation, &translation);
  1568. keyTimes.push_back(stopTime);
  1569. keyValues.push_back(translation.x);
  1570. keyValues.push_back(translation.y);
  1571. keyValues.push_back(translation.z);
  1572. AnimationChannel* channel = createAnimationChannel(fbxNode, Transform::ANIMATE_TRANSLATE, keyTimes, keyValues);
  1573. animation->add(channel);
  1574. }
  1575. void copyMatrix(const FbxMatrix& fbxMatrix, float* matrix)
  1576. {
  1577. int i = 0;
  1578. for (int row = 0; row < 4; ++row)
  1579. {
  1580. for (int col = 0; col < 4; ++col)
  1581. {
  1582. matrix[i++] = (float)fbxMatrix.Get(row, col);
  1583. }
  1584. }
  1585. }
  1586. void copyMatrix(const FbxMatrix& fbxMatrix, Matrix& matrix)
  1587. {
  1588. int i = 0;
  1589. for (int row = 0; row < 4; ++row)
  1590. {
  1591. for (int col = 0; col < 4; ++col)
  1592. {
  1593. matrix.m[i++] = (float)fbxMatrix.Get(row, col);
  1594. }
  1595. }
  1596. }
  1597. bool isGroupAnimationPossible(FbxScene* fbxScene)
  1598. {
  1599. FbxNode* rootNode = fbxScene->GetRootNode();
  1600. if (rootNode)
  1601. {
  1602. if (isGroupAnimationPossible(rootNode))
  1603. return true;
  1604. }
  1605. return false;
  1606. }
  1607. bool isGroupAnimationPossible(FbxNode* fbxNode)
  1608. {
  1609. if (fbxNode)
  1610. {
  1611. FbxMesh* fbxMesh = fbxNode->GetMesh();
  1612. if (isGroupAnimationPossible(fbxMesh))
  1613. return true;
  1614. const int childCount = fbxNode->GetChildCount();
  1615. for (int i = 0; i < childCount; ++i)
  1616. {
  1617. if (isGroupAnimationPossible(fbxNode->GetChild(i)))
  1618. return true;
  1619. }
  1620. }
  1621. return false;
  1622. }
  1623. bool isGroupAnimationPossible(FbxMesh* fbxMesh)
  1624. {
  1625. if (fbxMesh)
  1626. {
  1627. const int deformerCount = fbxMesh->GetDeformerCount();
  1628. for (int i = 0; i < deformerCount; ++i)
  1629. {
  1630. FbxDeformer* deformer = fbxMesh->GetDeformer(i);
  1631. if (deformer->GetDeformerType() == FbxDeformer::eSkin)
  1632. {
  1633. FbxSkin* fbxSkin = static_cast<FbxSkin*>(deformer);
  1634. if (fbxSkin)
  1635. {
  1636. return true;
  1637. }
  1638. }
  1639. }
  1640. }
  1641. return false;
  1642. }
  1643. void generateTangentsAndBinormals(FbxNode* fbxNode, const EncoderArguments& arguments)
  1644. {
  1645. if (!fbxNode)
  1646. return;
  1647. const char* name = fbxNode->GetName();
  1648. if (name && strlen(name) > 0)
  1649. {
  1650. FbxMesh* fbxMesh = fbxNode->GetMesh();
  1651. if (fbxMesh && arguments.isGenerateTangentBinormalId(std::string(name)))
  1652. {
  1653. fbxMesh->GenerateTangentsDataForAllUVSets();
  1654. }
  1655. }
  1656. // visit child nodes
  1657. const int childCount = fbxNode->GetChildCount();
  1658. for (int i = 0; i < childCount; ++i)
  1659. {
  1660. generateTangentsAndBinormals(fbxNode->GetChild(i), arguments);
  1661. }
  1662. }
  1663. #endif