-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgltf2cpp.hpp
More file actions
663 lines (589 loc) · 15 KB
/
gltf2cpp.hpp
File metadata and controls
663 lines (589 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#pragma once
#include <djson/json.hpp>
#include <gltf2cpp/build_version.hpp>
#include <gltf2cpp/dyn_array.hpp>
#include <gltf2cpp/version.hpp>
#include <array>
#include <cstring>
#include <functional>
#include <optional>
#include <unordered_map>
#include <variant>
#include <vector>
namespace gltf2cpp {
///
/// \brief Alias for an index for a particular type.
///
template <typename Type>
using Index = std::size_t;
///
/// \brief Geometric vector modelled as an array of Types.
///
template <typename Type, std::size_t Dim>
using TVec = std::array<Type, Dim>;
template <std::size_t Dim>
using Vec = TVec<float, Dim>;
template <std::size_t Dim>
using UVec = TVec<std::uint32_t, Dim>;
///
/// \brief 4x4 float matrix.
///
using Mat4x4 = std::array<Vec<4>, 4>;
///
/// \brief Alias for callable that returns bytes given a URI.
///
using GetBytes = std::function<std::span<std::byte const>(std::string_view)>;
///
/// \brief GLTF Material Alpha Mode.
///
enum class AlphaMode : std::uint32_t {
eOpaque = 0,
eBlend,
eMask,
};
///
/// \brief GLTF Buffer Target.
///
enum class BufferTarget : std::uint32_t {
eArrayBuffer = 34962,
eElementArrayBuffer = 34693,
};
///
/// \brief GLTF Accessor Component Type.
///
enum class ComponentType : std::uint32_t {
eByte = 5120,
eUnsignedByte = 5121,
eShort = 5122,
eUnsignedShort = 5123,
eUnsignedInt = 5125,
eFloat = 5126,
};
///
/// \brief GLTF Sampler Filter.
///
enum class Filter : std::uint32_t {
eNearest = 9728,
eLinear = 9729,
eNearestMipmapNearest = 9984,
eLinearMipmapNearest = 9985,
eNearestMipmapLinear = 9986,
eLinearMipmapLinear = 9987,
};
///
/// \brief GLTF Animation Interpolation.
///
enum class Interpolation : std::uint32_t {
eLinear,
eStep,
eCubicSpline,
};
///
/// \brief GLTF Primitive Mode.
///
enum class PrimitiveMode : std::uint32_t {
ePoints = 0,
eLines = 1,
eLineLoop = 2,
eLineStrip = 3,
eTriangles = 4,
eTriangleStrip = 5,
eTriangleFan = 6,
};
///
/// \brief GLTF Sampler Wrap.
///
enum class Wrap : std::uint32_t {
eClampEdge = 33071,
eMirrorRepeat = 33648,
eRepeat = 10497,
};
struct Buffer {
ByteArray bytes{};
};
struct BufferView {
Index<Buffer> buffer{};
std::size_t offset{};
std::size_t length{};
BufferTarget target{};
std::optional<std::size_t> stride{};
std::span<std::byte const> to_span(std::span<Buffer const> buffers) const;
};
///
/// \brief Convert ComponentType to its corresponding C++ type.
///
template <ComponentType C>
constexpr auto from_component_type() {
if constexpr (C == ComponentType::eByte) {
return std::int8_t{};
} else if constexpr (C == ComponentType::eUnsignedByte) {
return std::uint8_t{};
} else if constexpr (C == ComponentType::eShort) {
return std::int16_t{};
} else if constexpr (C == ComponentType::eUnsignedShort) {
return std::uint16_t{};
} else if constexpr (C == ComponentType::eUnsignedInt) {
return std::uint32_t{};
} else {
return float{};
}
}
///
/// \brief Alias to convert ComponentType to its corresponding C++ type.
///
template <ComponentType C>
using FromComponentType = decltype(from_component_type<C>());
///
/// \brief GLTF Transform encoded as Translation, Rotation, Scale.
///
struct Trs {
Vec<3> translation{};
Vec<4> rotation{{1.0f, 0.0f, 0.0f, 0.0f}};
Vec<3> scale{Vec<3>{{1.0f, 1.0f, 1.0f}}};
};
///
/// \brief GLTF Transform encoded as Trs or a 4x4 matrix.
///
using Transform = std::variant<Trs, Mat4x4>;
///
/// \brief GLTF Accessor.
///
/// gltf2cpp does not expose raw GLTF Buffers or BufferViews; instead each Accessor's data
/// is pre-interpreted and stored directly within it as a flat array of ComponentType.
/// Eg, Data for Type::eVec4 / ComponentType::Float will contain 4x float components
/// for each element (and not a Vec<4> for each element).
///
struct Accessor {
template <ComponentType C>
using ComponentArray = DynArray<FromComponentType<C>>;
using UnsignedByte = ComponentArray<ComponentType::eUnsignedByte>;
using Byte = ComponentArray<ComponentType::eByte>;
using Short = ComponentArray<ComponentType::eShort>;
using UnsignedShort = ComponentArray<ComponentType::eUnsignedShort>;
using UnsignedInt = ComponentArray<ComponentType::eUnsignedInt>;
using Float = ComponentArray<ComponentType::eFloat>;
using Data = std::variant<UnsignedByte, Byte, Short, UnsignedShort, UnsignedInt, Float>;
enum class Type { eScalar, eVec2, eVec3, eVec4, eMat2, eMat3, eMat4, eCOUNT_ };
std::string name{};
std::optional<Index<BufferView>> buffer_view{};
std::size_t byte_offset{};
Data data{};
ComponentType component_type{};
Type type{};
std::size_t count{};
bool normalized{};
dj::Json extensions{};
dj::Json extras{};
///
/// \brief Obtain the width multiplier for type.
/// \param type Type to get width multiplier for
/// \returns Width multiplier for type
///
/// Eg: Type::eVec2 = 2, Type::eMat3 = 9
///
static constexpr std::size_t type_coeff(Accessor::Type type);
///
/// \brief Convert a GLTF type semantic to its corresponding Type.
/// \param key Semantic key to convert to Type
/// \returns Corresponding Type for key
///
static Type to_type(std::string_view key);
///
/// \brief Obtain data as a vector of u32.
/// \returns Data as std::vector of u32
///
/// component_type must be unsigned.
///
std::vector<std::uint32_t> to_u32() const;
///
/// \brief Obtain data as a vector of Vec<Dim>.
/// \returns Data as std::vector of Vec<Dim>
///
/// type must be Type::eVecI (I == Dim), and component_type must be ComponentType::eFloat.
///
template <std::size_t Dim>
std::vector<Vec<Dim>> to_vec() const;
///
/// \brief Obtain data as a vector of Mat4x4
/// \returns Data as std::vefctor of Mat4x4
///
/// type must be Type::eMat4, and component_type must be ComponentType::eFloat.
///
std::vector<Mat4x4> to_mat4() const;
};
struct Node;
///
/// \brief GLTF Animation.
///
struct Animation {
///
/// \brief GLTF Animation Path.
///
enum class Path { eTranslation, eRotation, eScale, eWeights };
///
/// \brief GLTF Animation Sampler.
///
struct Sampler {
std::span<float const> input{};
Interpolation interpolation{};
Index<Accessor> output{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Animation Target.
///
struct Target {
std::optional<Index<Node>> node{};
Path path{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Animation Channel.
///
struct Channel {
Index<Sampler> sampler{};
Target target{};
dj::Json extensions{};
dj::Json extras{};
};
std::string name{};
std::vector<Channel> channels{};
std::vector<Sampler> samplers{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Asset.
///
struct Asset {
std::string copyright{};
std::string generator{};
Version version{};
Version min_version{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Camera.
///
struct Camera {
///
/// \brief GLTF Perspective Camera.
///
struct Perspective {
float yfov{};
float znear{};
float aspect_ratio{};
std::optional<float> zfar{};
};
///
/// \brief GLTF Orthographic Camera.
///
struct Orthographic {
float xmag{};
float ymag{};
float zfar{};
float znear{};
};
std::string name{};
std::variant<Perspective, Orthographic> payload{Perspective{}};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Image.
///
struct Image {
ByteArray bytes{};
std::string name{};
std::string source_filename{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Mesh Primitive Attributes.
///
using AttributeMap = std::unordered_map<std::string, Index<Accessor>>;
///
/// \brief GLTF Mesh Primitive Geometry.
///
/// Geometry represents all the Attributes in a Mesh Primitive.
/// Positions, normals, tangents, tex_coords, colors, and indices are pre-parsed for convenience.
///
/// tex_coords and colors are nested vectors, where the Ith element corresponds to SEMANTIC_I,
/// eg. tex_coords[2] is populated from the TEXCOORD_2 Attribute's Accessor.
///
/// These vectors (except indices and joints) will only be populated if the corresponding
/// Accessor's ComponentType is eFloat.
/// For other component types, obtain and use the Accessor directly.
/// Since the POSITION Attribute is required to be in Accessor::Type::eVec3 / ComponentType::eFloat
/// format by the GLTF spec, .positions is always expected to be populated.
/// normals, tangents, tex_coords, and colors will either be empty or the same size as positions.
/// joints and weights will have the same size.
///
/// Note: weights is only parsed if the ComponentType is eFloat. Otherwise the corresponding
/// array will be empty.
///
struct Geometry {
AttributeMap attributes{};
std::vector<Vec<3>> positions{};
std::vector<Vec<3>> normals{};
std::vector<Vec<4>> tangents{};
std::vector<std::vector<Vec<2>>> tex_coords{};
std::vector<std::vector<Vec<3>>> colors{};
std::vector<std::uint32_t> indices{};
std::vector<std::vector<UVec<4>>> joints{};
std::vector<std::vector<Vec<4>>> weights{};
};
struct Texture;
///
/// \brief GLTF Material Texture Info.
///
struct TextureInfo {
Index<Texture> texture{};
Index<Accessor> tex_coord{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Material Normal Texture Info.
///
struct NormalTextureInfo {
TextureInfo info{};
float scale{1.0f};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Material Occlusion Texture Info.
///
struct OcclusionTextureInfo {
TextureInfo info{};
float strength{1.0f};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Material PBR Metallic Roughness.
///
struct PbrMetallicRoughness {
std::optional<TextureInfo> base_color_texture{};
std::optional<TextureInfo> metallic_roughness_texture{};
Vec<4> base_color_factor{1.0f, 1.0f, 1.0f, 1.0f};
float metallic_factor{1.0f};
float roughness_factor{1.0f};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Material.
///
struct Material {
std::string name{};
PbrMetallicRoughness pbr{};
std::optional<NormalTextureInfo> normal_texture{};
std::optional<OcclusionTextureInfo> occlusion_texture{};
std::optional<TextureInfo> emissive_texture{};
Vec<3> emissive_factor{};
AlphaMode alpha_mode{AlphaMode::eOpaque};
float alpha_cutoff{0.5f};
bool double_sided{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Morph Target.
///
struct MorphTarget {
AttributeMap attributes{};
std::vector<Vec<3>> positions{};
std::vector<Vec<3>> normals{};
std::vector<Vec<4>> tangents{};
std::vector<std::vector<Vec<2>>> tex_coords{};
std::vector<std::vector<Vec<3>>> colors{};
};
///
/// \brief GLTF Mesh and its primitives.
///
struct Mesh {
///
/// \brief GLTF Mesh Primitive.
///
struct Primitive {
Geometry geometry{};
std::optional<Index<Accessor>> indices{};
std::optional<Index<Material>> material{};
std::vector<MorphTarget> targets{};
PrimitiveMode mode{PrimitiveMode::eTriangles};
dj::Json extensions{};
dj::Json extras{};
};
std::string name{};
std::vector<Primitive> primitives{};
std::vector<float> weights{};
dj::Json extensions{};
dj::Json extras{};
};
struct Skin;
///
/// \brief GLTF Scene Node.
///
struct Node {
std::string name{};
Transform transform{};
Index<Node> self{};
std::vector<Index<Node>> children{};
std::optional<Index<Node>> parent{};
std::optional<Index<Camera>> camera{};
std::optional<Index<Mesh>> mesh{};
std::optional<Index<Skin>> skin{};
std::vector<float> weights{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Texture Sampler.
///
struct Sampler {
std::string name{};
std::optional<Filter> min_filter{};
std::optional<Filter> mag_filter{};
Wrap wrap_s{Wrap::eRepeat};
Wrap wrap_t{Wrap::eRepeat};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Skin.
///
struct Skin {
std::string name{};
std::vector<Mat4x4> inverse_bind_matrices{};
std::optional<Index<Node>> skeleton{};
std::vector<Index<Node>> joints{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Texture.
///
struct Texture {
std::string name{};
std::optional<Index<Sampler>> sampler{};
Index<Image> source{};
bool linear{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief GLTF Scene.
///
struct Scene {
std::string name{};
std::vector<Index<Node>> root_nodes{};
dj::Json extensions{};
dj::Json extras{};
};
///
/// \brief Metadata for a GLTF asset.
///
struct Metadata {
std::size_t images{};
std::size_t textures{};
std::size_t primitives{};
std::size_t animations{};
std::vector<std::string> extensions_used{};
std::vector<std::string> extensions_required{};
};
///
/// \brief GLTF root.
///
/// Contains all the data parsed from a GLTF file (and resources it points to).
///
struct Root {
std::vector<Buffer> buffers{};
std::vector<BufferView> buffer_views{};
std::vector<Accessor> accessors{};
std::vector<Animation> animations{};
std::vector<Camera> cameras{};
std::vector<Image> images{};
std::vector<Material> materials{};
std::vector<Mesh> meshes{};
std::vector<Node> nodes{};
std::vector<Sampler> samplers{};
std::vector<Skin> skins{};
std::vector<Texture> textures{};
std::vector<Scene> scenes{};
std::optional<Index<Scene>> start_scene{};
std::vector<std::string> extensions_used{};
std::vector<std::string> extensions_required{};
dj::Json extensions{};
dj::Json extras{};
Asset asset{};
///
/// \brief Check if this instance represents a parsed GLTF asset.
/// \returns true if asset.version has been set
///
/// This is based on the fact that gltf.asset and gltf.asset.version are required fields.
///
explicit operator bool() const { return asset.version > Version{}; }
};
///
/// \brief Parser to obtain Metadata / Root given a Json and GetBytes.
///
struct Parser {
///
/// \brief Json to parse.
///
dj::Json const& json;
///
/// \brief Obtain Metadata.
/// \returns Metadata for json
///
Metadata metadata() const;
///
/// \brief Parse GLTF data.
/// \param get_bytes Callable to load bytes given a URI (relative to the input JSON)
/// \returns Parsed GLTF Root
///
Root parse(GetBytes const& get_bytes) const;
};
///
/// \brief Parse json as GLTF.
/// \param json_path path to .gltf JSON
/// \returns Parsed GLTF Root
///
Root parse(char const* json_path);
// impl
namespace detail {
std::string print_error(char const* msg);
void expect(bool pred, char const* expr) noexcept(false);
} // namespace detail
#define GLTF2CPP_EXPECT(expr) detail::expect(!!(expr), #expr)
constexpr std::size_t Accessor::type_coeff(Accessor::Type type) {
switch (type) {
default:
case Accessor::Type::eScalar: return 1u;
case Accessor::Type::eVec2: return 2u;
case Accessor::Type::eVec3: return 3u;
case Accessor::Type::eVec4: return 4u;
case Accessor::Type::eMat2: return 2u * 2u;
case Accessor::Type::eMat3: return 3u * 3u;
case Accessor::Type::eMat4: return 4u * 4u;
}
}
template <std::size_t Dim>
std::vector<Vec<Dim>> Accessor::to_vec() const {
GLTF2CPP_EXPECT(type_coeff(type) == Dim);
GLTF2CPP_EXPECT(std::holds_alternative<Float>(data));
auto ret = std::vector<Vec<Dim>>{};
auto const& d = std::get<Float>(data);
GLTF2CPP_EXPECT(d.size() % Dim == 0);
ret.resize(d.size() / Dim);
std::memcpy(ret.data(), d.data(), d.span().size_bytes());
return ret;
}
#undef GLTF2CPP_EXPECT
} // namespace gltf2cpp