光线追踪,半透明球体在中心有一个点

Ray tracing, translucent sphere has a dot in centre

我正在构建一个光线追踪器作为一项任务。我正在尝试让折射对球体起作用,但我只做了一半。问题是我无法摆脱球体中心的黑点

这是交集的代码:

double a = rayDirection.DotProduct(rayDirection);
double b = rayOrigin.VectAdd(sphereCenter.Negative()).VectMult(2).DotProduct(rayDirection);
double c = rayOrigin.VectAdd(sphereCenter.Negative()).DotProduct(rayOrigin.VectAdd(sphereCenter.Negative())) - (radius * radius);
double discriminant = b * b - 4 * a * c;

if (discriminant >= 0)
{
    // the ray intersects the sphere
    // the first root
    double root1 = ((-1 * b - sqrt(discriminant)) / 2.0 * a) - 0.000001;
    double root2 = ((-1 * b + sqrt(discriminant)) / 2.0 * a) - 0.000001;

    if (root1 > 0.00001)
    {
        // the first root is the smallest positive root
        return root1;
    }
    else
    {
        // the second root is the smallest positive root    
        return root2;
    }
}
else
{
    // the ray missed the sphere
    return -1;
}

这是负责计算新折射光线方向的代码:

double n1 = refractionRay.GetRefractiveIndex();
double n2 = sceneObjects.at(indexOfWinningObject)->GetMaterial().GetRefractiveIndex();

if (n1 == n2)
{
    // ray inside the same material, means that it is going to be refracted outside, 
    n2 = 1.000293;
}

double n  = n1 / n2;

Vect I = refractionRay.GetRayDirection();
Vect N = sceneObjects.at(indexOfWinningObject)->GetNormalAt(intersectionPosition);

double cosTheta1 = -N.DotProduct(I);
// we need the normal pointing towards the side the ray is coming from
if (cosTheta1 < 0) 
{
    N = N.Negative();
    cosTheta1 = -N.DotProduct(I);
}

double cosTheta2 = sqrt(1 - (n * n) * (1 - (cosTheta1 * cosTheta1)));
Vect refractionDirection = I.VectMult(n).VectAdd(N.VectMult(n * cosTheta1 - cosTheta2));


Ray newRefractionRay(intersectionPosition.VectAdd(refractionDirection.VectMult(0.001)), refractionDirection, n2, refractionRay.GetRemainingIntersections());

在创建新的折射光线时,我尝试将方向乘以一个小值添加到交点位置,以使这条新光线的原点在球体内。如果我改变那个小值,黑点的大小就会改变。如果我做得太大,球体的边缘也会开始变黑。

如果我给对象添加颜色,它看起来像这样:

如果让那个小常数变大 (0.1),就会发生这种情况:

是否有我应该考虑的特殊情况?谢谢!

您应该删除计算两个根时减去的 epsilon 因子:

double root1 = ((-1 * b - sqrt(discriminant)) / 2.0 * a);
double root2 = ((-1 * b + sqrt(discriminant)) / 2.0 * a);

根据我的经验,您唯一需要与 epsilon 进行比较的地方是根据您的:

if (root1 > 0.00001)

注意:如果 root1 <= epsilon

只计算一次平方根,也可以只计算 root2 来提高性能