《OGRE 3D 渲染引擎原理与架构》10.2 姿态动画

姿态动画(pose animation)用稀疏映射记录顶点偏移:Pose 只存受影响的顶点索引到偏移的映射,动画关键帧以 (poseIndex, influence) 引用若干姿势叠加。lab 复现稀疏偏移存储、Mesh 的姿势列表管理,以及多 pose 按 influence 叠加混合。

10.2 姿态动画

10.1 的 morph 每帧都存整套顶点位置,大网格吃不消。**姿态动画(pose animation)**换了个思路:只记录「哪些顶点要动、往哪动」,即一个稀疏映射 vertexIndex → offset。这样一个姿势(如「微笑」)只含十几条偏移,播放时按影响力叠加到基础顶点上。这一节解析 Pose 的数据结构、Mesh 怎么管理姿势,以及多个姿势的叠加混合。

本章与站内既有内容的分工

helio 四卷没有姿势系统;cg 三卷讲了形变权重但没谈稀疏存储。这里解析真实 OGRE 的 Pose/VertexPoseKeyFrame::PoseRef:稀疏偏移怎么存、姿势怎么被关键帧引用、多个姿势如何按 influence 叠加。10.3 会把 morph 与 pose 并进实体运行时的选择逻辑。

Pose:稀疏的顶点偏移表

Pose 的核心是一张 std::map<uint32, Vector3f>——顶点索引到偏移。构造函数里的 target 指目标几何体:0 表示 Mesh 共享几何体,1+ 表示子网格专用几何体:

// OgreMain/include/OgrePose.h:Pose(v14.6.3 源码摘录,已删减)
    class _OgreExport Pose : public AnimationAlloc
    {
    public:
        /** Constructor
            @param target The target vertexdata index (0 for shared, 1+ for 
                dedicated at the submesh index + 1)
            @param name Optional name
        */
        Pose(ushort target, const String& name = BLANKSTRING);
        /// Return the name of the pose (may be blank)
        const String& getName(void) const { return mName; }
        /// Return the target geometry index of the pose
        ushort getTarget(void) const { return mTarget; }
        /// A collection of vertex offsets based on the vertex index
        typedef std::map<uint32, Vector3f> VertexOffsetMap;
        ...
        /** Adds an offset to a vertex for this pose. 
        @param index The vertex index
        @param offset The position offset for this pose
        */
        void addVertex(uint32 index, const Vector3f& offset);
        ...
        /** Gets a const reference to the vertex offsets. */
        const VertexOffsetMap& getVertexOffsets(void) const { return mVertexOffsetMap; }

「稀疏」是精髓:smile 姿势只登记嘴角那几根顶点的偏移,其余顶点根本不在表里,混合时不动它们。对比 morph 的整份快照,pose 在内存与带宽上省得多,代价是它只能表达「偏移叠加」这种线性形变。

Mesh:姿势的容器

姿势按几何目标归属 Mesh。Mesh::createPose 建姿势,getPoseList 返回全部姿势供动画引用:

// OgreMain/include/OgreMesh.h:Mesh::createPose/getPoseList(v14.6.3 源码摘录,已删减)
        /** Create a new Pose for this mesh or one of its submeshes.
        @param target
            The target geometry index; 0 is the shared Mesh geometry, 1+ is the
            dedicated SubMesh geometry belonging to submesh index + 1.
        @param name
            Name to give the pose, which is optional.
        @return
            A new Pose ready for population.
        */
        Pose* createPose(ushort target, const String& name = BLANKSTRING);
        /** Get the number of poses */
        size_t getPoseCount(void) const { return mPoseList.size(); }
        /** Retrieve an existing Pose by index */
        Pose* getPose(size_t index) const { return mPoseList.at(index); }
        ...
        /** Get pose list. */
        const PoseList& getPoseList(void) const;

所有姿势放在一个列表里,跨共享/专用几何体共用一个编号空间——VertexPoseKeyFrame::PoseRef 里的 poseIndex 指的就是这个列表的下标。

PoseRef:关键帧如何引用姿势

VertexPoseKeyFrame 不像 morph 那样存数据,它只存一组「姿势引用」:每个引用给出姿势下标和影响力:

// OgreMain/include/OgreKeyFrame.h:VertexPoseKeyFrame::PoseRef(v14.6.3 源码摘录,已删减)
        struct PoseRef
        {
            /** The linked pose index.

                The Mesh contains all poses for all vertex data in one list, both 
                for the shared vertex data and the dedicated vertex data on submeshes.
                The 'target' on the parent track must match the 'target' on the 
                linked pose.
            */
            ushort poseIndex;
            /** Influence level of the linked pose. 
                1.0 for full influence (full offset), 0.0 for no influence.
            */
            float influence;

            PoseRef(ushort p, float i) : poseIndex(p), influence(i) {}
        };

因此一个姿态关键帧可以在同一时刻引用多个姿势(比如 smile=1.0、blink=0.5 同时生效)。最终结果 = 基础顶点位置 + Σ(poseOffset × influence),这就是 lab 里 blendPoses 复现的叠加公式。influence 为 0 的姿势自然被完全忽略。

源码定位

上游引用固定到 OGRE v14.6.3 源码 。

可运行产物:C++ 最小验证

下面用纯 C++ 复现 Pose 的稀疏存储、Mesh 姿势管理与多姿势叠加混合。它与 src/10-2-pose-animation.cpp 逐字节一致:

// file: src/10-2-pose-animation.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/10-2-pose-animation.cpp -o bin/10-2-pose-animation && ./bin/10-2-pose-animation
//
// 复现 Pose 姿态动画的叠加混合(对应 OgrePose.h 56/70/88/108、OgreMesh.h 916/920/950、OgreKeyFrame.h 193/208):
//   Pose 以稀疏映射 vertexIndex → offset 存顶点偏移(VertexOffsetMap);
//   Mesh::createPose/getPose/getPoseList 管理姿势列表;
//   VertexPoseKeyFrame::PoseRef 以 (poseIndex, influence) 引用姿势;
//   结果顶点 = 基础位置 + Σ(poseOffset × influence),多个 pose 可叠加。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"

#include <cstdint>
#include <map>
#include <string>
#include <vector>

namespace ogre_repro {

struct Vec3 {
  float x = 0.f;
  float y = 0.f;
  float z = 0.f;
  Vec3() = default;
  Vec3(float px, float py, float pz) : x(px), y(py), z(pz) {}
};

Vec3 operator+(const Vec3& a, const Vec3& b) { return Vec3(a.x + b.x, a.y + b.y, a.z + b.z); }
Vec3 operator*(const Vec3& a, float s) { return Vec3(a.x * s, a.y * s, a.z * s); }

// 对应 OgrePose.h 56:Pose
class Pose {
 public:
  Pose(uint16_t target, const std::string& name) : mTarget(target), mName(name) {}
  // 对应 OgrePose.h 66:getName
  const std::string& getName() const { return mName; }
  // 对应 OgrePose.h 68:getTarget
  uint16_t getTarget() const { return mTarget; }
  // 对应 OgrePose.h 88:addVertex(稀疏:只记录受影响的顶点)
  void addVertex(uint32_t index, const Vec3& offset) { mVertexOffsetMap[index] = offset; }
  // 对应 OgrePose.h 108:getVertexOffsets
  const std::map<uint32_t, Vec3>& getVertexOffsets() const { return mVertexOffsetMap; }

 private:
  uint16_t mTarget;
  std::string mName;
  // 对应 OgrePose.h 70:VertexOffsetMap
  std::map<uint32_t, Vec3> mVertexOffsetMap;
};

// 对应 OgreMesh.h 的 Pose 管理:createPose / getPose / getPoseList
class Mesh {
 public:
  // 对应 OgreMesh.h 916:createPose
  Pose* createPose(uint16_t target, const std::string& name) {
    mPoseList.push_back(new Pose(target, name));
    return mPoseList.back();
  }
  // 对应 OgreMesh.h 918:getPoseCount
  size_t getPoseCount() const { return mPoseList.size(); }
  // 对应 OgreMesh.h 920:getPose
  Pose* getPose(size_t index) const { return mPoseList.at(index); }
  // 对应 OgreMesh.h 950:getPoseList
  const std::vector<Pose*>& getPoseList() const { return mPoseList; }

 private:
  std::vector<Pose*> mPoseList;
};

// 对应 OgreKeyFrame.h 193:VertexPoseKeyFrame::PoseRef
struct PoseRef {
  uint16_t poseIndex;
  float influence;
  PoseRef(uint16_t p, float i) : poseIndex(p), influence(i) {}
};

// 姿态混合:把各 pose 的偏移按 influence 叠加到基础顶点
std::vector<Vec3> blendPoses(const std::vector<Vec3>& base, const std::vector<PoseRef>& refs,
                             const std::vector<Pose*>& poseList) {
  std::vector<Vec3> out = base;
  for (const PoseRef& ref : refs) {
    const Pose* pose = poseList[ref.poseIndex];
    for (auto& kv : pose->getVertexOffsets()) {
      out[kv.first] = out[kv.first] + kv.second * ref.influence;
    }
  }
  return out;
}

}  // namespace ogre_repro

int main() {
  using namespace ogre_repro;

  // Mesh 创建两个 pose:稀疏偏移只覆盖部分顶点
  Mesh mesh;
  Pose* smile = mesh.createPose(0, "smile");
  smile->addVertex(1, Vec3(0.f, 0.1f, 0.f));
  smile->addVertex(2, Vec3(0.f, 0.05f, 0.f));
  Pose* blink = mesh.createPose(0, "blink");
  blink->addVertex(2, Vec3(0.f, -0.2f, 0.f));
  CHECK_EQ(mesh.getPoseCount(), 2u);
  CHECK_EQ(mesh.getPose(0)->getName(), "smile");
  CHECK_EQ(mesh.getPoseList().size(), 2u);

  // 稀疏映射:顶点 0 没有偏移,顶点 1 有
  CHECK_EQ(smile->getVertexOffsets().count(0), 0u);
  CHECK_EQ(smile->getVertexOffsets().at(1).y, 0.1f);

  // 基础顶点
  std::vector<Vec3> base = {Vec3(0.f, 0.f, 0.f), Vec3(1.f, 0.f, 0.f), Vec3(1.f, 1.f, 0.f)};

  // 单个 pose 全影响力:只动被引用的顶点
  std::vector<PoseRef> refs1;
  refs1.emplace_back(0, 1.0f);
  std::vector<Vec3> out1 = blendPoses(base, refs1, mesh.getPoseList());
  CHECK_EQ(out1[0].y, 0.f);
  CHECK_EQ(out1[1].y, 0.1f);
  CHECK_EQ(out1[2].y, 1.05f);

  // 两个 pose 叠加:smile(influence=0.5) + blink(influence=1.0)
  std::vector<PoseRef> refs2;
  refs2.emplace_back(0, 0.5f);
  refs2.emplace_back(1, 1.0f);
  std::vector<Vec3> out2 = blendPoses(base, refs2, mesh.getPoseList());
  CHECK_EQ(out2[1].y, 0.05f);                    // 0.1 * 0.5
  CHECK_EQ(out2[2].y, 1.f + 0.05f * 0.5f - 0.2f * 1.0f);

  return lab::report("10-2 姿态动画");
}

本机实测(Apple clang 16,clang++ -std=c++20 -Wall):编译通过(零警告),断言 10/10 通过。

小结

  • Pose 用稀疏 map<uint32, Vector3f> 记录「哪些顶点动、往哪动」,未登记的顶点完全不动。
  • 姿势按几何目标归属 Mesh:0 是共享几何体,1+ 是子网格专用几何体。
  • 所有姿势共用 mPoseList 一个编号空间,PoseRef 用 poseIndex 引用它们。
  • 姿态关键帧以 (poseIndex, influence) 引用姿势,同一时刻可引用多个。
  • 结果 = 基础顶点 + Σ(poseOffset × influence);多个姿势按影响力线性叠加,可彼此混合。

阅读导航:上一节:10.1 顶点变形动画 · 下一节:10.3 顶点动画使用 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析》15.3 落地:从 Neo4j 到自研图引擎
  2. 《大规模分布式计算深入解析·图计算与图数据库内核》 · 目录与学习路径(45 篇)
  3. 《大规模分布式计算深入解析》15.2 十一卷主线:从切分到图