HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GLTF_Loader.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) COPYRIGHTYEAR
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  */
27 
28 #ifndef __SOP_GLTFLOADER_H__
29 #define __SOP_GLTFLOADER_H__
30 
31 #include "GLTF_API.h"
32 #include "GLTF_Types.h"
33 
34 #include <UT/UT_Array.h>
35 #include <UT/UT_ArrayStringSet.h>
36 #include <UT/UT_Function.h>
37 #include <UT/UT_Lock.h>
38 #include <UT/UT_String.h>
39 #include <UT/UT_UniquePtr.h>
40 #include <UT/UT_WorkBuffer.h>
41 #include <SYS/SYS_Types.h>
42 
43 class UT_JSONValue;
44 class UT_JSONValueMap;
45 class UT_JSONValueArray;
46 
47 namespace GLTF_NAMESPACE
48 {
49 
50 //=================================================
51 
52 ///
53 /// A class for loading a GLTF file into a more usuable structure.
54 ///
56 {
57 public:
58  GLTF_Loader();
60  virtual ~GLTF_Loader();
61 
62  // Delete copy constructor
63  GLTF_Loader(GLTF_Loader &loader) = delete;
64  GLTF_Loader(const GLTF_Loader &loader) = delete;
65 
66  ///
67  /// Loads and parses the JSON data within this GLTF file.
68  /// Does not load any associated buffer data.
69  /// @return Whether or not the load suceeded
70  ///
71  bool Load(UT_WorkBuffer& errors);
72 
73  ///
74  /// Loads all data that can be accessed with the given accessor and returns
75  /// a pointer to the beginning of the data. The caller is not responsible
76  /// for deleting the returned data.
77  /// @return Whether or not the accessor data load suceeded
78  ///
79  bool LoadAccessorData(const GLTF_Accessor &accessor, unsigned char *&data) const;
80 
82  GLTF_Animation *createAnimation(GLTF_Handle& idx);
83  GLTF_Buffer *createBuffer(GLTF_Handle& idx);
84  GLTF_BufferView *createBufferView(GLTF_Handle& idx);
85  GLTF_Camera *createCamera(GLTF_Handle& idx);
86  GLTF_Image *createImage(GLTF_Handle& idx);
87  GLTF_Material *createMaterial(GLTF_Handle& idx);
88  GLTF_Mesh *createMesh(GLTF_Handle& idx);
89  GLTF_Node *createNode(GLTF_Handle& idx);
91  GLTF_Scene *createScene(GLTF_Handle& idx);
92  GLTF_Skin *createSkin(GLTF_Handle& idx);
93  GLTF_Texture *createTexture(GLTF_Handle& idx);
94 
95  // Extension components:
96 
97  GLTF_Light &createLight(GLTF_Handle &idx);
98 
99  GLTF_Accessor const *getAccessor(GLTF_Handle idx) const;
100  GLTF_Animation const *getAnimation(GLTF_Handle idx) const;
101  GLTF_Asset const *getAsset() const;
102  GLTF_Buffer const *getBuffer(GLTF_Handle idx) const;
103  GLTF_BufferView const *getBufferView(GLTF_Handle idx) const;
104  GLTF_Camera const *getCamera(GLTF_Handle idx) const;
105  GLTF_Image const *getImage(GLTF_Handle idx) const;
106  GLTF_Material const *getMaterial(GLTF_Handle idx) const;
107  GLTF_Mesh const *getMesh(GLTF_Handle idx) const;
108  GLTF_Node const *getNode(GLTF_Handle idx) const;
109  GLTF_Sampler const *getSampler(GLTF_Handle idx) const;
110  GLTF_Handle getDefaultScene() const;
111  GLTF_Scene const *getScene(GLTF_Handle idx) const;
112  GLTF_Skin const *getSkin(GLTF_Handle idx) const;
113  GLTF_Texture const *getTexture(GLTF_Handle idx) const;
114 
115  GLTF_Accessor *getAccessor(GLTF_Handle idx);
116  GLTF_Animation *getAnimation(GLTF_Handle idx);
117  GLTF_Asset *getAsset();
118  GLTF_Buffer *getBuffer(GLTF_Handle idx);
119  GLTF_BufferView *getBufferView(GLTF_Handle idx);
120  GLTF_Camera *getCamera(GLTF_Handle idx);
121  GLTF_Image *getImage(GLTF_Handle idx);
122  GLTF_Material *getMaterial(GLTF_Handle idx);
123  GLTF_Mesh *getMesh(GLTF_Handle idx);
124  GLTF_Node *getNode(GLTF_Handle idx);
125  GLTF_Sampler *getSampler(GLTF_Handle idx);
126  GLTF_Scene *getScene(GLTF_Handle idx);
127  GLTF_Skin *getSkin(GLTF_Handle idx);
128  GLTF_Texture *getTexture(GLTF_Handle idx);
129 
130  const UT_Array<GLTF_Accessor *> &getAccessors() const;
131  const UT_Array<GLTF_Animation *> &getAnimations() const;
132  const UT_Array<GLTF_Buffer *> &getBuffers() const;
133  const UT_Array<GLTF_BufferView *> &getBufferViews() const;
134  const UT_Array<GLTF_Camera *> &getCameras() const;
135  const UT_Array<GLTF_Extension *> &getExtensions() const;
136  const UT_Array<GLTF_Image *> &getImages() const;
137  const UT_Array<GLTF_Material *> &getMaterials() const;
138  const UT_Array<GLTF_Mesh *> &getMeshes() const;
139  const UT_Array<GLTF_Node *> &getNodes() const;
140  const UT_Array<GLTF_Sampler *> &getSamplers() const;
141  const UT_Array<GLTF_Scene *> &getScenes() const;
142  const UT_Array<GLTF_Skin *> &getSkins() const;
143  const UT_Array<GLTF_Texture *> &getTextures() const;
144 
145  void removeBuffer(GLTF_Handle idx);
146  void removeNode(GLTF_Handle idx);
147 
148  void setDefaultScene(const GLTF_Handle &idx);
149  void setAsset(const GLTF_Asset &asset);
150 
151  exint getNumAccessors() const;
152  exint getNumAnimations() const;
153  exint getNumBuffers() const;
154  exint getNumBufferViews() const;
155  exint getNumCameras() const;
156  exint getNumExtensions() const;
157  exint getNumImages() const;
158  exint getNumMaterials() const;
159  exint getNumMeshes() const;
160  exint getNumNodes() const;
161  exint getNumSamplers() const;
162  exint getNumScenes() const;
163  exint getNumSkins() const;
164  exint getNumTextures() const;
165 
166  const UT_ArrayStringSet& getUsedExtensions() const;
167  const UT_ArrayStringSet& getRequiredExtensions() const;
168 
169 private:
170  template<typename T>
171  GLTF_Extension* createExtension(bool addExtensionProperty);
172 
173  // Handles reading the JSON map, which may be external or
174  // embedded in a GLB file
175  bool ReadJSON(const UT_JSONValueMap &root_json,
176  UT_WorkBuffer& errors);
177 
178  bool ReadGLTF(UT_WorkBuffer& errors);
179  bool ReadGLB(UT_WorkBuffer& errors);
180 
181  bool ReadAsset(const UT_JSONValueMap &asset_json);
182 
183  // Read items from GLTF JSON -> GLTF struct
184  bool ReadNode(const UT_JSONValueMap &node_json, const exint idx);
185  bool ReadBuffer(const UT_JSONValueMap &buffer_json, const exint idx);
186  bool ReadBufferView(const UT_JSONValueMap &bufferview_json, const exint idx);
187  bool ReadAccessor(const UT_JSONValueMap &accessor_json, const exint idx);
188  bool ReadPrimitive(const UT_JSONValue *primitive_json, GLTF_Primitive *primitive);
189  bool ReadMesh(const UT_JSONValueMap &mesh_json, const exint idx);
190  bool ReadTexture(const UT_JSONValueMap &texture_json, const exint idx);
191  bool ReadSampler(const UT_JSONValueMap &sampler_json, const exint idx);
192  bool ReadImage(const UT_JSONValueMap &image_json, const exint idx);
193  bool ReadScene(const UT_JSONValueMap &scene_json, const exint idx);
194  bool ReadMaterial(const UT_JSONValueMap &material_json, const exint idx);
195 
196  // Utility: Takes an array of maps, and calls funcs() on every item
197  bool ReadArrayOfMaps(const UT_JSONValue *arr, bool required,
198  const UT_Function<bool(const UT_JSONValueMap &,
199  const exint idx)> func);
200 
201  UT_String myFilename;
202  UT_String myBasePath;
203 
204  bool myIsLoaded;
205 
206  // Retrieves the buffer at idx, potentially from cache if cached.
207  bool LoadBuffer(uint32 idx, unsigned char *&buffer_data) const;
208 
209  // Simply an indexed array of pointers to buffer data
210  UT_Array<GLTF_Accessor *> myAccesors;
211  UT_Array<GLTF_Animation *> myAnimations;
212  GLTF_Asset myAsset;
213  UT_Array<GLTF_Buffer *> myBuffers;
214  UT_Array<GLTF_BufferView *> myBufferViews;
215  UT_Array<GLTF_Camera *> myCameras;
216  UT_Array<GLTF_Extension *> myExtensions;
217  UT_Array<GLTF_Image *> myImages;
218  UT_Array<GLTF_Material *> myMaterials;
219  UT_Array<GLTF_Mesh *> myMeshes;
221  UT_Array<GLTF_Sampler *> mySamplers;
222  uint32 myScene = GLTF_INVALID_IDX;
223  UT_Array<GLTF_Scene *> myScenes;
224  UT_Array<GLTF_Skin *> mySkins;
225  UT_Array<GLTF_Texture *> myTextures;
226 
227  // Maps extension types to an index in the extension array
228  UT_Map<GLTF_ExtensionType, exint> myExtensionMap;
229  UT_ArrayStringSet myUsedExtensions;
230  UT_ArrayStringSet myRequiredExtensions;
231 
232  // Loading is transparent to the caller -- we want const
233  // semantics that
234  mutable UT_Lock myAccessorLock;
235  mutable UT_Array<UT_UniquePtr<uint8[]>> myBufferCache;
236 };
237 
238 //=================================================
239 
240 } // end GLTF_NAMESPACE
241 
242 #endif
#define GLTF_API
Definition: GLTF_API.h:37
GT_API const UT_StringHolder filename
UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects.
myNodes
Definition: UT_RTreeImpl.h:708
__hostdev__ SampleFromVoxels< TreeOrAccT, Order, UseCache > createSampler(const TreeOrAccT &acc)
Factory free-function for a sampler of specific polynomial orders.
UT_JSONValueArray stores a list of UT_JSONValue objects.
uint32 GLTF_Handle
Definition: GLTF_Types.h:49
int64 exint
Definition: SYS_Types.h:125
ReadAccessor< ValueT, LEVEL0, LEVEL1, LEVEL2 > createAccessor(const NanoGrid< ValueT > &grid)
Free-standing function for convenient creation of a ReadAccessor with optional and customizable node ...
Definition: NanoVDB.h:7474
#define GLTF_NAMESPACE
Definition: GLTF_API.h:42
std::function< T > UT_Function
Definition: UT_Function.h:37
#define GLTF_INVALID_IDX
Definition: GLTF_Types.h:45
GLenum func
Definition: glcorearb.h:783
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
unsigned int uint32
Definition: SYS_Types.h:40
Definition: format.h:895