Allow reaxising mesh

This commit is contained in:
2026-04-13 12:58:54 -05:00
parent fd82486431
commit bae1ff3759
5 changed files with 132 additions and 11 deletions
@@ -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);
}
@@ -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
);
+24
View File
@@ -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);
+13
View File
@@ -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.
*
+12 -3
View File
@@ -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;