File: gltf.hpp

package info (click to toggle)
glvis 4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,780 kB
  • sloc: cpp: 35,217; ansic: 5,695; sh: 340; makefile: 301; python: 193
file content (363 lines) | stat: -rw-r--r-- 12,263 bytes parent folder | download
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
// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
// LICENSE and NOTICE for details. LLNL-CODE-443271.
//
// This file is part of the GLVis visualization tool and library. For more
// information and source code availability see https://glvis.org.
//
// GLVis is free software; you can redistribute it and/or modify it under the
// terms of the BSD-3 license. We welcome feedback and contributions, see file
// CONTRIBUTING.md for details.

#ifndef GLVIS_GLTF_HPP
#define GLVIS_GLTF_HPP

#include <string>
#include <vector>
#include <array>
#include <fstream>
#include <limits>
#include <memory>


class glTF_Builder
{
public:
   typedef std::array<float,2> vec2f;
   typedef std::array<float,3> vec3f;
   typedef std::array<float,4> color4f;
   typedef std::vector<float> vecnf;

protected:
   template <typename T>
   struct node_type
   {
      bool valid;
      std::string key;
      T value;
   };

   typedef node_type<bool>        node_bool;
   typedef node_type<unsigned>    node_unsigned;
   typedef node_type<float>       node_float;
   typedef node_type<std::string> node_string;
   typedef node_type<vec3f>       node_vec3f;
   typedef node_type<color4f>     node_color4f;
   typedef node_type<vecnf>       node_vecnf;

   struct struct_buffer
   {
      node_string uri = { false, "uri", "" };
      node_unsigned byteLength = { false, "byteLength", 0 };

      std::unique_ptr<std::ofstream> file;
   };
   struct struct_buffer_view
   {
      node_unsigned buffer = { false, "buffer", 0 };
      node_unsigned byteOffset = { false, "byteOffset", 0 };
      node_unsigned byteLength = { false, "byteLength", 0 };
      node_unsigned byteStride = { false, "byteStride", 0};
      node_unsigned target = { false, "target", 0 };
   };
   struct struct_accessor
   {
      node_unsigned bufferView = { false, "bufferView", 0 };
      node_unsigned byteOffset = { false, "byteOffset", 0 };
      node_unsigned componentType = { false, "componentType", 0 };
      node_unsigned count = { false, "count", 0 };
      node_string type = { false, "type", "" };
      node_vecnf min = { false, "min", {} };
      node_vecnf max = { false, "max", {} };
      node_bool normalized = { false, "normalized", false };
   };
   struct struct_image
   {
      node_string uri = { false, "uri", "" };
      node_string name = { false, "name", "" };
   };
   struct struct_sampler
   {
      node_unsigned magFilter = { false, "magFilter", 0 };
      node_unsigned minFilter = { false, "minFilter", 0 };
      node_unsigned wrapS = { false, "wrapS", 0 };
      node_unsigned wrapT = { false, "wrapT", 0 };
   };
   struct struct_texture
   {
      node_unsigned sampler = { false, "sampler", 0 };
      node_unsigned source = { false, "source", 0 };
   };
   struct struct_texture_info
   {
      node_unsigned index = { false, "index", 0 };
      node_unsigned texCoord = { false, "texCoord", 0 };
   };
   struct struct_pbrMetallicRoughness
   {
      node_color4f baseColorFactor =
      { false, "baseColorFactor", {{0.f, 0.f, 0.f, 0.f}} };
      node_type<struct_texture_info> baseColorTexture =
      { false, "baseColorTexture", {} };
      node_float metallicFactor = { false, "metallicFactor", 0.f };
      node_float roughnessFactor = { false, "roughnessFactor", 0.f };
   };
   struct struct_material
   {
      node_type<struct_pbrMetallicRoughness> pbrMetallicRoughness =
      { false, "pbrMetallicRoughness", {} };
      node_bool doubleSided = { false, "doubleSided", false };
      node_string name = { false, "name", "" };
   };
   struct struct_attributes
   {
      node_unsigned POSITION = { false, "POSITION", 0 };
      node_unsigned NORMAL = { false, "NORMAL", 0 };
      node_unsigned TEXCOORD_0 = { false, "TEXCOORD_0", 0 };
      node_unsigned COLOR_0 = { false, "COLOR_0", 0 };

   };
   struct struct_primitive
   {
      node_type<struct_attributes> attributes = { false, "attributes", {} };
      node_unsigned indices = { false, "indices", 0 };
      node_unsigned material = { false, "material", 0 };
      node_unsigned mode = { false, "mode", 0 };
   };
   struct struct_mesh
   {
      std::vector<struct_primitive> primitives;
      node_string name = { false, "name", "" };
   };
   struct struct_node
   {
      node_unsigned mesh = { false, "mesh", 0 };
      node_vec3f scale = { false, "scale", {{0.f, 0.f, 0.f}} };
      node_vec3f translation = { false, "translation", {{0.f, 0.f, 0.f}} };
      node_string name = { false, "name", "" };
   };

   // begin: printing functions
   static const char *sep(size_t i) { return (i==0) ? "" : ","; }
   template <typename T>
   static void print_node(std::ostream &out,
                          int &pfx_counter,
                          const std::string &pfx,
                          const node_type<T> &n)
   {
      if (n.valid)
      {
         out << sep(pfx_counter++) << pfx << '"' << n.key << "\" : ";
         print(out, n.value);
      }
   }

   static void print(std::ostream &out, const bool &v) { out << v; }
   static void print(std::ostream &out, const unsigned &v) { out << v; }
   static void print(std::ostream &out, const float &v) { out << v; }
   static void print(std::ostream &out, const std::string &v)
   { out << '"' << v << '"'; }
   template <typename T, size_t s>
   static void print(std::ostream &out, const std::array<T,s> &v)
   {
      out << '[';
      for (size_t i = 0; i != s; ++i) { out << sep(i) << ' ' << v[i]; }
      out << " ]";
   }
   template <typename T>
   static void print(std::ostream &out, const std::vector<T> &v)
   {
      out << '[';
      for (size_t i = 0; i != v.size(); ++i) { out << sep(i) << ' ' << v[i]; }
      out << " ]";
   }
   static void print(std::ostream &out, const struct_attributes &a)
   {
      out << '{';
      int pos = 0;
      print_node(out, pos, "\n        ", a.POSITION);
      print_node(out, pos, "\n        ", a.NORMAL);
      print_node(out, pos, "\n        ", a.TEXCOORD_0);
      print_node(out, pos, "\n        ", a.COLOR_0);
      out << "\n      }";
   }
   static void print(std::ostream &out, const struct_texture_info &ti)
   {
      out << '{';
      int pos = 0;
      print_node(out, pos, "\n        ", ti.index);
      print_node(out, pos, "\n        ", ti.texCoord);
      out << "\n      }";
   }
   static void print(std::ostream &out, const struct_pbrMetallicRoughness &pbr)
   {
      out << '{';
      int pos = 0;
      print_node(out, pos, "\n      ", pbr.baseColorFactor);
      print_node(out, pos, "\n      ", pbr.baseColorTexture);
      print_node(out, pos, "\n      ", pbr.metallicFactor);
      print_node(out, pos, "\n      ", pbr.roughnessFactor);
      out << "\n    }";
   }
   // end: printing functions

   const std::string file_prefix;

   std::vector<struct_buffer> buffers;
   std::vector<struct_buffer_view> buffer_views;
   std::vector<struct_accessor> accessors;
   std::vector<struct_image> images;
   std::vector<struct_sampler> samplers;
   std::vector<struct_texture> textures;
   std::vector<struct_material> materials;
   std::vector<struct_mesh> meshes;
   std::vector<struct_node> nodes;

public:
   static constexpr unsigned INVALID_ID = std::numeric_limits<unsigned>::max();
   typedef struct { unsigned id; } buffer_id;
   typedef struct { unsigned id; } buffer_view_id;
   typedef struct { unsigned id; } accessor_id;
   typedef struct { unsigned id; } image_id;
   typedef struct { unsigned id; } sampler_id;
   typedef struct { unsigned id; } texture_id;
   typedef struct { unsigned id; } material_id;
   typedef struct { unsigned id; } mesh_id;
   typedef struct { unsigned id; } node_id;

   // buffer view option
   enum struct target_type
   {
      ARRAY_BUFFER = 34962,
      ELEMENT_ARRAY_BUFFER = 34963
   };

   // accessor option
   enum struct component_type
   {
      BYTE           = 5120,
      UNSIGNED_BYTE  = 5121,
      SHORT          = 5122,
      UNSIGNED_SHORT = 5123,
      UNSIGNED_INT   = 5125,
      FLOAT          = 5126
   };
   // accessor option
   enum struct tensor_type
   {
      SCALAR = 0, VEC2, VEC3, VEC4, MAT2, MAT3, MAT4
   };
   // string constants corresponding to the tensor_type constants
   static const char *tensorTypes[];

   // sampler option: magnification filter
   enum struct mag_filter { NEAREST = 9728, LINEAR = 9729 };
   // sampler option: minification filter
   enum struct min_filter
   {
      NEAREST = 9728, LINEAR = 9729, NEAREST_MIPMAP_NEAREST = 9984,
      LINEAR_MIPMAP_NEAREST = 9985, NEAREST_MIPMAP_LINEAR = 9986,
      LINEAR_MIPMAP_LINEAR = 9987
   };
   // sampler option: S/T (or U/V) wrapping mode
   enum struct wrap_type
   {
      CLAMP_TO_EDGE = 33071, MIRRORED_REPEAT = 33648, REPEAT = 10497
   };

   struct pbr_matallic_roughness
   {
      bool haveTexture;  // if true, baseColorTexture must be defined
      color4f baseColorFactor;
      texture_id baseColorTexture;
      float metallicFactor;
      float roughnessFactor;
   };


   glTF_Builder(const std::string &filePrefix)
      : file_prefix(filePrefix)
   { }

   buffer_id addBuffer(const std::string &bufferName);

   buffer_view_id addBufferView(buffer_id buffer,
                                const void *data,
                                size_t byteLength,
                                size_t byteStride,
                                size_t byteAlign,
                                target_type target);

   // can be called after the call to addBufferView() that created the given
   // bufferView but only before the next call to addBufferView()
   void appendToBufferView(buffer_view_id bufferView,
                           const void *data,
                           size_t byteLength);

   // count must be >= 1
   accessor_id addAccessor(buffer_view_id bufferView,
                           size_t byteOffset,
                           component_type componentType,
                           size_t count,
                           tensor_type tensorType);

   // count must be >= 1
   accessor_id addAccessorVec2f(buffer_view_id bufferView,
                                size_t byteOffset,
                                size_t count,
                                vec2f min,
                                vec2f max);

   // count must be >= 1
   accessor_id addAccessorVec3f(buffer_view_id bufferView,
                                size_t byteOffset,
                                size_t count,
                                vec3f min,
                                vec3f max);

   image_id addImage(const std::string &imageName,
                     int width,
                     int height,
                     const color4f *pixels);

   sampler_id addSampler(mag_filter magFilter = mag_filter::NEAREST,
                         min_filter minFilter = min_filter::NEAREST,
                         wrap_type wrapS = wrap_type::CLAMP_TO_EDGE,
                         wrap_type wrapT = wrap_type::CLAMP_TO_EDGE);

   texture_id addTexture(sampler_id sampler, image_id source);

   material_id addMaterial(const std::string &materialName,
                           const pbr_matallic_roughness &pbrMetallicRoughness,
                           bool doubleSided = false);

   mesh_id addMesh(const std::string &meshName);

   void addMeshTriangles(mesh_id mesh,
                         accessor_id vertexPositions,
                         accessor_id vertexNormals,
                         accessor_id vertexTexCoords0,
                         accessor_id vertexIndices,
                         material_id material);

   void addMeshLines(mesh_id mesh,
                     accessor_id vertexPositions,
                     accessor_id vertexTexcoords0,
                     accessor_id vertexColors0,
                     material_id material);

   node_id addNode(const std::string &nodeName);

   void addNodeMesh(node_id node, mesh_id mesh);

   void addNodeScale(node_id node, vec3f scale);

   void addNodeTranslation(node_id node, vec3f translation);

   void getMaterialPBRMR(material_id material,
                         pbr_matallic_roughness &pbr_mr_copy);

   int writeFile();
};

#endif // GLVIS_GLTF_HPP