光线跟踪器在底部和阴影形状上阴影更亮阴影

时间:2016-02-29 00:08:25

标签: c++ graphics 3d rendering raytracing

这是阴影的代码,虽然我在GitHub的我的存储库中拥有所有代码以便于阅读,this is the main code for the shadows,这是sphere-ray intersections的代码,这是plane-ray intersections的代码:

bool shadowed = false;
Color finalColor = closestObjectColor.Scalar(AMBIENTLIGHT); // Add ambient light to the calculation
for(const auto &lightSource : lightSources)
{
    Vector lightDir = (lightSource->position - intersectionRayPos).Normalize(); // Calculate the directional vector towards the lightSource
    FPType cosineAngle = closestObjectNormal.Dot(lightDir);
    finalColor = finalColor.Scalar(cosineAngle);

    if(cosineAngle > 0)
    {
        Ray shadowRay(intersectionRayPos, (lightSource->position - intersectionRayPos).Normalize()); // Cast a ray from the first intersection to the light

        std::vector<FPType> secondaryIntersections;
        for(const auto &sceneObject : sceneObjects)
        {
            secondaryIntersections.push_back(sceneObject->GetIntersection(shadowRay));
        }

        Vector distanceToLight = lightSource->position + (intersectionRayPos.Negative()).Normalize();
        FPType distanceToLightMagnitude = distanceToLight.Magnitude();

        for(const auto &secondaryIntersection : secondaryIntersections)
        {
            if(secondaryIntersection > TOLERANCE && secondaryIntersection <= distanceToLightMagnitude)
            {
                shadowed = true;
                finalColor = finalColor.Scalar(cosineAngle);
                break;
            }
        }
    }
}
return finalColor.Clip();

问题在于我的阴影在底部或阴影的起点较浅,这张照片中很好地说明了,你可以看到阴影的奇怪形状,我不确定是不是或者不诚实: enter image description here

在这里可以观察到相同的效果,并且可以非常明显地看到灰色球体后面的栗色球体上的阴影: enter image description here

另一个错误发生在另一个球体前面的阴影以不良方式移位(蓝色球体后面的栗色球体上的阴影):

enter image description here

1 个答案:

答案 0 :(得分:0)

我们用来计算阴影的一种简单方法是:

int hitCount = 0; for(light in allLights){     Ray point2Light = ...     object = GetObjectHit(point2Light);     if(object == light)         hitCount ++; }

float shadowDarkness =(float)hitCount /(float)allLights.count;