1

我在网上阅读了很多光线追踪算法。但是,我对阴影和阴影没有清楚的了解。根据我的理解,下面的伪代码是否正确?

for each primitive 
    check for intersection
    if there is one 
         do color be half of the background color
         Ishadow = true
         break

for each ambient light in environment        
    calculate light contribution to the color

if ( Ishadow == false ) 
   for each point light 
     calculate diffuse shading
     calculate reflection direction
     calculate specular light


trace for reflection ray // (i)  
add color returned from i after multiplied by some coefficient

trace for refraction ray // (ii)
add color returned from ii after multiplied by some coefficient

return color value calculated until this point
4

2 回答 2

0

伪代码的问题在于它很容易获得足够的“伪”,以至于它变成了我们试图通过远离自然语言来避免的歧义。“颜色是背景颜色的一半?” 这条线在您遍历光源之前出现的事实令人困惑。在迭代光源之前如何设置 Ishadow?

也许更好的描述是:

given a ray in space
    find nearest object with which ray intersects
    for each point light
        if normal at surface of intersected object points toward light (use dot product for this)
            cast a ray into space from the surface toward the light
                if ray intersection is closer than light* light is shadowed at this point

*如果您在阴影中看到奇怪的伪影,那么每个程序员在编写他们的第一个光线追踪器时都会犯一个错误。浮点(或双精度)数学是不精确的,在进行阴影跟踪时,您会经常(大约一半时间)重新与自己相交。如果没有图表,解释有点难以描述,但让我看看我能做什么。

如果您在球体表面上有一个交点,在大多数情况下,该点在浮点寄存器中的表示在数学上并不精确。它要么稍微在球体内部,要么稍微在球体外部。如果它在球体内,并且您尝试对光源进行交叉测试,则最近的交叉点将是球体本身。相交距离将非常小,因此您可以简单地拒绝任何比 0.000001 单位更近的阴影射线相交。如果您的几何体都是凸的并且无法合法地对其自身进行阴影化,那么您可以在进行阴影测试时直接跳过对球体的测试。

于 2012-10-29T17:31:57.533 回答
0

您应该将阴影与正常的光线追踪路径整合:对于每个屏幕像素,您发送一条穿过场景的光线并最终确定最近的对象交叉点:在最接近的对象交叉点处,您首先会读出像素颜色(该点对象的纹理),除了计算反射向量等(使用法线向量)之外,您现在可以从该交叉点向场景中的每个光源投射光线:如果这些光线在击中光源之前与其他对象相交,则相交点处于阴影中,您可以相应地调整该点的最终颜色。

于 2012-10-29T17:27:43.803 回答