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