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

时间:2015-04-10 20:13:54

标签: transparency raytracing translucency

我将光线追踪器构建为一项任务。我试图让屈光度为球体工作,我让它半工作。问题是我无法摆脱球体中心的黑点

enter image description here

这是交集的代码:

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());

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

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

enter image description here

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

enter image description here

我应该考虑一个特殊情况吗?谢谢!

1 个答案:

答案 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)

注意:只需进行一次平方根计算,并且只计算root2 root1 <= epsilon

,您就可以提高性能。