《OGRE 3D 渲染引擎原理与架构》5.2 SceneNode 与场景图

SceneNode 是场景图的组织单元,从 Node 继承父指针与位置,派生变换沿父链累加。createChildSceneNode 建子树,移动父节点子节点跟着走。lab 用 C++ 复现三级节点树与派生位置累加语义。

5.2 SceneNode 与场景图

SceneNode 是场景图的基本组织单元。它本身不可渲染,它存在的意义是把位置/朝向组织成层级——物体挂到节点上,节点挂到父节点上,整个场景形成一棵树。

从 Node 继承来的骨架

SceneNode 直接继承 Node。OgreNode.h 的 Node 持有关键的三样东西:父指针、局部变换、子节点列表。它提供 getParent()、getChild()、createChild()、_update() 等全套树操作。位置语义上,每个节点存的是相对父节点的局部变换。

// OgreMain/include/OgreNode.h:Node 类成员(v14.6.3 教学示意,参照上游同名实现,已删减)
    class _OgreExport Node
    {
        ...
    protected:
        /// Pointer to parent node
        Node* mParent;
        /// Parent node *will* be notified of moves
        ...
    };

SceneNode 在 Node 之上加了场景相关的扩展:把可移动对象挂进来(attachObject,5.3 讲)、维护 mObjectsByName 名字表、以及 createChildSceneNode/getParentSceneNode 这些场景友好的命名方法。它的 _update 递归地把局部变换合成出派生变换,传递给子节点与挂载对象。

派生位置:沿父链累加

一个节点的派生位置是它到根之间所有节点局部平移的累加。这棵三节点树的派生位置:

  • Root 在原点 (0,0,0);
  • Middle 挂在 Root 下,局部平移 (10,0,0) → 派生 (10,0,0);
  • Leaf 挂在 Middle 下,局部平移 (0,5,0) → 派生 (10,5,0)。

移动父节点 Middle 到 (20,0,0),Leaf 的派生位置自动变成 (20,5,0)——这就是场景图的精髓:子节点不需要知道父节点在哪,它只知道自己在父节点里的相对位置。渲染时逐帧从根开始 _update,把派生矩阵算出来分发给子节点和物体。

这个「相对位置 + 逐帧合成」的模型,和 helio 卷一的 Transform 树是同一个思想;区别在于 OGRE 用纯 C++ 手写递归 _update,helio 用 TypeScript 的派生变换缓存。对照可见:实现语言可以不同,层级变换的数学与数据流是通用的。

场景图的两条不变式

  1. 无环:节点只能挂一次,OGRE 用断言阻止把已挂载节点再挂到别处;
  2. 名字可查:SceneNode 维护名字→节点表,getChild(name) 是 O(1) 查表而非遍历。

这两条让场景图既能快速遍历(渲染要逐帧走全树),又能快速定位(游戏逻辑按名字找节点)。

源码定位

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

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

下面复现三节点场景图:createChildSceneNode 建子树,派生位置沿父链累加,移动父节点子节点跟着走。它与 src/05-2-scene-node-tree.cpp 逐字节一致:

// file: src/05-2-scene-node-tree.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/05-2-scene-node-tree.cpp -o bin/05-2-scene-node-tree && ./bin/05-2-scene-node-tree
//
// 复现 OgreSceneNode/Node 的层级树:createChildSceneNode 建子节点,
// 派生位置沿父链累加(简化:只累加平移,等价于 OGRE 的 _update 语义)。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"

#include <string>
#include <vector>

namespace ogre_repro {

struct Vec3 {
  double x = 0, y = 0, z = 0;
  Vec3 operator+(const Vec3& o) const { return {x + o.x, y + o.y, z + o.z}; }
  bool operator==(const Vec3& o) const { return x == o.x && y == o.y && z == o.z; }
};

// 对应 OgreNode:位置 + 父节点;对应 createChildSceneNode
class Node {
 public:
  std::string name;
  Node* parent = nullptr;
  Vec3 position;
  std::vector<Node*> children;

  Node(const std::string& n) : name(n) {}

  // 对应 SceneNode::createChildSceneNode(name, translate)
  Node* createChildSceneNode(const std::string& childName, const Vec3& translate) {
    Node* child = new Node(childName);
    child->parent = this;
    child->position = translate;
    children.push_back(child);
    return child;
  }

  Node* getParentSceneNode() const { return parent; }

  // 对应 Node 派生位置:沿父链累加局部平移
  Vec3 derivedPosition() const {
    Vec3 p = position;
    for (Node* n = parent; n; n = n->parent) p = p + n->position;
    return p;
  }
};

}  // namespace ogre_repro

int main() {
  using namespace ogre_repro;

  Node root("Root");
  Node* middle = root.createChildSceneNode("Middle", {10, 0, 0});
  Node* leaf = middle->createChildSceneNode("Leaf", {0, 5, 0});

  CHECK(root.getParentSceneNode() == nullptr);
  CHECK(middle->getParentSceneNode() == &root);
  CHECK(leaf->getParentSceneNode() == middle);

  // 派生位置:leaf = root(0) + middle(10) + leaf(5) = (10,5,0)
  CHECK((leaf->derivedPosition() == Vec3{10, 5, 0}));
  CHECK((middle->derivedPosition() == Vec3{10, 0, 0}));
  CHECK((root.derivedPosition() == Vec3{0, 0, 0}));

  // 移动父节点,子节点派生位置跟着变
  middle->position = {20, 0, 0};
  CHECK((leaf->derivedPosition() == Vec3{20, 5, 0}));

  return lab::report("05-2 场景图:SceneNode 层级与变换");
}

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

小结

  • SceneNode 继承 Node,Node 持有父指针、局部变换与子节点列表。
  • 派生变换逐帧由 _update 沿父链合成,子节点只存相对父节点的位置。
  • 两条不变式:无环(断言保证)、名字可查(O(1) 查表)。
  • 场景图与 helio 的 Transform 树同思想、不同实现语言。

阅读导航:上一节:5.1 SceneManager 职责与工厂 · 下一节:5.3 MovableObject 与附着 。

继续阅读

探索更多技术文章

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

全部文章 返回首页

「books」更多文章

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