这是用于阴影的代码,尽管为了便于阅读,我将所有代码都放在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();
问题是我的阴影在底部或阴影的开始处较浅,这在这张图中得到了很好的说明,你也可以看到阴影的奇怪形状,我不确定这是正确的还是不诚实的:
在这里可以观察到相同的效果,并且可以非常明显地看到灰色球体后面的栗色球体上的阴影:
另一个bug是,一个球体前面的阴影以一种不好的方式移动(蓝色球体后面的栗色球体上的阴影):
发布于 2016-03-03 13:17:01
我们用来计算阴影的一种简单方法是:
int hitCount = 0;for (光线中的光线){allLights point2Light = ...object = GetObjectHit(point2Light);if(object == hitCount++) hitCount++;}
float shadowDarkness = (float)hitCount / (float)allLights.count;
https://stackoverflow.com/questions/35689999
复制相似问题