12.3 布告板渲染
12.1 说了集合怎么管板,12.2 说了板怎么摆——这一节把两者汇成能画的东西:一个 RenderOperation。BillboardSet 作为 Renderable,在 getRenderOperation 里填好顶点/索引元数据;索引在缓冲创建时一次性生成(每板两三角形),法线每帧按板朝向算出。这一节解析这三件事。
本章与站内既有内容的分工
ogre-render 卷讲过 RenderOperation 与 Renderable 契约;helio 四卷的单精灵用单独 draw。这里解析真实 OGRE 的 BillboardSet::getRenderOperation 与 _createBuffers 里的索引生成:为什么用索引而非 6 个独立顶点(省 1/3 顶点变换)、索引顺序怎么排、法线怎么算成面向相机。
getRenderOperation:批量面片的元数据
BillboardSet 是集合级 Renderable——整集合只填一个渲染操作。非点渲染时,每板贡献 4 顶点、6 索引,总顶点数 = 活板数 × 4,总索引数 = 活板数 × 6:
// OgreMain/src/OgreBillboardSet.cpp:BillboardSet::getRenderOperation(v14.6.3 源码摘录,已删减)
void BillboardSet::getRenderOperation(RenderOperation& op)
{
op.vertexData = mVertexData.get();
op.vertexData->vertexStart = 0;
if (mPointRendering)
{
op.operationType = RenderOperation::OT_POINT_LIST;
op.useIndexes = false;
op.indexData = 0;
op.vertexData->vertexCount = mNumVisibleBillboards;
}
else
{
op.operationType = RenderOperation::OT_TRIANGLE_LIST;
op.useIndexes = true;
op.vertexData->vertexCount = mNumVisibleBillboards * 4;
op.indexData = mIndexData.get();
op.indexData->indexCount = mNumVisibleBillboards * 6;
op.indexData->indexStart = 0;
}
}
mPointRendering 时退化成点列表(每板 1 顶点、无索引);常规路径是 OT_TRIANGLE_LIST + 索引。
索引生成:每板两个三角形
索引在 _createBuffers 里一次性生成(每帧不变),因为板的顶点布局固定。源码注释画了板的顶点排布,并解释为什么用索引——顶点变换数量减 1/3:
// OgreMain/src/OgreBillboardSet.cpp:_createBuffers 索引生成(v14.6.3 源码摘录,已删减)
/* Create indexes (will be the same every frame)
Using indexes because it means 1/3 less vertex transforms (4 instead of 6)
Billboard layout relative to camera:
0-----1
| /|
| / |
|/ |
2-----3
*/
HardwareBufferLockGuard indexLock(mIndexData->indexBuffer, HardwareBuffer::HBL_DISCARD);
ushort* pIdx = static_cast<ushort*>(indexLock.pData);
for(
size_t idx, idxOff, bboard = 0;
bboard < mPoolSize;
++bboard )
{
// Do indexes
idx = bboard * 6;
idxOff = bboard * 4;
pIdx[idx] = static_cast<unsigned short>(idxOff); // + 0;, for clarity
pIdx[idx+1] = static_cast<unsigned short>(idxOff + 2);
pIdx[idx+2] = static_cast<unsigned short>(idxOff + 1);
pIdx[idx+3] = static_cast<unsigned short>(idxOff + 1);
pIdx[idx+4] = static_cast<unsigned short>(idxOff + 2);
pIdx[idx+5] = static_cast<unsigned short>(idxOff + 3);
}
第 b 板的 6 个索引是 (0,2,1) 与 (1,2,3)(相对 idxOff = b*4):两个三角形共享 1↔2 那条对角线。顶点 0=左上、1=右上、2=左下、3=右下,见 12.2 的 genVertOffsets 输出顺序。
quadNormal:面向相机的法线
法线没有存在缓冲里,而是每帧按板朝向算出来。genQuadVertices 里旋转板时用两对角向量叉积求轴向(BBR_VERTEX 旋转模式):
// OgreMain/src/OgreBillboardSet.cpp:genQuadVertices(v14.6.3 源码摘录,已删减)
void BillboardSet::genQuadVertices(const Vector3* const offsets, const Billboard& bb)
{
...
if (bb.mRotation != Radian(0))
{
if (mRotationType == BBR_VERTEX)
{
// TODO: Cache axis when billboard type is BBT_POINT or BBT_PERPENDICULAR_COMMON
Vector3 axis = (offsets[3] - offsets[0]).crossProduct(offsets[2] - offsets[1]).normalisedCopy();
Matrix3 rotation;
rotation.FromAngleAxis(axis, bb.mRotation);
...
法线 = 右上一左上(沿第一条对角线)叉积右上一右下(沿第二条对角线),归一化后就是板的朝向轴。对于 12.2 的 POINT 板,这个轴恰好指向相机;对于 PERPENDICULAR 板,它是板的固定法线。lab 里 quadNormal 复现这个叉积,并加了朝向修正:若法线与「板中心到相机」的方向夹角为钝角,就翻转,保证法线永远指向观察者一侧。
一个面片从方向变成顶点与索引
BillboardSet 根据相机、BillboardType 和 origin 计算面片朝向与偏移,再把多个面片写到同一渲染操作。距离排序与朝向更新需要随相机观察。
用同一组布告板切换朝向类型和是否排序,记录生成四角、索引数和相机前后关系。可区分朝向问题与透明顺序。
源码定位
OgreMain/src/OgreBillboardSet.cpp:getRenderOperation(483)、_createBuffers索引生成(596-638)、genQuadVertices(848)、法线轴叉积(868)OgreMain/include/OgreBillboardSet.h:BillboardSet : Renderable(118)、_updateRenderQueue(716 附近)OgreMain/include/OgreBillboard.h:Billboard(68)、mPosition/mDirection(82-84)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面用纯 C++ 复现布告板的三角形面片生成与面向相机的法线。它与 src/12-3-billboard-rendering.cpp 逐字节一致:
// file: src/12-3-billboard-rendering.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/12-3-billboard-rendering.cpp -o bin/12-3-billboard-rendering && ./bin/12-3-billboard-rendering
//
// 复现布告板集合的渲染面片(对应 OgreBillboardSet.h 227/258、OgreBillboardSet.cpp 483/596/832/848):
// getRenderOperation 用 OT_TRIANGLE_LIST 输出 4 顶点 + 6 索引 / 板(两三角形 0,2,1 与 1,2,3);
// 索引按顶点参数偏移顺序写(左下/右上 交错生成两个三角形);
// quadNormal 用两对角向量叉积,使法线面向相机方向。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cstdint>
#include <vector>
namespace ogre_repro {
// 对应 OgreRenderOperation.h 65 的 OperationType 节选
enum OperationType : uint8_t {
OT_TRIANGLE_LIST = 0, // OgreRenderOperation.h 67
OT_TRIANGLE_STRIP,
OT_TRIANGLE_FAN
};
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 cross(const Vec3& a, const Vec3& b) {
return Vec3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
float dot(const Vec3& a, const Vec3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
// 一个布告板的 4 个角点(lt/rt/lb/rb,与 genVertOffsets 顺序一致)
struct Quad {
Vec3 lt;
Vec3 rt;
Vec3 lb;
Vec3 rb;
};
// 对应 OgreBillboardSet.h 227:getRenderOperation(节选)
// 用 OT_TRIANGLE_LIST 填充 positions 与 indices:每板 4 顶点 + 6 索引。
void buildRenderOperation(const std::vector<Quad>& quads, std::vector<Vec3>& positions,
std::vector<uint32_t>& indices, OperationType& opType) {
opType = OT_TRIANGLE_LIST;
positions.clear();
indices.clear();
for (const Quad& q : quads) {
size_t base = positions.size();
positions.push_back(q.lt);
positions.push_back(q.rt);
positions.push_back(q.lb);
positions.push_back(q.rb);
// 对应 OgreBillboardSet.cpp 596 起的索引生成:两三角形共享 (rt, lb) 边
indices.push_back(static_cast<uint32_t>(base + 0));
indices.push_back(static_cast<uint32_t>(base + 2));
indices.push_back(static_cast<uint32_t>(base + 1));
indices.push_back(static_cast<uint32_t>(base + 1));
indices.push_back(static_cast<uint32_t>(base + 2));
indices.push_back(static_cast<uint32_t>(base + 3));
}
}
// 对应 OgreBillboardSet.cpp 832 的 quadNormal:
// 用两对角向量(rt->lt 与 rt->rb)的叉积,使法线朝向相机。
Vec3 quadNormal(const Quad& q, const Vec3& camPos) {
Vec3 n = cross(q.rt - q.lt, q.rt - q.rb);
// 确保法线指向相机一侧
Vec3 toCam = camPos - Vec3((q.lt.x + q.rb.x) * 0.5f, (q.lt.y + q.rb.y) * 0.5f,
(q.lt.z + q.rb.z) * 0.5f);
if (dot(n, toCam) < 0.f)
n = Vec3(-n.x, -n.y, -n.z);
return n;
}
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
OperationType opType = OT_TRIANGLE_STRIP;
std::vector<Vec3> positions;
std::vector<uint32_t> indices;
// 一个中心在原点的布告板,2x2,法线 +Z
Quad q;
q.lt = Vec3(-1.f, 1.f, 0.f);
q.rt = Vec3(1.f, 1.f, 0.f);
q.lb = Vec3(-1.f, -1.f, 0.f);
q.rb = Vec3(1.f, -1.f, 0.f);
buildRenderOperation({q}, positions, indices, opType);
// 三角形列表:每板 4 顶点 + 6 索引 = 2 个三角形
CHECK_EQ(opType, OT_TRIANGLE_LIST);
CHECK_EQ(positions.size(), 4u);
CHECK_EQ(indices.size(), 6u);
// 索引顺序:0,2,1 与 1,2,3(两个三角形共享 1/2 边)
CHECK_EQ(indices[0], 0u);
CHECK_EQ(indices[1], 2u);
CHECK_EQ(indices[2], 1u);
CHECK_EQ(indices[3], 1u);
CHECK_EQ(indices[4], 2u);
CHECK_EQ(indices[5], 3u);
// 两个布告板:8 顶点 + 12 索引
Quad q2 = q;
q2.lt = Vec3(4.f, 1.f, 0.f);
q2.rt = Vec3(6.f, 1.f, 0.f);
q2.lb = Vec3(4.f, -1.f, 0.f);
q2.rb = Vec3(6.f, -1.f, 0.f);
buildRenderOperation({q, q2}, positions, indices, opType);
CHECK_EQ(positions.size(), 8u);
CHECK_EQ(indices.size(), 12u);
// 第二板的索引相对偏移 4
CHECK_EQ(indices[6], 4u);
CHECK_EQ(indices[11], 7u);
// 法线:叉积 (lt-rt) × (rb-rt) = +Z,指向相机(相机在 +Z)
Vec3 n = quadNormal(q, Vec3(0.f, 0.f, 10.f));
CHECK_EQ(n.x, 0.f);
CHECK_EQ(n.y, 0.f);
CHECK(n.z > 0.f);
// 相机在 -Z:法线自动翻转仍指向相机
Vec3 n2 = quadNormal(q, Vec3(0.f, 0.f, -10.f));
CHECK(n2.z < 0.f);
return lab::report("12-3 布告板渲染");
}
本机实测(Apple clang 16,clang++ -std=c++20 -Wall):编译通过(零警告),断言 17/17 通过。
小结
getRenderOperation填OT_TRIANGLE_LIST:每板 4 顶点 + 6 索引,整集合一次渲染。- 索引一次性生成(每帧不变),每板两个三角形
(0,2,1)与(1,2,3),共享对角线。 - 用索引而非 6 个独立顶点,顶点变换数减 1/3(4 而非 6)。
- 法线由两对角向量叉积得到,
BBR_VERTEX旋转时用作旋转轴。 - 法线按「板中心到相机」方向修正朝向,保证永远面向观察者。
阅读导航:上一节:12.2 布告板类型 · 下一节:13.1 Overlay 系统 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。