4.3 Ray 与相交测试
射线是「拾取」操作的数学基础:鼠标点击屏幕一点,反投影出一条射线,与场景里的包围盒求交,命中哪个物体就选哪个。OgreRay.h 的 Ray 结构很薄——一个原点、一个方向:
// OgreMain/include/OgreRay.h:Ray(v14.6.3 教学示意,参照上游同名实现,已删减)
class _OgreExport Ray
{
protected:
Vector3 mOrigin;
Vector3 mDirection;
...
};
参数化形式 p(t) = o + t*d,t >= 0。与平面相交、与 AABB 相交都在这条线上求解。
与平面相交:一次除法
平面方程 n·p + d = 0,代入 p = o + t*d:n·(o + t*d) + d = 0,解出
t = -(n·o + d) / (n·d)
两条失交路径:分母为 0(射线与平面平行);或 t < 0(平面在射线身后)。命中时 t 是射线参数。方向向量归一化后,t 才等于从原点量起的世界距离。这是拾取中最快的一档判定。
与 AABB 相交:slab 法
包围盒求交用 slab 法(夹逼法):把盒看成三组平行平面(x、y、z 各两个),分别求射线进入/离开每组的 t,取交集。
对每个轴:
t1 = (min - o)/d, t2 = (max - o)/d (可能交换,保证 t1 <= t2)
tmin = max(tmin, t1)
tmax = min(tmax, t2)
命中 ⇔ tmin <= tmax 且 tmax >= 0
Ray::intersects(box) 转发到 OgreMath.cpp 中返回 {hit, t} 的重载,该重载检查近侧面。接收 d1/d2 的重载使用分轴夹逼,含轴顺序和区间提前退出等分支。上面的代码块描述第二种算法。
射线在 OGRE 里的用途
- 鼠标拾取:
Camera::getCameraToViewportRay把屏幕坐标反投影成世界射线,SceneManager::createRayQuery用它扫场景; - 可见性测试:射线与 AABB 相交作为剔除粗筛;
- 玩家交互:命中检测(如点击选择)。
射线本身不持有「命中了谁」,它只回答「沿这个方向能走多远」。命中的对象是谁,由场景查询(RaySceneQuery)把相交 t 与包围盒对应起来。这个分工——「数学只算距离,归属由查询系统管」——是 OGRE 分层的一个典型例子。
源码定位
OgreMain/include/OgreRay.h:Ray(mOrigin/mDirection)OgreMain/src/OgreMath.cpp:Math::intersects(平面和两个 AABB 重载)OgreMain/include/OgreSceneQuery.h:RaySceneQuery(把射线相交接到场景对象上)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面复现射线与平面求交(含平行/背对失交)与 slab 法命中判定。它与 src/04-3-ray-intersection.cpp 逐字节一致:
// file: src/04-3-ray-intersection.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/04-3-ray-intersection.cpp -o bin/04-3-ray-intersection && ./bin/04-3-ray-intersection
//
// 复现 OgreRay 的射线与 OgreRay/OgreMath 中射线求交机制:
// 射线 p = o + t*d;与平面 n·p+d=0 求 t;与 AABB 用 slab 法求最近 t。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <algorithm>
namespace ogre_repro {
struct Ray {
double ox, oy, oz, dx, dy, dz;
// 教学辅助函数以 -1 表示失交。上游 Ray 接口返回 {hit, t}。
double intersectsPlane(double nx, double ny, double nz, double d) const {
const double denom = nx * dx + ny * dy + nz * dz;
if (denom == 0.0) return -1.0; // 平行
const double t = -(nx * ox + ny * oy + nz * oz + d) / denom;
return t >= 0.0 ? t : -1.0;
}
// slab 法求与 AABB 的交
bool intersectsBox(double minx, double miny, double minz,
double maxx, double maxy, double maxz) const {
double tmin = 0.0, tmax = 1e18;
// 依次处理 x/y/z 三组 slab
double tx1 = (minx - ox) / dx, tx2 = (maxx - ox) / dx;
if (tx1 > tx2) std::swap(tx1, tx2);
tmin = std::max(tmin, tx1);
tmax = std::min(tmax, tx2);
double ty1 = (miny - oy) / dy, ty2 = (maxy - oy) / dy;
if (ty1 > ty2) std::swap(ty1, ty2);
tmin = std::max(tmin, ty1);
tmax = std::min(tmax, ty2);
double tz1 = (minz - oz) / dz, tz2 = (maxz - oz) / dz;
if (tz1 > tz2) std::swap(tz1, tz2);
tmin = std::max(tmin, tz1);
tmax = std::min(tmax, tz2);
return tmin <= tmax && tmax >= 0.0;
}
};
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// 从 (0,0,0) 沿 +x 的射线,与平面 y=5 平行(不交)
Ray r{0, 0, 0, 1, 0, 0};
CHECK(r.intersectsPlane(0, 1, 0, -5) == -1.0);
// 从 (0,0,0) 沿 +y,交 y=5 于 t=5
Ray r2{0, 0, 0, 0, 1, 0};
CHECK(r2.intersectsPlane(0, 1, 0, -5) == 5.0);
// 反向(背对平面)不交
Ray r3{0, 0, 0, 0, -1, 0};
CHECK(r3.intersectsPlane(0, 1, 0, -5) == -1.0);
// 与 AABB [1,3]×[1,3]×[1,3]:从原点沿 (1,1,1) 命中
Ray r4{0, 0, 0, 1, 1, 1};
CHECK(r4.intersectsBox(1, 1, 1, 3, 3, 3));
// 同一射线不命中盒 [5,7]×[1,3]×[1,3](x 方向够不着)
Ray r5{0, 0, 0, 1, 1, 1};
CHECK(!r5.intersectsBox(5, 1, 1, 7, 3, 3));
return lab::report("04-3 数学库:Ray 与相交测试");
}
本机实测(Apple clang 16,clang++ -std=c++20):编译通过,断言 5/5 通过。
小结
- Ray 是「原点 + 方向」的薄结构,参数化
p = o + t*d,只回答距离不回答归属。 - 与平面求交是
t = -(n·o+d)/(n·d),平行与背对是两条失交路径。 - 与 AABB 求交用 slab 法,三轴夹逼 t 区间,
tmin <= tmax即命中。 - 拾取链路:反投影射线 → rayQuery → 相交 t 匹配对象。
阅读导航:上一节:4.2 Matrix、Plane 与 AABB · 下一节:5.1 SceneManager 职责与工厂 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。