SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
Skeleton.hpp
Go to the documentation of this file.
1
#ifndef _SKELETON_HPP_
2
#define _SKELETON_HPP_
3
4
#include <
Core/OSDef.hpp
>
5
#include <
Math/Matrix.hpp
>
6
#include <string>
7
#include <vector>
8
#include <unordered_map>
9
10
namespace
Sleak
{
11
12
static
constexpr
int
MAX_BONES
= 256;
13
static
constexpr
int
MAX_BONE_INFLUENCE
= 4;
14
15
/// One skinning joint: its offset matrix and parent link within the skeleton.
16
/// @ingroup animation
17
struct
Bone
{
18
std::string
name
;
19
int
id
;
20
int
parentId
= -1;
21
Math::Matrix4
offsetMatrix
;
// mesh space -> bone space
22
};
23
24
/// Full scene node (bones + non-bone nodes).
25
/// @ingroup animation
26
struct
NodeData
{
27
std::string
name
;
28
Math::Matrix4
defaultTransform
;
// node's local transform from scene graph
29
int
boneIndex
= -1;
// index into bone array, -1 if not a bone
30
std::vector<int>
children
;
// indices into node array
31
};
32
33
/// Bone hierarchy plus the full imported scene node tree, used to drive
34
/// skinned mesh animation.
35
/// @ingroup animation
36
class
ENGINE_API
Skeleton
{
37
public
:
38
Skeleton
() : m_globalInverseTransform(
Math
::
Matrix4
::Identity()) {}
39
40
int
GetBoneCount
()
const
{
return
static_cast<
int
>
(m_bones.size()); }
41
42
int
FindBoneId
(
const
std::string& name)
const
{
43
auto
it = m_boneNameToId.find(name);
44
return
(it != m_boneNameToId.end()) ? it->second : -1;
45
}
46
47
const
Bone
&
GetBone
(
int
id
)
const
{
return
m_bones[id]; }
48
49
/// Appends bone, assigning it the next bone id.
50
int
AddBone
(
const
Bone
& bone) {
51
int
id
=
static_cast<
int
>
(m_bones.size());
52
Bone
b = bone;
53
b.
id
= id;
54
m_boneNameToId[b.
name
] = id;
55
m_bones.push_back(b);
56
return
id;
57
}
58
59
const
Math::Matrix4
&
GetGlobalInverseTransform
()
const
{
60
return
m_globalInverseTransform;
61
}
62
63
void
SetGlobalInverseTransform
(
const
Math::Matrix4
& mat) {
64
m_globalInverseTransform = mat;
65
}
66
67
/// Appends node, returning its index in the node array.
68
int
AddNode
(
const
NodeData
& node) {
69
int
idx =
static_cast<
int
>
(m_nodes.size());
70
m_nodes.push_back(node);
71
m_nodeNameToIndex[node.
name
] = idx;
72
return
idx;
73
}
74
75
/// Links childIdx under parentIdx in the node tree.
76
void
AddNodeChild
(
int
parentIdx,
int
childIdx) {
77
if
(parentIdx >= 0 && parentIdx <
static_cast<
int
>
(m_nodes.size()))
78
m_nodes[parentIdx].children.push_back(childIdx);
79
}
80
81
int
GetNodeCount
()
const
{
return
static_cast<
int
>
(m_nodes.size()); }
82
const
NodeData
&
GetNode
(
int
idx)
const
{
return
m_nodes[idx]; }
83
84
int
GetRootNodeIndex
()
const
{
return
m_nodes.empty() ? -1 : 0; }
85
86
int
FindNodeIndex
(
const
std::string& name)
const
{
87
auto
it = m_nodeNameToIndex.find(name);
88
return
(it != m_nodeNameToIndex.end()) ? it->second : -1;
89
}
90
91
bool
HasNodeTree
()
const
{
return
!m_nodes.empty(); }
92
93
/// Reassigns the parent of boneId, e.g. once the full hierarchy is known.
94
void
SetBoneParent
(
int
boneId,
int
parentId) {
95
if
(boneId >= 0 && boneId <
static_cast<
int
>
(m_bones.size()))
96
m_bones[boneId].parentId = parentId;
97
}
98
99
private
:
100
std::vector<Bone> m_bones;
101
std::unordered_map<std::string, int> m_boneNameToId;
102
Math::Matrix4
m_globalInverseTransform;
103
104
// Full scene node tree
105
std::vector<NodeData> m_nodes;
106
std::unordered_map<std::string, int> m_nodeNameToIndex;
107
};
108
109
}
// namespace Sleak
110
111
#endif
// _SKELETON_HPP_
Matrix.hpp
OSDef.hpp
Sleak::Skeleton::SetBoneParent
void SetBoneParent(int boneId, int parentId)
Reassigns the parent of boneId, e.g. once the full hierarchy is known.
Definition
Skeleton.hpp:94
Sleak::Skeleton::Skeleton
Skeleton()
Definition
Skeleton.hpp:38
Sleak::Skeleton::GetNode
const NodeData & GetNode(int idx) const
Definition
Skeleton.hpp:82
Sleak::Skeleton::GetBoneCount
int GetBoneCount() const
Definition
Skeleton.hpp:40
Sleak::Skeleton::AddNodeChild
void AddNodeChild(int parentIdx, int childIdx)
Links childIdx under parentIdx in the node tree.
Definition
Skeleton.hpp:76
Sleak::Skeleton::AddBone
int AddBone(const Bone &bone)
Appends bone, assigning it the next bone id.
Definition
Skeleton.hpp:50
Sleak::Skeleton::AddNode
int AddNode(const NodeData &node)
Appends node, returning its index in the node array.
Definition
Skeleton.hpp:68
Sleak::Skeleton::FindNodeIndex
int FindNodeIndex(const std::string &name) const
Definition
Skeleton.hpp:86
Sleak::Skeleton::FindBoneId
int FindBoneId(const std::string &name) const
Definition
Skeleton.hpp:42
Sleak::Skeleton::GetBone
const Bone & GetBone(int id) const
Definition
Skeleton.hpp:47
Sleak::Skeleton::GetRootNodeIndex
int GetRootNodeIndex() const
Definition
Skeleton.hpp:84
Sleak::Skeleton::SetGlobalInverseTransform
void SetGlobalInverseTransform(const Math::Matrix4 &mat)
Definition
Skeleton.hpp:63
Sleak::Skeleton::HasNodeTree
bool HasNodeTree() const
Definition
Skeleton.hpp:91
Sleak::Skeleton::GetNodeCount
int GetNodeCount() const
Definition
Skeleton.hpp:81
Sleak::Skeleton::GetGlobalInverseTransform
const Math::Matrix4 & GetGlobalInverseTransform() const
Definition
Skeleton.hpp:59
Sleak::Math
Vectors, matrices, quaternions, colors, AABBs, and random helpers.
Definition
GameObject.hpp:13
Sleak::Math::Matrix4
Matrix< float, 4, 4 > Matrix4
Definition
Matrix.hpp:413
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::MAX_BONES
static constexpr int MAX_BONES
Definition
Skeleton.hpp:12
Sleak::MAX_BONE_INFLUENCE
static constexpr int MAX_BONE_INFLUENCE
Definition
Skeleton.hpp:13
Sleak::Bone
Definition
Skeleton.hpp:17
Sleak::Bone::name
std::string name
Definition
Skeleton.hpp:18
Sleak::Bone::id
int id
Definition
Skeleton.hpp:19
Sleak::Bone::offsetMatrix
Math::Matrix4 offsetMatrix
Definition
Skeleton.hpp:21
Sleak::Bone::parentId
int parentId
Definition
Skeleton.hpp:20
Sleak::NodeData
Definition
Skeleton.hpp:26
Sleak::NodeData::children
std::vector< int > children
Definition
Skeleton.hpp:30
Sleak::NodeData::boneIndex
int boneIndex
Definition
Skeleton.hpp:29
Sleak::NodeData::defaultTransform
Math::Matrix4 defaultTransform
Definition
Skeleton.hpp:28
Sleak::NodeData::name
std::string name
Definition
Skeleton.hpp:27
include
public
Runtime
Skeleton.hpp
Generated on
for SleakEngine by
1.18.0