9.1 Skeleton 与 Bone
骨骼动画(skeletal animation)是角色动画的主流方式:网格的顶点不逐帧重写,而是由一套骨骼层级驱动。Skeleton 是骨架资源,Bone 是单个骨骼。这一节讲两者在 OGRE 源码里的关系。
本章与站内既有内容的分工
helio 卷三的动画系统讲 TypeScript 实现与动画状态机;cg 卷三的几何动画讲蒙皮数学。本系列 9.1~9.3 讲 OGRE 源码里骨架数据组织(Skeleton/Bone)、动画数据定义(Animation/轨道/关键帧)与播放状态机(AnimationState)三层结构。
Skeleton:骨架资源的容器
OgreSkeleton.h 里 Skeleton 继承 Resource(6.1 生命周期)与 AnimationContainer(携带动画),本质是一个骨骼列表:
// OgreMain/include/OgreSkeleton.h:Skeleton 与 createBone(v14.6.3 教学示意,参照上游同名实现,已删减)
class _OgreExport Skeleton : public Resource, public AnimationContainer
{
...
/** Creates a brand new Bone owned by this Skeleton.
This method creates an unattached new Bone for this skeleton.
Unless this is to be a root bone (there may be more than one of
these), you must attach it to another Bone in the skeleton using addChild for it to be any use.
...
Note that this method automatically generates a handle for the bone, which you
can retrieve using Bone::getHandle.
*/
virtual Bone* createBone(void);
virtual Bone* createBone(const String& name);
virtual Bone* createBone(const String& name, unsigned short handle);
...
unsigned short getNumBones(void) const { return (unsigned short)mBoneList.size(); }
...
const BoneList& getRootBones() const;
三个关键点:
createBone三种重载:自动 handle、命名 + 自动 handle、命名 + 显式 handle。handle 是骨骼的数字身份,与骨骼名互补——动画轨道用 handle 定位骨骼(见 9.2);- 根骨骼可以多个:注释明说「may be more than one of these」——一个骨架允许有多个没有父骨骼的根(比如角色身体 + 武器两条链);
getRootBones:返回所有没有父骨骼的骨骼。渲染时从根开始逐级遍历层级。
Bone:只有 handle 的 Node
OgreBone.h 的 Bone 直接继承 Node(5.2 的变换树基类),加了一个骨骼身份:
// OgreMain/include/OgreBone.h:Bone(v14.6.3 源码摘录,已删减)
class _OgreExport Bone : public Node
{
...
private:
unsigned short mHandle;
...
public:
unsigned short getHandle(void) const { return mHandle; }
Bone 复用 Node 的父指针、局部变换与子列表——骨骼层级就是一棵 Node 树,Bone::createChild 挂子骨骼。骨骼的 handle 在创建时由 Skeleton 分配(getHandle 取回),动画系统拿 handle 当键。这也解释了为什么 createBone 强调「自动生成 handle」——handle 是动画与骨骼之间的索引契约。
骨骼怎么连成链
骨架的搭建是两步:先 createBone 造根,再沿根 createChild 挂子:
Root(0)
└─ Spine(1)
├─ Head(2)
└─ ArmL(3)
子骨骼创建后要登记回 Skeleton 的骨骼列表,否则 Skeleton 不知道它存在。渲染时 _notifyBoneCreated 这类回调把 child 补进列表;遍历动画时从 getRootBones 的每个根递归下行。
骨架资产与实体的当前姿态
把骨骼名、父子关系、绑定姿态和动画轨迹视作资源侧输入。运行时评估需要实体关联的骨架实例和当前变换。共享网格和骨架资源时,是否共享运行时姿态仍取决于实例配置。
让两个引用同一网格的实体采用不同时间位置,比较骨骼矩阵和蒙皮顶点。显式共享骨架实例时另测姿态关联。
资产骨架与实例姿态
flowchart LR
M["Mesh 资源"] -.->|"引用"| S["Skeleton 资源与轨迹"]
M -.-> E["Entity"]
E --> I["运行时 SkeletonInstance"]
S -->|"构建骨骼并提供轨迹"| I
A["AnimationState 的时间和权重"] -->|"评估"| I
I --> B["当前骨骼矩阵"]
B --> C["CPU 更新顶点或 GPU 蒙皮参数"]
源码定位
OgreMain/include/OgreSkeleton.h:Skeleton(createBone三重载、getNumBones/getRootBones)OgreMain/src/OgreSkeleton.cpp:骨骼创建与层级实现OgreMain/include/OgreBone.h:Bone : public Node(mHandle/getHandle/createChild)OgreMain/include/OgreNode.h:Node(父指针/局部变换/子列表)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面复现骨骼创建、子链挂接、登记与根骨骼判定。它与 src/09-1-skeleton-bone.cpp 逐字节一致:
// file: src/09-1-skeleton-bone.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/09-1-skeleton-bone.cpp -o bin/09-1-skeleton-bone && ./bin/09-1-skeleton-bone
//
// 复现 OgreSkeleton 与 OgreBone:骨骼容器 + 骨骼节点层级。
// createBone 自动分配 handle,createBone(name, handle) 显式指定;
// 根骨骼 = 没有父骨骼的骨骼。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <string>
#include <vector>
namespace ogre_repro {
class Skeleton;
// 对应 OgreNode 的简化:父指针 + 子列表
class Node {
public:
Node* mParent = nullptr;
std::vector<Node*> mChildren;
// 对应 Node::addChild
void addChild(Node* child) {
child->mParent = this;
mChildren.push_back(child);
}
Node* getParent() const { return mParent; }
bool isRoot() const { return mParent == nullptr; }
};
// 对应 OgreBone:Node 子类 + 骨骼 handle + 所属 Skeleton
class Bone : public Node {
public:
std::string mName;
unsigned short mHandle;
Skeleton* mOwner;
Bone(Skeleton* owner, const std::string& name, unsigned short handle)
: mName(name), mHandle(handle), mOwner(owner) {}
unsigned short getHandle() const { return mHandle; }
// 对应 Bone::createChild:创建并登记到所属 Skeleton
Bone* createChild(const std::string& childName, unsigned short childHandle);
};
// 对应 OgreSkeleton:骨骼列表 + 创建方法
class Skeleton {
public:
std::vector<Bone*> mBoneList;
unsigned short mNextHandle = 0;
~Skeleton() {
for (auto b : mBoneList) delete b;
}
// 对应 Skeleton::createBone():自动分配 handle
Bone* createBone() { return createBone("", mNextHandle++); }
// 对应 Skeleton::createBone(name):自动分配 handle
Bone* createBone(const std::string& name) {
return createBone(name, mNextHandle++);
}
// 对应 Skeleton::createBone(name, handle)
Bone* createBone(const std::string& name, unsigned short handle) {
Bone* b = new Bone(this, name, handle);
mBoneList.push_back(b);
if (handle >= mNextHandle) mNextHandle = static_cast<unsigned short>(handle + 1);
return b;
}
// 子骨骼登记(对应 Skeleton 收到 child 创建通知)
void _notifyBoneCreated(Bone* b) { mBoneList.push_back(b); }
unsigned short getNumBones() const {
return static_cast<unsigned short>(mBoneList.size());
}
// 对应 Skeleton::getRootBones:没有父骨骼的骨骼
std::vector<Bone*> getRootBones() const {
std::vector<Bone*> roots;
for (auto b : mBoneList)
if (b->isRoot()) roots.push_back(b);
return roots;
}
};
// Bone::createChild 定义在 Skeleton 之后:创建 + 挂树 + 登记
Bone* Bone::createChild(const std::string& childName, unsigned short childHandle) {
Bone* child = new Bone(mOwner, childName, childHandle);
addChild(child);
mOwner->_notifyBoneCreated(child);
return child;
}
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
Skeleton skel;
// 根骨骼:createBone() 自动分配 handle 0
Bone* root = skel.createBone("Root");
CHECK(root->getHandle() == 0u);
CHECK(root->isRoot());
// 子骨骼:显式 handle(对应 Bone::createChild)
Bone* spine = root->createChild("Spine", 1);
Bone* head = spine->createChild("Head", 2);
Bone* armL = spine->createChild("ArmL", 3);
CHECK_EQ(skel.getNumBones(), 4u);
CHECK(spine->getParent() == root);
CHECK(head->getParent() == spine);
CHECK(armL->getHandle() == 3u);
// 根骨骼只有 Root(spine/head/armL 都有父)
std::vector<Bone*> roots = skel.getRootBones();
CHECK_EQ(roots.size(), 1u);
CHECK(roots[0] == root);
// 再建一条独立的腿(另一个根骨骼,多根合法)
Bone* legRoot = skel.createBone("LegRoot", 4);
CHECK_EQ(skel.getRootBones().size(), 2u);
// 创建时给显式 handle 会影响后续自动分配(next 从 5 开始)
Bone* tail = skel.createBone("Tail");
CHECK(tail->getHandle() == 5u);
return lab::report("09-1 骨架动画:Skeleton 与 Bone");
}
本机实测(Apple clang 16,clang++ -std=c++20):编译通过,断言 10/10 通过。
小结
- Skeleton 继承 Resource 与 AnimationContainer,是骨骼列表 + 动画容器的骨架资源。
createBone三种重载:自动 handle、命名、命名 + 显式 handle。- Bone 继承 Node,加
mHandle——handle 是动画与骨骼之间的索引契约。 - 根骨骼可多个,
getRootBones返回无父骨骼;子骨骼经Bone::createChild创建并登记回 Skeleton。
阅读导航:上一节:8.3 材质脚本与状态合并 · 下一节:9.2 Animation 与关键帧 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。