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  myAttributeData[info] = GLTF_AttributeVariant(
110  std::in_place_type<UT_Array<T>>);
111  UT_ASSERT(std::holds_alternative<UT_Array<T>>(myAttributeData[info]));
112  return std::get<UT_Array<T>>(myAttributeData[info]);
113  }
114 
115  draco::EncoderBuffer* getDracoEncoderBuffer(
116  const GLTF_Index mesh_id,
117  const GLTF_Index prim_id)
118  {
119  GLTF_MeshPrimId key(mesh_id, prim_id);
120  return myDracoInfo[key].encoderBuffer.get();
121  }
122 
124  const GLTF_Index mesh_id,
125  const GLTF_Index prim_id,
126  const GLTF_Index count)
127  {
128  GLTF_MeshPrimId key(mesh_id, prim_id);
129  myDracoInfo[key].indicesCount = count;
130  }
131 
132  UT_Array<uint32_t>& createVertexIndices(
133  const GLTF_Index mesh_id,
134  const GLTF_Index prim_id);
135 
136  void setPositionMinMax(
137  const GLTF_AttributeInfo& info,
138  const UT_Vector3F& min,
139  const UT_Vector3F& max);
140 
141  bool containsMaterial(const char* node_path);
142  GLTF_Index createOrGetMaterial(const char* node_path);
143 
144  bool containsImage(const char* name)
145  {
146  return myImageData.contains(name);
147  }
148 
149  GLTF_Index createImage(const char* name);
150  UT_Array<int8_t>& getImageBuffer(const char* name);
151  GLTF_Index getImageIndex(const char* name);
152 
153  bool embedImageFile(
154  const UT_StringHolder& filepath,
155  const IMG_Format* format,
156  const char* img_name);
157  bool copyImageFileAndExport(
158  const UT_StringHolder& filepath,
159  const IMG_Format* format,
160  const char* img_name);
161 
162  bool exportTextureWithImage(
163  const UT_StringHolder& image_path,
164  const UT_StringHolder& image_name,
165  const IMG_Format* format,
166  GLTF_ResourceExportLocation export_loc,
167  GLTF_Index& tex_id);
168 
170  {
171  return myIO.getCgltfData();
172  }
173  const cgltf_data* getCgltfData() const
174  {
175  return myIO.getCgltfData();
176  }
177 
179  {
180  return myIO.getCgltfWrapper();
181  }
182 
183 private:
184  void flushDataToCgltf();
185  void flushAnimData();
186  void flushBuffer();
187 
188  void decomposeRestTransformMatricesForAnimatedNodes();
189 
190 private:
191 
193  UT_WeakPtr<GLTF_AnimationData> myAnimationData;
194  GLTF_ErrorManager myErrors;
195 
196  UT_Array<uint8_t> myBuffer;
197 
198  UT_Map<
200  GLTF_AttributeVariant> myAttributeData;
201 
202  using GLTF_MeshPrimId = std::pair<GLTF_Index, GLTF_Index>;
203 
204  UT_Map<
205  GLTF_MeshPrimId,
206  GLTF_DracoMeshCompressionInfo> myDracoInfo;
207 
208  UT_Map<
209  std::pair<
211  UT_Array<uint32_t>> myVertexIndices;
212 
214  GLTF_Index> myMaterials;
215 
216  UT_StringMap<
217  std::pair<
218  GLTF_Index,
219  UT_Optional<
220  UT_Array<int8_t>>>> myImageData;
221 
222  UT_StringHolder myResourceDirectory;
223 
224  UT_StringHolder myCopyright;
225 };
226 
227 #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:115
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:178
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
bool containsImage(const char *name)
Definition: GLTF_Writer.h:144
void setDracoPrimIndexCount(const GLTF_Index mesh_id, const GLTF_Index prim_id, const GLTF_Index count)
Definition: GLTF_Writer.h:123
cgltf_data * getCgltfData()
Definition: GLTF_Writer.h:169
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 UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
#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:173