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