C++ 使用Blinn Phong计算光线跟踪器上像素的漫反射r、g、B值

C++ 使用Blinn Phong计算光线跟踪器上像素的漫反射r、g、B值,c++,raytracing,lighting,shading,C++,Raytracing,Lighting,Shading,我试图用Blinn-Phong公式计算像素的RGB值。为此,我使用此函数: Material getPixelColor(Ray ray, double min, int index, std::vector<Object*> Objects, std::vector<Object*> lightSources) { Vector intersectionPoint = ray.getOrigin() + ray.getDirection() * min;

我试图用Blinn-Phong公式计算像素的RGB值。为此,我使用此函数:

Material getPixelColor(Ray ray, double min, int index, std::vector<Object*> Objects, std::vector<Object*> lightSources) {

    Vector intersectionPoint = ray.getOrigin() + ray.getDirection() * min;
    Vector n = Objects.at(index)->getNormalAt(intersectionPoint);
    Vector reflectiondirection = ray.getDirection() - n * Vector::dot(ray.getDirection(), n) * 2;
    Ray reflectionRay(intersectionPoint, reflectiondirection);

    // check if ray intersects any other object;
    double minimum = INFINITY;
    int count = 0, indx = -1;
    for (auto const& obj : Objects) {
        double distance = obj->Intersect(reflectionRay);
        if (minimum > distance) {
            minimum = distance;
            indx = count;
        }
        count++;
    }

    Material result(0,0,0);
    if (recurseDepth >= 5 || indx == -1) {
        recurseDepth = 0;
        // Check if object is lit for each light source
        for (auto const& light : lightSources) {
            // Blinn-Phong
            Vector lightDirection = (light->getPosition() - intersectionPoint).normalize();
            double nl = Vector::dot(n, lightDirection);
            nl = nl > 0 ? nl : 0.0;
            result = result + (Objects.at(index)->getMaterial() * light->getMaterial() * nl);
        }
    }
    else{
        recurseDepth++;
        result = result + getPixelColor(reflectionRay, minimum, indx, Objects, lightSources);
    }
    return result;
}
材质getPixelColor(光线、双最小值、整数索引、std::vector对象、std::vector光源){
向量相交点=ray.getOrigin()+ray.getDirection()*min;
向量n=Objects.at(index)->getNormalAt(intersectionPoint);
向量反射率方向=ray.getDirection()-n*向量::点(ray.getDirection(),n)*2;
光线反射光线(相交点、反射方向);
//检查光线是否与其他物体相交;
双最小值=无穷大;
int count=0,indx=-1;
用于(自动常量和对象:对象){
双距离=obj->相交(reflectionRay);
如果(最小>距离){
最小值=距离;
indx=计数;
}
计数++;
}
材料结果(0,0,0);
如果(递归深度>=5 | | indx==-1){
递归深度=0;
//检查是否为每个光源照亮了对象
用于(自动常数和光源:光源){
//冯伯林
Vector lightDirection=(灯光->getPosition()-intersectionPoint).normalize();
双nl=向量::点(n,光方向);
nl=nl>0?nl:0.0;
result=result+(Objects.at(index)->getMaterial()*light->getMaterial()*nl);
}
}
否则{
recurseDepth++;
结果=结果+getPixelColor(反射率光线、最小值、indx、对象、光源);
}
返回结果;
}
我得到的结果是:

这就是没有阴影的情况:


几个小时以来,我一直在努力寻找解决方案,但无法解决。我使用了错误的公式吗?

经过大量研究,我删除了从其他物体获得颜色的部分:

Material getPixelColor(Ray ray, double min, int index, std::vector<Object*> Objects, std::vector<Object*> lightSources) {
    Vector intersectionPoint = ray.getOrigin() + ray.getDirection() * min;
    Vector n = Objects.at(index)->getNormalAt(intersectionPoint);

    Material result(0,0,0);
    // Check if object is lit for each light source
    for (auto const& light : lightSources) {
        //create a ray to the light and check if there is an object between the two
        Vector lightDirection = (light->getPosition() - intersectionPoint).normalize();
        Ray lightRay(intersectionPoint, lightDirection);

        bool hit = false;
        for (auto const& obj : Objects) {
            double distance = obj->Intersect(lightRay);
            if (INFINITY > distance && distance > 0.0001) {
                hit = true;
                break;
            }
        }

        if (!hit) {
            // Blinn-Phong
            double nl = Vector::dot(n, lightDirection);

            // clamp nl between 0 and 1
            if (nl > 1.0) {
                nl = 1.0;
            }
            else if (nl < 0.0) {
                nl = 0.0;
            }

            result = result + (Objects.at(index)->getMaterial() * nl);
        }
    }
    return result;
}
材质getPixelColor(光线、双最小值、整数索引、std::vector对象、std::vector光源){
向量相交点=ray.getOrigin()+ray.getDirection()*min;
向量n=Objects.at(index)->getNormalAt(intersectionPoint);
材料结果(0,0,0);
//检查是否为每个光源照亮了对象
用于(自动常数和光源:光源){
//创建光线到灯光,并检查两者之间是否存在对象
Vector lightDirection=(灯光->getPosition()-intersectionPoint).normalize();
光线光线光线(相交点、光线方向);
bool-hit=false;
用于(自动常量和对象:对象){
双距离=obj->相交(光线);
如果(无穷大>距离&距离>0.0001){
命中=真;
打破
}
}
如果(!命中){
//冯伯林
双nl=向量::点(n,光方向);
//将nl夹在0和1之间
如果(nl>1.0){
nl=1.0;
}
否则如果(nl<0.0){
nl=0.0;
}
结果=结果+(Objects.at(index)->getMaterial()*nl);
}
}
返回结果;
}
所以我得到了想要的结果: