llgltfanimation.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**
  2. * @file animation.cpp
  3. * @brief LL GLTF Animation Implementation
  4. *
  5. * $LicenseInfo:firstyear=2024&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2024, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "tinygltf/tiny_gltf.h"
  28. #include "llgltfanimation.h"
  29. #include "llgltfasset.h"
  30. #include "llgltfbufferutil.h"
  31. using namespace LLGLTF;
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // LLGLTF::Animation::Sampler sub-class
  34. ///////////////////////////////////////////////////////////////////////////////
  35. Animation::Sampler::Sampler()
  36. : mMinTime(-F32_MAX),
  37. mMaxTime(F32_MAX),
  38. mInput(INVALID_INDEX),
  39. mOutput(INVALID_INDEX)
  40. {
  41. }
  42. const Animation::Sampler& Animation::Sampler::operator=(const tinygltf::AnimationSampler& src)
  43. {
  44. mInput = src.input;
  45. mOutput = src.output;
  46. mInterpolation = src.interpolation;
  47. return *this;
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////
  50. // LLGLTF::Animation::Channel sub-class
  51. ///////////////////////////////////////////////////////////////////////////////
  52. Animation::Channel::Channel()
  53. : mSampler(INVALID_INDEX)
  54. {
  55. }
  56. const Animation::Channel& Animation::Channel::operator=(const tinygltf::AnimationChannel& src)
  57. {
  58. mSampler = src.sampler;
  59. mTarget.mNode = src.target_node;
  60. mTarget.mPath = src.target_path;
  61. return *this;
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // LLGLTF::Animation::RotationChannel sub-class
  65. ///////////////////////////////////////////////////////////////////////////////
  66. const Animation::RotationChannel& Animation::RotationChannel::operator=(const tinygltf::AnimationChannel& src)
  67. {
  68. Animation::Channel::operator=(src);
  69. return *this;
  70. }
  71. ///////////////////////////////////////////////////////////////////////////////
  72. // LLGLTF::Animation::TranslationChannel sub-class
  73. ///////////////////////////////////////////////////////////////////////////////
  74. const Animation::TranslationChannel& Animation::TranslationChannel::operator=(const tinygltf::AnimationChannel& src)
  75. {
  76. Animation::Channel::operator=(src);
  77. return *this;
  78. }
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // LLGLTF::Animation::ScaleChannel sub-class
  81. ///////////////////////////////////////////////////////////////////////////////
  82. const Animation::ScaleChannel& Animation::ScaleChannel::operator=(const tinygltf::AnimationChannel& src)
  83. {
  84. Animation::Channel::operator=(src);
  85. return *this;
  86. }
  87. ///////////////////////////////////////////////////////////////////////////////
  88. // LLGLTF::Animation class
  89. ///////////////////////////////////////////////////////////////////////////////
  90. Animation::Animation()
  91. : mMinTime(0.f),
  92. mMaxTime(0.f),
  93. mTime(0.f)
  94. {
  95. }
  96. void Animation::allocateGLResources(Asset& asset)
  97. {
  98. if (!mSamplers.empty())
  99. {
  100. mMinTime = FLT_MAX;
  101. mMaxTime = -FLT_MAX;
  102. for (auto& sampler : mSamplers)
  103. {
  104. sampler.allocateGLResources(asset);
  105. mMinTime = llmin(sampler.mMinTime, mMinTime);
  106. mMaxTime = llmax(sampler.mMaxTime, mMaxTime);
  107. }
  108. }
  109. else
  110. {
  111. mMinTime = mMaxTime = 0.f;
  112. }
  113. for (auto& channel : mRotationChannels)
  114. {
  115. channel.allocateGLResources(asset, mSamplers[channel.mSampler]);
  116. }
  117. for (auto& channel : mTranslationChannels)
  118. {
  119. channel.allocateGLResources(asset, mSamplers[channel.mSampler]);
  120. }
  121. }
  122. void Animation::update(Asset& asset, F32 dt)
  123. {
  124. mTime += dt;
  125. apply(asset, mTime);
  126. }
  127. void Animation::apply(Asset& asset, F32 time)
  128. {
  129. // Convert time to animation loop time
  130. time = fmod(time, mMaxTime - mMinTime) + mMinTime;
  131. // apply each channel
  132. for (auto& channel : mRotationChannels)
  133. {
  134. channel.apply(asset, mSamplers[channel.mSampler], time);
  135. }
  136. for (auto& channel : mTranslationChannels)
  137. {
  138. channel.apply(asset, mSamplers[channel.mSampler], time);
  139. }
  140. };
  141. void Animation::Sampler::allocateGLResources(Asset& asset)
  142. {
  143. Accessor& accessor = asset.mAccessors[mInput];
  144. mMinTime = accessor.mMin[0];
  145. mMaxTime = accessor.mMax[0];
  146. mFrameTimes.resize(accessor.mCount);
  147. LLStrider<F32> frame_times = mFrameTimes.data();
  148. copy(asset, accessor, frame_times);
  149. }
  150. void Animation::Sampler::getFrameInfo(Asset& asset, F32 time, U32& frame_idx,
  151. F32& t)
  152. {
  153. if (time < mMinTime)
  154. {
  155. frame_idx = 0;
  156. t = 0.f;
  157. return;
  158. }
  159. if (mFrameTimes.size() > 1)
  160. {
  161. if (time > mMaxTime)
  162. {
  163. frame_idx = mFrameTimes.size() - 2;
  164. t = 1.f;
  165. return;
  166. }
  167. frame_idx = mFrameTimes.size() - 2;
  168. t = 1.f;
  169. for (U32 i = 0; i < mFrameTimes.size() - 1; ++i)
  170. {
  171. if (time >= mFrameTimes[i] && time < mFrameTimes[i + 1])
  172. {
  173. frame_idx = i;
  174. t = (time - mFrameTimes[i]) /
  175. (mFrameTimes[i + 1] - mFrameTimes[i]);
  176. return;
  177. }
  178. }
  179. }
  180. else
  181. {
  182. frame_idx = 0;
  183. t = 0.f;
  184. }
  185. }
  186. void Animation::RotationChannel::allocateGLResources(Asset& asset,
  187. Animation::Sampler& sampler)
  188. {
  189. Accessor& accessor = asset.mAccessors[sampler.mOutput];
  190. copy(asset, accessor, mRotations);
  191. }
  192. void Animation::RotationChannel::apply(Asset& asset, Sampler& sampler,
  193. F32 time)
  194. {
  195. Node& node = asset.mNodes[mTarget.mNode];
  196. U32 frame_idx;
  197. F32 t;
  198. sampler.getFrameInfo(asset, time, frame_idx, t);
  199. if (sampler.mFrameTimes.size() == 1)
  200. {
  201. node.setRotation(mRotations[0]);
  202. }
  203. else
  204. {
  205. // Interpolate
  206. LLQuaternion q0(mRotations[frame_idx].get_value());
  207. LLQuaternion q1(mRotations[frame_idx + 1].get_value());
  208. LLQuaternion qf = slerp(t, q0, q1);
  209. qf.normalize();
  210. node.setRotation(glh::quaternionf(qf.mQ));
  211. }
  212. }
  213. void Animation::TranslationChannel::allocateGLResources(Asset& asset,
  214. Animation::Sampler& sampler)
  215. {
  216. Accessor& accessor = asset.mAccessors[sampler.mOutput];
  217. copy(asset, accessor, mTranslations);
  218. }
  219. void Animation::TranslationChannel::apply(Asset& asset, Sampler& sampler,
  220. F32 time)
  221. {
  222. Node& node = asset.mNodes[mTarget.mNode];
  223. U32 frame_idx;
  224. F32 t;
  225. sampler.getFrameInfo(asset, time, frame_idx, t);
  226. if (sampler.mFrameTimes.size() == 1)
  227. {
  228. node.setTranslation(mTranslations[0]);
  229. }
  230. else
  231. {
  232. // Interpolate
  233. const glh::vec3f& v0 = mTranslations[frame_idx];
  234. const glh::vec3f& v1 = mTranslations[frame_idx + 1];
  235. glh::vec3f vf = v0 + t * (v1 - v0);
  236. node.setTranslation(vf);
  237. }
  238. }
  239. void Animation::ScaleChannel::allocateGLResources(Asset& asset,
  240. Animation::Sampler& sampler)
  241. {
  242. Accessor& accessor = asset.mAccessors[sampler.mOutput];
  243. copy(asset, accessor, mScales);
  244. }
  245. void Animation::ScaleChannel::apply(Asset& asset, Sampler& sampler, F32 time)
  246. {
  247. Node& node = asset.mNodes[mTarget.mNode];
  248. U32 frame_idx;
  249. F32 t;
  250. sampler.getFrameInfo(asset, time, frame_idx, t);
  251. if (sampler.mFrameTimes.size() == 1)
  252. {
  253. node.setScale(mScales[0]);
  254. }
  255. else
  256. {
  257. // Interpolate
  258. const glh::vec3f& v0 = mScales[frame_idx];
  259. const glh::vec3f& v1 = mScales[frame_idx + 1];
  260. glh::vec3f vf = v0 + t * (v1 - v0);
  261. node.setScale(vf);
  262. }
  263. }
  264. const Animation& Animation::operator=(const tinygltf::Animation& src)
  265. {
  266. mName = src.name;
  267. mSamplers.resize(src.samplers.size());
  268. for (U32 i = 0; i < src.samplers.size(); ++i)
  269. {
  270. mSamplers[i] = src.samplers[i];
  271. }
  272. for (U32 i = 0; i < src.channels.size(); ++i)
  273. {
  274. if (src.channels[i].target_path == "rotation")
  275. {
  276. mRotationChannels.push_back(RotationChannel());
  277. mRotationChannels.back() = src.channels[i];
  278. }
  279. if (src.channels[i].target_path == "translation")
  280. {
  281. mTranslationChannels.push_back(TranslationChannel());
  282. mTranslationChannels.back() = src.channels[i];
  283. }
  284. if (src.channels[i].target_path == "scale")
  285. {
  286. mScaleChannels.push_back(ScaleChannel());
  287. mScaleChannels.back() = src.channels[i];
  288. }
  289. }
  290. return *this;
  291. }
  292. ///////////////////////////////////////////////////////////////////////////////
  293. // LLGLTF::Skin class
  294. ///////////////////////////////////////////////////////////////////////////////
  295. // Note: this should be in llgltfasset.cpp, but for some reason copy() is not
  296. // found when placed over there... *TODO: investigate and move this method
  297. // where it does belong ! HB
  298. void Skin::allocateGLResources(Asset& asset)
  299. {
  300. if (mInverseBindMatrices != INVALID_INDEX)
  301. {
  302. Accessor& accessor = asset.mAccessors[mInverseBindMatrices];
  303. copy(asset, accessor, mInverseBindMatricesData);
  304. }
  305. }