Tiny3D
Loading...
Searching...
No Matches
t3dmodel.h
Go to the documentation of this file.
1
6#ifndef TINY3D_T3DMODEL_H
7#define TINY3D_T3DMODEL_H
8
9#include "t3d.h"
10
11#ifdef __cplusplus
12extern "C"
13{
14#endif
15
16#define T3D_ALPHA_MODE_DEFAULT 0
17#define T3D_ALPHA_MODE_OPAQUE 1
18#define T3D_ALPHA_MODE_CUTOUT 2
19#define T3D_ALPHA_MODE_TRANSP 3
20
21#define T3D_FOG_MODE_DEFAULT 0
22#define T3D_FOG_MODE_DISABLED 1
23#define T3D_FOG_MODE_ACTIVE 2
24
25typedef struct {
26 float low;
27 float height;
28 int8_t mask;
29 int8_t shift;
30 uint8_t mirror;
31 uint8_t clamp;
33
34typedef struct {
35 uint32_t texReference; // dynamic/offscreen texture if non-zero, can be set in fast64
36 char* texPath;
37 uint32_t textureHash;
38 sprite_t* texture;
39 uint16_t texWidth;
40 uint16_t texHeight;
41
45
46typedef struct {
47 uint64_t colorCombiner;
48 uint64_t otherModeValue;
49 uint64_t otherModeMask;
50 uint32_t blendMode;
51 uint32_t renderFlags;
52
53 uint8_t _unused00_; // see: T3D_ALPHA_MODE_xxx
54 uint8_t fogMode; // see: T3D_FOG_MODE_xxx
55 uint8_t setColorFlags;
56 uint8_t vertexFxFunc;
57
58 color_t primColor;
59 color_t envColor;
60 color_t blendColor;
61
62 char* name;
63 T3DMaterialTexture textureA;
64 T3DMaterialTexture textureB;
66
67typedef struct {
68 T3DVertPacked *vert;
69 uint16_t vertLoadCount;
70 uint16_t vertDestOffset;
71
72 uint8_t *indices;
73 uint16_t numIndices;
74 uint16_t matrixIdx;
75 uint8_t numStripIndices[4];
76 uint8_t idxSeqBase;
77 uint8_t idxSeqCount;
78 uint8_t _padding[2];
79
81
82typedef struct {
83 char* name;
84 uint16_t numParts;
85 uint16_t triCount;
86 T3DMaterial* material;
87 // can be used freely by the user for recording, will be freed automatically by t3d
88 rspq_block_t *userBlock;
89 uint8_t isVisible; // set by culling checks, otherwise no effect on rendering
90 uint8_t _padding;
91 uint8_t userValue0; // free values usable by users
92 uint8_t userValue1; // free values usable by users
93 int16_t aabbMin[3];
94 int16_t aabbMax[3];
95
96 T3DObjectPart parts[]; // real array
97} T3DObject;
98
99typedef struct {
100 int16_t aabbMin[3];
101 int16_t aabbMax[3];
102 uint16_t value;
103} T3DBvhNode;
104
105typedef struct {
106 uint16_t nodeCount;
107 uint16_t dataCount;
108 T3DBvhNode nodes[];
109 // uint16_t data[]; // T3DObject pointer, shifted by 3, relative to 'objectBasePtr'
110} T3DBvh;
111
112typedef struct {
113 char* name;
114 uint16_t parentIdx;
115 uint16_t depth;
116 T3DVec3 scale;
117 T3DQuat rotation;
118 T3DVec3 position;
120
121typedef struct {
122 uint16_t boneCount;
123 uint16_t _reserved;
124 T3DChunkBone bones[];
126
127typedef struct {
128 uint16_t targetIdx;
129 uint8_t targetType;
130 uint8_t attributeIdx;
131 float quantScale;
132 float quantOffset;
134
135typedef struct {
136 char* name;
137 float duration;
138 uint32_t keyframeCount;
139 uint16_t channelsQuat;
140 uint16_t channelsScalar;
141 char* filePath;
142 T3DAnimChannelMapping channelMappings[];
144
145typedef union {
146 char type;
147 uint32_t offset;
149
150typedef struct {
151 char magic[4];
152 uint32_t chunkCount;
153
154 uint16_t totalVertCount;
155 uint16_t totalIndexCount;
156
157 uint32_t chunkIdxVertices;
158 uint32_t chunkIdxIndices;
159 uint32_t chunkIdxMaterials;
160 char* stringTablePtr;
161
162 // can be used freely by the user for recording, will be freed automatically by t3d
163 rspq_block_t *userBlock;
164
165 int16_t aabbMin[3];
166 int16_t aabbMax[3];
167
168 T3DChunkOffset chunkOffsets[];
169} T3DModel;
170
171typedef struct {
172 union {
173 void* chunk;
174 T3DObject *object;
175 T3DMaterial *material;
176 T3DChunkSkeleton *skeleton;
177 T3DChunkAnim *anim;
178 };
179
180 const T3DModel *_model;
181 uint16_t _idx;
182 char _chunkType;
184
185// Types of chunks contained in T3DModel.
186enum T3DModelChunkType {
187 T3D_CHUNK_TYPE_VERTICES = 'V',
188 T3D_CHUNK_TYPE_INDICES = 'I',
189 T3D_CHUNK_TYPE_MATERIAL = 'M',
190 T3D_CHUNK_TYPE_OBJECT = 'O',
191 T3D_CHUNK_TYPE_SKELETON = 'S',
192 T3D_CHUNK_TYPE_ANIM = 'A',
193 T3D_CHUNK_TYPE_BVH = 'B'
194};
195
203T3DModel* t3d_model_load(const char *path);
204
205// callback for custom drawing, this hooks into the tile-setting section
206typedef void (*T3DModelTileCb)(void* userData, rdpq_texparms_t *tileParams, rdpq_tile_t tile);
207typedef bool (*T3DModelFilterCb)(void* userData, const T3DObject *obj);
208typedef void (*T3DModelDynTextureCb)(
209 void* userData, const T3DMaterial *material, rdpq_texparms_t *tileParams, rdpq_tile_t tile
210);
211
212// Defines settings and callbacks for custom drawing
213typedef struct {
214 void* userData;
215 T3DModelTileCb tileCb; // callback to modify tile settings
216 T3DModelFilterCb filterCb; // callback to filter parts
217 T3DModelDynTextureCb dynTextureCb; // callback to set dynamic textures, aka "Texture Reference" in fast64
218 const T3DMat4FP *matrices;
220
225typedef struct {
226 uint32_t lastTextureHashA;
227 uint32_t lastTextureHashB;
228 uint8_t lastFogMode;
229 uint32_t lastRenderFlags;
230 uint64_t lastCC;
231 color_t lastPrimColor;
232 color_t lastEnvColor;
233 color_t lastBlendColor;
234 uint8_t lastVertFXFunc;
235 uint16_t lastUvGenParams[2];
236 uint64_t lastOtherMode;
237 uint32_t lastBlendMode;
238 T3DModelDrawConf* drawConf; // @TODO: legacy, remove at some point
240
246 return (T3DModelState){
247 .lastFogMode = 0xFF,
248 .lastVertFXFunc = T3D_VERTEX_FX_NONE,
249 .lastOtherMode = 0xFF,
250 .lastBlendMode = 0xFFFFFFFF
251 };
252}
253
254
259void t3d_model_free(T3DModel* model);
260
267void t3d_model_draw_custom(const T3DModel* model, T3DModelDrawConf conf);
268
274static inline void t3d_model_draw(const T3DModel* model) {
276 .userData = NULL,
277 .tileCb = NULL,
278 .filterCb = NULL
279 });
280}
281
292void t3d_model_draw_object(const T3DObject *object, const T3DMat4FP *boneMatrices);
293
306
319static inline T3DVertPacked* t3d_model_get_vertices(const T3DModel *model) {
320 uint32_t offset = model->chunkOffsets[model->chunkIdxVertices].offset & 0x00FFFFFF;
321 return (T3DVertPacked*)((char*)model + offset);
322}
323
338void t3d_model_make_object_vert_placeholder(const T3DModel *model, T3DObject *object, uint8_t segmentId);
339
347static inline const T3DChunkSkeleton* t3d_model_get_skeleton(const T3DModel *model) {
348 for(uint32_t i = 0; i < model->chunkCount; i++) {
349 if(model->chunkOffsets[i].type == T3D_CHUNK_TYPE_SKELETON) {
350 uint32_t offset = model->chunkOffsets[i].offset & 0x00FFFFFF;
351 return (T3DChunkSkeleton*)((char*)model + offset);
352 }
353 }
354 return NULL;
355}
356
362static inline uint32_t t3d_model_get_animation_count(const T3DModel *model) {
363 uint32_t count = 0;
364 for(uint32_t i = 0; i < model->chunkCount; i++) {
365 if(model->chunkOffsets[i].type == T3D_CHUNK_TYPE_ANIM)count++;
366 }
367 return count;
368}
369
376void t3d_model_get_animations(const T3DModel *model, T3DChunkAnim **anims);
377
386T3DChunkAnim* t3d_model_get_animation(const T3DModel *model, const char* name);
387
394T3DObject* t3d_model_get_object(const T3DModel *model, const char *name);
395
403static inline T3DObject* t3d_model_get_object_by_index(const T3DModel *model, uint32_t index) {
404 uint32_t offset = model->chunkOffsets[index].offset & 0x00FFFFFF;
405 return (T3DObject*)((char*)model + offset);
406}
407
414T3DMaterial* t3d_model_get_material(const T3DModel *model, const char *name);
415
438static inline T3DModelIter t3d_model_iter_create(const T3DModel *model, enum T3DModelChunkType chunkType) {
439 return (T3DModelIter){
440 .chunk = NULL,
441 ._model = model,
442 ._idx = 0,
443 ._chunkType = chunkType,
444 };
445}
446
459
467static inline const T3DBvh* t3d_model_bvh_get(const T3DModel *model) {
468 for(uint32_t i = 0; i < model->chunkCount; i++) {
469 if(model->chunkOffsets[i].type == T3D_CHUNK_TYPE_BVH) {
470 uint32_t offset = model->chunkOffsets[i].offset & 0x00FFFFFF;
471 return (T3DBvh*)((char*)model + offset);
472 }
473 }
474 return NULL;
475}
476
487void t3d_model_bvh_query_frustum(const T3DBvh *bvh, const T3DFrustum *frustum);
488
489#ifdef __cplusplus
490}
491#endif
492
493#endif
Definition t3dmodel.h:127
Definition t3dmodel.h:99
Definition t3dmodel.h:105
Definition t3dmodel.h:135
Definition t3dmodel.h:112
Definition t3dmodel.h:121
Definition t3dmath.h:38
Definition t3dmodel.h:25
Definition t3dmodel.h:34
Definition t3dmodel.h:46
Definition t3dmodel.h:213
Definition t3dmodel.h:171
Definition t3dmodel.h:225
Definition t3dmodel.h:150
Definition t3dmodel.h:67
Definition t3dmodel.h:82
void t3d_model_free(T3DModel *model)
Definition t3dmodel.c:452
static T3DModelIter t3d_model_iter_create(const T3DModel *model, enum T3DModelChunkType chunkType)
Definition t3dmodel.h:438
T3DChunkAnim * t3d_model_get_animation(const T3DModel *model, const char *name)
Definition t3dmodel.c:482
void t3d_model_bvh_query_frustum(const T3DBvh *bvh, const T3DFrustum *frustum)
Definition t3dmodel.c:564
static T3DVertPacked * t3d_model_get_vertices(const T3DModel *model)
Definition t3dmodel.h:319
static const T3DChunkSkeleton * t3d_model_get_skeleton(const T3DModel *model)
Definition t3dmodel.h:347
void t3d_model_make_object_vert_placeholder(const T3DModel *model, T3DObject *object, uint8_t segmentId)
Definition t3dmodel.c:295
T3DObject * t3d_model_get_object(const T3DModel *model, const char *name)
Definition t3dmodel.c:493
T3DMaterial * t3d_model_get_material(const T3DModel *model, const char *name)
Definition t3dmodel.c:514
bool t3d_model_iter_next(T3DModelIter *iter)
Definition t3dmodel.c:525
T3DModel * t3d_model_load(const char *path)
Definition t3dmodel.c:185
static T3DObject * t3d_model_get_object_by_index(const T3DModel *model, uint32_t index)
Definition t3dmodel.h:403
static T3DModelState t3d_model_state_create()
Definition t3dmodel.h:245
void t3d_model_draw_object(const T3DObject *object, const T3DMat4FP *boneMatrices)
Definition t3dmodel.c:305
void t3d_model_get_animations(const T3DModel *model, T3DChunkAnim **anims)
Definition t3dmodel.c:504
void t3d_model_draw_custom(const T3DModel *model, T3DModelDrawConf conf)
Definition t3dmodel.c:274
void t3d_model_draw_material(T3DMaterial *mat, T3DModelState *state)
Definition t3dmodel.c:364
static void t3d_model_draw(const T3DModel *model)
Definition t3dmodel.h:274
static const T3DBvh * t3d_model_bvh_get(const T3DModel *model)
Definition t3dmodel.h:467
static uint32_t t3d_model_get_animation_count(const T3DModel *model)
Definition t3dmodel.h:362
Definition t3dmodel.h:145