From 57a5158400a7e1ccccfd06af292e782d8eff2e4b Mon Sep 17 00:00:00 2001 From: 0scar Date: Mon, 26 Feb 2024 14:55:21 +0100 Subject: Add model asset type --- src/resources/CMakeLists.txt | 1 + src/resources/include/engine/resources.h | 12 ++++++ src/resources/include/engine/resources/model.h | 32 ++++++++++++++ src/resources/src/model.c | 58 ++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 src/resources/include/engine/resources/model.h create mode 100644 src/resources/src/model.c (limited to 'src/resources') diff --git a/src/resources/CMakeLists.txt b/src/resources/CMakeLists.txt index 6cb68d3..8fd9413 100644 --- a/src/resources/CMakeLists.txt +++ b/src/resources/CMakeLists.txt @@ -10,6 +10,7 @@ add_library(daw_resources src/fonts.c src/scripts.c src/textures.c + src/model.c ) target_include_directories(daw_resources PRIVATE ${DAW_INCLUDE_DIRS}) diff --git a/src/resources/include/engine/resources.h b/src/resources/include/engine/resources.h index 707bbde..d967403 100644 --- a/src/resources/include/engine/resources.h +++ b/src/resources/include/engine/resources.h @@ -34,6 +34,7 @@ enum Asset { Asset_shader, Asset_shaderprog, Asset_texture, + Asset_model, }; typedef struct { @@ -69,6 +70,11 @@ typedef struct { i32 bpc; } Asset_TextureSpec; +typedef struct { + enum Asset type; + const char* path; +} Asset_ModelSpec; + typedef union { enum Asset type; Asset_AudioSpec audio; @@ -76,8 +82,11 @@ typedef union { Asset_ShaderSpec shader; Asset_ShaderProgramSpec shaderprog; Asset_TextureSpec texture; + Asset_ModelSpec model; } asset_t; +#include + // The resource spec typedef struct { /* Assorted asset specification, makes reloading them easier. */ @@ -93,6 +102,9 @@ typedef struct { usize texture_len; Texture* texture; + + usize model_len; + Model* model; } Resources; #define TextureDefinition(_path, ...) unimplemented diff --git a/src/resources/include/engine/resources/model.h b/src/resources/include/engine/resources/model.h new file mode 100644 index 0000000..d22dc21 --- /dev/null +++ b/src/resources/include/engine/resources/model.h @@ -0,0 +1,32 @@ +#ifndef ENGINE_RESOURCES_MODEL_H +#define ENGINE_RESOURCES_MODEL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +typedef enum { + Model_error, + Model_obj, +} ModelType; + +typedef struct { + ModelType format; + + GLuint m_uiVAO; + GLuint m_uiVBO; + GLuint m_uiIBO; + unsigned m_uiNumIndices; +} Model; + +#include +Model load_model(const Asset_ModelSpec *restrict ms); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/resources/src/model.c b/src/resources/src/model.c new file mode 100644 index 0000000..893036a --- /dev/null +++ b/src/resources/src/model.c @@ -0,0 +1,58 @@ +#include +#include + +Model load_model(const Asset_ModelSpec *restrict ms) { + ModelType format = Model_error; + + isize vertices_len = 0; + vec3 *vertices = malloc(sizeof(vec3) * 512); + usize *vertices_idx = malloc(sizeof(usize) * 512); + + isize uvs_len = 0; + vec2 *uvs = malloc(sizeof(vec2) * 512); + usize *uvs_idx = malloc(sizeof(usize) * 512); + + isize normals_len = 0; + vec3 *normals = malloc(sizeof(vec3) * 512); + usize *normals_idx = malloc(sizeof(usize) * 512); + + // For now, just default to obj + format = Model_obj; + + //FILE* f = fopen(ms->path, "r"); + //if (f == NULL) { + // ERROR("Failed to load file " TERM_COLOR_YELLOW "%s" TERM_COLOR_RESET, ms->path); + // return (Model){.format = format}; + //} + + //// Start the import on the given file with some example postprocessing + //// Usually - if speed is not the most important aspect for you - you'll t + //// probably to request more postprocessing than we do in this example. + //const struct aiScene* scene = aiImportFile( pFile, + // aiProcess_CalcTangentSpace | + // aiProcess_Triangulate | + // aiProcess_JoinIdenticalVertices | + // aiProcess_SortByPType); + + //// If the import failed, report it + //if( NULL == scene) { + // DoTheErrorLogging( aiGetErrorString()); + // return false; + //} + + //// Now we can access the file's contents + //DoTheSceneProcessing( scene); + + //// We're done. Release all resources associated with this import + //aiReleaseImport( scene); + //return true; + + { + Model m = {}; + m.format = format; + + // TODO add index array + + return m; + } +} -- cgit v1.3