The small3d library
Tiny C++ 3D game development library for Win/MacOS/Linux/FreeBSD
Loading...
Searching...
No Matches
GlbFile.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include <fstream>
13#include <string>
14#include <stack>
15#include <vector>
16#include <memory>
17#include <cstring>
18#include "Math.hpp"
19#include "Model.hpp"
20#include "File.hpp"
21
22namespace small3d {
23
30 class GlbFile : public File {
31
32 private:
33
34 enum class ValueType { number = 0, charstring, character, MARKER };
35
36 struct Token {
37 ValueType valueType = ValueType::charstring;
38 std::string value;
39 std::shared_ptr<Token> next;
40 std::string name;
41 };
42
43 struct Node {
44 uint32_t index = 0;
45 std::string name = "";
46 Mat4 transformation = Mat4(1.0f);
47 small3d::Quat rotation = { 0.0f, 0.0f, 0.0f, 1.0f };
48 Vec3 scale = Vec3(1.0f, 1.0f, 1.0f);
49 Vec3 translation = Vec3(0.0f, 0.0f, 0.0f);
50 uint32_t skin = 0;
51 bool noSkin = false;
52 uint32_t mesh = 0;
53 std::vector<uint32_t> children;
54 };
55
56 struct Skin {
57 std::string name;
58 uint32_t inverseBindMatrices = 0;
59 std::vector<uint32_t> joints;
60 uint32_t skeleton = 0;
61 bool foundSkeleton = false;
62 };
63
64 struct AnimationSampler {
65 uint32_t input = 0;
66 std::string interpolation;
67 uint32_t output = 0;
68 };
69
70 struct ChannelTarget {
71 uint32_t node = 0;
72 std::string path;
73 };
74
75 struct AnimationChannel {
76 uint32_t sampler = 0;
77 ChannelTarget target;
78 };
79
80 struct Animation {
81 std::string name;
82 std::vector<AnimationChannel> channels;
83 std::vector<AnimationSampler> samplers;
84 };
85
86 const uint32_t CHUNK_TYPE_JSON = 0x4E4F534A;
87 const uint32_t CHUNK_TYPE_BIN = 0x004E4942;
88
89 std::shared_ptr<Token> jsonRootToken;
90 std::vector<char> binBuffer;
91
92 // The result of parsing json is a series of token queues,
93 // each queue being one of the found maps or lists. Some
94 // of the nodes in the queues are references to other
95 // queues.
96 std::vector<std::shared_ptr<Token>> token_queues;
97
98 std::shared_ptr<Token> getToken(const std::string&, size_t);
99 void printTokensRecursive(std::shared_ptr<Token>);
100 void lexJson(const std::vector<char>&, uint32_t);
101 std::shared_ptr<Token> createToken(ValueType, const std::string&);
102 void parseJson(std::shared_ptr<Token>);
103 std::vector<std::shared_ptr<Token>> getTokens(uint32_t);
104
105 void printToken(const std::shared_ptr<Token>& token);
106
107 std::shared_ptr<Token> getToken(const std::string& name);
108
109 std::vector<std::shared_ptr<Token>> getChildTokens(const std::shared_ptr<Token>& token);
110
111 std::shared_ptr<Token> getChildToken(const std::shared_ptr<GlbFile::Token>& token, const std::string& name);
112
113 std::vector<char> getBufferByView(const size_t index);
114
115 std::vector<char> getBufferByAccessor(const size_t index, int& componentType);
116
117 std::vector<char> getBufferByAccessor(const size_t index);
118
119 bool existNode(const uint32_t index);
120
121 Node getNode(const uint32_t index);
122
123 bool existNode(const std::string& name);
124
125 Node getNode(const std::string& name);
126
127 bool existParentNode(const uint32_t index);
128
129 Node getParentNode(const uint32_t index);
130
131 bool existNodeForMesh(const uint32_t meshIndex);
132
133 Node getNodeForMesh(const uint32_t meshIndex);
134
135 bool existSkin(const uint32_t index);
136
137 Skin getSkin(const uint32_t index);
138
139 bool existSkin(const std::string& name);
140
141 Skin getSkin(const std::string& name);
142
143 bool existAnimation(const uint32_t index);
144
145 Animation getAnimation(const uint32_t index);
146
147 Animation getAnimation(const std::string& name);
148
149 GlbFile(); // No default constructor
150
151 // Forbid moving and copying
152 GlbFile(GlbFile const&) = delete;
153 void operator=(GlbFile const&) = delete;
154 GlbFile(GlbFile&&) = delete;
155 void operator=(GlbFile&&) = delete;
156
157 public:
158
163 explicit GlbFile(const std::string& fileLocation);
164
170
175 void printTokensSerial();
176
183 void load(Model& model, const std::string& meshName = "") override;
184
190 std::vector<std::string> getMeshNames() override;
191
192 private:
193
194 // Add an animation to an animation vector
195 void addAnimation(std::vector<Model::Animation>& animations, uint32_t animationIdx, const Animation& animation,
196 const AnimationChannel& channel, Model& model);
197
198
199 };
200
201}
File parser virtual class.
Vectors, matrices and other math things.
A 3D model class.
Abstract file parser class.
Definition File.hpp:23
.glb (glTF) file parser class. It can load meshes, textures and linear animations from samplers with ...
Definition GlbFile.hpp:30
void load(Model &model, const std::string &meshName="") override
Load a mesh from the file into a Model.
Definition GlbFile.cpp:764
void printTokensRecursive()
Recursively print all tokens, starting from the head of the first token queue, dereferencing queue re...
Definition GlbFile.cpp:322
std::vector< std::string > getMeshNames() override
Get a list of the names of the meshes contained in the file.
Definition GlbFile.cpp:1028
void printTokensSerial()
Print tokens as found in the token queues, without dereferencing queue references.
Definition GlbFile.cpp:326
A 3D model. It can be loaded from a WavefrontFile or GlbFile. It can also be constructed procedurally...
Definition Model.hpp:34
Definition BinaryFile.hpp:15
4x4 float matrix
Definition Math.hpp:112
Quaternion.
Definition Math.hpp:141
3 component float vector
Definition Math.hpp:21