llgltfloader.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @file llgltfloader.h
  3. * @brief LLGLTFLoader definition
  4. *
  5. * $LicenseInfo:firstyear=2022&license=viewergpl$
  6. *
  7. * Copyright (c) 2022, 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_LLGLTFLOADER_H
  33. #define LL_LLGLTFLOADER_H
  34. #include "llmodelloader.h"
  35. namespace tinygltf
  36. {
  37. class Model;
  38. struct Mesh;
  39. }
  40. // gltf_* structs are temporary, used to organize the subset of data that
  41. // eventually goes into the material LLSD.
  42. // Uses GL enums
  43. class gltf_sampler
  44. {
  45. public:
  46. // GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST,
  47. // GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR or
  48. // GL_LINEAR_MIPMAP_LINEAR
  49. S32 minFilter;
  50. S32 magFilter; // GL_NEAREST or GL_LINEAR
  51. S32 wrapS; // GL_CLAMP_TO_EDGE, GL_MIRRORED_REPEAT, GL_REPEAT
  52. S32 wrapT; // GL_CLAMP_TO_EDGE, GL_MIRRORED_REPEAT, GL_REPEAT
  53. #if 0 // Found in some sample files, but not part of glTF 2.0 spec. Ignored.
  54. S32 wrapR;
  55. #endif
  56. std::string name; // Optional, currently unused
  57. // Extensions and extras are sampler optional fields that we do not
  58. // support, at least initially.
  59. };
  60. class gltf_image
  61. {
  62. public:
  63. // Note that glTF images are defined with row 0 at the top (opposite of
  64. // OpenGL).
  65. U8* data; // Pointer to decoded image data
  66. U32 size; // In bytes, regardless of channel width
  67. U32 width;
  68. U32 height;
  69. U32 numChannels; // Range 1..4
  70. // Converted from gltf "bits", expects only 8, 16 or 32 as input
  71. U32 bytesPerChannel;
  72. // One of (TINYGLTF_COMPONENT_TYPE)_UNSIGNED_BYTE, _UNSIGNED_SHORT,
  73. // _UNSIGNED_INT, or _FLOAT
  74. U32 pixelType;
  75. };
  76. class gltf_texture
  77. {
  78. public:
  79. LLUUID imageUuid;
  80. U32 imageIdx;
  81. U32 samplerIdx;
  82. };
  83. class gltf_render_material
  84. {
  85. public:
  86. std::string name;
  87. // This field is populated after upload
  88. LLUUID material_uuid;
  89. // *TODO: add traditional (diffuse, normal, specular) UUIDs here, or add
  90. // this struct to LLTextureEntry ?
  91. // Scalar values
  92. // Linear encoding. Multiplied with vertex color, if present.
  93. LLColor4 baseColor;
  94. // Emissive mulitiplier, assumed linear encoding (spec 2.0 is silent)
  95. LLColor4 emissiveColor;
  96. std::string alphaMode; // "OPAQUE", "MASK" or "BLEND"
  97. double alphaMask; // Alpha cut-off
  98. double metalness;
  99. double roughness;
  100. // Scale applies only to X,Y components of normal
  101. double normalScale;
  102. double occlusionScale; // Strength multiplier for occlusion
  103. // Textures
  104. // Always sRGB encoded
  105. U32 baseColorTexIdx;
  106. // Always linear, roughness in G channel, metalness in B channel
  107. U32 metalRoughTexIdx;
  108. // Linear, valid range R[0-1], G[0-1], B[0.5-1].
  109. // Normal = texel * 2 - vec3(1.0)
  110. U32 normalTexIdx;
  111. // Linear, occlusion in R channel, 0 meaning fully occluded, 1 meaning not
  112. // occluded.
  113. U32 occlusionTexIdx;
  114. // Always stored as sRGB, in nits (candela / meter^2)
  115. U32 emissiveTexIdx;
  116. // Texture coordinates
  117. U32 baseColorTexCoords;
  118. U32 metalRoughTexCoords;
  119. U32 normalTexCoords;
  120. U32 occlusionTexCoords;
  121. U32 emissiveTexCoords;
  122. bool hasPBR;
  123. bool hasBaseTex;
  124. bool hasMRTex;
  125. bool hasNormalTex;
  126. bool hasOcclusionTex;
  127. bool hasEmissiveTex;
  128. };
  129. class LLGLTFLoader : public LLModelLoader
  130. {
  131. protected:
  132. LOG_CLASS(LLGLTFLoader);
  133. public:
  134. typedef std::map<std::string, LLImportMaterial> material_map;
  135. LLGLTFLoader(const std::string& filename, S32 lod,
  136. load_callback_t load_cb,
  137. joint_lookup_func_t joint_lookup_func,
  138. texture_load_func_t texture_load_func,
  139. state_callback_t state_cb, void* userdata,
  140. JointTransformMap& joint_transform_map,
  141. JointNameSet& joints_from_nodes,
  142. JointMap& legal_joint_names, U32 max_joints_per_mesh);
  143. virtual bool openFile(const std::string& filename);
  144. private:
  145. bool parseMeshes();
  146. #if 0 // *TOTO
  147. void uploadMeshes();
  148. #endif
  149. bool parseMaterials();
  150. void uploadMaterials();
  151. bool populateModelFromMesh(LLModel* modelp, const tinygltf::Mesh& mesh);
  152. // *TOTO
  153. LLUUID imageBufferToTextureUUID(const gltf_texture& tex)
  154. {
  155. return LLUUID::null;
  156. }
  157. protected:
  158. tinygltf::Model mGltfModel;
  159. std::vector<gltf_render_material> mMaterials;
  160. std::vector<gltf_texture> mTextures;
  161. std::vector<gltf_image> mImages;
  162. std::vector<gltf_sampler> mSamplers;
  163. bool mGltfLoaded;
  164. bool mMeshesLoaded;
  165. bool mMaterialsLoaded;
  166. };
  167. #endif // LL_LLGLTFLOADER_H