3.2 视锥剔除
3.1 是「应用主动提问」——调用方建查询、拿结果。这一节反过来,是渲染时引擎被动筛选:每帧可见性收集都要回答「这个包围盒在不在相机视锥里」。视锥是一个平截头金字塔,Frustum 把它切成 6 个世界空间平面(近、远、左、右、上、下),判定的核心是一条规则:只要某个平面能把包围盒的 8 个角全部打到负侧,盒子就必然在视锥外,直接剔除。
本章与站内既有内容的分工
helio 四卷从零实现引擎但不用这套接口;cg 三卷讲平面与相交的数学,不落成 OGRE 类;卷一 ogre-core 在 4.2 讲 Plane/AxisAlignedBox 数据、5.3 讲 MovableObject 挂节点,但没展开「每帧怎么筛」。本节把视锥 6 平面 + isVisible 系列 + MovableObject 可见性旗标串成一条完整的剔除链路,是 3.3 Octree 场景管理的前置。
Frustum 的 6 个平面
OgreFrustum.h 用枚举把 6 个平面固定成有编号的槽位,updateFrustumPlanesImpl 从投影×视图组合矩阵里按行抽取并归一化:
// OgreMain/include/OgreFrustum.h:FrustumPlane(v14.6.3 源码摘录,已删减)
/** Worldspace clipping planes.
*/
enum FrustumPlane : uint8
{
FRUSTUM_PLANE_NEAR = 0,
FRUSTUM_PLANE_FAR = 1,
FRUSTUM_PLANE_LEFT = 2,
FRUSTUM_PLANE_RIGHT = 3,
FRUSTUM_PLANE_TOP = 4,
FRUSTUM_PLANE_BOTTOM = 5
};
// OgreMain/src/OgreFrustum.cpp:updateFrustumPlanesImpl(v14.6.3 源码摘录,已删减)
Matrix4 combo = mProjMatrix * mViewMatrix;
mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.x = combo[3][0] + combo[0][0];
mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.y = combo[3][1] + combo[0][1];
mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.z = combo[3][2] + combo[0][2];
mFrustumPlanes[FRUSTUM_PLANE_LEFT].d = combo[3][3] + combo[0][3];
...
// Renormalise any normals which were not unit length
for(auto & p : mFrustumPlanes)
{
Real length = p.normal.normalise();
p.d /= length;
}
平面法线指向视锥内侧(正侧 = 内侧),所以「整盒在负侧」等价于「整盒在视锥外」。这套 Gribb-Hartmann 式行抽取的好处是平面自动跟随相机位移与朝向,任何投影类型都能用。
isVisible:平面测试序列
OgreFrustum.h 声明了三个重载——盒、球、顶点,公共的判定语义是「6 平面逐一测试,任一平面判负即出局」:
// OgreMain/include/OgreFrustum.h:Frustum::isVisible(v14.6.3 源码摘录,已删减)
/** Tests whether the given container is visible in the Frustum.
@param bound
Bounding box to be checked (world space).
@param culledBy
Optional pointer to an int which will be filled by the plane number which culled
the box if the result was @c false;
@return
If the box was visible, @c true is returned.
*/
virtual bool isVisible(const AxisAlignedBox& bound, FrustumPlane* culledBy = 0) const;
virtual bool isVisible(const Sphere& bound, FrustumPlane* culledBy = 0) const;
virtual bool isVisible(const Vector3& vert, FrustumPlane* culledBy = 0) const;
盒版实现是最典型的:空盒恒不可见、无限盒恒可见、远距离为 0(无限视锥)时跳过远平面,然后逐平面用 Plane::getSide(centre, halfSize) 判位:
// OgreMain/src/OgreFrustum.cpp:Frustum::isVisible(const AxisAlignedBox&)(v14.6.3 源码摘录,已删减)
// Null boxes always invisible
if (bound.isNull()) return false;
// Infinite boxes always visible
if (bound.isInfinite()) return true;
// Make any pending updates to the calculated frustum planes
updateFrustumPlanes();
// Get centre of the box
Vector3 centre = bound.getCenter();
// Get the half-size of the box
Vector3 halfSize = bound.getHalfSize();
// For each plane, see if all points are on the negative side
// If so, object is not visible
for (int plane = 0; plane < 6; ++plane)
{
// Skip far plane if infinite view frustum
if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
continue;
Plane::Side side = mFrustumPlanes[plane].getSide(centre, halfSize);
if (side == Plane::NEGATIVE_SIDE)
{
// ALL corners on negative side therefore out of view
if (culledBy)
*culledBy = (FrustumPlane)plane;
return false;
}
}
return true;
Plane::getSide(centre, halfSize) 用「中心带符号距离 ± 半径在法线上的投影」一次性判断整盒位侧:中心距离比半对角投影还远,则整盒在正侧;更近且中心为负则整盒在负侧;其余情况盒子跨过平面、不能剔除。球的版本更简单——getDistance(center) < -radius 即负侧。这正是 3.3 里 OctreeCamera 把返回细化为 NONE/PARTIAL/FULL 三级的基础。
从 SceneNode 到 RenderQueue:剔除被谁调用
剔除不是孤立函数。SceneNode::_findVisibleObjects 从根节点级联:先测节点自己的世界 AABB,不在视锥里就直接 return,整棵子树都跳过;在视锥里才把挂着的对象送进渲染队列:
// OgreMain/src/OgreSceneNode.cpp:SceneNode::_findVisibleObjects(v14.6.3 源码摘录,已删减)
void SceneNode::_findVisibleObjects(Camera* cam, RenderQueue* queue,
VisibleObjectsBoundsInfo* visibleBounds, bool includeChildren,
bool displayNodes, bool onlyShadowCasters)
{
// Check self visible
if (!cam->isVisible(mWorldAABB))
return;
// Add all entities
for (auto *o : mObjectsByName)
{
queue->processVisibleObject(o, cam, onlyShadowCasters, visibleBounds);
}
if (includeChildren)
{
for (auto child : getChildren())
{
SceneNode* sceneChild = static_cast<SceneNode*>(child);
sceneChild->_findVisibleObjects(cam, queue, visibleBounds, includeChildren,
displayNodes, onlyShadowCasters);
}
}
...
}
注意「盒不可见就整棵子树跳过」是剔除优化的关键——祖先盒一旦被判负,后代不必再测。对象自己的可见性另有一条独立旗标线:MovableObject 的 mVisible 布尔位由 setVisible 控制,与视锥判定正交:
// OgreMain/include/OgreMovableObject.h:setVisible/getVisible(v14.6.3 源码摘录,已删减)
void setVisible(bool visible) { mVisible = visible; }
/** Gets this object whether to be visible or not, if it has a renderable component.
Returns the value set by MovableObject::setVisible only.
*/
bool getVisible(void) const { return mVisible; }
// OgreMain/include/OgreMovableObject.h:可见性旗标(v14.6.3 源码摘录,已删减)
bool mVisible : 1;
两种机制的分工是:mVisible 是应用拍板的「显/隐」,视锥是引擎推导的「看得见/看不见」。前者不进场景结构、只翻旗标,适合频繁的小改动;后者每帧跑 6 平面测试,决定对象是否进渲染队列。再叠加 mVisibilityFlags(与 SceneManager 的可见性掩码按位与)与 mQueryFlags,OGRE 的可见性实际上是一条「视锥 × 旗标 × 掩码」的多重过滤链。
候选过滤到提交的两层边界
场景遍历可以先用节点或空间结构筛掉整支对象。RenderQueue::processVisibleObject 还会根据阴影条件和对象可见性处理剩余候选。Ray 查询的命中结果不会自动把对象提交到渲染队列。
原生场景只移动相机,候选、三角形和非背景像素同步消失。用同一对象做 Ray 查询时,要单独解释查询掩码和包围盒判定。
节点过滤与对象过滤
flowchart TD
A["SceneManager 遍历节点"] --> B{"节点边界通过视锥检查?"}
B -->|"否"| X["结束该分支"]
B -->|"是"| C["检查节点上的对象"]
C --> D{"通过阴影和可见性条件?"}
D -->|"是"| E["更新并提交 Renderable"]
D -->|"否"| F["跳过绘制或仅合并阴影接收范围"]
C --> G["遍历子节点"]
G --> B
源码定位
OgreMain/include/OgreFrustum.h:enum FrustumPlane(6 平面槽位)OgreMain/src/OgreFrustum.cpp:Frustum::isVisible(盒/球/顶点三版)与updateFrustumPlanesImplOgreMain/include/OgreFrustum.h:getFrustumPlanes/getFrustumPlaneOgreMain/src/OgreSceneNode.cpp:SceneNode::_findVisibleObjects(级联剔除)OgreMain/include/OgreMovableObject.h:setVisible/getVisible、mVisible旗标
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面复现视锥剔除核心:用 6 个正侧朝内的平面(近/远/左/右/上/下)做 getSide(centre, halfSize) 判定,任一平面判负即剔除,并记录 culledBy 平面编号;同时覆盖空盒、无限盒与球三版语义。它与 src/03-2-frustum-culling.cpp 逐字节一致:
// file: src/03-2-frustum-culling.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/03-2-frustum-culling.cpp -o bin/03-2-frustum-culling && ./bin/03-2-frustum-culling
//
// 复现 OgreFrustum.h / OgreFrustum.cpp 的视锥剔除:
// 6 平面索引(FRUSTUM_PLANE_NEAR/FAR/LEFT/RIGHT/TOP/BOTTOM);
// Plane::getSide(centre, halfSize) 判定 AABB 相对平面的位置;
// Frustum::isVisible:任一平面 NEGATIVE_SIDE 即剔除,
// 空盒恒不可见、无限盒恒可见、无限远平面跳过。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cmath>
#include <cstdint>
namespace ogre_repro {
struct Vec3 {
float x, y, z;
};
// 对应 OgreFrustum.h:6 平面索引
enum FrustumPlane : uint8_t {
FRUSTUM_PLANE_NEAR = 0,
FRUSTUM_PLANE_FAR = 1,
FRUSTUM_PLANE_LEFT = 2,
FRUSTUM_PLANE_RIGHT = 3,
FRUSTUM_PLANE_TOP = 4,
FRUSTUM_PLANE_BOTTOM = 5
};
// 平面:nx*x + ny*y + nz*z + d = 0,正侧朝视锥内
struct Plane {
float nx, ny, nz, d;
float getDistance(const Vec3& p) const {
return nx * p.x + ny * p.y + nz * p.z + d;
}
// 对应 Plane::getSide(centre, halfSize):
// -1 NEGATIVE_SIDE 整盒在负侧;+1 POSITIVE_SIDE 整盒在正侧;0 跨平面
int getSide(const Vec3& centre, const Vec3& halfSize) const {
float dist = getDistance(centre);
float maxAbsDist =
std::abs(nx) * halfSize.x + std::abs(ny) * halfSize.y +
std::abs(nz) * halfSize.z;
if (dist - maxAbsDist > 0) return +1;
if (dist + maxAbsDist < 0) return -1;
return 0;
}
};
// 轴对齐盒:isNull / isInfinite
struct AABB {
Vec3 min, max;
bool isNull() const { return min.x > max.x; }
bool isInfinite() const {
return min.x == -1e20f && min.y == -1e20f && min.z == -1e20f &&
max.x == 1e20f && max.y == 1e20f && max.z == 1e20f;
}
Vec3 getCenter() const {
return Vec3{(min.x + max.x) / 2, (min.y + max.y) / 2, (min.z + max.z) / 2};
}
Vec3 getHalfSize() const {
return Vec3{(max.x - min.x) / 2, (max.y - min.y) / 2, (max.z - min.z) / 2};
}
};
// 球
struct Sphere {
Vec3 center;
float radius;
};
// 对应 OgreFrustum.h:6 平面 + isVisible 系列
struct Frustum {
Plane planes[6];
float mFarDist;
// 对应 Frustum::isVisible(const AxisAlignedBox&, FrustumPlane*)
bool isVisible(const AABB& bound, FrustumPlane* culledBy = nullptr) const {
if (bound.isNull()) return false;
if (bound.isInfinite()) return true;
Vec3 centre = bound.getCenter();
Vec3 halfSize = bound.getHalfSize();
for (int plane = 0; plane < 6; ++plane) {
if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0) continue;
if (planes[plane].getSide(centre, halfSize) == -1) {
if (culledBy) *culledBy = static_cast<FrustumPlane>(plane);
return false;
}
}
return true;
}
// 对应 Frustum::isVisible(const Sphere&, FrustumPlane*)
bool isVisible(const Sphere& sphere, FrustumPlane* culledBy = nullptr) const {
for (int plane = 0; plane < 6; ++plane) {
if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0) continue;
if (planes[plane].getDistance(sphere.center) < -sphere.radius) {
if (culledBy) *culledBy = static_cast<FrustumPlane>(plane);
return false;
}
}
return true;
}
};
// 构造相机位于原点、朝 -z 的对称视锥 6 平面(正侧朝内)
Frustum makeFrustum(float fovy, float aspect, float n, float f) {
float h = std::tan(fovy / 2);
float w = h * aspect;
Frustum fr;
fr.mFarDist = f;
fr.planes[FRUSTUM_PLANE_NEAR] = Plane{0, 0, -1, -n};
fr.planes[FRUSTUM_PLANE_FAR] = Plane{0, 0, 1, f};
fr.planes[FRUSTUM_PLANE_LEFT] = Plane{1, 0, -w, 0};
fr.planes[FRUSTUM_PLANE_RIGHT] = Plane{-1, 0, -w, 0};
fr.planes[FRUSTUM_PLANE_TOP] = Plane{0, -1, -h, 0};
fr.planes[FRUSTUM_PLANE_BOTTOM] = Plane{0, 1, -h, 0};
return fr;
}
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// fovy=45°,aspect=1,near=1,far=100
Frustum fr = makeFrustum(0.785398f /*45°*/, 1.0f, 1.0f, 100.0f);
// A:完全在视锥内(中心 z=-10,边长 2)
AABB inside{{-1, -1, -11}, {1, 1, -9}};
CHECK(fr.isVisible(inside));
// B:相机背后(中心 z=+5)→ 被 NEAR 平面剔除
AABB behind{{-1, -1, 4}, {1, 1, 6}};
FrustumPlane culledBy = FRUSTUM_PLANE_NEAR;
CHECK(!fr.isVisible(behind, &culledBy));
CHECK_EQ(static_cast<int>(culledBy), static_cast<int>(FRUSTUM_PLANE_NEAR));
// C:左侧远处(x=-100)→ 被 LEFT 平面剔除
AABB left{{-101, -1, -11}, {-99, 1, -9}};
culledBy = FRUSTUM_PLANE_NEAR;
CHECK(!fr.isVisible(left, &culledBy));
CHECK_EQ(static_cast<int>(culledBy), static_cast<int>(FRUSTUM_PLANE_LEFT));
// D:超出远平面(z=-200)→ 被 FAR 平面剔除
AABB beyond{{-1, -1, -201}, {1, 1, -199}};
culledBy = FRUSTUM_PLANE_NEAR;
CHECK(!fr.isVisible(beyond, &culledBy));
CHECK_EQ(static_cast<int>(culledBy), static_cast<int>(FRUSTUM_PLANE_FAR));
// E:跨近平面(z∈[-1.5,0.5])→ 部分可见,不被剔除
AABB straddle{{-1, -1, -1.5f}, {1, 1, 0.5f}};
CHECK(fr.isVisible(straddle));
// 空盒恒不可见、无限盒恒可见
AABB nullBox{{1, 1, 1}, {-1, -1, -1}};
CHECK(!fr.isVisible(nullBox));
AABB infinite{{-1e20f, -1e20f, -1e20f}, {1e20f, 1e20f, 1e20f}};
CHECK(fr.isVisible(infinite));
// 球:视锥内可见、远平面外剔除
Sphere sInside{{0, 0, -10}, 1.0f};
CHECK(fr.isVisible(sInside));
Sphere sFar{{0, 0, -200}, 1.0f};
CHECK(!fr.isVisible(sFar));
return lab::report("03-2 视锥剔除:6 平面 AABB 测试");
}
本机实测(Apple clang 16,clang++ -std=c++20 -Wall):编译零警告,断言 12/12 通过。
小结
- 视锥 = 6 个正侧朝内的世界空间平面,法线由投影×视图矩阵按行抽取并归一化。
isVisible对盒/球/顶点共用同一规则:任一平面判NEGATIVE_SIDE即剔除。- 空盒恒不可见、无限盒恒可见、无限远平面(
mFarDist==0)跳过——三个边界先处理。 SceneNode::_findVisibleObjects用祖先 AABB 做整子树级联剔除,不可见即整枝跳过。MovableObject::mVisible与应用旗标是另一条正交的显隐线,与视锥判定相乘。
阅读导航:上一节:3.1 场景查询 · 下一节:3.3 Octree 场景管理 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。