1、光线跟踪伪程序
// ray:光线
// isReflectLightRay:指示是否灯光反射的光线
Color RayTracing(ray, isReflectLightRay)
{
if(ray not intersect object) return 0;
intsectPoint = ray.intersect(obj);
isInShadow = is intsectPoint in shadow;
undirectLightReflectColor = RayTracing(reflectRay, true); // 间接光照
if(!isReflectLightRay)
{
for each light
if(!isInShadow) // 直接光照
lightColor += ComputeLightColor(intsectPoint, light);
if(is reflect material) // 反射颜色
reflectColor = RayTracing(reflectRay, false);
if(is refract material) // 折射颜色
refractColor = RayTracing(refractRay, false);
color = textureColor*(lightColor+undirectLightReflectColor)+reflectColor+refractColor;
if(isInShadow) // 阴影
return color*shadowColor;
return color;
}
for each light
if(!isInShadow)
directLightRelfectColor += colorLight*matReflect;
return 0.8*(directLightRelfectColor+undirectLightReflectColor);
}
// 调用
for each pixel
RayTracing(ray, false)
isReflectLightRay指示是否光的反射,用于区分主射线和折射反射光线,如果是光的反射,就不用计算光照,只需要计算直接光反射和间接光照。