summaryrefslogtreecommitdiff
path: root/include/model.h
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2026-01-31 16:12:21 -0800
committer3gg <3gg@shellblade.net>2026-01-31 16:12:21 -0800
commitcd6d2340a6fcbd2376aad291081ae5d667bd3317 (patch)
tree72e2b03314f0a49a7a2f25a1b1a249569af6c679 /include/model.h
parentbe25789773aaae89dcfeee2816a3dfce29753981 (diff)
Add a ground. Add support for multiple objects and materials in MDL
Diffstat (limited to 'include/model.h')
-rw-r--r--include/model.h60
1 files changed, 56 insertions, 4 deletions
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 @@
1#pragma once 1#pragma once
2 2
3#include <assert.h>
3#include <stddef.h> 4#include <stddef.h>
4#include <stdint.h> 5#include <stdint.h>
5 6
6constexpr size_t ModelPathLen = 256; 7constexpr size_t ModelPathLen = 256;
8constexpr size_t ModelNameLen = 64;
9constexpr size_t ModelMaxObjects = 256; // uint8_t
10constexpr size_t ModelMaxMaterials = 256; // uint8_t
11constexpr size_t ModelMaxVerts = 4294967296; // uint32_t
12constexpr size_t ModelMaxFaces = 4294967296; // uint32_t
7 13
8typedef uint16_t mdIdx; 14typedef uint16_t mdIdx;
9typedef struct mdVert { mdIdx position, texcoord, normal; } mdVert; 15typedef struct mdVert { mdIdx position, texcoord, normal; } mdVert;
@@ -11,9 +17,18 @@ typedef struct mdTri { mdVert v0, v1, v2; } mdTri;
11typedef struct mdVec2 { float x, y; } mdVec2; 17typedef struct mdVec2 { float x, y; } mdVec2;
12typedef struct mdVec3 { float x, y, z; } mdVec3; 18typedef struct mdVec3 { float x, y, z; } mdVec3;
13 19
14typedef struct Material { 20typedef struct ModelObject {
21 uint32_t offset; // FlatModel: offset into indices. IndexedModel: offset into tris.
22 uint32_t count; // FloatModel: number of indices. IndexedModel: number of tris.
23 uint8_t material; // Material index.
24 uint8_t pad[3];
25 char name[ModelNameLen];
26} ModelObject;
27
28typedef struct ModelMaterial {
29 char name[ModelNameLen];
15 char diffuseTexture[ModelPathLen]; 30 char diffuseTexture[ModelPathLen];
16} Material; 31} ModelMaterial;
17 32
18// Every three indices form a triangle, and each index indexes all attribute 33// Every three indices form a triangle, and each index indexes all attribute
19// arrays simultaneously. This is best for a fast, linear-scan rendering. 34// arrays simultaneously. This is best for a fast, linear-scan rendering.
@@ -28,6 +43,8 @@ typedef struct FlatModel {
28 uint32_t offsetTexcoords; 43 uint32_t offsetTexcoords;
29 uint32_t offsetNormals; 44 uint32_t offsetNormals;
30 /* 45 /*
46 [objects] -- numObjects Object
47 [materials] -- numMaterials Material
31 [indices] -- numIdxs mdIdx 48 [indices] -- numIdxs mdIdx
32 [positions] -- numVerts mdVec3 49 [positions] -- numVerts mdVec3
33 [texcoords] -- numVerts mdVec2 50 [texcoords] -- numVerts mdVec2
@@ -53,6 +70,8 @@ typedef struct IndexedModel {
53 uint32_t offsetTexcoords; 70 uint32_t offsetTexcoords;
54 uint32_t offsetNormals; 71 uint32_t offsetNormals;
55 /* 72 /*
73 [objects] -- numObjects Object
74 [materials] -- numMaterials Material
56 [triangles] -- numTris mdTri 75 [triangles] -- numTris mdTri
57 [positions] -- numPositions mdVec3 76 [positions] -- numPositions mdVec3
58 [texcoords] -- numTexcoords mdVec2 77 [texcoords] -- numTexcoords mdVec2
@@ -68,9 +87,42 @@ typedef enum ModelType {
68 87
69typedef struct Model { 88typedef struct Model {
70 uint32_t type; 89 uint32_t type;
71 Material material; 90 // Counts.
91 uint8_t numObjects;
92 uint8_t numMaterials;
93 uint8_t pad[2];
94 // Offsets.
95 uint32_t offsetObjects;
96 uint32_t offsetMaterials;
97 // Model details.
72 union { 98 union {
73 FlatModel flat; 99 FlatModel flat;
74 IndexedModel indexed; 100 IndexedModel indexed;
75 }; 101 };
76} Model; 102} Model;
103
104static inline const ModelObject* modelObjects(const Model* model) {
105 assert(model);
106 switch (model->type) {
107 case ModelTypeIndexed:
108 return (const ModelObject*)(model->indexed.data + model->offsetObjects);
109 case ModelTypeFlat:
110 return (const ModelObject*)(model->flat.data + model->offsetObjects);
111 default:
112 assert(false);
113 break;
114 }
115}
116
117static inline const ModelMaterial* modelMaterials(const Model* model) {
118 assert(model);
119 switch (model->type) {
120 case ModelTypeIndexed:
121 return (const ModelMaterial*)(model->indexed.data + model->offsetMaterials);
122 case ModelTypeFlat:
123 return (const ModelMaterial*)(model->flat.data + model->offsetMaterials);
124 default:
125 assert(false);
126 break;
127 }
128}