Refração no Raytracing?

Eu tenho trabalhado no meu raytracer novamente. Eu adicionei reflexão e suporte multithreading. Atualmente, estou trabalhando para adicionar refrações, mas é apenas metade do trabalho.

Como você pode ver, há uma esfera central (sem destaque especular), uma esfera refletora (à direita) e uma esfera refratária (esquerda). Estou muito feliz com as reflexões, parece muito bom. Para refrações, está meio que funcionando ... a luz é refratada e todas as sombras das esferas são visíveis na esfera (índice de refração 1.4), mas existe um anel preto externo.

EDIT: Aparentemente, o anel preto fica maior e, portanto, a esfera menor, quando eu aumento o índice de refração da esfera. Pelo contrário, ao diminuir o índice de refração, a esfera aumenta e o anel preto diminui ... até que, com o índice de refração definido como um, o anel desaparece totalmente. IOR = 1,9 IOR = 1,1 IOR = 1.00001 E, curiosamente, em IOR = 1, a esfera perde sua transparência e fica branca.

Acho que cobri a reflexão interna total e não é o problema aqui.

Agora o código: estou usando ooperator | para produto escalar, então(vec|vec) é um produto escalar e ooperator ~ inverter vetores. Os objetos, tanto as luzes quanto as esferas, são armazenados emObject **objects;. Função Raytrace

Colour raytrace(const Ray &r, const int &depth)
{
    //first find the nearest intersection of a ray with an object
    Colour finalColour = skyBlue *(r.getDirection()|Vector(0,0,-1)) * SKY_FACTOR;
    double t, t_min = INFINITY;
    int index_nearObj = -1;
    for(int i = 0; i < objSize; i++)
    {
        if(!dynamic_cast<Light *>(objects[i]))//skip light src
        {
            t = objects[i]->findParam(r);
            if(t > 0 && t < t_min)
            {
                t_min = t;
                index_nearObj = i;
            }
        }
    }
    //no intersection
    if(index_nearObj < 0)
        return finalColour;

    Vector intersect = r.getOrigin() + r.getDirection()*t_min;
    Vector normal = objects[index_nearObj]->NormalAtIntersect(intersect);
    Colour objectColor = objects[index_nearObj]->getColor();
    Ray rRefl, rRefr; //reflected and refracted Ray
    Colour refl = finalColour, refr = finalColour; //reflected and refracted colours
    double reflectance = 0, transmittance = 0;

    if(objects[index_nearObj]->isReflective() && depth < MAX_TRACE_DEPTH)
    {
        //handle reflection
        rRefl = objects[index_nearObj]->calcReflectingRay(r, intersect, normal);
        refl = raytrace(rRefl, depth + 1);
        reflectance = 1;
    }

    if(objects[index_nearObj]->isRefractive() && depth < MAX_TRACE_DEPTH)
    {
        //handle transmission
        rRefr = objects[index_nearObj]->calcRefractingRay(r, intersect, normal, reflectance, transmittance);
        refr = raytrace(rRefr, depth + 1);
    }

    Ray rShadow; //shadow ray
    bool shadowed;
    double t_light = -1;

    Colour localColour;
    Vector tmpv;

    //get material properties
    double ka = 0.2; //ambient coefficient
    double kd; //diffuse coefficient
    double ks; //specular coefficient

    Colour ambient = ka * objectColor; //ambient component
    Colour diffuse, specular;
    double brightness;
    localColour = ambient;
    //look if the object is in shadow or light
    //do this by casting a ray from the obj and
    // check if there is an intersection with another obj
    for(int i = 0; i < objSize; i++)
    {
        if(dynamic_cast<Light *>(objects[i])) //if object is a light
        {
            //for each light
            shadowed = false;
            //create Ray to light
            tmpv = objects[i]->getPosition() - intersect;
            rShadow = Ray(intersect  + (!tmpv) * BIAS, tmpv);
            t_light = objects[i]->findParam(rShadow);

            if(t_light < 0) //no imtersect, which is quite impossible
                continue;

            //then we check if that Ray intersects one object that is not a light
            for(int j = 0; j < objSize; j++)
            {
                    if(!dynamic_cast<Light *>(objects[j]) && j != index_nearObj)//if obj is not a light
                    {
                        t = objects[j]->findParam(rShadow);
                        //if it is smaller we know the light is behind the object
                        //--> shadowed by this light
                        if (t >= 0 && t < t_light)
                        {
                            // Set the flag and stop the cycle
                            shadowed = true;
                            break;
                        }
                    }
            }

            if(!shadowed)
            {
                rRefl = objects[index_nearObj]->calcReflectingRay(rShadow, intersect, normal);
                //reflected ray from ligh src, for ks
                kd = maximum(0.0, (normal|rShadow.getDirection()));
                if(objects[index_nearObj]->getShiny() <= 0)
                    ks = 0;
                else
                    ks = pow(maximum(0.0, (r.getDirection()|rRefl.getDirection())), objects[index_nearObj]->getShiny());
                diffuse = kd * objectColor;// * objects[i]->getColour();
                specular = ks * objects[i]->getColor();
                brightness = 1 /(1 + t_light * DISTANCE_DEPENDENCY_LIGHT);
                localColour += brightness * (diffuse + specular);
            }
        }
    }
    finalColour = localColour + (transmittance * refr + reflectance * refl);
    return finalColour;
}

Agora, a função que calcula o raio refratado, usei vários sites diferentes para obter recursos, e cada um tinha algoritmos semelhantes. Este é o melhor que pude fazer até agora. Pode ser apenas um pequeno detalhe que não estou vendo ...

Ray Sphere::calcRefractingRay(const Ray &r, const Vector &intersection,Vector &normal, double & refl, double &trans)const
{
    double n1, n2, n;
    double cosI = (r.getDirection()|normal);
    if(cosI > 0.0)
    {
        n1 = 1.0;
        n2 = getRefrIndex();
        normal = ~normal;//invert
    }
    else
    {
        n1 = getRefrIndex();
        n2 = 1.0;
        cosI = -cosI;
    }
    n = n1/n2;
    double sinT2 = n*n * (1.0 - cosI * cosI);
    double cosT = sqrt(1.0 - sinT2);
    //fresnel equations
    double rn = (n1 * cosI - n2 * cosT)/(n1 * cosI + n2 * cosT);
    double rt = (n2 * cosI - n1 * cosT)/(n2 * cosI + n2 * cosT);
    rn *= rn;
    rt *= rt;
    refl = (rn + rt)*0.5;
    trans = 1.0 - refl;
    if(n == 1.0)
        return r;
    if(cosT*cosT < 0.0)//tot inner refl
    {
        refl = 1;
        trans = 0;
        return calcReflectingRay(r, intersection, normal);
    }
    Vector dir = n * r.getDirection() + (n * cosI - cosT)*normal;
    return Ray(intersection + dir * BIAS, dir);
}

EDIT: Eu também alterei o índice de refração.

    if(cosI > 0.0)
    {
        n1 = 1.0;
        n2 = getRefrIndex();
        normal = ~normal;
    }
    else
    {
        n1 = getRefrIndex();
        n2 = 1.0;
        cosI = -cosI;
    }

para

if(cosI > 0.0)
{
    n1 = getRefrIndex();
    n2 = 1.0;
    normal = ~normal;
}
else
{
    n1 = 1.0;
    n2 = getRefrIndex();
    cosI = -cosI;
}

Então eu entendo isso, e quase o mesmo (ainda de cabeça para baixo) com um índice de refração em 1! E o cálculo da reflexão:

Ray Sphere::calcReflectingRay(const Ray &r, const Vector &intersection, const Vector &normal)const
{
    Vector rdir = r.getDirection();
    Vector dir = rdir - 2 * (rdir|normal) * normal;
    return Ray(intersection + dir*BIAS, dir);
    //the Ray constructor automatically normalizes directions
}

Então, minha pergunta é: como faço para corrigir o círculo preto externo? Qual versão está correta?

A ajuda é muito apreciada :)

Isso é compilado no Linux usando o g ++ 4.8.2.

questionAnswers(1)

yourAnswerToTheQuestion