Skip to content

Commit 088d161

Browse files
committed
Add: Enums and utility functions
1 parent 1d346b7 commit 088d161

File tree

5 files changed

+172
-33
lines changed

5 files changed

+172
-33
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_library(fastgltf
55
add_library(fastgltf::fastgltf ALIAS fastgltf)
66
target_include_directories(fastgltf PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}> $<INSTALL_INTERFACE:include>)
77
target_compile_features(fastgltf PUBLIC cxx_std_17)
8+
set_property(TARGET fastgltf PROPERTY C_STANDARD 90) #C89/90
89
compiler_flags(TARGET fastgltf)
910
enable_debug_inlining(TARGET fastgltf_simdjson)
1011

@@ -23,7 +24,7 @@ endif()
2324
target_compile_definitions(fastgltf PRIVATE "FASTGLTF_USE_CUSTOM_SMALLVECTOR=$<BOOL:${FASTGLTF_USE_CUSTOM_SMALLVECTOR}>")
2425

2526
install(
26-
FILES "base64_decode.hpp" "fastgltf_parser.hpp" "fastgltf_types.hpp" "fastgltf_util.hpp"
27+
FILES "base64_decode.hpp" "fastgltf_parser.hpp" "fastgltf_types.hpp" "fastgltf_util.hpp" "fastgltf_c.h"
2728
TYPE INCLUDE
2829
)
2930

src/fastgltf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,7 @@ std::unique_ptr<fg::glTF> fg::Parser::loadGLTF(GltfDataBuffer* buffer, fs::path
21472147
data->decodeCallback = decodeCallback;
21482148
data->userPointer = userPointer;
21492149

2150-
return std::unique_ptr<glTF>(new glTF(std::move(data), std::move(directory), options, extensions));
2150+
return std::unique_ptr<glTF>(new (std::nothrow) glTF(std::move(data), std::move(directory), options, extensions));
21512151
}
21522152

21532153
std::unique_ptr<fg::glTF> fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs::path directory, Options options) {
@@ -2203,7 +2203,7 @@ std::unique_ptr<fg::glTF> fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs:
22032203
data->decodeCallback = decodeCallback;
22042204
data->userPointer = userPointer;
22052205

2206-
auto gltf = std::unique_ptr<glTF>(new glTF(std::move(data), std::move(directory), options, extensions));
2206+
auto gltf = std::unique_ptr<glTF>(new (std::nothrow) glTF(std::move(data), std::move(directory), options, extensions));
22072207

22082208
// Is there enough room for another chunk header?
22092209
if (header.length > (offset + sizeof(BinaryGltfChunk))) {

src/fastgltf_c.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
#include <fastgltf_parser.hpp>
44
#include <fastgltf_types.hpp>
55

6+
fastgltf_component_type fastgltf_get_component_type(unsigned int componentType) {
7+
return static_cast<fastgltf_component_type>(fastgltf::getComponentType(
8+
static_cast<std::underlying_type_t<fastgltf::ComponentType>>(componentType)));
9+
}
10+
11+
fastgltf_accessor_type fastgltf_get_accessor_type(const char* string) {
12+
return static_cast<fastgltf_accessor_type>(fastgltf::getAccessorType(std::string_view { string }));
13+
}
14+
615
fastgltf_parser* fastgltf_create_parser(fastgltf_extensions extensions) {
716
return reinterpret_cast<fastgltf_parser*>(
8-
new fastgltf::Parser(static_cast<fastgltf::Extensions>(extensions)));
17+
new (std::nothrow) fastgltf::Parser(static_cast<fastgltf::Extensions>(extensions)));
918
}
1019

1120
void fastgltf_destroy_parser(fastgltf_parser* parser) {
@@ -50,7 +59,8 @@ void fastgltf_destroy_gltf(fastgltf_gltf* gltf) {
5059
}
5160

5261
fastgltf_error fastgltf_parse(fastgltf_gltf* gltf, fastgltf_category categories) {
53-
return static_cast<fastgltf_error>(reinterpret_cast<fastgltf::glTF*>(gltf)->parse(static_cast<fastgltf::Category>(categories)));
62+
return static_cast<fastgltf_error>(
63+
reinterpret_cast<fastgltf::glTF*>(gltf)->parse(static_cast<fastgltf::Category>(categories)));
5464
}
5565

5666
fastgltf_asset* fastgltf_get_parsed_asset(fastgltf_gltf* gltf) {

src/fastgltf_c.h

Lines changed: 145 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#ifndef FASTGLTF_C_H
2+
#define FASTGLTF_C_H
3+
4+
#ifdef __cplusplus
5+
#include <cstddef>
6+
#else
7+
#include <stddef.h>
8+
#endif
9+
110
enum fastgltf_extensions {
211
KHR_texture_transform = 1 << 1,
312
KHR_texture_basisu = 1 << 2,
@@ -32,24 +41,140 @@ enum fastgltf_error {
3241
enum fastgltf_category {
3342
CategoryNone = 0,
3443

35-
CategoryBuffers = 1 << 0,
44+
CategoryBuffers = 1 << 0,
3645
CategoryBufferViews = 1 << 1 | CategoryBuffers,
37-
CategoryAccessors = 1 << 2 | CategoryBufferViews,
38-
CategoryImages = 1 << 3 | CategoryBufferViews,
39-
CategorySamplers = 1 << 4,
40-
CategoryTextures = 1 << 5 | CategoryImages | CategorySamplers,
41-
CategoryAnimations = 1 << 6 | CategoryAccessors,
42-
CategoryCameras = 1 << 7,
43-
CategoryMaterials = 1 << 8 | CategoryTextures,
44-
CategoryMeshes = 1 << 9 | CategoryAccessors | CategoryMaterials,
45-
CategorySkins = 1 << 10 | CategoryAccessors | (1 << 11),
46-
CategoryNodes = 1 << 11 | CategoryCameras | CategoryMeshes | CategorySkins,
47-
CategoryScenes = 1 << 12 | CategoryNodes,
48-
CategoryAsset = 1 << 13,
49-
50-
CategoryAll = CategoryAsset | CategoryScenes | CategoryAnimations,
46+
CategoryAccessors = 1 << 2 | CategoryBufferViews,
47+
CategoryImages = 1 << 3 | CategoryBufferViews,
48+
CategorySamplers = 1 << 4,
49+
CategoryTextures = 1 << 5 | CategoryImages | CategorySamplers,
50+
CategoryAnimations = 1 << 6 | CategoryAccessors,
51+
CategoryCameras = 1 << 7,
52+
CategoryMaterials = 1 << 8 | CategoryTextures,
53+
CategoryMeshes = 1 << 9 | CategoryAccessors | CategoryMaterials,
54+
CategorySkins = 1 << 10 | CategoryAccessors | (1 << 11),
55+
CategoryNodes = 1 << 11 | CategoryCameras | CategoryMeshes | CategorySkins,
56+
CategoryScenes = 1 << 12 | CategoryNodes,
57+
CategoryAsset = 1 << 13,
58+
59+
CategoryAll = CategoryAsset | CategoryScenes | CategoryAnimations,
60+
};
61+
62+
enum fastgltf_primitive_type {
63+
PrimitiveTypePoints = 0,
64+
PrimitiveTypeLines = 1,
65+
PrimitiveTypeLineLoop = 2,
66+
PrimitiveTypeLineStrip = 3,
67+
PrimitiveTypeTriangles = 4,
68+
PrimitiveTypeTriangleStrip = 5,
69+
PrimitiveTypeTriangleFan = 6,
70+
};
71+
72+
enum fastgltf_accessor_type {
73+
AccessorTypeInvalid = 0,
74+
AccessorTypeScalar = (1 << 8) | 1,
75+
AccessorTypeVec2 = (2 << 8) | 2,
76+
AccessorTypeVec3 = (3 << 8) | 3,
77+
AccessorTypeVec4 = ( 4 << 8) | 4,
78+
AccessorTypeMat2 = ( 4 << 8) | 5,
79+
AccessorTypeMat3 = ( 9 << 8) | 6,
80+
AccessorTypeMat4 = (16 << 8) | 7,
81+
};
82+
83+
enum fastgltf_component_type {
84+
ComponentTypeInvalid = 0,
85+
ComponentTypeByte = ( 8 << 16) | 5120,
86+
ComponentTypeUnsignedByte = ( 8 << 16) | 5121,
87+
ComponentTypeShort = (16 << 16) | 5122,
88+
ComponentTypeUnsignedShort = (16 << 16) | 5123,
89+
ComponentTypeUnsignedInt = (32 << 16) | 5125,
90+
ComponentTypeFloat = (32 << 16) | 5126,
91+
ComponentTypeDouble = (64 << 16) | 5130,
92+
};
93+
94+
enum fastgltf_filter {
95+
FilterNearest = 9728,
96+
FilterLinear = 9729,
97+
FilterNearestMipMapNearest = 9984,
98+
FilterLinearMipMapNearest = 9985,
99+
FilterNearestMipMapLinear = 9986,
100+
FilterLinearMipMapLinear = 9987,
101+
};
102+
103+
enum fastgltf_wrap {
104+
WrapClampToEdge = 33071,
105+
WrapMirroredRepeat = 33648,
106+
WrapRepeat = 10497,
107+
};
108+
109+
enum BufferTarget {
110+
BufferTargetArrayBuffer = 34962,
111+
BufferTargetElementArrayBuffer = 34963,
112+
};
113+
114+
enum MimeType {
115+
MimeTypeNone = 0,
116+
MimeTypeJPEG = 1,
117+
MimeTypePNG = 2,
118+
MimeTypeKTX2 = 3,
119+
MimeTypeDDS = 4,
120+
MimeTypeGltfBuffer = 5,
121+
MimeTypeOctetStream = 6,
122+
};
123+
124+
enum AnimationInterpolation {
125+
AnimationInterpolationLinear = 0,
126+
AnimationInterpolationStep = 1,
127+
AnimationInterpolationCubicSpline = 2,
51128
};
52129

130+
enum AnimationPath {
131+
AnimationPathTranslation = 1,
132+
AnimationPathRotation = 2,
133+
AnimationPathScale = 3,
134+
AnimationPathWeights = 4,
135+
};
136+
137+
enum CameraType {
138+
CameraTypePerspective = 0,
139+
CameraTypeOrthographic = 1,
140+
};
141+
142+
enum AlphaMode {
143+
AlphaModeOpaque = 0,
144+
AlphaModeMask = 1,
145+
AlphaModeBlend = 2,
146+
};
147+
148+
enum MeshoptCompressionMode {
149+
MeshoptCompressionModeNone = 0,
150+
MeshoptCompressionModeAttributes = 1,
151+
MeshoptCompressionModeTriangles = 2,
152+
MeshoptCompressionModeIndices = 3,
153+
};
154+
155+
enum MeshoptCompressionFilter {
156+
MeshoptCompressionFilterNone = 0,
157+
MeshoptCompressionFilterOctahedral = 1,
158+
MeshoptCompressionFilterQuaternion = 2,
159+
MeshoptCompressionFilterExponential = 3,
160+
};
161+
162+
inline unsigned int getNumComponents(fastgltf_accessor_type type) {
163+
return (type >> 8) & 0xFF;
164+
}
165+
166+
inline unsigned int getComponentBitSize(fastgltf_component_type type) {
167+
return (type & 0xFFFF0000) >> 16;
168+
}
169+
170+
inline unsigned int getElementByteSize(fastgltf_accessor_type type, fastgltf_component_type componentType) {
171+
return getNumComponents(type) * (getComponentBitSize(componentType) / 8);
172+
}
173+
174+
inline unsigned int getGLComponentType(fastgltf_component_type type) {
175+
return type & 0xFFFF;
176+
}
177+
53178
#define FASTGLTF_EXPORT
54179

55180
#ifdef __cplusplus
@@ -61,6 +186,9 @@ typedef struct fastgltf_gltf_data_buffer_s fastgltf_gltf_data_buffer;
61186
typedef struct fastgltf_gltf_s fastgltf_gltf;
62187
typedef struct fastgltf_asset_s fastgltf_asset;
63188

189+
FASTGLTF_EXPORT fastgltf_component_type fastgltf_get_component_type(unsigned int componentType);
190+
FASTGLTF_EXPORT fastgltf_accessor_type fastgltf_get_accessor_type(const char* string);
191+
64192
FASTGLTF_EXPORT fastgltf_parser* fastgltf_create_parser(fastgltf_extensions extensions);
65193
FASTGLTF_EXPORT void fastgltf_destroy_parser(fastgltf_parser* parser);
66194

@@ -83,3 +211,5 @@ FASTGLTF_EXPORT void fastgltf_destroy_asset(fastgltf_asset* asset);
83211
#endif
84212

85213
#undef FASTGLTF_EXPORT
214+
215+
#endif

src/fastgltf_types.hpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,23 @@ namespace fastgltf {
183183
};
184184

185185
enum class AlphaMode : uint8_t {
186-
Opaque,
187-
Mask,
188-
Blend,
186+
Opaque = 0,
187+
Mask = 1,
188+
Blend = 2,
189189
};
190190

191191
enum class MeshoptCompressionMode : uint8_t {
192192
None = 0,
193-
Attributes,
194-
Triangles,
195-
Indices,
193+
Attributes = 1,
194+
Triangles = 2,
195+
Indices = 3,
196196
};
197197

198198
enum class MeshoptCompressionFilter : uint8_t {
199199
None = 0,
200-
Octahedral,
201-
Quaternion,
202-
Exponential,
200+
Octahedral = 1,
201+
Quaternion = 2,
202+
Exponential = 3,
203203
};
204204

205205
enum class LightType : uint8_t {
@@ -216,13 +216,11 @@ namespace fastgltf {
216216
* a Vec3 accessor type this will return 3, as a Vec3 contains 3 components.
217217
*/
218218
constexpr uint32_t getNumComponents(AccessorType type) noexcept {
219-
return static_cast<uint32_t>(
220-
(static_cast<decltype(std::underlying_type_t<AccessorType>())>(type) >> 8) & 0xFF);
219+
return static_cast<uint32_t>((to_underlying(type) >> 8) & 0xFF);
221220
}
222221

223222
constexpr uint32_t getComponentBitSize(ComponentType componentType) noexcept {
224-
auto masked =
225-
static_cast<decltype(std::underlying_type_t<ComponentType>())>(componentType) & 0xFFFF0000;
223+
auto masked = to_underlying(componentType) & 0xFFFF0000;
226224
return (masked >> 16);
227225
}
228226

0 commit comments

Comments
 (0)