From cd6d2340a6fcbd2376aad291081ae5d667bd3317 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 31 Jan 2026 16:12:21 -0800 Subject: Add a ground. Add support for multiple objects and materials in MDL --- include/model.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/model.h b/include/model.h index 0730809..10c9cdf 100644 --- a/include/model.h +++ b/include/model.h @@ -1,9 +1,15 @@ #pragma once +#include #include #include -constexpr size_t ModelPathLen = 256; +constexpr size_t ModelPathLen = 256; +constexpr size_t ModelNameLen = 64; +constexpr size_t ModelMaxObjects = 256; // uint8_t +constexpr size_t ModelMaxMaterials = 256; // uint8_t +constexpr size_t ModelMaxVerts = 4294967296; // uint32_t +constexpr size_t ModelMaxFaces = 4294967296; // uint32_t typedef uint16_t mdIdx; typedef struct mdVert { mdIdx position, texcoord, normal; } mdVert; @@ -11,9 +17,18 @@ typedef struct mdTri { mdVert v0, v1, v2; } mdTri; typedef struct mdVec2 { float x, y; } mdVec2; typedef struct mdVec3 { float x, y, z; } mdVec3; -typedef struct Material { +typedef struct ModelObject { + uint32_t offset; // FlatModel: offset into indices. IndexedModel: offset into tris. + uint32_t count; // FloatModel: number of indices. IndexedModel: number of tris. + uint8_t material; // Material index. + uint8_t pad[3]; + char name[ModelNameLen]; +} ModelObject; + +typedef struct ModelMaterial { + char name[ModelNameLen]; char diffuseTexture[ModelPathLen]; -} Material; +} ModelMaterial; // Every three indices form a triangle, and each index indexes all attribute // arrays simultaneously. This is best for a fast, linear-scan rendering. @@ -28,6 +43,8 @@ typedef struct FlatModel { uint32_t offsetTexcoords; uint32_t offsetNormals; /* + [objects] -- numObjects Object + [materials] -- numMaterials Material [indices] -- numIdxs mdIdx [positions] -- numVerts mdVec3 [texcoords] -- numVerts mdVec2 @@ -53,6 +70,8 @@ typedef struct IndexedModel { uint32_t offsetTexcoords; uint32_t offsetNormals; /* + [objects] -- numObjects Object + [materials] -- numMaterials Material [triangles] -- numTris mdTri [positions] -- numPositions mdVec3 [texcoords] -- numTexcoords mdVec2 @@ -68,9 +87,42 @@ typedef enum ModelType { typedef struct Model { uint32_t type; - Material material; + // Counts. + uint8_t numObjects; + uint8_t numMaterials; + uint8_t pad[2]; + // Offsets. + uint32_t offsetObjects; + uint32_t offsetMaterials; + // Model details. union { FlatModel flat; IndexedModel indexed; }; } Model; + +static inline const ModelObject* modelObjects(const Model* model) { + assert(model); + switch (model->type) { + case ModelTypeIndexed: + return (const ModelObject*)(model->indexed.data + model->offsetObjects); + case ModelTypeFlat: + return (const ModelObject*)(model->flat.data + model->offsetObjects); + default: + assert(false); + break; + } +} + +static inline const ModelMaterial* modelMaterials(const Model* model) { + assert(model); + switch (model->type) { + case ModelTypeIndexed: + return (const ModelMaterial*)(model->indexed.data + model->offsetMaterials); + case ModelTypeFlat: + return (const ModelMaterial*)(model->flat.data + model->offsetMaterials); + default: + assert(false); + break; + } +} -- cgit v1.2.3