HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GLTF_CgltfWrapper.h
Go to the documentation of this file.
1 #ifndef __GLTF_CgltfWrapper_h__
2 #define __GLTF_CgltfWrapper_h__
3 
4 // cgltf_write.h includes cgltf.h, and because it's a header only library, the
5 // header guards will cause issues when trying to compile both the cgltf.h and
6 // cgltf_write.h implementation in GLTF_CgltfWrapper.C
7 #include <cgltf_write.h>
8 
9 #include "GLTFZ_API.h"
10 
11 #include "GLTF_Types.h"
12 
13 #include <UT/UT_Assert.h>
14 #include <UT/UT_JSONParser.h>
15 #include <UT/UT_Options.h>
16 #include <UT/UT_UniquePtr.h>
17 
18 #include <stdlib.h>
19 #include <string.h>
20 
21 inline constexpr const char* cgltf_GltfVersion = "2.0";
22 inline constexpr const char* cgltf_GltfGenerator = "SideFX Houdini";
23 
24 #define GLTF_DEFINE_APPEND_FUNC( \
25  func_name, cgltf_type, container, count_member) \
26  cgltf_type& func_name() \
27  { \
28  cgltf_type& new_elem = appendProperty(&container, count_member); \
29  initProperty(&new_elem); \
30  return new_elem; \
31  }
32 
33 template <typename T>
36 {
37  return reinterpret_cast<GLTF_Index>(ptr) - 1;
38 }
39 
40 template <typename T>
41 T*
43 {
44  return reinterpret_cast<T*>(id + 1);
45 }
46 
48 {
49 public:
51  {
52  void operator()(cgltf_data* data) const;
53  };
54 
55 public:
58 
60  {
61  myCgltfData = std::move(data);
62  }
63 
64  cgltf_data* getCgltfData() { return myCgltfData.get(); }
65  const cgltf_data* getCgltfData() const { return myCgltfData.get(); }
66 
67  cgltf_options& getCgltfOptions() { return myCgltfOptions; }
68  const cgltf_options& getCgltfOptions() const { return myCgltfOptions; }
69 
70  bool fixupPointers();
71 
72 private:
73  inline static void gltf_free_func(void* user, void* ptr) { free(ptr); }
74 
75 public:
76  static void writeCharProperty(char** prop, const char* src)
77  {
78  *prop = (char*)malloc(sizeof(char) * (strlen(src) + 1));
79  strcpy(*prop, src);
80  }
81 
82  template <typename T>
83  static void initProperty(T* prop)
84  {
85  memset(prop, 0, sizeof(*prop));
86  }
87 
88  void initCgltfData(bool writing_mode = false)
89  {
90  initProperty(&myCgltfOptions);
91 
92  myCgltfData.reset(nullptr);
93  cgltf_data* new_data = (cgltf_data*)malloc(sizeof(cgltf_data));
94  initProperty(new_data);
95  myCgltfData.reset(new_data);
96 
97  myCgltfData->memory.free_func = &gltf_free_func;
98 
99  if (writing_mode)
100  initAsHoudiniAsset(myCgltfData->asset);
101  }
102 
104  {
105  asset.version
106  = (char*)malloc(sizeof(char) * (strlen(cgltf_GltfVersion) + 1));
107  strcpy(asset.version, cgltf_GltfVersion);
108  asset.generator = (char*)malloc(
109  sizeof(char) * (strlen(cgltf_GltfGenerator) + 1));
110  strcpy(asset.generator, cgltf_GltfGenerator);
111  asset.copyright = nullptr;
112  asset.min_version = nullptr;
113  asset.extensions = nullptr;
114  initProperty(&asset.extras);
115  }
116 
118  cgltf_attribute& attrib,
119  cgltf_size accessor_index,
120  const char* name,
121  cgltf_accessor* data_accessor = nullptr)
122  {
123  attrib.name = (char*)malloc(sizeof(char) * (strlen(name) + 1));
124  strcpy(attrib.name, name);
126  attrib.index = accessor_index;
127  attrib.data = data_accessor;
128  }
129 
130  template <typename T>
131  T& appendProperty(T** propList, cgltf_size& propSize)
132  {
133  if (*propList == nullptr && propSize == 0)
134  {
135  *propList = (T*)malloc(sizeof(T));
136  propSize = 1;
137  return (*propList)[0];
138  }
139 
140  cgltf_size old_size = propSize;
141  T* old_prop = *propList;
142 
143  propSize += 1;
144  *propList = (T*)malloc(sizeof(T) * propSize);
145 
146  for (cgltf_size i = 0; i < old_size; ++i)
147  (*propList)[i] = old_prop[i];
148 
149  free(old_prop);
150  old_prop = nullptr;
151 
152  return (*propList)[propSize - 1];
153  }
154 
155  template <typename T>
156  T*& appendPropertyRef(T*** propList, cgltf_size& propSize)
157  {
158  if (*propList == nullptr && propSize == 0)
159  {
160  *propList = (T**)malloc(sizeof(T*));
161  propSize = 1;
162  return (*propList)[0];
163  }
164 
165  cgltf_size old_size = propSize;
166  T** old_prop = *propList;
167 
168  ++propSize;
169  *propList = (T**)malloc(sizeof(T*) * propSize);
170 
171  for (cgltf_size i = 0; i < old_size; ++i)
172  (*propList)[i] = old_prop[i];
173 
174  free(old_prop);
175  old_prop = nullptr;
176 
177  return (*propList)[propSize - 1];
178  }
179 
181  {
182  buf.size = size;
183  myCgltfData->bin = data;
184  myCgltfData->bin_size = size;
185  }
186 
187  template <typename T>
188  static bool propHasExtras(const T* prop)
189  {
190  return prop->extras.data != nullptr;
191  }
192 
194  {
195  UT_AutoJSONParser parser(extras.data, strlen(extras.data) + 1);
196  UT_Options options;
197  options.load(parser, false, nullptr, false);
198  return options;
199  }
200 
201  // ---------------- Property Appenders ------------------
202  // Below are provided macro-defined property appends as well as custom
203  // property appenders. These are necessary because cgltf memory is manually
204  // managed and we don't want anyone but this class to really be managing
205  // that memory
206  //
207 
209  appendAccessor,
211  myCgltfData->accessors,
212  myCgltfData->accessors_count);
214  appendAnimation,
216  myCgltfData->animations,
217  myCgltfData->animations_count);
219  appendBuffer,
220  cgltf_buffer,
221  myCgltfData->buffers,
222  myCgltfData->buffers_count);
224  appendBufferView,
226  myCgltfData->buffer_views,
227  myCgltfData->buffer_views_count);
229  appendCamera,
230  cgltf_camera,
231  myCgltfData->cameras,
232  myCgltfData->cameras_count);
234  appendImage,
235  cgltf_image,
236  myCgltfData->images,
237  myCgltfData->images_count);
239  appendLight,
240  cgltf_light,
241  myCgltfData->lights,
242  myCgltfData->lights_count);
244  appendMaterial,
246  myCgltfData->materials,
247  myCgltfData->materials_count);
249  appendMesh,
250  cgltf_mesh,
251  myCgltfData->meshes,
252  myCgltfData->meshes_count);
254  appendNode,
255  cgltf_node,
256  myCgltfData->nodes,
257  myCgltfData->nodes_count);
259  appendTexture,
261  myCgltfData->textures,
262  myCgltfData->textures_count);
263 
265  {
266  cgltf_scene& new_scene = appendProperty(
267  &myCgltfData->scenes, myCgltfData->scenes_count);
268  initProperty(&new_scene);
269  cgltf_size scene_id = myCgltfData->scenes_count - 1;
270  myCgltfData->scene = reinterpret_cast<cgltf_scene*>(scene_id + 1);
271  return new_scene;
272  }
273 
275  cgltf_size mesh_id,
276  cgltf_size prim_id,
277  cgltf_size accessor_id,
279  const char* name)
280  {
281  UT_ASSERT(mesh_id < myCgltfData->meshes_count);
282  cgltf_mesh& mesh = myCgltfData->meshes[mesh_id];
283  UT_ASSERT(prim_id < mesh.primitives_count);
284 
285  cgltf_attribute& new_attrib = appendProperty(
286  &mesh.primitives[prim_id].attributes,
287  mesh.primitives[prim_id].attributes_count);
288  new_attrib.type = type;
289  initAttribute(
290  new_attrib, accessor_id, name,
291  INDEX_TO_CGLTF_PTR<cgltf_accessor>(accessor_id));
292 
293  return new_attrib;
294  }
295 
297  cgltf_size mesh_id,
298  cgltf_size prim_id,
299  cgltf_size draco_id,
301  const char* name)
302  {
303  UT_ASSERT(mesh_id < myCgltfData->meshes_count);
304  cgltf_mesh& mesh = myCgltfData->meshes[mesh_id];
305  UT_ASSERT(prim_id < mesh.primitives_count);
306 
307  cgltf_attribute& new_attrib = appendProperty(
309  mesh.primitives[prim_id]
311  new_attrib.type = type;
312  initAttribute(
313  new_attrib, draco_id, name,
314  INDEX_TO_CGLTF_PTR<cgltf_accessor>(draco_id));
315 
316  return new_attrib;
317  }
318 
320  {
321  UT_ASSERT(mesh_id < myCgltfData->meshes_count);
322  cgltf_mesh& mesh = myCgltfData->meshes[mesh_id];
323  cgltf_primitive& new_prim = appendProperty(
324  &mesh.primitives, mesh.primitives_count);
325  initProperty(&new_prim);
327  return new_prim;
328  }
329 
331  {
332  cgltf_animation_sampler& anim_sampler = appendProperty(
333  &anim.samplers, anim.samplers_count);
334  initProperty(&anim_sampler);
335  return anim_sampler;
336  }
337 
339  {
340  cgltf_animation_channel& anim_channel = appendProperty(
341  &anim.channels, anim.channels_count);
342  initProperty(&anim_channel);
343  return anim_channel;
344  }
345 
346  // ---------------- Buffers ------------------
347  //
348  cgltf_result loadBufferFile(
349  cgltf_size buf_size,
350  const char* buf_uri,
351  const char* gltf_path,
352  void** data);
353 
354 private:
355  cgltf_options myCgltfOptions;
357 };
358 
359 #endif // __GLTF_CgltfWrapper_h__
void initCgltfData(bool writing_mode=false)
type
Definition: core.h:556
cgltf_size size
Definition: cgltf.h:277
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
char * generator
Definition: cgltf.h:762
cgltf_attribute_type
Definition: cgltf.h:164
size_t cgltf_size
Definition: cgltf.h:104
GLboolean * data
Definition: glcorearb.h:131
GLTF_Index CGLTF_PTR_TO_INDEX(T *ptr)
T *& appendPropertyRef(T ***propList, cgltf_size &propSize)
T * INDEX_TO_CGLTF_PTR(GLTF_Index id)
cgltf_scene & appendSceneAndSetToDefault()
cgltf_result
Definition: cgltf.h:119
cgltf_data * getCgltfData()
cgltf_size channels_count
Definition: cgltf.h:748
void initAttribute(cgltf_attribute &attrib, cgltf_size accessor_index, const char *name, cgltf_accessor *data_accessor=nullptr)
cgltf_animation_sampler & appendAnimationSampler(cgltf_animation &anim)
cgltf_primitive * primitives
Definition: cgltf.h:625
cgltf_extension * extensions
Definition: cgltf.h:767
cgltf_attribute * attributes
Definition: cgltf.h:610
static void writeCharProperty(char **prop, const char *src)
void initAsHoudiniAsset(cgltf_asset &asset)
cgltf_extras extras
Definition: cgltf.h:765
cgltf_accessor * data
Definition: cgltf.h:367
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
constexpr const char * cgltf_GltfVersion
char * copyright
Definition: cgltf.h:761
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
constexpr const char * cgltf_GltfGenerator
cgltf_size primitives_count
Definition: cgltf.h:626
char * min_version
Definition: cgltf.h:764
bool load(const char *filename)
cgltf_primitive_type type
Definition: cgltf.h:607
char * version
Definition: cgltf.h:763
cgltf_attribute & appendAttribute(cgltf_size mesh_id, cgltf_size prim_id, cgltf_size accessor_id, cgltf_attribute_type type, const char *name)
char * data
Definition: cgltf.h:266
T & appendProperty(T **propList, cgltf_size &propSize)
GLuint const GLchar * name
Definition: glcorearb.h:786
std::size_t GLTF_Index
Definition: GLTF_Types.h:13
cgltf_size samplers_count
Definition: cgltf.h:746
static UT_Options getExtrasAsOptions(const cgltf_extras &extras)
#define GLTF_DEFINE_APPEND_FUNC(func_name, cgltf_type, container, count_member)
GLsizeiptr size
Definition: glcorearb.h:664
static void initProperty(T *prop)
void setMainBuffer(cgltf_buffer &buf, uint8_t *data, cgltf_size size)
A map of string to various well defined value types.
Definition: UT_Options.h:87
cgltf_primitive & appendPrimitive(cgltf_size mesh_id)
cgltf_int index
Definition: cgltf.h:366
cgltf_size attributes_count
Definition: cgltf.h:611
cgltf_attribute * attributes
Definition: cgltf.h:597
cgltf_animation_sampler * samplers
Definition: cgltf.h:745
cgltf_animation_channel * channels
Definition: cgltf.h:747
auto ptr(T p) -> const void *
Definition: format.h:4331
cgltf_attribute & appendDracoAttribute(cgltf_size mesh_id, cgltf_size prim_id, cgltf_size draco_id, cgltf_attribute_type type, const char *name)
cgltf_draco_mesh_compression draco_mesh_compression
Definition: cgltf.h:616
cgltf_animation_channel & appendAnimationChannel(cgltf_animation &anim)
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
cgltf_size attributes_count
Definition: cgltf.h:598
#define GLTFZ_API
Definition: GLTFZ_API.h:37
cgltf_attribute_type type
Definition: cgltf.h:365
char * name
Definition: cgltf.h:364
void setCgltfData(UT_UniquePtr< cgltf_data, CgltfDataDeleter > &&data)
const cgltf_options & getCgltfOptions() const
Definition: format.h:1821
static bool propHasExtras(const T *prop)
cgltf_options & getCgltfOptions()
const cgltf_data * getCgltfData() const
GLenum src
Definition: glcorearb.h:1793