Python 光线投射器,投射光线功能不正确地解释遮挡光线
我得到了一个错误,说我没有考虑被遮挡的光线,当光线被遮挡时,我的镜面反射增加了。这就是要添加到的镜面反射部分,x表示我的颜色类别的r,g,orb:light.Color.x*s.finish.specular*spectrensePython 光线投射器,投射光线功能不正确地解释遮挡光线,python,raycasting,Python,Raycasting,我得到了一个错误,说我没有考虑被遮挡的光线,当光线被遮挡时,我的镜面反射增加了。这就是要添加到的镜面反射部分,x表示我的颜色类别的r,g,orb:light.Color.x*s.finish.specular*spectrense def in_shadow (sphere_list, sphere, ray_to_light, light): new_list = list() for s in sphere_list: if sphere != s:
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
def in_shadow(球体列表、球体、光线到灯光、灯光):
新建列表=列表()
对于球体列表中的s:
如果球体!=s:
新列表。追加(个)
对于新列表中的:
如果球体与点相交(光线到灯光,s):
x1=光线到光线.pt.x-光线.pt.x
y1=光线到光线.pt.y-光线.pt.y
z1=光线到光线.pt.z-光线.pt.z
dist1=数学sqrt(x1+y1+z1)
x2=光线到光线点x-s.center.x
y2=光线到光线点y-s中心y
z2=光线到光线.pt.z-s.center.z
dist2=数学sqrt(x2+y2+z2)
#到灯光的距离,到球体的距离
#检查到球体的距离是否<到灯光的距离
#如果是,返回0
如果dist2小于dist1:
返回0
返回1
def投射光线(光线、球体列表、颜色、灯光、点):
#计数=0
dist=-1
cp=颜色(1.0,1.0,1.0)
对于球体列表中的s:
如果球体与点相交(光线,s):
#计数+=1
p=球体\交点\点(光线,s)
vec=从_到(s.中心,p)的向量_
N=标准化_向量(vec)
norm_scaled=比例向量(N,0.01)
pe=平移点(p,标准值)
l=向量从到(pe,light.pt)
l_dir=规范化_向量(l)
点=点向量(N,l_dir)
r=射线(pe,l_方向)
dotNScaled=dot*2
reflecVec=差分向量(l\U方向,刻度向量(N,点刻度))
V=从_到(点,pe)的向量
Vdir=标准化_向量(V)
spec=点向量(reflecVec,Vdir)
m=阴影中(球体列表、s、r、灯光)
如果(点电流):
电流
如果(等级库=0):
等级=等级**(1/s表面粗糙度)
打印类型(s.finish.diffuse)
r=(s.color.r*s.finish.ambient*color.r)\
+(灯光、颜色、r*s.finish.diffuse*dot*s.color.r*m)\
+(浅色、颜色、r*s.finish、镜面反射*镜面强烈)
g=(s.color.g*s.finish.ambient*color.g)\
+(灯光.color.g*s.finish.漫反射*dot*s.color.g*m)\
+(浅色、颜色、g*s.finish、镜面反射、高强度)
b=(s.color.b*s.finish.ambient*color.b)\
+(灯光、颜色、b*s.finish.diffuse*dot*s.color.b*m)\
+(浅色、颜色、b*s.finish、镜面反射、高强度)
cp=颜色(r、g、b)
#如果计数>1:
#打印“两个相交!”
返回cp
我想在某个地方,我没有考虑到球体前面有另一个球体的情况,因此镜面反射部分在不应该的时候被添加到球体上,在第一个球体后面产生了奇怪的白光,而第一个球体不应该在那里。我确信这段代码中的某个地方有一个bug,但我找不到它