HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GLTF_ExportRoot.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef __GLTF_EXPORTROOT_h__
27 #define __GLTF_EXPORTROOT_h__
28 
29 #include "GLTFZ_API.h"
30 
31 #include <GLTF/GLTF_Types.h>
32 #include <GLTFZ/GLTF_ImageExport.h>
33 #include <UT/UT_Array.h>
34 #include <UT/UT_ArraySet.h>
35 #include <UT/UT_WorkBuffer.h>
36 
37 #include <GLTF/GLTF_Loader.h>
38 
39 #include <vector>
40 
41 constexpr const char *GENERATOR_STRING = "Houdini GLTF 2.0 Exporter";
42 constexpr const char *GLTF_VERSION = "2.0";
43 
44 class UT_WorkBuffer;
45 class UT_JSONWriter;
46 class UT_OFStream;
47 class OP_Node;
48 
49 namespace GLTF_NAMESPACE
50 {
51 
53 {
54 public:
56  {
57  bool exportNames = false;
58  };
59 
61  ~GLTF_ExportRoot();
62 
63  bool HasCachedChannelImage(
66  GetCachedChannelImage(const UT_Array<GLTF_ChannelMapping> &mapping);
67  void
68  InsertCachedChannelImage(const UT_Array<GLTF_ChannelMapping> &mapping,
69  GLTF_Handle idx);
70 
71  UT_Map<UT_StringHolder, GLTF_Handle> &GetImageCache();
72  UT_Map<const OP_Node *, GLTF_Handle> &GetMaterialCache();
73 
74  UT_Map<UT_StringHolder, UT_StringHolder>& GetMorphTargetToChannelMap();
75  UT_Map<UT_StringHolder, exint>& GetMorphTargetChannels();
76 
77  /// This keeps track of the amount of times a specific filename
78  /// outputted to to avoid name collisions
79  UT_Map<UT_StringHolder, GLTF_Int> &GetNameUsagesMap();
80 
81  ///
82  /// Allocates additional space in the buffer in index bid.
83  /// TODO: stop allocating from reallocing space.
84  ///
85  void *BufferAlloc(GLTF_Handle bid, GLTF_Offset bytes, GLTF_Offset alignment,
87 
88  ///
89  /// Returns a reference to the internal root GLTF object
90  ///
91  GLTF_Loader &loader();
92 
93  ///
94  /// Exports this structure as a GLTF file.
95  ///
96  bool ExportGLTF(const UT_String &path);
97 
98  ///
99  /// Exports the file as GLB on os. If a uri is defined on buffer 0,
100  /// it will be ignored. Otherwise, there are no modifications to the
101  /// outputted JSON.
102  ///
103  bool ExportAsGLB(const UT_String &path);
104  void SerializeJSON(UT_JSONWriter &writer);
105 
106  //
107  // Convenience functions: Will return the index of the created
108  // node in the parameter idx.
109  //
110  GLTF_Accessor &CreateAccessor(GLTF_Handle &idx);
111  GLTF_Animation &CreateAnimation(GLTF_Handle &idx);
112  GLTF_Buffer &CreateBuffer(GLTF_Handle &idx);
113  GLTF_BufferView &CreateBufferview(GLTF_Handle &idx);
114  GLTF_Camera &CreateCamera(GLTF_Handle &idx);
115  GLTF_Node &CreateNode(GLTF_Handle &idx);
116  GLTF_Mesh &CreateMesh(GLTF_Handle &idx);
117  GLTF_Scene &CreateScene(GLTF_Handle &idx);
118  GLTF_Skin &CreateSkin(GLTF_Handle &idx);
119  GLTF_Image &CreateImage(GLTF_Handle &idx);
120  GLTF_Texture &CreateTexture(GLTF_Handle &idx);
121  GLTF_Material &CreateMaterial(GLTF_Handle &idx);
122 
123  // Extension components
124  GLTF_Light &createLight(GLTF_Handle &idx);
125 
126  GLTF_Accessor *getAccessor(GLTF_Handle idx);
127  GLTF_Animation *getAnimation(GLTF_Handle idx);
128  GLTF_Asset getAsset();
129  GLTF_Buffer *getBuffer(GLTF_Handle idx);
130  GLTF_BufferView *getBufferView(GLTF_Handle idx);
131  GLTF_Camera *getCamera(GLTF_Handle idx);
132  GLTF_Image *getImage(GLTF_Handle idx);
133  GLTF_Material *getMaterial(GLTF_Handle idx);
134  GLTF_Mesh *getMesh(GLTF_Handle idx);
135  GLTF_Node *getNode(GLTF_Handle idx);
136  GLTF_Sampler *getSampler(GLTF_Handle idx);
137  GLTF_Handle getDefaultScene();
138  GLTF_Scene *getScene(GLTF_Handle idx);
139  GLTF_Skin *getSkin(GLTF_Handle idx);
140  GLTF_Texture *getTexture(GLTF_Handle idx);
141 
142  void SetDefaultScene(GLTF_Handle idx);
143 
144  const UT_Array<GLTF_Accessor *> &getAccessors() const;
145  const UT_Array<GLTF_Animation *> &getAnimations() const;
146  const UT_Array<GLTF_Buffer *> &getBuffers() const;
147  const UT_Array<GLTF_BufferView *> &getBufferViews() const;
148  const UT_Array<GLTF_Camera *> &getCameras() const;
149  const UT_Array<GLTF_Extension *> &getExtensions() const;
150  const UT_Array<GLTF_Image *> &getImages() const;
151  const UT_Array<GLTF_Material *> &getMaterials() const;
152  const UT_Array<GLTF_Mesh *> &getMeshes() const;
153  const UT_Array<GLTF_Node *> &getNodes() const;
154  const UT_Array<GLTF_Sampler *> &getSamplers() const;
155  const UT_Array<GLTF_Scene *> &getScenes() const;
156  const UT_Array<GLTF_Skin *> &getSkins() const;
157  const UT_Array<GLTF_Texture *> &getTextures() const;
158 
159  template <typename T>
161  const UT_Array<T>& arr,
163  {
164  T min, max;
165  GLTF_Handle bv_index = createScalarBufferView(arr, min, max);
166 
168  GLTF_Accessor &accessor = CreateAccessor(index);
169  accessor.bufferView = bv_index;
170  accessor.componentType = type;
171  accessor.count = arr.entries();
172  accessor.type = GLTF_TYPE_SCALAR;
173  accessor.min.append(min);
174  accessor.max.append(max);
175 
176  return index;
177  }
178 
179  template <typename T>
181  const UT_Array<UT_Array<T>* >& arr,
183  {
184  T min, max;
185  GLTF_Handle bv_index = createScalarBufferViewInterleaved(arr, min, max);
186 
188  GLTF_Accessor& accessor = CreateAccessor(index);
189  accessor.bufferView = bv_index;
190  accessor.componentType = type;
191  accessor.count = arr.entries() * arr[0]->entries();
192  accessor.type = GLTF_TYPE_SCALAR;
193  accessor.min.append(min);
194  accessor.max.append(max);
195 
196  return index;
197  }
198 
199  template <typename T>
201  const UT_Array<T>& arr,
202  T& min,
203  T& max)
204  {
205  const exint count = arr.entries();
206  GLTF_Offset size = count * sizeof(T);
208  void *buffer = BufferAlloc(0, size, sizeof(T), offset);
209 
210  memcpy(buffer, arr.getRawArray(), size);
211  auto min_max = std::minmax_element(arr.begin(), arr.end());
212 
213  GLTF_Handle bv_index;
214  GLTF_BufferView &bufferview = CreateBufferview(bv_index);
215  bufferview.buffer = 0;
216  bufferview.byteLength = size;
217  bufferview.byteOffset = offset;
218 
219  min = *min_max.first;
220  max = *min_max.second;
221  return bv_index;
222  }
223 
224  template <typename T>
226  const UT_Array<UT_Array<T>* >& arr,
227  T& min,
228  T& max)
229  {
230  const exint count = arr.entries() * arr[0]->entries();
231  GLTF_Offset size = count * sizeof(T);
233  void* buffer = BufferAlloc(0, size, sizeof(T), offset);
234  T* buf_t = static_cast<T*>(buffer);
235 
236  T local_min = (*arr[0])[0];
237  T local_max = (*arr[0])[0];
238 
239  for (exint i = 0; i < arr[0]->entries(); ++i)
240  {
241  for (exint j = 0; j < arr.entries(); ++j)
242  {
243  buf_t[i * arr.entries() + j] = (*arr[j])[i];
244 
245  if ((*arr[j])[i] < local_min)
246  local_min = (*arr[j])[i];
247  if ((*arr[j])[i] > local_max)
248  local_max = (*arr[j])[i];
249  }
250  }
251 
252  GLTF_Handle bv_index;
253  GLTF_BufferView& bufferview = CreateBufferview(bv_index);
254  bufferview.buffer = 0;
255  bufferview.byteLength = size;
256  bufferview.byteOffset = offset;
257 
258  min = local_min;
259  max = local_max;
260  return bv_index;
261  }
262 
263 private:
264  void
265  OutputName(UT_JSONWriter &writer, const char *string, const char *value);
266 
267  void SerializeAsset(UT_JSONWriter &writer);
268  void SerializeAnimations(UT_JSONWriter &writer);
269  void SerializeAccessors(UT_JSONWriter &writer);
270  void SerializeBuffers(UT_JSONWriter &writer);
271  void SerializeBufferViews(UT_JSONWriter &writer);
272  void SerializeCameras(UT_JSONWriter &writer);
273  void SerializeExtensions(UT_JSONWriter &writer);
274  void SerializeNodes(UT_JSONWriter &writer);
275  void SerializeMeshes(UT_JSONWriter &writer);
276  void SerializeMaterials(UT_JSONWriter &writer);
277  void SerializePrimitives(UT_JSONWriter &writer,
278  const UT_Array<GLTF_Primitive> &primitive);
279  void SerializeTextures(UT_JSONWriter &writer);
280  void SerializeImages(UT_JSONWriter &writer);
281  void SerializeScenes(UT_JSONWriter &writer);
282  void SerializeSkins(UT_JSONWriter &writer);
283 
284  bool OutputBuffer(const char *folder, GLTF_Handle buffer) const;
285  void OutputGLBBuffer(UT_OFStream &stream) const;
286 
287  // Pre-output pass: these should be used only before outputting as they
288  // may potentially invalidate handles
289  void ResolveBufferLengths();
290  void RemoveEmptyBuffers();
291 
292  // Any external files must be copied to the output folder as
293  // GLTF is only required to support "http://" and relative URI
294  void ConvertAbsolutePaths(const UT_String &base_path);
295 
296  // Returns true if opened output stream to given path.
297  // Automatically creates directories in path.
298  bool OpenFileStreamAtPath(const UT_String &path, UT_OFStream &os);
299 
300  UT_Array<UT_Array<char>> myBufferData;
301 
302  UT_Map<UT_StringHolder, GLTF_Handle> myNameUsagesMap;
305 
306  UT_Map<UT_StringHolder, UT_StringHolder> myMorphTargetToChannelMap;
307  UT_Map<UT_StringHolder, exint> myMorphTargetChannels;
308 
309  std::map<std::vector<GLTF_ChannelMapping>, GLTF_Handle>
310  myChannelImageMap;
311 
312  GLTF_Loader myLoader;
313  ExportSettings mySettings;
314 };
315 
316 } // namespace GLTF_NAMESPACE
317 
318 #endif
type
Definition: core.h:977
const T * getRawArray() const
Definition: UT_Array.h:650
GLTF_Handle addScalarArray(const UT_Array< T > &arr, GLTF_ComponentType type)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
uint32 GLTF_Handle
Definition: GLTF_Types.h:48
int64 exint
Definition: SYS_Types.h:125
GLuint GLuint stream
Definition: glcorearb.h:1831
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:35
GLuint buffer
Definition: glcorearb.h:659
uint32 GLTF_Offset
Definition: GLTF_Types.h:47
GLsizeiptr size
Definition: glcorearb.h:663
#define GLTF_NAMESPACE
Definition: GLTF_API.h:42
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLTF_Handle createScalarBufferView(const UT_Array< T > &arr, T &min, T &max)
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
iterator begin()
Definition: UT_Array.h:817
GLint GLsizei count
Definition: glcorearb.h:404
GLenum GLenum GLenum GLenum mapping
Definition: glew.h:14162
Portable replacement for std::ofstream.
Definition: UT_OFStream.h:26
UT_Array< fpreal64 > max
Definition: GLTF_Types.h:235
exint append()
Definition: UT_Array.h:95
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:481
GLTF_Handle createScalarBufferViewInterleaved(const UT_Array< UT_Array< T > * > &arr, T &min, T &max)
GLuint index
Definition: glcorearb.h:785
GLTF_ComponentType componentType
Definition: GLTF_Types.h:228
GLsizei const GLfloat * value
Definition: glcorearb.h:823
GLintptr offset
Definition: glcorearb.h:664
GLTF_Handle addScalarArraysInterleaved(const UT_Array< UT_Array< T > * > &arr, GLTF_ComponentType type)
constexpr const char * GENERATOR_STRING
#define GLTFZ_API
Definition: GLTFZ_API.h:37
constexpr const char * GLTF_VERSION
GLdouble s
Definition: glew.h:1395
iterator end()
End iterator.
Definition: UT_Array.h:822
Definition: format.h:3611
UT_Array< fpreal64 > min
Definition: GLTF_Types.h:236