10.3 顶点动画使用
10.1 的 morph 和 10.2 的 pose 都是「数据怎么组织」,这一节回答「运行时怎么用」:实体(Entity)每帧要决定把哪一份顶点数据绑定到渲染器。它可能用原始数据,也可能用软件算完的蒙皮/morph 结果,还可能在硬件里做 morph——选谁由动画类型 + 软件请求计数共同决定。这一节解析 VertexDataBindChoice 与 chooseVertexDataForBinding。
本章与站内既有内容的分工
helio 四卷的实体直接绑原始顶点数据;ogre-render 卷讲过顶点缓冲但没谈「按需换数据源」。这里解析真实 OGRE 的 Entity::addSoftwareAnimationRequest/chooseVertexDataForBinding 与 SubMesh::useSharedVertices:软件请求计数怎么用、四种绑定选择怎么互斥、共享/独立顶点数据怎么选。lab 复现的正是这张决策表。
软件动画请求:谁来强制软件计算
OGRE 默认尽量用硬件算动画(蒙皮着色器、硬件 morph),但有些场景必须拿到 CPU 侧的真实顶点——例如碰撞检测、精确取模。Entity 用一个请求计数表达这个需求:任何调用方 addSoftwareAnimationRequest 一次,计数就 +1;只有计数归零才会考虑纯硬件路径:
// OgreMain/include/OgreEntity.h:Entity::getSoftwareAnimationRequests(v14.6.3 源码摘录,已删减)
int getSoftwareAnimationRequests(void) const { return mSoftwareAnimationRequests; }
...
void addSoftwareAnimationRequest(bool normalsAlso);
...
/** Returns whether or not this entity is either morph or pose animated.
*/
bool hasVertexAnimation(void) const;
请求必须成对:不用软件动画时必须 removeSoftwareAnimationRequest,计数已经为零再移除会抛异常。normalsAlso 参数额外要求连法线一起软件计算。
chooseVertexDataForBinding:四选一
拿到「有没有顶点动画」与「软件请求数」两个输入后,实体选择绑定哪份顶点数据。枚举与选择函数在 OgreEntity.h 相邻定义:
// OgreMain/include/OgreEntity.h:VertexDataBindChoice(v14.6.3 源码摘录,已删减)
/// Identify which vertex data we should be sending to the renderer.
enum VertexDataBindChoice
{
BIND_ORIGINAL,
BIND_SOFTWARE_SKELETAL,
BIND_SOFTWARE_MORPH,
BIND_HARDWARE_MORPH
};
/// Choose which vertex data to bind to the renderer.
VertexDataBindChoice chooseVertexDataForBinding(bool hasVertexAnim);
选择逻辑是优先级嵌套的:
| 条件 | 选择 | 含义 |
|---|---|---|
| 有软件请求 + 有顶点动画 | BIND_SOFTWARE_MORPH | CPU 算完 morph/pose 的临时缓冲 |
| 有软件请求 + 无顶点动画 | BIND_SOFTWARE_SKELETAL | CPU 算完骨骼蒙皮的临时缓冲 |
| 无软件请求 + 有顶点动画 | BIND_HARDWARE_MORPH | 交给硬件 morph,绑专用缓冲 |
| 无软件请求 + 无顶点动画 | BIND_ORIGINAL | 原始顶点数据,最省 |
可见软件请求一旦出现就压过硬件路径;顶点动画的有无则决定走 morph 还是骨骼蒙皮的临时缓冲。SubEntity 层面同样暴露 getVertexDataForBinding(OgreSubEntity.h:241),把选择落实到每个子实体。
共享与独立顶点数据
另一个运行时选择是数据归属。SubMesh 有一个布尔字段决定它用 Mesh 的共享顶点数据还是自己的独立数据:
// OgreMain/include/OgreSubMesh.h:SubMesh::useSharedVertices(v14.6.3 源码摘录,已删减)
/// Indicates if this submesh shares vertex data with other meshes or whether it has it's own vertices.
bool useSharedVertices;
useSharedVertices == true 时该子网格顶点与其它子网格共享同一份缓冲(动画只需算一次);false 时有独立缓冲,可以单独做 morph 或蒙皮。这解释了 createVertexTrack 的 handle 约定:0 指共享几何体,1+ 指第 (handle-1) 个子网格的专用几何体。
顶点动画怎样选择绘制时的缓冲
Morph 用关键帧顶点数据,Pose 以顶点偏移和权重表达形态。程序支持顶点动画时,渲染可绑定动画输入。在软件路径,CPU 更新后的顶点数据进入 RenderOperation。
取同一帧的一个顶点,分别记录原值、动画贡献和选择的绑定。比较硬件和软件路径时先确认顶点声明及相同的权重。
动画贡献和顶点绑定
flowchart LR
A["当前动画状态"] --> T["VertexAnimationTrack"]
T --> K{"动画类型"}
K -->|"Morph"| M["帧顶点缓冲"]
K -->|"Pose"| P["姿态偏移和权重"]
M --> F{"绘制路径"}
P --> F
F -->|"软件"| S["生成或更新 CPU 蒙皮结果"]
F -->|"硬件"| H["绑定源数据与程序所需的动画数据"]
S --> R["RenderOperation"]
H --> R
源码定位
OgreMain/include/OgreEntity.h:getSoftwareAnimationRequests(652)、addSoftwareAnimationRequest(680)、removeSoftwareAnimationRequest(689)、hasVertexAnimation(699)、VertexDataBindChoice(789)、chooseVertexDataForBinding(797)OgreMain/include/OgreSubEntity.h:getVertexDataForBinding(241)OgreMain/include/OgreSubMesh.h:useSharedVertices(143)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面用纯 C++ 复现顶点动画的选用与互斥决策表。它与 src/10-3-vertex-animation-usage.cpp 逐字节一致:
// file: src/10-3-vertex-animation-usage.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/10-3-vertex-animation-usage.cpp -o bin/10-3-vertex-animation-usage && ./bin/10-3-vertex-animation-usage
//
// 复现顶点动画的选用与互斥(对应 OgreEntity.h 652/680/699/789/797、OgreSubEntity.h 241、OgreSubMesh.h 80/143):
// Entity 用软件动画请求计数 + hasVertexAnimation 决定是否做顶点动画;
// chooseVertexDataForBinding 在 ORIGINAL / SOFTWARE_SKELETAL / SOFTWARE_MORPH / HARDWARE_MORPH
// 四者中选择绑定到渲染器的顶点数据(顶点动画与骨骼动画走不同的临时缓冲);
// SubMesh::useSharedVertices 决定用共享还是独立顶点数据。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cstdint>
namespace ogre_repro {
// 对应 OgreEntity.h 789:VertexDataBindChoice
enum VertexDataBindChoice {
BIND_ORIGINAL,
BIND_SOFTWARE_SKELETAL,
BIND_SOFTWARE_MORPH,
BIND_HARDWARE_MORPH
};
// 对应 OgreSubMesh.h 60(节选):SubMesh 顶点数据
struct SubMesh {
// 对应 OgreSubMesh.h 143:useSharedVertices
bool useSharedVertices = false;
// 对应 OgreSubMesh.h 80 的 resetVertexData 语义:独立数据存在与否
bool hasDedicatedVertexData() const { return !useSharedVertices; }
};
// 对应 OgreEntity.h 的顶点动画使用(简化)
class Entity {
public:
Entity() { mSubMesh.useSharedVertices = false; }
// 对应 OgreEntity.h 652:getSoftwareAnimationRequests
int getSoftwareAnimationRequests() const { return mSoftwareAnimationRequests; }
// 对应 OgreEntity.h 680:addSoftwareAnimationRequest
void addSoftwareAnimationRequest(bool /*normalsAlso*/) { ++mSoftwareAnimationRequests; }
// 对应 OgreEntity.h 689:removeSoftwareAnimationRequest
void removeSoftwareAnimationRequest(bool /*normalsAlso*/) { --mSoftwareAnimationRequests; }
// 对应 OgreEntity.h 699:hasVertexAnimation
bool hasVertexAnimation() const { return mHasVertexAnimation; }
// 对应 OgreEntity.h 797:chooseVertexDataForBinding
VertexDataBindChoice chooseVertexDataForBinding(bool hasVertexAnim) {
if (mSoftwareAnimationRequests > 0)
return hasVertexAnim ? BIND_SOFTWARE_MORPH : BIND_SOFTWARE_SKELETAL;
if (hasVertexAnim) return BIND_HARDWARE_MORPH;
return BIND_ORIGINAL;
}
bool mHasVertexAnimation = false;
int mSoftwareAnimationRequests = 0;
SubMesh mSubMesh;
};
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// 无顶点动画:绑定原始数据
Entity e;
CHECK(!e.hasVertexAnimation());
CHECK_EQ(e.chooseVertexDataForBinding(false), BIND_ORIGINAL);
// 有顶点动画但无软件请求:硬件 morph(顶点动画专用缓冲)
e.mHasVertexAnimation = true;
CHECK(e.hasVertexAnimation());
CHECK_EQ(e.chooseVertexDataForBinding(true), BIND_HARDWARE_MORPH);
// 有软件请求:即使硬件可用也强制软件(软件顶点动画缓冲)
e.addSoftwareAnimationRequest(false);
CHECK_EQ(e.getSoftwareAnimationRequests(), 1);
CHECK_EQ(e.chooseVertexDataForBinding(true), BIND_SOFTWARE_MORPH);
e.removeSoftwareAnimationRequest(false);
CHECK_EQ(e.getSoftwareAnimationRequests(), 0);
// 无顶点动画但要求软件:软件骨骼蒙皮
e.mHasVertexAnimation = false;
e.addSoftwareAnimationRequest(false);
CHECK_EQ(e.chooseVertexDataForBinding(false), BIND_SOFTWARE_SKELETAL);
// SubMesh 共享/独立顶点数据选择
CHECK(e.mSubMesh.hasDedicatedVertexData()); // 默认独立
e.mSubMesh.useSharedVertices = true;
CHECK(!e.mSubMesh.hasDedicatedVertexData());
return lab::report("10-3 顶点动画使用");
}
本机实测(Apple clang 16,clang++ -std=c++20 -Wall):编译通过(零警告),断言 10/10 通过。
小结
addSoftwareAnimationRequest/removeSoftwareAnimationRequest用请求计数表达「必须软件算顶点」;计数归零才可能走硬件路径。VertexDataBindChoice四选一:BIND_ORIGINAL/BIND_SOFTWARE_SKELETAL/BIND_SOFTWARE_MORPH/BIND_HARDWARE_MORPH。- 软件请求优先:有请求走软件,否则有顶点动画走硬件 morph,否则绑原始数据。
SubMesh::useSharedVertices决定共享还是独立顶点数据,进而决定动画是算一次还是各算各的。- 顶点轨迹的 handle 约定与数据归属呼应:0 = 共享几何体,1+ = 子网格专用几何体。
阅读导航:上一节:10.2 姿态动画 · 下一节:11.1 天空盒与天空平面 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。