This commit is contained in:
Aandreba 2025-02-19 12:15:36 +01:00
parent a2f5121a52
commit a8f658b0cf
2 changed files with 70 additions and 11 deletions

View file

@ -36,8 +36,9 @@ pub fn build(b: *std.Build) void {
},
});
simdjson_lib.addIncludePath(simdjson_dep.path("singleheader"));
b.installArtifact(simdjson_lib);
const lib: *std.Build.Step.Compile = switch (preferred_link_mode) {
const fastgltf_lib: *std.Build.Step.Compile = switch (preferred_link_mode) {
inline else => |x| switch (x) {
.static => std.Build.addStaticLibrary,
.dynamic => std.Build.addSharedLibrary,
@ -49,11 +50,11 @@ pub fn build(b: *std.Build) void {
.link_libc = true,
}),
};
lib.linkLibCpp();
lib.linkLibrary(simdjson_lib);
lib.addIncludePath(fastgltf_dep.path(b.pathJoin(&.{"include"})));
lib.addIncludePath(simdjson_dep.path("include"));
lib.addCSourceFiles(.{
fastgltf_lib.linkLibCpp();
fastgltf_lib.linkLibrary(simdjson_lib);
fastgltf_lib.addIncludePath(fastgltf_dep.path(b.pathJoin(&.{"include"})));
fastgltf_lib.addIncludePath(simdjson_dep.path("include"));
fastgltf_lib.addCSourceFiles(.{
.root = fastgltf_dep.path("."),
.files = &(.{ "src/fastgltf.cpp", "src/base64.cpp", "src/io.cpp" }),
.flags = &.{
@ -62,10 +63,17 @@ pub fn build(b: *std.Build) void {
b.fmt("--std={s}", .{compile_target}),
},
});
lib.root_module.addCMacro("FASTGLTF_USE_CUSTOM_SMALLVECTOR", if (use_custom_smallvec) "1" else "0");
lib.root_module.addCMacro("FASTGLTF_ENABLE_DEPRECATED_EXT", if (enable_deprecated_ext) "1" else "0");
lib.root_module.addCMacro("FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL", if (disable_custom_memory_pool) "1" else "0");
lib.root_module.addCMacro("FASTGLTF_USE_64BIT_FLOAT", if (use_64bit_float) "1" else "0");
fastgltf_lib.root_module.addCMacro("FASTGLTF_USE_CUSTOM_SMALLVECTOR", if (use_custom_smallvec) "1" else "0");
fastgltf_lib.root_module.addCMacro("FASTGLTF_ENABLE_DEPRECATED_EXT", if (enable_deprecated_ext) "1" else "0");
fastgltf_lib.root_module.addCMacro("FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL", if (disable_custom_memory_pool) "1" else "0");
fastgltf_lib.root_module.addCMacro("FASTGLTF_USE_64BIT_FLOAT", if (use_64bit_float) "1" else "0");
b.installArtifact(fastgltf_lib);
b.installArtifact(lib);
const example = b.addExecutable(.{
.name = "fastgltf-example",
.target = target,
.optimize = optimize,
});
example.linkLibrary(fastgltf_lib);
b.installAr
}

51
example/main.cpp Normal file
View file

@ -0,0 +1,51 @@
#include <fastgltf/core.hpp>
#include <fastgltf/types.hpp>
bool load(std::filesystem::path path) {
// Creates a Parser instance. Optimally, you should reuse this across loads,
// but don't use it across threads. To enable extensions, you have to pass
// them into the parser's constructor.
fastgltf::Parser parser;
// The GltfDataBuffer class contains static factories which create a buffer
// for holding the glTF data. These return Expected<GltfDataBuffer>, which can
// be checked if an error occurs. The parser accepts any subtype of
// GltfDataGetter, which defines an interface for reading chunks of the glTF
// file for the Parser to handle. fastgltf provides a few predefined classes
// which inherit from GltfDataGetter, so choose whichever fits your usecase
// the best.
auto data = fastgltf::GltfDataBuffer::FromPath(path);
if (data.error() != fastgltf::Error::None) {
// The file couldn't be loaded, or the buffer could not be allocated.
return false;
}
// This loads the glTF file into the gltf object and parses the JSON.
// It automatically detects whether this is a JSON-based or binary glTF.
// If you know the type, you can also use loadGltfJson or loadGltfBinary.
auto asset =
parser.loadGltf(data.get(), path.parent_path(), fastgltf::Options::None);
if (auto error = asset.error(); error != fastgltf::Error::None) {
// Some error occurred while reading the buffer, parsing the JSON, or
// validating the data.
return false;
}
// The glTF 2.0 asset is now ready to be used. Simply call asset.get(),
// asset.get_if() or asset-> to get a direct reference to the Asset class. You
// can then access the glTF data structures, like, for example, with buffers:
for (auto &buffer : asset->buffers) {
// Process the buffers.
}
// Optionally, you can now also call the fastgltf::validate method. This will
// more strictly enforce the glTF spec and is not needed most of the time,
// though I would certainly recommend it in a development environment or when
// debugging to avoid mishaps.
// fastgltf::validate(asset.get());
return true;
}
int main() { return 0; }