HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GLTF_Writer.h
Go to the documentation of this file.
1 #ifndef __GLTF_Writer_h__
2 #define __GLTF_Writer_h__
3 
4 #include "GLTFZ_API.h"
5 #include "GLTF_AnimationData.h"
6 #include "GLTF_ErrorManager.h"
7 #include "GLTF_IO.h"
8 #include "GLTF_Types.h"
9 
10 #include <OBJ/OBJ_Camera.h>
11 #include <UT/UT_Array.h>
12 #include <UT/UT_CameraParms.h>
13 #include <UT/UT_Map.h>
14 #include <UT/UT_Optional.h>
15 #include <UT/UT_StringHolder.h>
16 #include <UT/UT_StringMap.h>
17 #include <UT/UT_UniquePtr.h>
18 #include <UT/UT_WeakPtr.h>
19 
20 #include <utility>
21 #include <variant>
22 
23 class IMG_Format;
24 class OBJ_Light;
25 
26 namespace draco
27 {
28  class EncoderBuffer;
29 }
30 
32 {
33 private:
34 
35  using GLTF_AttributeVariant = std::variant<
42 
43  struct GLTF_DracoMeshCompressionInfo
44  {
45  GLTF_DracoMeshCompressionInfo();
46  ~GLTF_DracoMeshCompressionInfo();
47 
49  GLTF_Index indicesCount = 0;
50  bool bufferViewExists = false;
51  GLTF_Index bufferView;
52  };
53 
54 public:
55  GLTF_Writer();
56  ~GLTF_Writer();
57 
58  bool writeToFile(const char* filepath);
59 
61  const UT_StringHolder& dir)
62  { myResourceDirectory = dir; }
63 
65  const UT_StringHolder& copyright)
66  { myCopyright = copyright; }
67 
68  bool initializeGltfData(
69  const char* filename,
70  const UT_WeakPtr<GLTF_AnimationData>& anim_data);
71 
73  {
74  return myErrors;
75  }
76 
77  GLTF_Index addNode();
78  GLTF_Index addMesh();
79  GLTF_Index addPrimitive(
80  const GLTF_Index mesh_id,
82  GLTF_Index addAttribute(
83  const GLTF_Index mesh_id,
84  const GLTF_Index prim_id,
86  const char* name);
87 
88  GLTF_Index addCamera(
90  const UT_CameraParms& parms);
91 
92  GLTF_Index addLight(
94 
95  void addRootNodeToScene(
96  GLTF_Index node_id,
97  GLTF_Index scene_id);
98 
99  void setNodeMesh(
100  GLTF_Index node_id,
101  GLTF_Index mesh_id);
102 
103  GLTF_Index defaultScene() const;
104 
105  template<typename T>
107  const GLTF_AttributeInfo& info)
108  {
109  GLTF_AttributeVariant& attrib_variant = myAttributeData[info];
110  attrib_variant = GLTF_AttributeVariant(std::in_place_type<UT_Array<T>>);
111  return std::get<UT_Array<T>>(attrib_variant);
112  }
113 
114  draco::EncoderBuffer* getDracoEncoderBuffer(
115  const GLTF_Index mesh_id,
116  const GLTF_Index prim_id)
117  {
118  GLTF_MeshPrimId key(mesh_id, prim_id);
119  return myDracoInfo[key].encoderBuffer.get();
120  }
121 
123  const GLTF_Index mesh_id,
124  const GLTF_Index prim_id,
125  const GLTF_Index count)
126  {
127  GLTF_MeshPrimId key(mesh_id, prim_id);
128  myDracoInfo[key].indicesCount = count;
129  }
130 
131  UT_Array<uint32_t>& createVertexIndices(
132  const GLTF_Index mesh_id,
133  const GLTF_Index prim_id);
134 
135  void setPositionMinMax(
136  const GLTF_AttributeInfo& info,
137  const UT_Vector3F& min,
138  const UT_Vector3F& max);
139 
140  bool containsMaterial(const char* node_path);
141  GLTF_Index createOrGetMaterial(const char* node_path);
142 
143  bool containsImage(const char* name)
144  {
145  return myImageData.contains(name);
146  }
147 
148  GLTF_Index createImage(const char* name);
149  UT_Array<int8_t>& getImageBuffer(const char* name);
150  GLTF_Index getImageIndex(const char* name);
151 
152  bool embedImageFile(
153  const UT_StringHolder& filepath,
154  const IMG_Format* format,
155  const char* img_name);
156  bool copyImageFileAndExport(
157  const UT_StringHolder& filepath,
158  const IMG_Format* format,
159  const char* img_name);
160 
161  bool exportTextureWithImage(
162  const UT_StringHolder& image_path,
163  const UT_StringHolder& image_name,
164  const IMG_Format* format,
165  GLTF_ResourceExportLocation export_loc,
166  GLTF_Index& tex_id);
167 
169  {
170  return myIO.getCgltfData();
171  }
172  const cgltf_data* getCgltfData() const
173  {
174  return myIO.getCgltfData();
175  }
176 
178  {
179  return myIO.getCgltfWrapper();
180  }
181 
182 private:
183  void flushDataToCgltf();
184  void flushAnimData();
185  void flushBuffer();
186 
187  void decomposeRestTransformMatricesForAnimatedNodes();
188 
189 private:
190 
192  UT_WeakPtr<GLTF_AnimationData> myAnimationData;
193  GLTF_ErrorManager myErrors;
194 
195  UT_Array<uint8_t> myBuffer;
196 
197  UT_Map<
199  GLTF_AttributeVariant> myAttributeData;
200 
201  using GLTF_MeshPrimId = std::pair<GLTF_Index, GLTF_Index>;
202 
203  UT_Map<
204  GLTF_MeshPrimId,
205  GLTF_DracoMeshCompressionInfo> myDracoInfo;
206 
207  UT_Map<
208  std::pair<
210  UT_Array<uint32_t>> myVertexIndices;
211 
213  GLTF_Index> myMaterials;
214 
215  UT_StringMap<
216  std::pair<
217  GLTF_Index,
218  UT_Optional<
219  UT_Array<int8_t>>>> myImageData;
220 
221  UT_StringHolder myResourceDirectory;
222 
223  UT_StringHolder myCopyright;
224 };
225 
226 #endif
void setResourceDirectory(const UT_StringHolder &dir)
Definition: GLTF_Writer.h:60
UT_Array< T > & createAttribute(const GLTF_AttributeInfo &info)
Definition: GLTF_Writer.h:106
GT_API const UT_StringHolder filename
Unsorted map container.
Definition: UT_Map.h:114
const GLTF_ErrorManager & getErrorManager() const
Definition: GLTF_Writer.h:72
GLTF_CameraProjection
Definition: GLTF_Types.h:73
GLTF_AttributeType
Definition: GLTF_Types.h:15
GLTF_ResourceExportLocation
Definition: GLTF_Types.h:59
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void setCopyright(const UT_StringHolder &copyright)
Definition: GLTF_Writer.h:64
draco::EncoderBuffer * getDracoEncoderBuffer(const GLTF_Index mesh_id, const GLTF_Index prim_id)
Definition: GLTF_Writer.h:114
std::optional< T > UT_Optional
Definition: UT_Optional.h:26
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
GLTF_CgltfWrapper * getCgltfWrapper()
Definition: GLTF_Writer.h:177
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
bool containsImage(const char *name)
Definition: GLTF_Writer.h:143
void setDracoPrimIndexCount(const GLTF_Index mesh_id, const GLTF_Index prim_id, const GLTF_Index count)
Definition: GLTF_Writer.h:122
cgltf_data * getCgltfData()
Definition: GLTF_Writer.h:168
GLuint const GLchar * name
Definition: glcorearb.h:786
std::size_t GLTF_Index
Definition: GLTF_Types.h:13
GA_API const UT_StringHolder parms
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLTF_PrimitiveType
Definition: GLTF_Types.h:51
GLTFZ_LightType
Definition: GLTF_Types.h:108
#define GLTFZ_API
Definition: GLTFZ_API.h:37
std::weak_ptr< T > UT_WeakPtr
Definition: UT_SharedPtr.h:49
GLint GLsizei count
Definition: glcorearb.h:405
const cgltf_data * getCgltfData() const
Definition: GLTF_Writer.h:172