llgltfanimation.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @file llgltfanimation.h
  3. * @brief LL GLTF Implementation
  4. *
  5. * $LicenseInfo:firstyear=2024&license=viewergpl$
  6. *
  7. * Copyright (c) 2024, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLGLTFANIMATION_H
  33. #define LL_LLGLTFANIMATION_H
  34. #include "llgltfaccessor.h"
  35. // *TODO: get rid of this (again) ! HB
  36. #include "glh_linear.h"
  37. // Saves from including "tinygltf/tiny_gltf.h" here. HB
  38. namespace tinygltf
  39. {
  40. struct Animation;
  41. struct AnimationChannel;
  42. struct AnimationSampler;
  43. }
  44. namespace LLGLTF
  45. {
  46. class Asset;
  47. class Animation
  48. {
  49. public:
  50. Animation();
  51. class Sampler
  52. {
  53. public:
  54. Sampler();
  55. void allocateGLResources(Asset& asset);
  56. const Sampler& operator=(const tinygltf::AnimationSampler& src);
  57. // Gets the frame index and time for the specified time. 'asset' is
  58. // the asset to reference for Accessors, 'time' is the animation
  59. // time to get the frame info for 'frame_idx' is the index of the
  60. // closest frame that precedes the specified time, and 't' is the
  61. // interpolant value between the frameIndex and the next frame.
  62. void getFrameInfo(Asset& asset, F32 time, U32& frame_idx, F32& t);
  63. public:
  64. std::vector<F32> mFrameTimes;
  65. std::string mInterpolation;
  66. F32 mMinTime;
  67. F32 mMaxTime;
  68. S32 mInput;
  69. S32 mOutput;
  70. };
  71. class Channel
  72. {
  73. public:
  74. Channel();
  75. const Channel& operator=(const tinygltf::AnimationChannel& src);
  76. class Target
  77. {
  78. public:
  79. LL_INLINE Target()
  80. : mNode(INVALID_INDEX)
  81. {
  82. }
  83. public:
  84. std::string mPath;
  85. S32 mNode;
  86. };
  87. public:
  88. std::string mTargetPath;
  89. std::string mName;
  90. Target mTarget;
  91. S32 mSampler;
  92. };
  93. class RotationChannel : public Channel
  94. {
  95. public:
  96. const RotationChannel& operator=(const tinygltf::AnimationChannel& src);
  97. // Prepares data needed for rendering. 'asset' is the asset to
  98. // reference for Accessors. 'sampler' is the sampler associated
  99. // with this channel.
  100. void allocateGLResources(Asset& asset, Sampler& sampler);
  101. void apply(Asset& asset, Sampler& sampler, F32 time);
  102. public:
  103. std::vector<glh::quaternionf> mRotations;
  104. };
  105. class TranslationChannel : public Channel
  106. {
  107. public:
  108. const TranslationChannel& operator=(const tinygltf::AnimationChannel& src);
  109. // Prepares data needed for rendering. 'asset' is the asset to
  110. // reference for Accessors. 'sampler' is the sampler associated
  111. // with this channel.
  112. void allocateGLResources(Asset& asset, Sampler& sampler);
  113. void apply(Asset& asset, Sampler& sampler, F32 time);
  114. public:
  115. std::vector<glh::vec3f> mTranslations;
  116. };
  117. class ScaleChannel : public Channel
  118. {
  119. public:
  120. const ScaleChannel& operator=(const tinygltf::AnimationChannel& src);
  121. // Prepares data needed for rendering. 'asset' is the asset to
  122. // reference for Accessors. 'sampler' is the sampler associated
  123. // with this channel.
  124. void allocateGLResources(Asset& asset, Sampler& sampler);
  125. void apply(Asset& asset, Sampler& sampler, F32 time);
  126. public:
  127. std::vector<glh::vec3f> mScales;
  128. };
  129. const Animation& operator=(const tinygltf::Animation& src);
  130. void allocateGLResources(Asset& asset);
  131. void update(Asset& asset, float dt);
  132. // apply this animation at the specified time
  133. void apply(Asset& asset, F32 time);
  134. public:
  135. std::string mName;
  136. std::vector<Sampler> mSamplers;
  137. std::vector<RotationChannel> mRotationChannels;
  138. std::vector<TranslationChannel> mTranslationChannels;
  139. std::vector<ScaleChannel> mScaleChannels;
  140. // Min/max time values for all samplers combined
  141. F32 mMinTime;
  142. F32 mMaxTime;
  143. // Current time of the animation
  144. F32 mTime;
  145. };
  146. }
  147. #endif // LL_LLGLTFANIMATION_H