【发布时间】:2016-11-12 14:32:03
【问题描述】:
我正在用 java 编写一个光线追踪器,我能够追踪球体,但我认为我追踪三角形的方式有问题。
据我所知,这是基本算法:
- 首先确定射线是否与三角形所在的平面相交。
- 剪裁所有点,使它们与三角形位于同一平面上(以
xy平面为例)。 - 根据您在沿新平面向任意方向发出光线时穿过的多边形边的数量,确定潜在的交点是落在三角形内部还是外部。
现在,这是我的实现(特别是第一点):
public Vector getIntersectionVector(Ray ray)
{
Vector planeIntersectionVector = getPlaneIntersectionVector(ray, getPlaneNormal());
if (planeIntersectionVector != null)
{
if (isIntersectionVectorInsideTriangle(planeIntersectionVector))
{
return planeIntersectionVector;
}
else
{
return null;
}
}
else
{
return null;
}
}
getPlaceIntersectionVector() 在哪里:
private Vector getPlaneIntersectionVector(Ray ray, Vector planeNormal)
{
double vd = planeNormal.dotProduct(ray.getDirection());
//(p_n \dot r_d) == 0, parallel. (p_n \dot r_d) > 0 Plane normal pointing away from ray.
if (vd >= 0)
{
return null;
}
double distance = planeNormal.distance(0d, 0d, 0d);
double vo = -(planeNormal.dotProduct(ray.getOrigin()) + distance);
double intersectionDistance = vo / vd;
//intersectionDistance <= 0 means the "intersection" is behind the ray, so not a real intersection
return (intersectionDistance <= 0) ? null : ray.getLocation(intersectionDistance);
}
其中t是点击中沿射线的距离,ro是射线的原点,rd是射线的方向, pn指的是三角形/平面的平面法线,d是三角形所在平面到原点(0,0,0)的距离@
我做错了吗?当我从图像(0,0) 中的第一个像素发出光线时,我看到intersectionDistance(或t)几乎是1100,这在我看来直觉上是错误的。我认为交叉点会更近。
以下是相关数据:
射线原点(0,0,1),射线方向大致为(0.000917, -0.4689, -0.8833)。
三角形的顶点为(-0.2, 0.1, 0.1)、(-0.2, -0.5, 0.2)、(-0.2, 0.1, -0.3),这使得平面正常(-1, 0, 0)。
根据我的代码,Ray 与距离 1090 的平面相交,正如我之前提到的,这对我来说似乎是错误的。场景在各个方向上只有-1.0到1.0,这意味着距离很远。
我做错飞机交叉口了吗?
如果您需要更多信息,请告诉我在哪里澄清要点。
【问题讨论】:
标签: java algorithm 3d intersection raytracing