光线跟踪光线盘交叉

时间:2013-05-20 00:25:44

标签: math graphics geometry intersection raytracing

所以我正在尝试编写代码,看看光线是否与平面圆盘相交,我希望在这里检查它。我的磁盘始终以负z轴为中心,因此其法线向量应为(0,0,-1)。

我这样做的方法是首先计算光线平面交点,然后确定该交叉点是否在磁盘的“范围”内。

在我的代码中,我得到一些似乎已关闭的数字,我不确定问题是否在此方法中,或者是否可能在其他地方。因此,如果此代码出现问题,我将非常感谢您的反馈! =)

这是我的代码:

float d = z_intercept; //This is where disk intersects z-axis. Can be + or -.
ray->d = Normalize(ray->d); 
Point p(0, 0, d); //This is the center point of the disk
Point p0(0, 1, d);
Point p1(1, 0, d);
Vector n = Normalize(Cross(p0-p, p1-p));//Calculate normal

float diameter = DISK_DIAMETER; //Constant value
float t = (-d-Dot(p-ray->o, n))/Dot(ray->d, n); //Calculate the plane intersection
Point intersection = ray->o + t*ray->d;
return (Distance(p, intersection) <= diameter/2.0f); //See if within disk


//This is my code to calculate distance
float RealisticCamera::Distance(Point p, Point i) 
{
return sqrt((p.x-i.x)*(p.x-i.x) + (p.y-i.y)*(p.y-i.y) + (p.z-i.z)*(p.z-i.z));
}

2 个答案:

答案 0 :(得分:1)

我的磁盘始终以负z轴为中心,因此其法线向量应为(0,0,-1)。

这一事实简化了计算。

退化案例:ray-&gt; d.z = 0 - &gt;如果ray-&gt; o.z = d则射线位于磁盘平面,检查为2Dd,否则射线是平行的,没有交叉点

常见案例:t = (d - ray->o.z) / ray->d.z

如果t为正值,则为此t找到x和y,并检查x ^ 2 + y ^ 2&lt; = disk_radius ^ 2

答案 1 :(得分:0)

计算 t 是错误的。

光线上的要点是:

ray->o + t * ray->d

特别是,光线上某点的坐标 z 是:

ray->o.z() + t * ray->d.z()

必须等于 d 。那就出来了

t = ( d - ray->o.z() ) / ray->d.z()