diff options
Diffstat (limited to 'include/model.h')
| -rw-r--r-- | include/model.h | 60 |
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 | ||
| 6 | constexpr size_t ModelPathLen = 256; | 7 | constexpr size_t ModelPathLen = 256; |
| 8 | constexpr size_t ModelNameLen = 64; | ||
| 9 | constexpr size_t ModelMaxObjects = 256; // uint8_t | ||
| 10 | constexpr size_t ModelMaxMaterials = 256; // uint8_t | ||
| 11 | constexpr size_t ModelMaxVerts = 4294967296; // uint32_t | ||
| 12 | constexpr size_t ModelMaxFaces = 4294967296; // uint32_t | ||
| 7 | 13 | ||
| 8 | typedef uint16_t mdIdx; | 14 | typedef uint16_t mdIdx; |
| 9 | typedef struct mdVert { mdIdx position, texcoord, normal; } mdVert; | 15 | typedef struct mdVert { mdIdx position, texcoord, normal; } mdVert; |
| @@ -11,9 +17,18 @@ typedef struct mdTri { mdVert v0, v1, v2; } mdTri; | |||
| 11 | typedef struct mdVec2 { float x, y; } mdVec2; | 17 | typedef struct mdVec2 { float x, y; } mdVec2; |
| 12 | typedef struct mdVec3 { float x, y, z; } mdVec3; | 18 | typedef struct mdVec3 { float x, y, z; } mdVec3; |
| 13 | 19 | ||
| 14 | typedef struct Material { | 20 | typedef 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 | |||
| 28 | typedef 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 | ||
| 69 | typedef struct Model { | 88 | typedef 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 | |||
| 104 | static 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 | |||
| 117 | static 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 | } | ||
