2.3 投影与裁剪
视锥参数(2.1 讲过)只是「设定」;真正干活的是三步数学:先由参数算出视口矩形(calcProjectionParameters),再由矩形构建投影矩阵(makePerspectiveMatrix),最后从投影矩阵提取六个裁剪平面(updateFrustumPlanesImpl)供 isVisible 做可见性判定。这一节沿着这条链走到底。
本章与站内既有内容的分工
cg 系列推导过透视投影与视锥裁剪的几何原理;这一节是 OGRE 里这套数学落成的具体函数与矩阵元素——每个常数、每个符号的位置都可对照源码。
第一步:视锥参数 → 视口矩形
透视视锥由 mFOVy(y 方向视野)与 mAspect 决定开口形状。calcProjectionParameters 先把这两个值加近裁剪距离换算成「近平面上的半宽半高」:
// OgreMain/src/OgreFrustum.cpp:Frustum::calcProjectionParameters(v14.6.3 源码摘录,已删减)
else if (mProjType == PT_PERSPECTIVE)
{
Radian thetaY (mFOVy * 0.5f);
Real tanThetaY = Math::Tan(thetaY);
Real tanThetaX = tanThetaY * mAspect;
Real nearFocal = mNearDist / mFocalLength;
Real nearOffsetX = mFrustumOffset.x * nearFocal;
Real nearOffsetY = mFrustumOffset.y * nearFocal;
Real half_w = tanThetaX * mNearDist;
Real half_h = tanThetaY * mNearDist;
mExtents = RealRect(-half_w + nearOffsetX, +half_h + nearOffsetY,
+half_w + nearOffsetX, -half_h + nearOffsetY);
}
核心公式:half_h = tan(FOVy/2) × nearDist、half_w = half_h × aspect。近平面半宽/半高由此确定,mFrustumOffset 在立体渲染时把视口中心平移出轴心。对透视视锥,任何「视锥开口多大」的问题都归结到这两个 tan 值。
第二步:矩形 → 透视投影矩阵
拿到 left/right/bottom/top 后,updateFrustumImpl 调 Math::makePerspectiveMatrix 构建矩阵:
// OgreMain/src/OgreFrustum.cpp:Frustum::updateFrustumImpl(v14.6.3 源码摘录,已删减)
RealRect rect = calcProjectionParameters();
Real left = rect.left, right = rect.right, top = rect.top, bottom = rect.bottom;
if (!mCustomProjMatrix)
{
// Recalc if frustum params changed
if (mProjType == PT_PERSPECTIVE)
{
mProjMatrix = Math::makePerspectiveMatrix(left, right, bottom, top, mNearDist, mFarDist);
矩阵本体在 OgreMath.cpp。注释自认「类似 glFrustum,刻意不做手工优化以保证可读性」:
// OgreMain/src/OgreMath.cpp:Math::makePerspectiveMatrix(v14.6.3 源码摘录,已删减)
Real inv_w = 1 / (right - left);
Real inv_h = 1 / (top - bottom);
Real inv_d = 1 / (zFar - zNear);
// Calc matrix elements
Real A = 2 * zNear * inv_w;
Real B = 2 * zNear * inv_h;
Real C = (right + left) * inv_w;
Real D = (top + bottom) * inv_h;
Real q, qn;
if (zFar == 0)
{
// Infinite far plane
q = Frustum::INFINITE_FAR_PLANE_ADJUST - 1;
qn = zNear * (Frustum::INFINITE_FAR_PLANE_ADJUST - 2);
}
else
{
q = - (zFar + zNear) * inv_d;
qn = -2 * (zFar * zNear) * inv_d;
}
Matrix4 ret = Matrix4::ZERO;
ret[0][0] = A;
ret[0][2] = C;
ret[1][1] = B;
ret[1][2] = D;
ret[2][2] = q;
ret[2][3] = qn;
ret[3][2] = -1;
zFar == 0 表示无限远裁剪(影子投影常用),此时 q/qn 用 INFINITE_FAR_PLANE_ADJUST 常数的特殊公式。有限远时就是教科书式 glFrustum:q = -(far+near)/(far-near)、qn = -2·far·near/(far-near),第 3 行第 2 列硬编码 -1(透视除 w)。
第三步:投影矩阵 → 六个裁剪平面
投影矩阵本身就能当裁剪平面用——这是 Gribb-Hartmann 法的经典做法:把 proj × view 的组合矩阵按行相加减,直接读出六个平面:
// OgreMain/src/OgreFrustum.cpp:Frustum::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];
mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.x = combo[3][0] - combo[0][0];
mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.y = combo[3][1] - combo[0][1];
mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.z = combo[3][2] - combo[0][2];
mFrustumPlanes[FRUSTUM_PLANE_RIGHT].d = combo[3][3] - combo[0][3];
六个平面的提取模式整齐对称:左右平面是第 4 行 ± 第 1 行,上下是第 4 行 ± 第 2 行,近远是第 4 行 ± 第 3 行。最后把所有平面的法线归一化(p.d /= length),得到单位法线 + 距离的世界空间平面。
第四步:isVisible —— 六面判定
有了平面,可见性就是一组点积。isVisible 对每个平面问「这个包围盒的中心,按半尺寸扩展后,是否整个在平面负侧」:
// OgreMain/src/OgreFrustum.cpp:Frustum::isVisible(v14.6.3 源码摘录,已删减)
bool Frustum::isVisible(const AxisAlignedBox& bound, FrustumPlane* culledBy) const
{
// 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) 一次判定「盒子的最大/最小角落在平面哪侧」:若最大角都在负侧(NEGATIVE_SIDE),说明整个盒子在平面外,直接返回不可见,还通过 culledBy 告诉你被哪个平面剔除。无限远视锥会跳过 far 平面(反正没有边界)。
投影变化如何进入可见性
相机与视口决定纵横比和投影。视锥平面用于筛选世界空间的对象或包围盒。目标 resize 后只有在适用的相机和视口配置下,新的尺寸才会反映到投影。
比较相同相机面对不同宽高比的窗口。固定对象边界,记录投影和近远平面,以及对象从可见变成被剔除的条件。
源码定位
OgreMain/src/OgreFrustum.cpp:Frustum::calcProjectionParameters(289-)、Frustum::updateFrustumImpl(341-)、Frustum::updateFrustumPlanesImpl(601-)、Frustum::isVisible(191-230)OgreMain/src/OgreMath.cpp:Math::makePerspectiveMatrix(719-756)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面复现投影矩阵公式与 Gribb-Hartmann 平面提取。它与 src/02-3-projection-clipping.cpp 逐字节一致:
// file: src/02-3-projection-clipping.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/02-3-projection-clipping.cpp -o bin/02-3-projection-clipping && ./bin/02-3-projection-clipping
//
// 复现透视投影矩阵与视锥裁剪(对应 OgreMath.cpp:Math::makePerspectiveMatrix 719-、
// OgreFrustum.cpp:calcProjectionParameters 289-、updateFrustumPlanesImpl 601-、isVisible 191-):
// calcProjectionParameters 由 FOVy/aspect/near 推左右上下(tanThetaY = tan(FOVy/2),half = tan*near);
// makePerspectiveMatrix 填 glFrustum 风格矩阵(A/B/C/D/q/qn);
// updateFrustumPlanesImpl 用 combo = proj*view 的六组行组合提取裁剪平面并归一化;
// isVisible 对每平面求带符号距离,任意平面负侧即剔除。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cmath>
namespace ogre_repro {
// 行主序 4x4 矩阵
struct Mat4 {
float m[16] = {};
float& operator()(int r, int c) { return m[r * 4 + c]; }
float operator()(int r, int c) const { return m[r * 4 + c]; }
Mat4 mul(const Mat4& o) const { // this * o
Mat4 r;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
float s = 0;
for (int k = 0; k < 4; ++k) s += (*this)(i, k) * o(k, j);
r(i, j) = s;
}
return r;
}
static Mat4 identity() {
Mat4 r;
for (int i = 0; i < 4; ++i) r(i, i) = 1;
return r;
}
};
// 平面:normal + d,signedDist 带符号距离
struct Plane {
float nx = 0, ny = 0, nz = 0, d = 0;
float signedDist(float x, float y, float z) const {
return nx * x + ny * y + nz * z + d;
}
void normalise() {
float len = std::sqrt(nx * nx + ny * ny + nz * nz);
nx /= len; ny /= len; nz /= len; d /= len;
}
};
// 对应 Math::makePerspectiveMatrix(OgreMath.cpp 719-):glFrustum 风格
Mat4 makePerspectiveMatrix(float left, float right, float bottom, float top,
float zNear, float zFar) {
float inv_w = 1.0f / (right - left);
float inv_h = 1.0f / (top - bottom);
float inv_d = 1.0f / (zFar - zNear);
float A = 2.0f * zNear * inv_w;
float B = 2.0f * zNear * inv_h;
float C = (right + left) * inv_w;
float D = (top + bottom) * inv_h;
float q = -(zFar + zNear) * inv_d;
float qn = -2.0f * (zFar * zNear) * inv_d;
Mat4 ret; // 全零
ret(0, 0) = A;
ret(0, 2) = C;
ret(1, 1) = B;
ret(1, 2) = D;
ret(2, 2) = q;
ret(2, 3) = qn;
ret(3, 2) = -1;
return ret;
}
// 对应 calcProjectionParameters(OgreFrustum.cpp 289-):
// FOVy/aspect/near 推左右上下(对称视锥,mFrustumOffset 为 0)
void calcProjectionParameters(float fovY, float aspect, float nearDist,
float& left, float& right,
float& bottom, float& top) {
float tanThetaY = std::tan(fovY * 0.5f);
float tanThetaX = tanThetaY * aspect;
float halfW = tanThetaX * nearDist;
float halfH = tanThetaY * nearDist;
left = -halfW;
right = +halfW;
bottom = -halfH;
top = +halfH;
}
// 对应 updateFrustumPlanesImpl(OgreFrustum.cpp 601-):
// combo = proj * view;六组行组合取平面并归一化
void extractPlanes(const Mat4& combo, Plane planes[6]) {
// 索引对应 FrustumPlane(OgreFrustum.h 58-):NEAR/FAR/LEFT/RIGHT/TOP/BOTTOM
planes[2] = Plane{combo(3, 0) + combo(0, 0), combo(3, 1) + combo(0, 1), // LEFT
combo(3, 2) + combo(0, 2), combo(3, 3) + combo(0, 3)};
planes[3] = Plane{combo(3, 0) - combo(0, 0), combo(3, 1) - combo(0, 1), // RIGHT
combo(3, 2) - combo(0, 2), combo(3, 3) - combo(0, 3)};
planes[4] = Plane{combo(3, 0) - combo(1, 0), combo(3, 1) - combo(1, 1), // TOP
combo(3, 2) - combo(1, 2), combo(3, 3) - combo(1, 3)};
planes[5] = Plane{combo(3, 0) + combo(1, 0), combo(3, 1) + combo(1, 1), // BOTTOM
combo(3, 2) + combo(1, 2), combo(3, 3) + combo(1, 3)};
planes[0] = Plane{combo(3, 0) + combo(2, 0), combo(3, 1) + combo(2, 1), // NEAR
combo(3, 2) + combo(2, 2), combo(3, 3) + combo(2, 3)};
planes[1] = Plane{combo(3, 0) - combo(2, 0), combo(3, 1) - combo(2, 1), // FAR
combo(3, 2) - combo(2, 2), combo(3, 3) - combo(2, 3)};
for (int i = 0; i < 6; ++i) planes[i].normalise();
}
// 对应 Frustum::isVisible(OgreFrustum.cpp 191-):
// 任意平面带符号距离 < 0(NEGATIVE_SIDE)即剔除
bool isVisible(const Plane planes[6], float x, float y, float z) {
for (int i = 0; i < 6; ++i)
if (planes[i].signedDist(x, y, z) < 0) return false;
return true;
}
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// 对称视锥:FOVy=45°、aspect=16:9、near=0.1、far=100
float fovY = 45.0f * 3.14159265f / 180.0f;
float aspect = 16.0f / 9.0f;
float left, right, bottom, top;
calcProjectionParameters(fovY, aspect, 0.1f, left, right, bottom, top);
CHECK(right > 0 && left == -right); // 对称
CHECK(top > 0 && bottom == -top);
// 透视投影矩阵关键元素
Mat4 proj = makePerspectiveMatrix(left, right, bottom, top, 0.1f, 100.0f);
float near = 0.1f, far = 100.0f;
CHECK(std::fabs(proj(0, 0) - 2 * near / (right - left)) < 1e-4f); // A
CHECK(std::fabs(proj(1, 1) - 2 * near / (top - bottom)) < 1e-4f); // B
CHECK(std::fabs(proj(2, 2) - (-(far + near) / (far - near))) < 1e-4f); // q
CHECK(std::fabs(proj(2, 3) - (-2 * far * near / (far - near))) < 1e-4f); // qn
CHECK(proj(3, 2) == -1.0f);
// 相机在原点、无旋转:view = 单位阵,combo = proj
Mat4 view = Mat4::identity();
Mat4 combo = proj.mul(view);
Plane planes[6];
extractPlanes(combo, planes);
// 视锥中心点(z=-5,介于近 0.1 与远 100 之间)可见
CHECK(isVisible(planes, 0.0f, 0.0f, -5.0f));
// 视锥外点被剔除:右侧超界、近平面之前
CHECK(!isVisible(planes, 5.0f, 0.0f, -5.0f)); // 右平面剔除(z=-5 处半宽约 3.68)
CHECK(!isVisible(planes, 0.0f, 0.0f, -0.01f)); // 近平面前剔除
CHECK(!isVisible(planes, 0.0f, 0.0f, -200.0f)); // 远平面外剔除
return lab::report("02-3 投影与裁剪");
}
本机实测(Apple clang 16,clang++ -std=c++20):编译通过(-Wall 零警告),断言 11/11 通过。
小结
- 视锥数学分四步:参数 → 视口矩形(
calcProjectionParameters)→ 投影矩阵(makePerspectiveMatrix)→ 裁剪平面(updateFrustumPlanesImpl)→ 判定(isVisible)。 - 透视矩阵是
glFrustum风格:A/B/C/D/q/qn六个元素,[3][2]=-1,zFar==0走无限远公式。 - 裁剪平面用 Gribb-Hartmann 法从
proj×view的行组合提取,对称模式好记:左右 = 行4±行1、上下 = 行4±行2、近远 = 行4±行3。 isVisible对每平面做带符号距离判定,整个盒子在平面负侧即剔除。
阅读导航:上一节:2.2 视口 · 下一节:3.1 场景查询 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。