HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GLTF_SceneData.h
Go to the documentation of this file.
1 #ifndef __GLTF_SceneData_h__
2 #define __GLTF_SceneData_h__
3 
4 #include "GLTFZ_API.h"
5 #include "GLTF_Types.h"
6 
7 #include <GA/GA_Types.h>
8 #include <UT/UT_Array.h>
9 #include <UT/UT_Map.h>
10 #include <UT/UT_Matrix4.h>
11 #include <UT/UT_Quaternion.h>
12 #include <UT/UT_RingBuffer.h>
13 #include <UT/UT_Vector3.h>
14 
16 {
17 public:
18  struct Node
19  {
21  bool myIsRoot;
23 
25 
27 
30 
33 
36 
37  bool myHasScale;
39 
40  // Expects a pointer to 16 floats (4x4 matrix)
41  void setMatrix(const float* mat);
42 
43  // Expects a pointer to 3 floats
44  void setTranslate(const float* trans);
45 
46  // Expects a pointer to 4 floats (quaternion)
47  void setRotation(const float* rot);
48 
49  // Expects a pointer to 3 floats
50  void setScale(const float* scale);
51  };
52 
53 public:
54  void setScene(const GLTF_Index id) { mySceneId = id; }
55  void addRoot(const GLTF_Index id);
56  void addChildNode(const GLTF_Index parent_id, const GLTF_Index id);
57  Node& getNode(GLTF_Index id) { return myNodes[id]; }
58  const Node& getNode(GLTF_Index id) const { return myNodes.at(id); }
59 
60  void clear()
61  {
62  myRootNodes.clear();
63  myNodes.clear();
64  }
65 
66 private:
67  GLTF_Index mySceneId;
68  UT_Array<GLTF_Index> myRootNodes;
70 
71 public:
72 
74  {
75  public:
77  : myScene(scene)
78  , myCurrentVisitingNode(0)
79  {
80  if (!myScene)
81  return;
82 
83  if (myScene->myRootNodes.entries() == 0)
84  {
85  myScene = nullptr;
86  return;
87  }
88 
89  myCurrentVisitingNode = myScene->myRootNodes[0];
90  initQueue();
91  }
92 
93  const Node& operator*() const
94  {
95  return myScene->myNodes.at(myCurrentVisitingNode);
96  }
97 
98  const Node* operator->() const
99  {
100  return &(**this);
101  }
102 
104  {
105  advance();
106  return *this;
107  }
108 
109  bool operator==(const ConstIterator& other) const
110  {
111  return myScene == other.myScene &&
112  myCurrentVisitingNode == other.myCurrentVisitingNode;
113  }
114 
115  bool operator!=(const ConstIterator& other) const
116  {
117  return !(*this == other);
118  }
119 
120  private:
121  void initQueue()
122  {
123  const GLTF_SceneData::Node& root =
124  myScene->myNodes.at(myCurrentVisitingNode);
125  for (exint i = 0; i < root.myChildren.entries(); ++i)
126  prepareSceneGraphDFS(root.myChildren[i]);
127 
128  for (exint i = 1; i < myScene->myRootNodes.entries(); ++i)
129  prepareSceneGraphDFS(myScene->myRootNodes[i]);
130  }
131 
132  void prepareSceneGraphDFS(const GLTF_Index node_id)
133  {
134  myVisitOrder.push(node_id);
135  const GLTF_SceneData::Node& node =
136  myScene->myNodes.at(node_id);
137  for (exint i = 0; i < node.myChildren.entries(); ++i)
138  prepareSceneGraphDFS(node.myChildren[i]);
139  }
140 
141  void advance()
142  {
143  // If no scene, that means that we are the end() iterator
144  if (!myScene)
145  return;
146 
147  if (myVisitOrder.size() > 0)
148  {
149  myCurrentVisitingNode = myVisitOrder.popFirst();
150  }
151  else
152  {
153  myScene = nullptr;
154  myCurrentVisitingNode = 0;
155  myVisitOrder.clear();
156  }
157  }
158 
159  const GLTF_SceneData* myScene;
160  GLTF_Index myCurrentVisitingNode;
161  UT_RingBuffer<GLTF_Index> myVisitOrder;
162  };
163 
164  friend class ConstIterator;
165 
166  ConstIterator begin() const { return ConstIterator(this); }
167  ConstIterator end() const { return ConstIterator(nullptr); }
168 };
169 
170 #endif // __GLTF_SceneData_h__
myNodes
Definition: UT_RTreeImpl.h:708
ConstIterator(const GLTF_SceneData *scene)
const Node & operator*() const
Unsorted map container.
Definition: UT_Map.h:114
GA_API const UT_StringHolder rot
ConstIterator end() const
int64 exint
Definition: SYS_Types.h:125
bool operator==(const ConstIterator &other) const
GA_Size GA_Offset
Definition: GA_Types.h:653
GA_API const UT_StringHolder scale
bool operator!=(const ConstIterator &other) const
UT_Quaternion myRotation
GA_API const UT_StringHolder trans
ConstIterator begin() const
GLuint id
Definition: glcorearb.h:655
const Node & getNode(GLTF_Index id) const
std::size_t GLTF_Index
Definition: GLTF_Types.h:13
const Node * operator->() const
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:669
UT_Array< GLTF_Index > myChildren
Node & getNode(GLTF_Index id)
#define GLTFZ_API
Definition: GLTFZ_API.h:37
void setScene(const GLTF_Index id)
UT_Vector3F myTranslate