10.1 顶点变形动画
骨骼动画动的是少数骨头,**顶点变形动画(morph animation)**则相反:它直接改变顶点位置本身。原理很朴素——预先录好几套「完整顶点位置」,播放时在相邻两套之间线性插值。代价是每帧快照都很大(等于整份顶点数据),所以它适合小网格的细腻表情、水面起伏这类「骨头表达不了」的形变。这一节解析 OGRE 的数据结构与插值语义。
本章与站内既有内容的分工
helio 四卷的网格没有 morph 通道;cg 三卷讲过顶点插值数学但没落到引擎结构。这里解析真实 OGRE 的 VertexAnimationType/VertexAnimationTrack/VertexMorphKeyFrame:类型枚举、快照关键帧的存储,以及软件 morph 的插值语义。10.2 的 Pose 走「稀疏偏移」路线,10.3 再讲实体在运行时怎么选用这两条路。
VertexAnimationType:顶点动画的两条路线
OGRE 把顶点动画分成两种,用一个枚举区分。源码注释把取舍说得很清楚——morph 简单直接但每帧是整份快照;pose 稀疏可混合但表达范围受限:
// OgreMain/include/OgreAnimationTrack.h:VertexAnimationType(v14.6.3 源码摘录,已删减)
enum VertexAnimationType : uint8
{
/// No animation
VAT_NONE = 0,
/// Morph animation is made up of many interpolated snapshot keyframes
VAT_MORPH = 1,
/// Pose animation is made up of a single delta pose keyframe
VAT_POSE = 2
};
注释还点明互斥关系:morph 不能与其它顶点动画混合(两个 morph 也不行);pose 可以彼此混合;两者都能与骨骼动画组合。这个约束在 10.3 的绑定选择里会再次出现。
VertexAnimationTrack:顶点轨迹
VertexAnimationTrack 是顶点动画的轨迹类,按类型分支创建对应关键帧。morph 分支的工厂方法与查询方法见下:
// OgreMain/include/OgreAnimationTrack.h:VertexAnimationTrack(v14.6.3 源码摘录,已删减)
class _OgreExport VertexAnimationTrack : public AnimationTrack
{
public:
/** Get the type of vertex animation we're performing. */
VertexAnimationType getAnimationType(void) const { return mAnimationType; }
/// @copydoc AnimationTrack::createKeyFrame
virtual VertexMorphKeyFrame* createVertexMorphKeyFrame(Real timePos);
/** Creates the single pose KeyFrame and adds it to this animation.
*/
virtual VertexPoseKeyFrame* createVertexPoseKeyFrame(Real timePos);
...
/** Returns the morph KeyFrame at the specified index. */
VertexMorphKeyFrame* getVertexMorphKeyFrame(unsigned short index) const;
注意 createVertexPoseKeyFrame 的注释——pose 动画的轨迹只有一个 pose 关键帧(每个 pose 内部是稀疏顶点偏移,见 10.2),而 morph 轨迹可以有很多个快照关键帧。
VertexMorphKeyFrame:一帧的顶点快照
morph 关键帧的内容就是一整套顶点位置的缓冲区。它不逐顶点存偏移,而是整块存绝对位置:
// OgreMain/include/OgreKeyFrame.h:VertexMorphKeyFrame(v14.6.3 源码摘录,已删减)
/** Specialised KeyFrame which stores absolute vertex positions for a complete
buffer, designed to be interpolated with other keys in the same track.
*/
class _OgreExport VertexMorphKeyFrame : public KeyFrame
{
public:
/** Sets the vertex buffer containing the source positions for this keyframe.
We assume that positions are the first 3 float elements in this buffer,
although we don't necessarily assume they're the only ones in there.
@param buf Vertex buffer link; will not be modified so can be shared
read-only data
*/
void setVertexBuffer(const HardwareVertexBufferSharedPtr& buf);
...
private:
HardwareVertexBufferSharedPtr mBuffer;
};
「positions are the first 3 float elements」——OGRE 假设位置是缓冲里每顶点前三个 float,但缓冲里还可以有法线、UV 等其它数据。插值语义是:给定时间点,在前一个快照和后一个快照之间对每个顶点位置做线性 lerp;落在快照上就是那一帧原样。这就是 lab 里 applyToPositions 复现的软件 morph 应用逻辑。
源码定位
OgreMain/include/OgreAnimationTrack.h:VertexAnimationType(449)、VertexAnimationTrack(462)、createVertexMorphKeyFrame(487)、createVertexPoseKeyFrame(491)、getVertexMorphKeyFrame(508)OgreMain/include/OgreKeyFrame.h:VertexMorphKeyFrame(153)、setVertexBuffer(166)、mBuffer(174)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面用纯 C++ 复现 morph 类型枚举、快照关键帧存储与前后快照线性插值。它与 src/10-1-morph-animation.cpp 逐字节一致:
// file: src/10-1-morph-animation.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/10-1-morph-animation.cpp -o bin/10-1-morph-animation && ./bin/10-1-morph-animation
//
// 复现 Morph 顶点动画的插值机制(对应 OgreAnimationTrack.h 449/487、OgreKeyFrame.h 153/166):
// VertexAnimationType::VAT_MORPH = 1;
// VertexMorphKeyFrame 是「一套完整顶点位置的快照」;
// 动画在前后两个快照之间按时间线性插值(软件 morph 的 apply 语义)。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cstdint>
#include <vector>
namespace ogre_repro {
// 对应 OgreAnimationTrack.h 449:VertexAnimationType
enum VertexAnimationType : uint8_t {
VAT_NONE = 0, // OgreAnimationTrack.h 452
VAT_MORPH = 1, // OgreAnimationTrack.h 454
VAT_POSE = 2 // OgreAnimationTrack.h 456
};
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 lerp(const Vec3& a, const Vec3& b, float t) {
return Vec3(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);
}
// 对应 OgreKeyFrame.h 153:VertexMorphKeyFrame,存整套顶点位置
struct VertexMorphKeyFrame {
float time;
std::vector<Vec3> positions;
explicit VertexMorphKeyFrame(float t) : time(t) {}
};
// 对应 OgreAnimationTrack.h 462:VertexAnimationTrack(morph 分支)
class VertexAnimationTrack {
public:
VertexAnimationTrack(uint16_t handle, VertexAnimationType animType)
: mHandle(handle), mAnimationType(animType) {}
// 对应 OgreAnimationTrack.h 449:getHandle
uint16_t getHandle() const { return mHandle; }
// 对应 OgreAnimationTrack.h 481:getAnimationType
VertexAnimationType getAnimationType() const { return mAnimationType; }
// 对应 OgreAnimationTrack.h 487:createVertexMorphKeyFrame
VertexMorphKeyFrame* createVertexMorphKeyFrame(float timePos) {
mKeyFrames.push_back(new VertexMorphKeyFrame(timePos));
return mKeyFrames.back();
}
// 对应 OgreAnimationTrack.h 508:getVertexMorphKeyFrame
VertexMorphKeyFrame* getVertexMorphKeyFrame(size_t index) const {
return mKeyFrames.at(index);
}
// 软件插值语义:在前后两套顶点快照之间按 t 线性插值
void applyToPositions(float timePos, std::vector<Vec3>& out) const {
size_t i = 0;
while (i + 1 < mKeyFrames.size() && mKeyFrames[i + 1]->time <= timePos) ++i;
const VertexMorphKeyFrame* kf1 = mKeyFrames[i];
const VertexMorphKeyFrame* kf2 =
(i + 1 < mKeyFrames.size()) ? mKeyFrames[i + 1] : mKeyFrames[i];
float span = kf2->time - kf1->time;
float t = span <= 0.f ? 0.f : (timePos - kf1->time) / span;
out.resize(kf1->positions.size());
for (size_t v = 0; v < kf1->positions.size(); ++v)
out[v] = lerp(kf1->positions[v], kf2->positions[v], t);
}
private:
uint16_t mHandle;
VertexAnimationType mAnimationType;
std::vector<VertexMorphKeyFrame*> mKeyFrames;
};
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// VertexAnimationType 枚举值
CHECK_EQ(VAT_NONE, 0);
CHECK_EQ(VAT_MORPH, 1);
CHECK_EQ(VAT_POSE, 2);
// morph 轨迹的类型标志与句柄
VertexAnimationTrack track(3, VAT_MORPH);
CHECK_EQ(track.getHandle(), 3);
CHECK_EQ(track.getAnimationType(), VAT_MORPH);
// 两个关键帧:每帧是一整套绝对顶点位置快照
VertexMorphKeyFrame* k0 = track.createVertexMorphKeyFrame(0.0f);
k0->positions = {Vec3(0.f, 0.f, 0.f), Vec3(1.f, 0.f, 0.f), Vec3(0.f, 1.f, 0.f)};
VertexMorphKeyFrame* k1 = track.createVertexMorphKeyFrame(1.0f);
k1->positions = {Vec3(0.f, 0.f, 0.f), Vec3(2.f, 0.f, 0.f), Vec3(0.f, 2.f, 0.f)};
CHECK_EQ(track.getVertexMorphKeyFrame(0)->time, 0.0f);
CHECK_EQ(track.getVertexMorphKeyFrame(1)->time, 1.0f);
// 时间 0.5:每个顶点在前后快照间线性插值
std::vector<Vec3> out;
track.applyToPositions(0.5f, out);
CHECK_EQ(out.size(), 3u);
CHECK_EQ(out[0].x, 0.f);
CHECK_EQ(out[1].x, 1.5f); // (1+2)/2
CHECK_EQ(out[2].y, 1.5f); // (1+2)/2
// 时间 1.0:到达末帧
track.applyToPositions(1.0f, out);
CHECK_EQ(out[1].x, 2.f);
CHECK_EQ(out[2].y, 2.f);
return lab::report("10-1 顶点变形动画");
}
本机实测(Apple clang 16,clang++ -std=c++20 -Wall):编译通过(零警告),断言 13/13 通过。
小结
VertexAnimationType三值:VAT_NONE=0、VAT_MORPH=1、VAT_POSE=2,morph 与 pose 互斥。VertexAnimationTrack是顶点动画轨迹,morph 分支用createVertexMorphKeyFrame建快照帧。VertexMorphKeyFrame存一整套绝对顶点位置(位置是缓冲里每顶点前 3 个 float)。- morph 插值:在前后两套快照之间对每个顶点位置线性 lerp,落在快照上原样取。
- morph 快照体积大,适合小网格的细腻形变;大网格的稀疏形变走 10.2 的 pose。
阅读导航:上一节:9.3 动画状态机 · 下一节:10.2 姿态动画 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。