From 1155bc8d5fee33b3728916255a9c8ac411b24b57 Mon Sep 17 00:00:00 2001 From: 0undefined Date: Wed, 11 Mar 2026 07:45:46 +0100 Subject: Add assimp & loader func --- state_mainstate/src/mainstate.c | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'state_mainstate/src') diff --git a/state_mainstate/src/mainstate.c b/state_mainstate/src/mainstate.c index 172f3a7..8dbbaa3 100644 --- a/state_mainstate/src/mainstate.c +++ b/state_mainstate/src/mainstate.c @@ -1,3 +1,5 @@ +#define CGLM_FORCE_DEPTH_ZERO_TO_ONE 1 + #include #include #include @@ -8,6 +10,11 @@ #include #include + +#include // Plain-C interface +#include // Output data structure +#include // Post processing flags + #define FOV_ORTHO 1 #ifdef FOV_ORTHO @@ -128,6 +135,79 @@ void window_resize_callback(ivec3* dst, ivec2 src) { //glm_ivec2_divs(src, 4, *dst); } +ShaderBuffer model_shaderbuf[3]; +ShaderBuffer light_shaderbuf[3]; + +static void load_model_from_file(const char* file_path, struct assimp_mesh *mesh) { + // 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( file_path, + aiProcess_CalcTangentSpace | + aiProcess_Triangulate | + //aiProcess_JoinIdenticalVertices | + aiProcess_SortByPType); + + // If the import failed, report it + if(scene == NULL) { + ERROR("Failed to import model: %s", aiGetErrorString()); + return; + } + + if (mesh == NULL) { + mesh = calloc(1, sizeof(struct assimp_mesh)); + } + + for (u32 m = 0; m < scene->mNumMeshes; m++) { + mesh->vertices_len = scene->mMeshes[m]->mNumVertices * 3; + mesh->normals_len = scene->mMeshes[m]->mNumVertices * 3; + //mesh_uv_len = scene->mMeshes[m]->mNumVertices * 2; + + mesh->vertices = calloc(scene->mMeshes[m]->mNumVertices * 3, sizeof(float)); + mesh->normals = calloc(scene->mMeshes[m]->mNumVertices * 3, sizeof(float)); + //mesh_uv = calloc(scene->mMeshes[m]->mNumVertices * 2, sizeof(float)); + for (u32 i = 0; i < scene->mMeshes[m]->mNumVertices; i++) { + mesh->vertices[i * 3 + 0] = scene->mMeshes[m]->mVertices[i].x; + mesh->vertices[i * 3 + 1] = scene->mMeshes[m]->mVertices[i].y; + mesh->vertices[i * 3 + 2] = scene->mMeshes[m]->mVertices[i].z; + + mesh->normals[i * 3 + 0] = scene->mMeshes[m]->mNormals[i].x; + mesh->normals[i * 3 + 1] = scene->mMeshes[m]->mNormals[i].y; + mesh->normals[i * 3 + 2] = scene->mMeshes[m]->mNormals[i].z; + + //if (scene->mMeshes[m]->mTextureCoords[i] == NULL) { + // mesh_uv[i * 2 + 0] = 0; + // mesh_uv[i * 2 + 1] = 0; + //} else { + // mesh_uv[i * 2 + 0] = scene->mMeshes[m]->mTextureCoords[i]->x; + // mesh_uv[i * 2 + 1] = scene->mMeshes[m]->mTextureCoords[i]->y; + //} + } + + // Count the vertices for all faces first:) + mesh->indices_len = 0; + for (u32 i = 0; i < scene->mMeshes[m]->mNumFaces; i++) { + mesh->indices_len += scene->mMeshes[m]->mFaces[i].mNumIndices; + } + + mesh->indices = calloc(mesh->indices_len, sizeof(float)); + usize mesh_idx = 0; + for (u32 i = 0; i < scene->mMeshes[m]->mNumFaces; i++) { + for (u32 j = 0; j < scene->mMeshes[m]->mFaces[i].mNumIndices; j++) { + mesh->indices[mesh_idx] = scene->mMeshes[m]->mFaces[i].mIndices[j]; + mesh_idx++; + } + } + + // normals?? + } + // Now we can access the file's contents + //DoTheSceneProcessing( scene); + + // We're done. Release all resources associated with this import + aiReleaseImport(scene); +} + Camera UI_Camera; void perspective_update_callback_ui(void *restrict w, Camera *restrict dst, void *s, ivec2 src) { mainstate_state *state = s; -- cgit v1.3