From bae1ff37590e78bb1bc20694f190a05d8e8e7d80 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 13 Apr 2026 12:58:54 -0500 Subject: [PATCH] Allow reaxising mesh --- .../asset/loader/display/assetmeshloader.c | 74 +++++++++++++++++-- .../asset/loader/display/assetmeshloader.h | 17 ++++- src/dusk/display/mesh/mesh.c | 24 ++++++ src/dusk/display/mesh/mesh.h | 13 ++++ src/dusk/engine/engine.c | 15 +++- 5 files changed, 132 insertions(+), 11 deletions(-) diff --git a/src/dusk/asset/loader/display/assetmeshloader.c b/src/dusk/asset/loader/display/assetmeshloader.c index 92ca40c3..b43505dd 100644 --- a/src/dusk/asset/loader/display/assetmeshloader.c +++ b/src/dusk/asset/loader/display/assetmeshloader.c @@ -79,10 +79,53 @@ errorret_t assetMeshLoader(assetfile_t *file) { ); } - // Convert Z-Up to Y-Up - float_t temp = verts[i * 3 + j].pos[1]; - verts[i * 3 + j].pos[1] = verts[i * 3 + j].pos[2]; - verts[i * 3 + j].pos[2] = temp; + switch(output->inputAxis) { + case MESH_INPUT_AXIS_Z_UP: + // Convert Z-Up to Y-Up + { + float_t temp = verts[i * 3 + j].pos[1]; + verts[i * 3 + j].pos[1] = verts[i * 3 + j].pos[2]; + verts[i * 3 + j].pos[2] = temp; + } + break; + + case MESH_INPUT_AXIS_X_UP: + // Convert X-Up to Y-Up + { + float_t temp = verts[i * 3 + j].pos[0]; + verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1]; + verts[i * 3 + j].pos[1] = temp; + } + break; + + case MESH_INPUT_AXIS_Y_DOWN: + // Invert Y axis + verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[1]; + break; + + case MESH_INPUT_AXIS_Z_DOWN: + // Convert Z-Up to Y-Up and invert Y axis + { + float_t temp = verts[i * 3 + j].pos[1]; + verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[2]; + verts[i * 3 + j].pos[2] = temp; + } + break; + + case MESH_INPUT_AXIS_X_DOWN: + // Convert X-Up to Y-Up and invert Y axis + { + float_t temp = verts[i * 3 + j].pos[0]; + verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1]; + verts[i * 3 + j].pos[1] = -temp; + } + break; + + case MESH_INPUT_AXIS_Y_UP: + default: + // No covnersion possible / Needed + break; + } } } @@ -108,15 +151,32 @@ errorret_t assetMeshLoader(assetfile_t *file) { errorOk(); } +errorret_t assetMeshLoadToOutput( + const char_t *path, + assetmeshoutput_t *output +) { + assertNotNull(path, "Path cannot be null"); + assertNotNull(output, "Output cannot be null"); + assertNotNull(output->outMesh, "Output mesh cannot be null"); + assertNotNull(output->outVertices, "Output vertices cannot be null"); + + return assetLoad(path, &assetMeshLoader, NULL, output); +} + errorret_t assetMeshLoad( const char_t *path, mesh_t *outMesh, - meshvertex_t **outVertices + meshvertex_t **outVertices, + const assetmeshinputaxis_t inputAxis ) { assertNotNull(path, "Path cannot be null"); assertNotNull(outMesh, "Output mesh cannot be null"); assertNotNull(outVertices, "Output vertices cannot be null"); - assetmeshoutput_t output = { outMesh, outVertices }; - return assetLoad(path, &assetMeshLoader, NULL, &output); + assetmeshoutput_t output = { + outMesh, + outVertices, + inputAxis + }; + return assetMeshLoadToOutput(path, &output); } \ No newline at end of file diff --git a/src/dusk/asset/loader/display/assetmeshloader.h b/src/dusk/asset/loader/display/assetmeshloader.h index 6ea43e01..00af5837 100644 --- a/src/dusk/asset/loader/display/assetmeshloader.h +++ b/src/dusk/asset/loader/display/assetmeshloader.h @@ -10,9 +10,20 @@ #include "display/mesh/mesh.h" #include "assert/assert.h" +typedef enum { + MESH_INPUT_AXIS_Y_UP,// Default + MESH_INPUT_AXIS_Z_UP, + MESH_INPUT_AXIS_X_UP, + + MESH_INPUT_AXIS_Y_DOWN, + MESH_INPUT_AXIS_Z_DOWN, + MESH_INPUT_AXIS_X_DOWN, +} assetmeshinputaxis_t; + typedef struct { mesh_t *outMesh; meshvertex_t **outVertices; + assetmeshinputaxis_t inputAxis; } assetmeshoutput_t; #pragma pack(push, 1) @@ -37,10 +48,14 @@ errorret_t assetMeshLoader(assetfile_t *file); * Handler for mesh assets. * * @param file Asset file to load the mesh from. + * @param outMesh Output mesh to load the data into. + * @param outVertices Output pointer to the vertex data, used for cleanup. + * @param inputAxis The axis orientation of the input mesh data. * @return Any error that occurs during loading. */ errorret_t assetMeshLoad( const char_t *path, mesh_t *outMesh, - meshvertex_t **outVertices + meshvertex_t **outVertices, + const assetmeshinputaxis_t inputAxis ); \ No newline at end of file diff --git a/src/dusk/display/mesh/mesh.c b/src/dusk/display/mesh/mesh.c index 005a6d48..5306aaa5 100644 --- a/src/dusk/display/mesh/mesh.c +++ b/src/dusk/display/mesh/mesh.c @@ -8,6 +8,7 @@ #include "mesh.h" #include "util/memory.h" #include "assert/assert.h" +#include "util/math.h" errorret_t meshInit( mesh_t *mesh, @@ -75,6 +76,29 @@ errorret_t meshDraw( errorOk(); } +void meshGetBounds( + const mesh_t *mesh, + vec3 outMin, + vec3 outMax +) { + assertNotNull(mesh, "Mesh cannot be NULL"); + assertNotNull(outMin, "Output min cannot be NULL"); + assertNotNull(outMax, "Output max cannot be NULL"); + + for(int i = 0; i < 3; i++) { + outMin[i] = FLT_MAX; + outMax[i] = -FLT_MAX; + } + + for(uint32_t i = 0; i < mesh->vertexCount; i++) { + meshvertex_t vert = mesh->vertices[i]; + for(int j = 0; j < 3; j++) { + outMin[j] = mathMin(outMin[j], vert.pos[j]); + outMax[j] = mathMax(outMax[j], vert.pos[j]); + } + } +} + int32_t meshGetVertexCount(const mesh_t *mesh) { assertNotNull(mesh, "Mesh cannot be NULL"); return meshGetVertexCountPlatform(mesh); diff --git a/src/dusk/display/mesh/mesh.h b/src/dusk/display/mesh/mesh.h index da16166e..9ae3f7a3 100644 --- a/src/dusk/display/mesh/mesh.h +++ b/src/dusk/display/mesh/mesh.h @@ -70,6 +70,19 @@ errorret_t meshDraw( const int32_t vertexCount ); +/** + * Gets the axis-aligned bounding box of a mesh. + * + * @param mesh The mesh to get the bounds of. + * @param outMin Output parameter for the minimum corner of the bounding box. + * @param outMax Output parameter for the maximum corner of the bounding box. + */ +void meshGetBounds( + const mesh_t *mesh, + vec3 outMin, + vec3 outMax +); + /** * Gets the vertex count of a mesh. * diff --git a/src/dusk/engine/engine.c b/src/dusk/engine/engine.c index 75a39f8c..1e404fe2 100644 --- a/src/dusk/engine/engine.c +++ b/src/dusk/engine/engine.c @@ -52,8 +52,8 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) { // FOF entityid_t cam = entityManagerAdd(); componentid_t camPos = entityAddComponent(cam, COMPONENT_TYPE_POSITION); - float_t distance = 200.0f; - float_t up = 50.0f; + float_t distance = 1.5f; + float_t up = distance / 2.0f; entityPositionLookAt( cam, camPos, @@ -69,9 +69,18 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) { ent1Mesh = entityAddComponent(ent1, COMPONENT_TYPE_MESH); ent1Mat = entityAddComponent(ent1, COMPONENT_TYPE_MATERIAL); - errorChain(assetMeshLoad("test/test.stl", &loadedMesh, &loadedVertices)); + errorChain(assetMeshLoad( + "test/Mei.stl", + &loadedMesh, + &loadedVertices, + MESH_INPUT_AXIS_Y_UP + )); entityMeshSetMesh(ent1, ent1Mesh, &loadedMesh); + vec3 min, max; + meshGetBounds(&loadedMesh, min, max); + printf("Mesh bounds: min(%f, %f, %f), max(%f, %f, %f)\n", min[0], min[1], min[2], max[0], max[1], max[2]); + shadermaterial_t *mat = entityMaterialGetShaderMaterial(ent1, ent1Mat); mat->unlit.color = COLOR_WHITE;