载入中...
搜索中...
未找到
AnimSkeleton.cpp
浏览该文件的文档.
3
4#include "common/Exception.h"
5
6namespace eve::animation {
7
8void AnimSkeleton::requireBone(int boneIndex) const {
9 if (boneIndex < 0 || boneIndex >= getBoneCount()) {
10 throw Exception("AnimSkeleton: invalid bone index %d", boneIndex);
11 }
12}
13
14int AnimSkeleton::addBone(const std::string &name, int parentIndex) {
15 if (parentIndex < -1 || parentIndex >= getBoneCount()) {
16 throw Exception("AnimSkeleton.addBone: invalid parent index %d", parentIndex);
17 }
18 if (name.empty()) {
19 throw Exception("AnimSkeleton.addBone: name must not be empty");
20 }
21 if (findBone(name) >= 0) {
22 throw Exception("AnimSkeleton.addBone: duplicate bone name '%s'", name.c_str());
23 }
24 Bone b;
25 b.name = name;
26 b.parent = parentIndex;
28 bones_.push_back(b);
29 return static_cast<int>(bones_.size()) - 1;
30}
31
32std::string AnimSkeleton::getBoneName(int boneIndex) const {
33 requireBone(boneIndex);
34 return bones_[static_cast<size_t>(boneIndex)].name;
35}
36
37int AnimSkeleton::findBone(const std::string &name) const {
38 for (int i = 0; i < getBoneCount(); ++i) {
39 if (bones_[static_cast<size_t>(i)].name == name) return i;
40 }
41 return -1;
42}
43
44int AnimSkeleton::getParent(int boneIndex) const {
45 requireBone(boneIndex);
46 return bones_[static_cast<size_t>(boneIndex)].parent;
47}
48
49void AnimSkeleton::setBindPosition(int boneIndex, float x, float y, float z) {
50 requireBone(boneIndex);
51 auto &t = bones_[static_cast<size_t>(boneIndex)].bind;
52 t.px = x;
53 t.py = y;
54 t.pz = z;
55}
56
57void AnimSkeleton::setBindRotation(int boneIndex, float x, float y, float z, float w) {
58 requireBone(boneIndex);
59 auto &t = bones_[static_cast<size_t>(boneIndex)].bind;
60 t.qx = x;
61 t.qy = y;
62 t.qz = z;
63 t.qw = w;
64 t.normalizeRotation();
65}
66
67void AnimSkeleton::setBindScale(int boneIndex, float x, float y, float z) {
68 requireBone(boneIndex);
69 auto &t = bones_[static_cast<size_t>(boneIndex)].bind;
70 t.sx = x;
71 t.sy = y;
72 t.sz = z;
73}
74
75float AnimSkeleton::getBindPositionX(int boneIndex) const {
76 requireBone(boneIndex);
77 return bones_[static_cast<size_t>(boneIndex)].bind.px;
78}
79float AnimSkeleton::getBindPositionY(int boneIndex) const {
80 requireBone(boneIndex);
81 return bones_[static_cast<size_t>(boneIndex)].bind.py;
82}
83float AnimSkeleton::getBindPositionZ(int boneIndex) const {
84 requireBone(boneIndex);
85 return bones_[static_cast<size_t>(boneIndex)].bind.pz;
86}
87float AnimSkeleton::getBindRotationX(int boneIndex) const {
88 requireBone(boneIndex);
89 return bones_[static_cast<size_t>(boneIndex)].bind.qx;
90}
91float AnimSkeleton::getBindRotationY(int boneIndex) const {
92 requireBone(boneIndex);
93 return bones_[static_cast<size_t>(boneIndex)].bind.qy;
94}
95float AnimSkeleton::getBindRotationZ(int boneIndex) const {
96 requireBone(boneIndex);
97 return bones_[static_cast<size_t>(boneIndex)].bind.qz;
98}
99float AnimSkeleton::getBindRotationW(int boneIndex) const {
100 requireBone(boneIndex);
101 return bones_[static_cast<size_t>(boneIndex)].bind.qw;
102}
103float AnimSkeleton::getBindScaleX(int boneIndex) const {
104 requireBone(boneIndex);
105 return bones_[static_cast<size_t>(boneIndex)].bind.sx;
106}
107float AnimSkeleton::getBindScaleY(int boneIndex) const {
108 requireBone(boneIndex);
109 return bones_[static_cast<size_t>(boneIndex)].bind.sy;
110}
111float AnimSkeleton::getBindScaleZ(int boneIndex) const {
112 requireBone(boneIndex);
113 return bones_[static_cast<size_t>(boneIndex)].bind.sz;
114}
115
116const TransformTRS &AnimSkeleton::bindLocal(int boneIndex) const {
117 requireBone(boneIndex);
118 return bones_[static_cast<size_t>(boneIndex)].bind;
119}
120
122 if (!pose) throw Exception("AnimSkeleton.applyBindPose: pose is null");
123 pose->resize(getBoneCount());
124 for (int i = 0; i < getBoneCount(); ++i) {
125 pose->local(i) = bones_[static_cast<size_t>(i)].bind;
126 }
127}
128
129} // namespace eve::animation
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
int w
uint32_t b
const char * name
Definition RockMesh.cpp:21
int parent
Definition TreeMesh.cpp:175
Evaluated local (and optional world) pose for an AnimSkeleton. Script type: AnimPose.
Definition AnimPose.h:15
TransformTRS & local(int boneIndex)
Definition AnimPose.cpp:74
void resize(int boneCount)
Definition AnimPose.cpp:44
float getBindPositionY(int boneIndex) const
float getBindScaleX(int boneIndex) const
int getParent(int boneIndex) const
void setBindPosition(int boneIndex, float x, float y, float z)
float getBindPositionZ(int boneIndex) const
float getBindRotationW(int boneIndex) const
float getBindScaleY(int boneIndex) const
float getBindRotationY(int boneIndex) const
float getBindScaleZ(int boneIndex) const
float getBindPositionX(int boneIndex) const
const TransformTRS & bindLocal(int boneIndex) const
float getBindRotationZ(int boneIndex) const
float getBindRotationX(int boneIndex) const
std::string getBoneName(int boneIndex) const
int findBone(const std::string &name) const
void setBindScale(int boneIndex, float x, float y, float z)
void setBindRotation(int boneIndex, float x, float y, float z, float w)
void applyBindPose(class AnimPose *pose) const
Fill pose locals with bind pose.
int addBone(const std::string &name, int parentIndex=-1)
Append a bone. parentIndex = -1 for root.
Local TRS used by skeletal animation (quaternion xyzw).
Definition AnimMath.h:9
static TransformTRS identity()
Definition AnimMath.h:14