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