Skip to content

Commit

Permalink
Add: Enums and utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Nov 5, 2022
1 parent ac1fdce commit 851df7b
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(fastgltf)
add_library(fastgltf::fastgltf ALIAS fastgltf)
target_include_directories(fastgltf PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}> $<INSTALL_INTERFACE:include>)
target_compile_features(fastgltf PUBLIC cxx_std_17)
set_property(TARGET fastgltf PROPERTY C_STANDARD 90) #C89/90
compiler_flags(TARGET fastgltf)

set_target_properties(fastgltf PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES)
Expand All @@ -19,7 +20,7 @@ endif()
add_source_directory(TARGET fastgltf FOLDER ".")

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
)

Expand Down
6 changes: 2 additions & 4 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ namespace fastgltf {
constexpr auto hashTextures = crc32("textures");

struct ParserData {
// Can simdjson not store this data itself?
std::vector<uint8_t> bytes;
simdjson::dom::document doc;
simdjson::dom::object root;

Expand Down Expand Up @@ -1815,7 +1813,7 @@ std::unique_ptr<fg::glTF> fg::Parser::loadGLTF(GltfDataBuffer* buffer, fs::path
data->unmapCallback = unmapCallback;
data->userPointer = userPointer;

return std::unique_ptr<glTF>(new glTF(std::move(data), std::move(directory), options, extensions));
return std::unique_ptr<glTF>(new (std::nothrow) glTF(std::move(data), std::move(directory), options, extensions));
}

std::unique_ptr<fg::glTF> fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs::path directory, Options options) {
Expand Down Expand Up @@ -1874,7 +1872,7 @@ std::unique_ptr<fg::glTF> fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs:
data->unmapCallback = unmapCallback;
data->userPointer = userPointer;

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

// Is there enough room for another chunk header?
if (header.length > (offset + sizeof(BinaryGltfChunk))) {
Expand Down
14 changes: 12 additions & 2 deletions src/fastgltf_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
#include <fastgltf_parser.hpp>
#include <fastgltf_types.hpp>

fastgltf_component_type fastgltf_get_component_type(unsigned int componentType) {
return static_cast<fastgltf_component_type>(fastgltf::getComponentType(
static_cast<std::underlying_type_t<fastgltf::ComponentType>>(componentType)));
}

fastgltf_accessor_type fastgltf_get_accessor_type(const char* string) {
return static_cast<fastgltf_accessor_type>(fastgltf::getAccessorType(std::string_view { string }));
}

fastgltf_parser* fastgltf_create_parser(fastgltf_extensions extensions) {
return reinterpret_cast<fastgltf_parser*>(
new fastgltf::Parser(static_cast<fastgltf::Extensions>(extensions)));
new (std::nothrow) fastgltf::Parser(static_cast<fastgltf::Extensions>(extensions)));
}

void fastgltf_destroy_parser(fastgltf_parser* parser) {
Expand Down Expand Up @@ -50,7 +59,8 @@ void fastgltf_destroy_gltf(fastgltf_gltf* gltf) {
}

fastgltf_error fastgltf_parse(fastgltf_gltf* gltf, fastgltf_category categories) {
return static_cast<fastgltf_error>(reinterpret_cast<fastgltf::glTF*>(gltf)->parse(static_cast<fastgltf::Category>(categories)));
return static_cast<fastgltf_error>(
reinterpret_cast<fastgltf::glTF*>(gltf)->parse(static_cast<fastgltf::Category>(categories)));
}

fastgltf_asset* fastgltf_get_parsed_asset(fastgltf_gltf* gltf) {
Expand Down
160 changes: 145 additions & 15 deletions src/fastgltf_c.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
#ifndef FASTGLTF_C_H
#define FASTGLTF_C_H

#ifdef __cplusplus
#include <cstddef>
#else
#include <stddef.h>
#endif

enum fastgltf_extensions {
KHR_texture_transform = 1 << 1,
KHR_texture_basisu = 1 << 2,
Expand Down Expand Up @@ -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
Expand All @@ -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);

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

#undef FASTGLTF_EXPORT

#endif
26 changes: 13 additions & 13 deletions src/fastgltf_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,28 @@ namespace fastgltf {
};

enum class CameraType : uint8_t {
Perspective,
Orthographic,
Perspective = 0,
Orthographic = 1,
};

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,
};
// clang-format on
#pragma endregion
Expand All @@ -154,11 +154,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<uint32_t>((static_cast<uint16_t>(type) >> 8) & 0xFF);
return static_cast<uint32_t>((to_underlying(type) >> 8) & 0xFF);
}

constexpr uint32_t getComponentBitSize(ComponentType componentType) noexcept {
auto masked = static_cast<uint32_t>(componentType) & 0xFFFF0000;
auto masked = to_underlying(componentType) & 0xFFFF0000;
return (masked >> 16);
}

Expand Down

0 comments on commit 851df7b

Please sign in to comment.