《OGRE 3D 渲染引擎原理与架构》7.2 VertexData 与顶点声明

顶点格式用 VertexDeclaration 描述:每个 VertexElement 声明来源、偏移、类型与语义。VES_POSITION/VET_FLOAT3 这样的枚举组合告诉渲染器顶点怎么读。VertexBufferBinding 把 source 绑到实际缓冲,vertexStart/vertexCount 允许一个缓冲装多个渲染对象。lab 用 C++ 复现元素声明、按来源算 stride 与绑定表。

7.2 VertexData 与顶点声明

顶点数据是网格的心脏:位置、法线、颜色、纹理坐标全在这里。OGRE 把「顶点长什么样」与「顶点存哪」分开描述——VertexDeclaration 管格式,VertexBufferBinding 管绑定。这一节拆开看。

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

cg 卷一的几何基础讲顶点的数学含义;本系列 7.2 讲 OGRE 里顶点格式的 C++ 描述方式:声明、元素、绑定,以及它们如何决定渲染器读内存的方式。

VertexData:声明 + 绑定 + 范围

OgreVertexIndexData.h 的 VertexData 把三个要素合在一起:

// OgreMain/include/OgreVertexIndexData.h:VertexData(v14.6.3 源码摘录,已删减)
    class _OgreExport VertexData : public VertexDataAlloc
    {
        ...
        /** Declaration of the the format of the vertex input.
        */
        VertexDeclaration* vertexDeclaration;
        /** Defines which vertex buffers are bound to which sources.
        */
        VertexBufferBinding* vertexBufferBinding;
        /// The position in the bound buffers to start reading vertex data from. This allows you to use a single buffer for many different renderables.
        uint32 vertexStart;
        /// The number of vertices to process in this particular rendering group
        uint32 vertexCount;
  • vertexDeclaration:顶点格式(每块数据是什么);
  • vertexBufferBinding:source → 缓冲 的映射(数据在哪);
  • vertexStart/vertexCount:从缓冲的哪个顶点开始、处理多少个——这让多个渲染对象能共享一个大缓冲,各用各的区间。

格式与存储分离 + 区间游标,是这套设计的三个支点。

VertexDeclaration:元素的列表

顶点格式由一组 VertexElement 组成,每个元素描述一块数据。语义枚举标明「这是什么」:

// OgreMain/include/OgreHardwareVertexBuffer.h:VertexElementSemantic(v14.6.3 源码摘录,已删减)
    enum VertexElementSemantic : uint8
    {
        /// Position, typically VET_FLOAT3
        VES_POSITION = 1,
        /// Blending weights
        VES_BLEND_WEIGHTS = 2,
        /// Blending indices
        VES_BLEND_INDICES = 3,
        /// Normal, typically VET_FLOAT3
        VES_NORMAL = 4,
        /// Colour, typically VET_UBYTE4
        VES_COLOUR = 5,
        /// Texture coordinates, typically VET_FLOAT2
        VES_TEXTURE_COORDINATES = 7,
        /// Tangent (X axis if normal is Z)
        VES_TANGENT = 9,
        ...
    };

类型枚举标明「怎么读」:

// OgreMain/include/OgreHardwareVertexBuffer.h:VertexElementType(v14.6.3 源码摘录,已删减)
    enum VertexElementType : uint8
    {
        VET_FLOAT1 = 0,
        VET_FLOAT2 = 1,
        VET_FLOAT3 = 2,
        VET_FLOAT4 = 3,
        ...
        VET_UBYTE4 = 9,
        ...
    };

VertexElement 本体记录五样东西:来源、偏移、类型、语义、索引(纹理坐标等多路输入的序号):

// OgreMain/include/OgreHardwareVertexBuffer.h:VertexElement(v14.6.3 源码摘录,已删减)
    class _OgreExport VertexElement : public VertexDataAlloc
    {
        /// The offset in the buffer that this element starts at
        size_t mOffset;
        /// The source vertex buffer, as bound to an index using VertexBufferBinding
        unsigned short mSource;
        /// Index of the item, only applicable for some elements like texture coords
        unsigned short mIndex;
        /// The type of element
        VertexElementType mType;
        /// The meaning of the element
        VertexElementSemantic mSemantic;
    ...
    };

VertexDeclaration::addElement(source, offset, type, semantic, index) 追加元素。一个「位置 + 法线 + 纹理坐标」的经典声明就是三次 addElement。

VertexBufferBinding:source → 缓冲

VertexDeclaration 里的 source 只是编号,真正指向哪个硬件缓冲由 VertexBufferBinding 管。它是一张 std::map<unsigned short, HardwareVertexBufferSharedPtr>:

// OgreMain/include/OgreHardwareVertexBuffer.h:VertexBufferBinding(v14.6.3 源码摘录,已删减)
    class _OgreExport VertexBufferBinding : public VertexDataAlloc
    {
        typedef std::map<unsigned short, HardwareVertexBufferSharedPtr> VertexBufferBindingMap;
        VertexBufferBindingMap mBindingMap;
        ...
        const VertexBufferBindingMap& getBindings(void) const;

一个声明里多个元素可以来自不同缓冲(source 0 存位置/法线,source 1 存纹理坐标),各 source 的 stride 由 VertexDeclaration::getVertexSize(source) 算出——渲染器据此跳行读取。

渲染时怎么用

渲染一个网格时,GPU 需要知道三件事:顶点怎么组织(declaration)、数据在哪(binding)、读多少(vertexStart/vertexCount)。三个来源正好由 VertexData 一次提供。这就是为什么 SubMesh 持有的是 VertexData* 而不是散乱的裸缓冲——它是「顶点读取指令」的完整封装。

源码定位

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

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

下面复现元素声明、按 source 算 stride 与绑定表。它与 src/07-2-vertex-data.cpp 逐字节一致:

// file: src/07-2-vertex-data.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/07-2-vertex-data.cpp -o bin/07-2-vertex-data && ./bin/07-2-vertex-data
//
// 复现 OgreVertexDeclaration/VertexElement 与 VertexData 的顶点格式:
//   位置/法线/纹理坐标各为 VertexElement;按 source 计算 stride;
//   VertexBufferBinding 把 source 绑到缓冲。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"

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

namespace ogre_repro {

// 对应 OgreHardwareVertexBuffer.h 的语义枚举(局部复制关键项)
enum VertexElementSemantic {
  VES_POSITION = 1,
  VES_NORMAL = 4,
  VES_COLOUR = 5,
  VES_TEXTURE_COORDINATES = 7
};

// 对应 VertexElementType 的部分类型与字节数
enum VertexElementType : uint8_t {
  VET_FLOAT1 = 0,
  VET_FLOAT2 = 1,
  VET_FLOAT3 = 2,
  VET_FLOAT4 = 3,
  VET_UBYTE4 = 9
};

// 对应 VertexElement::getTypeSize
size_t getTypeSize(VertexElementType etype) {
  switch (etype) {
    case VET_FLOAT1: return 4;
    case VET_FLOAT2: return 8;
    case VET_FLOAT3: return 12;
    case VET_FLOAT4: return 16;
    case VET_UBYTE4: return 4;
  }
  return 0;
}

// 对应 VertexElement:来源/偏移/类型/语义/索引
struct VertexElement {
  unsigned short mSource;
  size_t mOffset;
  VertexElementType mType;
  VertexElementSemantic mSemantic;
  unsigned short mIndex;

  size_t getSize() const { return getTypeSize(mType); }
};

// 对应 VertexDeclaration:元素列表 + addElement
class VertexDeclaration {
 public:
  std::vector<VertexElement> mElementList;

  // 对应 VertexDeclaration::addElement(source, offset, type, semantic, index)
  void addElement(unsigned short source, size_t offset, VertexElementType type,
                  VertexElementSemantic semantic, unsigned short index = 0) {
    mElementList.push_back({source, offset, type, semantic, index});
  }

  size_t getElementCount() const { return mElementList.size(); }

  // 对应 getVertexSize(source):按来源分组的 stride
  size_t getVertexSize(unsigned short source) const {
    size_t size = 0;
    for (const auto& e : mElementList)
      if (e.mSource == source) size += e.getSize();
    return size;
  }

  // 总 stride:所有来源的最大值(单缓冲时就是它)
  size_t getVertexSize() const {
    size_t total = 0;
    for (const auto& e : mElementList)
      if (e.mSource == 0) total += e.getSize();
    return total;
  }
};

// 对应 VertexBufferBinding:source → 缓冲
class VertexBufferBinding {
 public:
  std::map<unsigned short, int> mBindingMap;  // 简化:缓冲用 id 表示

  void setBinding(unsigned short source, int bufferId) {
    mBindingMap[source] = bufferId;
  }
  int getBufferId(unsigned short source) const {
    auto it = mBindingMap.find(source);
    return it == mBindingMap.end() ? -1 : it->second;
  }
};

// 对应 VertexData:声明 + 绑定 + 顶点范围
struct VertexData {
  VertexDeclaration* vertexDeclaration;
  VertexBufferBinding* vertexBufferBinding;
  uint32_t vertexStart = 0;
  uint32_t vertexCount = 0;

  VertexData(VertexDeclaration* d, VertexBufferBinding* b)
      : vertexDeclaration(d), vertexBufferBinding(b) {}
};

}  // namespace ogre_repro

int main() {
  using namespace ogre_repro;

  VertexDeclaration dcl;
  VertexBufferBinding bind;

  // 位置(FLOAT3,source 0,偏移 0)
  dcl.addElement(0, 0, VET_FLOAT3, VES_POSITION);
  // 法线(FLOAT3,source 0,偏移 12)
  dcl.addElement(0, 12, VET_FLOAT3, VES_NORMAL);
  // 纹理坐标(FLOAT2,source 1,独立缓冲)
  dcl.addElement(1, 0, VET_FLOAT2, VES_TEXTURE_COORDINATES);

  CHECK_EQ(dcl.getElementCount(), 3u);

  // stride:source 0 为 12+12=24,source 1 为 8
  CHECK(dcl.getVertexSize(0) == 24u);
  CHECK(dcl.getVertexSize(1) == 8u);

  // 元素字段正确
  const VertexElement& pos = dcl.mElementList[0];
  CHECK(pos.mSemantic == VES_POSITION);
  CHECK(pos.mType == VET_FLOAT3);
  CHECK(pos.mSource == 0);
  CHECK(pos.mOffset == 0u);
  CHECK(pos.getSize() == 12u);

  // 绑定:source 0 → 缓冲 5,source 1 → 缓冲 7
  bind.setBinding(0, 5);
  bind.setBinding(1, 7);
  CHECK(bind.getBufferId(0) == 5);
  CHECK(bind.getBufferId(1) == 7);

  // VertexData 组合:声明 + 绑定 + 顶点范围
  VertexData vd(&dcl, &bind);
  vd.vertexStart = 0;
  vd.vertexCount = 1024;
  CHECK(vd.vertexDeclaration == &dcl);
  CHECK(vd.vertexCount == 1024u);

  return lab::report("07-2 网格几何:VertexData 与顶点声明");
}

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

小结

  • VertexData 三要素:声明(格式)、绑定(存储)、范围(start/count)。
  • 语义枚举(VES_)标「是什么」,类型枚举(VET_)标「怎么读」。
  • VertexElement 五字段:source/offset/type/semantic/index。
  • VertexBufferBinding 是 source → 缓冲 的映射,多 source 支持多缓冲布局。
  • vertexStart/vertexCount 让多个渲染对象共享一个大缓冲。

阅读导航:上一节:7.1 Mesh 与 SubMesh · 下一节:7.3 IndexData 与索引缓冲 。

继续阅读

探索更多技术文章

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

全部文章 返回首页

「books」更多文章

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