1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
inline bool Bounds3::IntersectP(const Ray& ray, const Vector3f& invDir,
const std::array<int, 3>& dirIsNeg) const
{
// invDir: ray direction(x,y,z), invDir=(1.0/x,1.0/y,1.0/z), use this because Multiply is faster that Division
// dirIsNeg: ray direction(x,y,z), dirIsNeg=[int(x>0),int(y>0),int(z>0)], use this to simplify your logic
// TODO test if ray bound intersects
// xyz方向的法向量
Vector3f nX(1,0,0);
Vector3f nY(0,1,0);
Vector3f nZ(0,0,1);
// x方向两个t
float tx1 = dotProduct(pMin-ray.origin,nX)/ dotProduct(ray.direction,nX);
float tx2 = dotProduct(pMax-ray.origin,nX)/ dotProduct(ray.direction,nX);
// y方向两个t
float ty1 = dotProduct(pMin-ray.origin,nY)/ dotProduct(ray.direction,nY);
float ty2 = dotProduct(pMax-ray.origin,nY)/ dotProduct(ray.direction,nY);
// z方向两个t
float tz1 = dotProduct(pMin-ray.origin,nZ)/ dotProduct(ray.direction,nZ);
float tz2 = dotProduct(pMax-ray.origin,nZ)/ dotProduct(ray.direction,nZ);
// 保证1<2
if (tx1 > tx2){
float temp = tx1;
tx1 = tx2;
tx2 = temp;
}
if (ty1 > ty2){
float temp = ty1;
ty1 = ty2;
ty2 = temp;
}
if (tz1 > tz2){
float temp = tz1;
tz1 = tz2;
tz2 = temp;
}
float maxEnter = std::max(tx1,std::max(ty1,tz1));
float minExit = std::min(tx2,std::min(ty2,tz2));
return minExit > 0 && maxEnter <= minExit;
}
|