llgltfasset.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @file llgltfasset.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_LLGLTFASSET_H
  33. #define LL_LLGLTFASSET_H
  34. // *TODO: get rid of this (again) ! HB
  35. #include "glh_linear.h"
  36. #include "llgltfaccessor.h"
  37. #include "llgltfanimation.h"
  38. #include "llgltfmaterial.h"
  39. #include "llgltfprimitive.h"
  40. // Saves from including "tinygltf/tiny_gltf.h" here. HB
  41. namespace tinygltf
  42. {
  43. class Model;
  44. class Node;
  45. struct Image;
  46. struct Material;
  47. struct Mesh;
  48. struct Sampler;
  49. struct Scene;
  50. struct Skin;
  51. struct Texture;
  52. }
  53. namespace LLGLTF
  54. {
  55. class Asset;
  56. class Material
  57. {
  58. public:
  59. const Material& operator=(const tinygltf::Material& src);
  60. void allocateGLResources(Asset& asset);
  61. public:
  62. // Use LLFetchedGLTFMaterial for now, but eventually we will want to
  63. // use a more flexible GLTF material implementation instead of the
  64. // fixed packing version we use for sharable GLTF material assets.
  65. // Note: to avoid importing newview structures and keep this module
  66. // inside a separate llgltf library, I use a LLGLTFMaterial pointer
  67. // (i.e. a pointer of the parent class type) one, and instead of
  68. // calling two methods specific to LLFetchedGLTFMaterial (new() and
  69. // bind()), I use two exported wrapper functions implemented in
  70. // llfetchedgltfmaterial.cpp. HB
  71. LLPointer<LLGLTFMaterial> mMaterial;
  72. std::string mName;
  73. };
  74. // Note 16-byte aligned because we use a vector of Primitive. HB
  75. class alignas(16) Mesh
  76. {
  77. public:
  78. LL_ALIGNED16_NEW_DELETE
  79. const Mesh& operator=(const tinygltf::Mesh& src);
  80. bool allocateGLResources(Asset& asset);
  81. public:
  82. // Aligned member first...
  83. std::vector<Primitive> mPrimitives;
  84. // ... then the rest.
  85. std::vector<F64> mWeights;
  86. std::string mName;
  87. };
  88. // Note 16-byte aligned because we use LLMatrix4a member variables. HB
  89. class alignas(16) Node
  90. {
  91. public:
  92. LL_ALIGNED16_NEW_DELETE
  93. Node();
  94. const Node& operator=(const tinygltf::Node& src);
  95. // Sets mRenderMatrix to a transform that can be used for the current
  96. // render pass. 'modelview' is the parent's render matrix.
  97. void updateRenderTransforms(Asset& asset, const LLMatrix4a& modelview);
  98. // Updates mAssetMatrix and mAssetMatrixInv
  99. void updateTransforms(Asset& asset, const LLMatrix4a& parent_matrix);
  100. // Ensures mMatrix is valid; if mMatrixValid is false and mTRSValid is
  101. // true, will update mMatrix to match Translation/Rotation/Scale
  102. void makeMatrixValid();
  103. // Ensures Translation/Rotation/Scale are valid; if mTRSValid is false
  104. // and mMatrixValid is true, will update Translation/Rotation/Scale to
  105. // match mMatrix.
  106. void makeTRSValid();
  107. // Sets rotation of this node
  108. // SIDE EFFECT: invalidates mMatrix
  109. void setRotation(const glh::quaternionf& rotation);
  110. // Sets translation of this node
  111. // SIDE EFFECT: invalidates mMatrix
  112. void setTranslation(const glh::vec3f& translation);
  113. // Sets scale of this node
  114. // SIDE EFFECT: invalidates mMatrix
  115. void setScale(const glh::vec3f& scale);
  116. public:
  117. // Aligned members first...
  118. LLMatrix4a mMatrix; // Local transform
  119. LLMatrix4a mRenderMatrix; // Transform for rendering
  120. LLMatrix4a mAssetMatrix; // Local to asset space transf.
  121. LLMatrix4a mAssetMatrixInv; // Asset to local space transf.
  122. // ... then the rest.
  123. glh::quaternionf mRotation;
  124. glh::vec3f mTranslation;
  125. glh::vec3f mScale;
  126. std::string mName;
  127. std::vector<S32> mChildren;
  128. S32 mParent;
  129. S32 mMesh;
  130. S32 mSkin;
  131. // If true, mMatrix is valid and up to date
  132. bool mMatrixValid;
  133. // If true, translation/rotation/scale are valid and up to date
  134. bool mTRSValid;
  135. bool mNeedsApplyMatrix;
  136. };
  137. class Skin
  138. {
  139. public:
  140. Skin();
  141. void allocateGLResources(Asset& asset);
  142. void uploadMatrixPalette(Asset& asset, Node& node);
  143. const Skin& operator=(const tinygltf::Skin& src);
  144. public:
  145. std::string mName;
  146. std::vector<S32> mJoints;
  147. std::vector<glh::matrix4f> mInverseBindMatricesData;
  148. S32 mInverseBindMatrices;
  149. S32 mSkeleton;
  150. };
  151. class Scene
  152. {
  153. public:
  154. const Scene& operator=(const tinygltf::Scene& src);
  155. void updateTransforms(Asset& asset);
  156. void updateRenderTransforms(Asset& asset, const LLMatrix4a& modelview);
  157. public:
  158. std::string mName;
  159. std::vector<S32> mNodes;
  160. };
  161. class Texture
  162. {
  163. public:
  164. Texture();
  165. const Texture& operator=(const tinygltf::Texture& src);
  166. public:
  167. std::string mName;
  168. S32 mSampler;
  169. S32 mSource;
  170. };
  171. class Sampler
  172. {
  173. public:
  174. const Sampler& operator=(const tinygltf::Sampler& src);
  175. public:
  176. std::string mName;
  177. S32 mMagFilter;
  178. S32 mMinFilter;
  179. S32 mWrapS;
  180. S32 mWrapT;
  181. };
  182. class Image
  183. {
  184. public:
  185. const Image& operator=(const tinygltf::Image& src);
  186. LL_INLINE void allocateGLResources() {}
  187. public:
  188. std::string mName;
  189. std::string mUri;
  190. std::string mMimeType;
  191. std::vector<U8> mData;
  192. S32 mWidth;
  193. S32 mHeight;
  194. S32 mComponent;
  195. S32 mBits;
  196. };
  197. // C++ representation of a GLTF Asset
  198. // Note 16-byte aligned because we use a vectors of Node and Mesh. HB
  199. class alignas(16) Asset : public LLRefCount
  200. {
  201. public:
  202. LL_ALIGNED16_NEW_DELETE
  203. Asset();
  204. // Prepares the asset for rendering.
  205. // Returns true on success, or false on failure (at which point this
  206. // asset must be destroyed and never used). HB
  207. bool allocateGLResources(const std::string& filename,
  208. const tinygltf::Model& model);
  209. // Called periodically (typically once per frame). Any ongoing work
  210. // (such as animations) should be handled here NOT guaranteed to be
  211. // called every frame. MAY be called more than once per frame. Upon
  212. // return, all Node Matrix transforms should be up to date.
  213. void update();
  214. // Updates asset-to-node and node-to-asset transforms
  215. void updateTransforms();
  216. // Updates node render transforms
  217. void updateRenderTransforms(const LLMatrix4a& modelview);
  218. void render(bool opaque, bool rigged = false);
  219. void renderOpaque();
  220. void renderTransparent();
  221. // Return the index of the node that the line segment intersects with,
  222. // or -1 if no hit input and output values must be in this asset's
  223. // local coordinate frame
  224. S32 lineSegmentIntersect(const LLVector4a& start,
  225. const LLVector4a& end,
  226. LLVector4a* intersectp = NULL,
  227. LLVector2* tcoordp = NULL,
  228. LLVector4a* normp = NULL,
  229. LLVector4a* tangentp = NULL,
  230. S32* prim_hitp = NULL);
  231. const Asset& operator=(const tinygltf::Model& src);
  232. public:
  233. // Aligned members first...
  234. std::vector<Node> mNodes;
  235. std::vector<Mesh> mMeshes;
  236. // ... then the rest.
  237. std::vector<Scene> mScenes;
  238. std::vector<Material> mMaterials;
  239. std::vector<Buffer> mBuffers;
  240. std::vector<BufferView> mBufferViews;
  241. std::vector<Texture> mTextures;
  242. std::vector<Sampler> mSamplers;
  243. std::vector<Image> mImages;
  244. std::vector<Accessor> mAccessors;
  245. std::vector<Animation> mAnimations;
  246. std::vector<Skin> mSkins;
  247. // The last time update() was called according to gFrameTimeSeconds
  248. F32 mLastUpdateTime;
  249. };
  250. }
  251. #endif // LL_LLGLTFASSET_H