Tiny3D
Loading...
Searching...
No Matches
t3dskeleton.h
Go to the documentation of this file.
1
6#ifndef TINY3D_T3DSKELETON_H
7#define TINY3D_T3DSKELETON_H
8
9#include "t3dmodel.h"
10
11#ifdef __cplusplus
12extern "C"
13{
14#endif
15
21typedef struct {
22 T3DMat4x3 matrix;
23 T3DVec3 scale;
24 T3DQuat rotation;
25 T3DVec3 position;
26 int32_t hasChanged;
27 uint16_t parentIdx; // copy of the model's bone definition, avoids touching it in 't3d_skeleton_update'
28 uint16_t depth;
29} __attribute__((aligned(16))) T3DBone;
30
35typedef struct {
36 T3DBone* bones;
37 T3DMat4FP* boneMatricesFP; // fixed point matrix, used for rendering
38 uint8_t bufferCount; // number of matrices buffers
39 uint8_t currentBufferIdx;
40 const T3DChunkSkeleton* skeletonRef; // reference to the model, defines skeleton structure
42
53T3DSkeleton t3d_skeleton_create_buffered(const T3DModel *model, int bufferCount);
54
60static inline T3DSkeleton t3d_skeleton_create(const T3DModel *model) {
61 return t3d_skeleton_create_buffered(model, 1);
62}
63
69static inline void t3d_skeleton_use(const T3DSkeleton *skel) {
70 if(skel->bufferCount > 1) {
71 void* mat = skel->boneMatricesFP + skel->currentBufferIdx * skel->skeletonRef->boneCount;
72 t3d_segment_set(T3D_SEGMENT_SKELETON, mat);
73 }
74}
75
83T3DSkeleton t3d_skeleton_clone(const T3DSkeleton *skel, bool useMatrices);
84
91void t3d_skeleton_reset(T3DSkeleton *skeleton);
92
101void t3d_skeleton_blend(const T3DSkeleton *skelRes, const T3DSkeleton *skelA, const T3DSkeleton *skelB, float factor);
102
109void t3d_skeleton_update(T3DSkeleton *skeleton);
110
116void t3d_skeleton_destroy(T3DSkeleton* skeleton);
117
125int t3d_skeleton_find_bone(T3DSkeleton* skeleton, const char* name);
126
134static inline T3DVec3 t3d_skeleton_get_bone_pos_model_space(const T3DSkeleton *skeleton, int boneIdx){
135 assertf(boneIdx >= 0 && boneIdx < skeleton->skeletonRef->boneCount, "Bone index is out of bounds, idx: %i, boneCount: %i", boneIdx, skeleton->skeletonRef->boneCount);
136 // Access the bone at the given index
137 const T3DBone *bone = &skeleton->bones[boneIdx];
138
139 // Extract the position from the matrix.
140 return (T3DVec3) {{
141 bone->matrix.m[3][0],
142 bone->matrix.m[3][1],
143 bone->matrix.m[3][2]
144 }};
145}
146
152static inline void t3d_model_draw_skinned(const T3DModel* model, const T3DSkeleton* skeleton) {
153 t3d_model_draw_custom(model, (T3DModelDrawConf){
154 .userData = NULL,
155 .tileCb = NULL,
156 .filterCb = NULL,
157 .matrices = skeleton->bufferCount == 1
158 ? skeleton->boneMatricesFP
159 : (const T3DMat4FP*)t3d_segment_placeholder(T3D_SEGMENT_SKELETON)
160 });
161}
162
163#ifdef __cplusplus
164}
165#endif
166
167#endif //TINY3D_T3DSKELETON_H
Definition t3dmodel.h:121
Definition t3dmath.h:27
Definition t3dmodel.h:213
Definition t3dmodel.h:150
Definition t3dskeleton.h:35
Definition t3d.h:46
static void * t3d_segment_placeholder(uint8_t segmentId)
Definition t3d.h:687
void t3d_skeleton_blend(const T3DSkeleton *skelRes, const T3DSkeleton *skelA, const T3DSkeleton *skelB, float factor)
Definition t3dskeleton.c:126
static T3DVec3 t3d_skeleton_get_bone_pos_model_space(const T3DSkeleton *skeleton, int boneIdx)
Definition t3dskeleton.h:134
void t3d_skeleton_update(T3DSkeleton *skeleton)
Definition t3dskeleton.c:139
int t3d_skeleton_find_bone(T3DSkeleton *skeleton, const char *name)
Definition t3dskeleton.c:191
void t3d_skeleton_reset(T3DSkeleton *skeleton)
Definition t3dskeleton.c:114
static void t3d_model_draw_skinned(const T3DModel *model, const T3DSkeleton *skeleton)
Definition t3dskeleton.h:152
void t3d_skeleton_destroy(T3DSkeleton *skeleton)
Definition t3dskeleton.c:200
T3DSkeleton t3d_skeleton_create_buffered(const T3DModel *model, int bufferCount)
Definition t3dskeleton.c:82
T3DSkeleton t3d_skeleton_clone(const T3DSkeleton *skel, bool useMatrices)
Definition t3dskeleton.c:98
static void t3d_skeleton_use(const T3DSkeleton *skel)
Definition t3dskeleton.h:69
static T3DSkeleton t3d_skeleton_create(const T3DModel *model)
Definition t3dskeleton.h:60