Skip to content

Commit

Permalink
Use USD asset resolver for file IO
Browse files Browse the repository at this point in the history
This allows guc to work with custom resolvers.
  • Loading branch information
pablode committed Apr 19, 2024
1 parent 9ee3d42 commit e164933
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/libguc/src/cgltf_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

#include <pxr/base/tf/diagnostic.h>
#include <pxr/base/gf/math.h>
#include <pxr/usd/ar/asset.h>
#include <pxr/usd/ar/resolver.h>
#include <pxr/usd/ar/resolvedPath.h>

#define CGLTF_IMPLEMENTATION
#include <cgltf.h>
Expand Down Expand Up @@ -47,14 +50,51 @@ namespace detail
strcmp(name, "KHR_materials_volume") == 0 ||
strcmp(name, "KHR_texture_transform") == 0;
}

cgltf_result readFile(const cgltf_memory_options* memory_options,
const cgltf_file_options* file_options,
const char* path,
cgltf_size* size,
void** data)
{
TF_DEBUG(GUC).Msg("reading file %s\n", path);

ArResolver& resolver = ArGetResolver();
std::shared_ptr<ArAsset> asset = resolver.OpenAsset(ArResolvedPath(path));
if (!asset)
{
return cgltf_result_file_not_found;
}

(*size) = asset->GetSize();
(*data) = malloc(*size);

std::shared_ptr<const char> buffer = asset->GetBuffer();
memcpy(*data, buffer.get(), *size);

return cgltf_result_success;
}

void releaseFile(const cgltf_memory_options* memory_options,
const cgltf_file_options* file_options,
void* data)
{
free(data);
}
}

namespace guc
{
bool load_gltf(const char* gltfPath, cgltf_data** data)
{
cgltf_result result;

cgltf_file_options file_options = {};
file_options.read = detail::readFile;
file_options.release = detail::releaseFile;

cgltf_options options = {};
options.file = file_options;

result = cgltf_parse_file(&options, gltfPath, data);
if (result != cgltf_result_success)
Expand Down

0 comments on commit e164933

Please sign in to comment.