射线施法者,cast_ray函数错误地解释了遮挡的光线

时间:2016-11-17 11:19:58

标签: python raycasting

我收到的错误是说我没有考虑模糊的光线,而且当光线被遮挡时,我的镜面反射会被添加。这就是所添加的镜面反射部分是x表示我的Color类的r,g,orb:light.color.x * s.finish.specular * specIntense

def in_shadow (sphere_list, sphere, ray_to_light, light):
    new_list = list()
    for s in sphere_list:
        if sphere != s:
            new_list.append(s)
    for s in new_list:
        if sphere_intersection_point(ray_to_light, s):
            x1 = ray_to_light.pt.x - light.pt.x
            y1 = ray_to_light.pt.y - light.pt.y
            z1 = ray_to_light.pt.z - light.pt.z
            dist1 = math.sqrt(x1 + y1 + z1)
            x2 = ray_to_light.pt.x - s.center.x
            y2 = ray_to_light.pt.y - s.center.y
            z2 = ray_to_light.pt.z - s.center.z
            dist2 = math.sqrt(x2 + y2 + z2)
            # distance to light, distance to sphere
            # check if distance to sphere < distance to light
            # if so return 0
            if dist2 < dist1:
                return 0
    return 1

def cast_ray(ray, sphere_list, color, light, point):
        # count = 0
    dist = -1
    cp = Color(1.0, 1.0, 1.0)
    for s in sphere_list:
        if sphere_intersection_point(ray, s):
            # count += 1
            p = sphere_intersection_point(ray, s)
            vec = vector_from_to(s.center, p)
            N = normalize_vector(vec)
            norm_scaled = scale_vector(N, 0.01)
            pe = translate_point(p, norm_scaled)
            l = vector_from_to(pe, light.pt)
            l_dir = normalize_vector(l)
            dot = dot_vector(N, l_dir)
            r = Ray(pe, l_dir)
            dotNScaled = dot * 2
            reflecVec = difference_vector(l_dir, scale_vector(N, dotNScaled))
            V = vector_from_to(point, pe)
            Vdir = normalize_vector(V)
            spec = dot_vector(reflecVec, Vdir)
            m = in_shadow(sphere_list, s, r, light)
            if (dot <= 0):
                m = 0
            x = (ray.pt.x - p.x) ** 2
            y = (ray.pt.y - p.y) ** 2
            z = (ray.pt.z - p.z) ** 2
            curdist = math.sqrt(x + y + z)
            # print curdist
            if (dist < 0) or (dist > curdist):
                dist = curdist
                if (spec <= 0 ):
                    r =  ( s.color.r * s.finish.ambient * color.r ) \
                        + ( light.color.r * s.finish.diffuse * dot * s.color.r * m )
                    g =  ( s.color.g * s.finish.ambient * color.g ) \
                        + (light.color.g * s.finish.diffuse * dot * s.color.g * m ) 
                    b =  ( s.color.b * s.finish.ambient * color.b ) \
                        + (light.color.b * s.finish.diffuse * dot * s.color.b * m )
                    cp = Color(r, g, b)
                if ( spec >= 0 ):
                    specIntense = spec ** (1/s.finish.roughness)
                    print type(s.finish.diffuse)
                    r = (s.color.r * s.finish.ambient * color.r) \
                        + (light.color.r * s.finish.diffuse * dot * s.color.r * m) \
                        + (light.color.r * s.finish.specular * specIntense)
                    g = (s.color.g * s.finish.ambient * color.g) \
                        + (light.color.g * s.finish.diffuse * dot * s.color.g * m) \
                        + (light.color.g * s.finish.specular * specIntense)
                    b = (s.color.b * s.finish.ambient * color.b) \
                        + (light.color.b * s.finish.diffuse * dot * s.color.b * m) \
                        + (light.color.b * s.finish.specular * specIntense)
                    cp = Color(r, g, b)
    # if count > 1:
        # print 'intersects two!'
    return cp

我认为在某个地方,我不考虑球体在它前面有另一个球体的情况,因此镜面部分在它不应该被添加到它时,在第一个球体后面创建这个奇怪的白光不应该在那里。我确定这个代码中有一个错误,但我找不到它。

0 个答案:

没有答案