From 088d161c6cca24bd290e4921a0616f7144b5c2c6 Mon Sep 17 00:00:00 2001 From: sean Date: Sat, 5 Nov 2022 23:38:30 +0100 Subject: [PATCH] Add: Enums and utility functions --- src/CMakeLists.txt | 3 +- src/fastgltf.cpp | 4 +- src/fastgltf_c.cpp | 14 +++- src/fastgltf_c.h | 160 +++++++++++++++++++++++++++++++++++++---- src/fastgltf_types.hpp | 24 +++---- 5 files changed, 172 insertions(+), 33 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a077fb901..34fd80763 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ add_library(fastgltf add_library(fastgltf::fastgltf ALIAS fastgltf) target_include_directories(fastgltf PUBLIC $ $) target_compile_features(fastgltf PUBLIC cxx_std_17) +set_property(TARGET fastgltf PROPERTY C_STANDARD 90) #C89/90 compiler_flags(TARGET fastgltf) enable_debug_inlining(TARGET fastgltf_simdjson) @@ -23,7 +24,7 @@ endif() target_compile_definitions(fastgltf PRIVATE "FASTGLTF_USE_CUSTOM_SMALLVECTOR=$") install( - FILES "base64_decode.hpp" "fastgltf_parser.hpp" "fastgltf_types.hpp" "fastgltf_util.hpp" + FILES "base64_decode.hpp" "fastgltf_parser.hpp" "fastgltf_types.hpp" "fastgltf_util.hpp" "fastgltf_c.h" TYPE INCLUDE ) diff --git a/src/fastgltf.cpp b/src/fastgltf.cpp index d0e922267..ca393782b 100644 --- a/src/fastgltf.cpp +++ b/src/fastgltf.cpp @@ -2147,7 +2147,7 @@ std::unique_ptr fg::Parser::loadGLTF(GltfDataBuffer* buffer, fs::path data->decodeCallback = decodeCallback; data->userPointer = userPointer; - return std::unique_ptr(new glTF(std::move(data), std::move(directory), options, extensions)); + return std::unique_ptr(new (std::nothrow) glTF(std::move(data), std::move(directory), options, extensions)); } std::unique_ptr fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs::path directory, Options options) { @@ -2203,7 +2203,7 @@ std::unique_ptr fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs: data->decodeCallback = decodeCallback; data->userPointer = userPointer; - auto gltf = std::unique_ptr(new glTF(std::move(data), std::move(directory), options, extensions)); + auto gltf = std::unique_ptr(new (std::nothrow) glTF(std::move(data), std::move(directory), options, extensions)); // Is there enough room for another chunk header? if (header.length > (offset + sizeof(BinaryGltfChunk))) { diff --git a/src/fastgltf_c.cpp b/src/fastgltf_c.cpp index 43fce3f55..5ce59bf00 100644 --- a/src/fastgltf_c.cpp +++ b/src/fastgltf_c.cpp @@ -3,9 +3,18 @@ #include #include +fastgltf_component_type fastgltf_get_component_type(unsigned int componentType) { + return static_cast(fastgltf::getComponentType( + static_cast>(componentType))); +} + +fastgltf_accessor_type fastgltf_get_accessor_type(const char* string) { + return static_cast(fastgltf::getAccessorType(std::string_view { string })); +} + fastgltf_parser* fastgltf_create_parser(fastgltf_extensions extensions) { return reinterpret_cast( - new fastgltf::Parser(static_cast(extensions))); + new (std::nothrow) fastgltf::Parser(static_cast(extensions))); } void fastgltf_destroy_parser(fastgltf_parser* parser) { @@ -50,7 +59,8 @@ void fastgltf_destroy_gltf(fastgltf_gltf* gltf) { } fastgltf_error fastgltf_parse(fastgltf_gltf* gltf, fastgltf_category categories) { - return static_cast(reinterpret_cast(gltf)->parse(static_cast(categories))); + return static_cast( + reinterpret_cast(gltf)->parse(static_cast(categories))); } fastgltf_asset* fastgltf_get_parsed_asset(fastgltf_gltf* gltf) { diff --git a/src/fastgltf_c.h b/src/fastgltf_c.h index 233eea92b..6ca820418 100644 --- a/src/fastgltf_c.h +++ b/src/fastgltf_c.h @@ -1,3 +1,12 @@ +#ifndef FASTGLTF_C_H +#define FASTGLTF_C_H + +#ifdef __cplusplus +#include +#else +#include +#endif + enum fastgltf_extensions { KHR_texture_transform = 1 << 1, KHR_texture_basisu = 1 << 2, @@ -32,24 +41,140 @@ enum fastgltf_error { enum fastgltf_category { CategoryNone = 0, - CategoryBuffers = 1 << 0, + CategoryBuffers = 1 << 0, CategoryBufferViews = 1 << 1 | CategoryBuffers, - CategoryAccessors = 1 << 2 | CategoryBufferViews, - CategoryImages = 1 << 3 | CategoryBufferViews, - CategorySamplers = 1 << 4, - CategoryTextures = 1 << 5 | CategoryImages | CategorySamplers, - CategoryAnimations = 1 << 6 | CategoryAccessors, - CategoryCameras = 1 << 7, - CategoryMaterials = 1 << 8 | CategoryTextures, - CategoryMeshes = 1 << 9 | CategoryAccessors | CategoryMaterials, - CategorySkins = 1 << 10 | CategoryAccessors | (1 << 11), - CategoryNodes = 1 << 11 | CategoryCameras | CategoryMeshes | CategorySkins, - CategoryScenes = 1 << 12 | CategoryNodes, - CategoryAsset = 1 << 13, - - CategoryAll = CategoryAsset | CategoryScenes | CategoryAnimations, + CategoryAccessors = 1 << 2 | CategoryBufferViews, + CategoryImages = 1 << 3 | CategoryBufferViews, + CategorySamplers = 1 << 4, + CategoryTextures = 1 << 5 | CategoryImages | CategorySamplers, + CategoryAnimations = 1 << 6 | CategoryAccessors, + CategoryCameras = 1 << 7, + CategoryMaterials = 1 << 8 | CategoryTextures, + CategoryMeshes = 1 << 9 | CategoryAccessors | CategoryMaterials, + CategorySkins = 1 << 10 | CategoryAccessors | (1 << 11), + CategoryNodes = 1 << 11 | CategoryCameras | CategoryMeshes | CategorySkins, + CategoryScenes = 1 << 12 | CategoryNodes, + CategoryAsset = 1 << 13, + + CategoryAll = CategoryAsset | CategoryScenes | CategoryAnimations, +}; + +enum fastgltf_primitive_type { + PrimitiveTypePoints = 0, + PrimitiveTypeLines = 1, + PrimitiveTypeLineLoop = 2, + PrimitiveTypeLineStrip = 3, + PrimitiveTypeTriangles = 4, + PrimitiveTypeTriangleStrip = 5, + PrimitiveTypeTriangleFan = 6, +}; + +enum fastgltf_accessor_type { + AccessorTypeInvalid = 0, + AccessorTypeScalar = (1 << 8) | 1, + AccessorTypeVec2 = (2 << 8) | 2, + AccessorTypeVec3 = (3 << 8) | 3, + AccessorTypeVec4 = ( 4 << 8) | 4, + AccessorTypeMat2 = ( 4 << 8) | 5, + AccessorTypeMat3 = ( 9 << 8) | 6, + AccessorTypeMat4 = (16 << 8) | 7, +}; + +enum fastgltf_component_type { + ComponentTypeInvalid = 0, + ComponentTypeByte = ( 8 << 16) | 5120, + ComponentTypeUnsignedByte = ( 8 << 16) | 5121, + ComponentTypeShort = (16 << 16) | 5122, + ComponentTypeUnsignedShort = (16 << 16) | 5123, + ComponentTypeUnsignedInt = (32 << 16) | 5125, + ComponentTypeFloat = (32 << 16) | 5126, + ComponentTypeDouble = (64 << 16) | 5130, +}; + +enum fastgltf_filter { + FilterNearest = 9728, + FilterLinear = 9729, + FilterNearestMipMapNearest = 9984, + FilterLinearMipMapNearest = 9985, + FilterNearestMipMapLinear = 9986, + FilterLinearMipMapLinear = 9987, +}; + +enum fastgltf_wrap { + WrapClampToEdge = 33071, + WrapMirroredRepeat = 33648, + WrapRepeat = 10497, +}; + +enum BufferTarget { + BufferTargetArrayBuffer = 34962, + BufferTargetElementArrayBuffer = 34963, +}; + +enum MimeType { + MimeTypeNone = 0, + MimeTypeJPEG = 1, + MimeTypePNG = 2, + MimeTypeKTX2 = 3, + MimeTypeDDS = 4, + MimeTypeGltfBuffer = 5, + MimeTypeOctetStream = 6, +}; + +enum AnimationInterpolation { + AnimationInterpolationLinear = 0, + AnimationInterpolationStep = 1, + AnimationInterpolationCubicSpline = 2, }; +enum AnimationPath { + AnimationPathTranslation = 1, + AnimationPathRotation = 2, + AnimationPathScale = 3, + AnimationPathWeights = 4, +}; + +enum CameraType { + CameraTypePerspective = 0, + CameraTypeOrthographic = 1, +}; + +enum AlphaMode { + AlphaModeOpaque = 0, + AlphaModeMask = 1, + AlphaModeBlend = 2, +}; + +enum MeshoptCompressionMode { + MeshoptCompressionModeNone = 0, + MeshoptCompressionModeAttributes = 1, + MeshoptCompressionModeTriangles = 2, + MeshoptCompressionModeIndices = 3, +}; + +enum MeshoptCompressionFilter { + MeshoptCompressionFilterNone = 0, + MeshoptCompressionFilterOctahedral = 1, + MeshoptCompressionFilterQuaternion = 2, + MeshoptCompressionFilterExponential = 3, +}; + +inline unsigned int getNumComponents(fastgltf_accessor_type type) { + return (type >> 8) & 0xFF; +} + +inline unsigned int getComponentBitSize(fastgltf_component_type type) { + return (type & 0xFFFF0000) >> 16; +} + +inline unsigned int getElementByteSize(fastgltf_accessor_type type, fastgltf_component_type componentType) { + return getNumComponents(type) * (getComponentBitSize(componentType) / 8); +} + +inline unsigned int getGLComponentType(fastgltf_component_type type) { + return type & 0xFFFF; +} + #define FASTGLTF_EXPORT #ifdef __cplusplus @@ -61,6 +186,9 @@ typedef struct fastgltf_gltf_data_buffer_s fastgltf_gltf_data_buffer; typedef struct fastgltf_gltf_s fastgltf_gltf; typedef struct fastgltf_asset_s fastgltf_asset; +FASTGLTF_EXPORT fastgltf_component_type fastgltf_get_component_type(unsigned int componentType); +FASTGLTF_EXPORT fastgltf_accessor_type fastgltf_get_accessor_type(const char* string); + FASTGLTF_EXPORT fastgltf_parser* fastgltf_create_parser(fastgltf_extensions extensions); FASTGLTF_EXPORT void fastgltf_destroy_parser(fastgltf_parser* parser); @@ -83,3 +211,5 @@ FASTGLTF_EXPORT void fastgltf_destroy_asset(fastgltf_asset* asset); #endif #undef FASTGLTF_EXPORT + +#endif diff --git a/src/fastgltf_types.hpp b/src/fastgltf_types.hpp index 164c4b8a7..c5e810d4f 100644 --- a/src/fastgltf_types.hpp +++ b/src/fastgltf_types.hpp @@ -183,23 +183,23 @@ namespace fastgltf { }; enum class AlphaMode : uint8_t { - Opaque, - Mask, - Blend, + Opaque = 0, + Mask = 1, + Blend = 2, }; enum class MeshoptCompressionMode : uint8_t { None = 0, - Attributes, - Triangles, - Indices, + Attributes = 1, + Triangles = 2, + Indices = 3, }; enum class MeshoptCompressionFilter : uint8_t { None = 0, - Octahedral, - Quaternion, - Exponential, + Octahedral = 1, + Quaternion = 2, + Exponential = 3, }; enum class LightType : uint8_t { @@ -216,13 +216,11 @@ namespace fastgltf { * a Vec3 accessor type this will return 3, as a Vec3 contains 3 components. */ constexpr uint32_t getNumComponents(AccessorType type) noexcept { - return static_cast( - (static_cast())>(type) >> 8) & 0xFF); + return static_cast((to_underlying(type) >> 8) & 0xFF); } constexpr uint32_t getComponentBitSize(ComponentType componentType) noexcept { - auto masked = - static_cast())>(componentType) & 0xFFFF0000; + auto masked = to_underlying(componentType) & 0xFFFF0000; return (masked >> 16); }