-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton.h
86 lines (72 loc) · 1.82 KB
/
skeleton.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once
#include <stdint.h>
#include <stdio.h>
#include <cassert>
#include <string.h>
#include "linear_math/matrix.h"
//#define BONE_NAME_LENGTH 80
//#define SKELETON_MAX_BONE_COUNT 200
#define BONE_NAME_LENGTH 40
#define SKELETON_MAX_BONE_COUNT 70
namespace Anim
{
struct bone
{
char Name[BONE_NAME_LENGTH];
mat4 BindPose;
mat4 InverseBindPose;
int32_t ParentIndex;
};
struct skeleton // root is always the 0'th bone
{
int32_t BoneCount;
bone Bones[SKELETON_MAX_BONE_COUNT];
};
void PrintSkeleton(const skeleton* Skeleton);
void AddBone(Anim::skeleton* Skeleton, Anim::bone Bone);
int32_t GetBoneIndexFromName(const skeleton* Skeleton, const char* Name);
inline void
AddBone(Anim::skeleton* Skeleton, Anim::bone Bone)
{
assert(Skeleton->BoneCount < SKELETON_MAX_BONE_COUNT);
assert(Skeleton->BoneCount >= 0);
Skeleton->Bones[Skeleton->BoneCount++] = Bone;
}
inline int
r_GetBoneDepth(const Anim::skeleton* Skeleton, int BoneIndex)
{
assert(BoneIndex >= 0 && BoneIndex < Skeleton->BoneCount);
int ParentIndex = Skeleton->Bones[BoneIndex].ParentIndex;
if(ParentIndex < 0)
{
return 0;
}
return 1 + r_GetBoneDepth(Skeleton, ParentIndex);
}
inline void
PrintSkeleton(const skeleton* Skeleton)
{
for(int i = 0; i < Skeleton->BoneCount; i++)
{
const bone* Bone = &Skeleton->Bones[i];
int BoneDepth = r_GetBoneDepth(Skeleton, i);
for(int d = 0; d < BoneDepth; d++)
{
printf(" ");
}
printf("%d: %s\n", i, Bone->Name);
}
}
inline int32_t
GetBoneIndexFromName(const Anim::skeleton* Skeleton, const char* Name)
{
for(int i = 0; i < Skeleton->BoneCount; i++)
{
if(strcmp(Skeleton->Bones[i].Name, Name) == 0)
{
return i;
}
}
return -1;
}
}