Add gltf loader

Added MIT licensed external library for loading gltf
files

Signed-off-by: Piotr Krygier <piotrkrygier@everyonecancode@xyz>
This commit is contained in:
Piotr Krygier
2026-03-12 13:21:24 +01:00
parent c6825caa69
commit dd70f5e5b0
7 changed files with 7289 additions and 6 deletions
+31 -3
View File
@@ -1,8 +1,14 @@
#include "file_utils.h"
#define CGLTF_IMPLEMENTATION
#include "cgltf.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vulkan/vulkan_core.h>
#include "SDL3/SDL_log.h"
#include "commons.h"
@@ -151,7 +157,7 @@ rse_err_t file_load_pixels(const char* file_name, unsigned char* buffer, size_t*
return RSE_ERROR_INTERNAL_ERROR;
}
image_buffer_size = *width * *height * 4; // TODO: "4" is very hardcoded value. Change this
image_buffer_size = *width * *height * 4; // TODO: "4" is very hardcoded value. Change this
if ((buffer != NULL) && (image_buffer_size > *buffer_size)) {
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
@@ -176,7 +182,7 @@ image_free:
return ret;
}
rse_err_t file_read(const char* file_name, size_t* bytes_count, char* buffer)
rse_err_t file_read_bytes(const char* file_name, size_t* bytes_count, char* buffer)
{
rse_err_t ret = RSE_ERROR_NO_ERROR;
FILE* file = NULL;
@@ -238,5 +244,27 @@ rse_err_t file_write(const char* file_name, size_t bytes_count, char* buffer)
fwrite(buffer, bytes_count, 1, file);
fclose(file);
return RSE_ERROR_NO_ERROR;
return ret;
}
rse_err_t file_load_gltf(const char* file_name)
{
char real_path[MAX_PATH_LENGTH] = {0};
rse_err_t ret = RSE_ERROR_NO_ERROR;
cgltf_options options = {0};
cgltf_data* data = NULL;
strncpy(real_path, file_name, MAX_PATH_LENGTH);
if ((ret = file_append_full_path(real_path, MAX_PATH_LENGTH)) != RSE_ERROR_NO_ERROR) {
return ret;
}
if (cgltf_parse_file(&options, real_path, &data) != cgltf_result_success) {
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "Failed to parse GLTF data");
return RSE_ERROR_INTERNAL_ERROR;
}
cgltf_free(data);
return ret;
}