def especularidadTrue(point, source, seg): #ecuacion recta punto-fuente m1 = (point.y - source.y) / (point.x - source.x) b1 = point.y - (m1 * point.x) #ecuacion recta ambos puntos del segmento m2 = (seg[1].y - seg[0].y) / (seg[1].x - seg[0].x) b2 = seg[1].y - (m2 * seg[1].x) #despejamos x x = (b2 - b1) / (m1 - m2) #buscamos la y y = (m1 * x) + b1 interseccion = Point(x, y) #distancia desde origen hasta interseccion, en eje x d = x - source.x if (d < 0): reflejo = Point(x + d, source.y) else: reflejo = Point(x + d, source.y) #ecuacion de la recta del reflejo m3 = (y - reflejo.y) / (x - reflejo.x) b3 = y - (m3 * x) dir = interseccion - reflejo length = rt.length(dir) length2 = rt.length(rt.normalize(dir)) dist = rt.raySegmentIntersect(point, dir, seg[0], seg[1]) if dist > 0 and length2 > dist: #return [length, m3, b3] return length #return [0,0,0] return 0
def rayRefraction(point, source, seg): distancia = point.distance(seg[0], seg[1]) nuevaFuente = Point(source.x + (distancia * 2), source.y) dir = nuevaFuente - point length = rt.length(dir) length2 = rt.length(rt.normalize(dir)) dist = rt.raySegmentIntersect(point, dir, seg[0], seg[1]) if dist > 0 and length2 > dist: return length return 0
def especularidadFalse(point, source, seg): medio = Point((seg[0].x + seg[1].x) / 2, (seg[0].y + seg[1].y) / 2) nuevaFuente = Point(2 * medio.x - source.x, 2 * medio.y - source.y) dir = nuevaFuente - point length = rt.length(dir) length2 = rt.length(rt.normalize(dir)) dist = rt.raySegmentIntersect(point, dir, seg[0], seg[1]) if dist > 0 and length2 > dist: return length return 0
def raytrace(): #Raytraces the scene progessively while True: #random point in the image point = Point(random.uniform(0, 500), random.uniform(0, 500)) #pixel color pixel = 0 for source in sources: #calculates direction to light source dir = source - point #add jitter #dir.x += random.uniform(0, 25) #dir.y += random.uniform(0, 25) #distance between point and light source length = rt.length(dir) #normalized distance to source length2 = rt.length(rt.normalize(dir)) free = True for seg in segments: #check if ray intersects with segment dist = rt.raySegmentIntersect(point, dir, seg[0], seg[1]) #if intersection, or if intersection is closer than light source if dist > 0 and length2 > dist: free = False break if free: intensity = (1 - (length / 500))**2 #print(len) #intensity = max(0, min(intensity, 255)) values = (ref[int(point.y)][int(point.x)])[:3] #combine color, light source and light color values = values * intensity * light #add all light sources pixel += values #average pixel value and assign px[int(point.x)][int(point.y)] = pixel // len(sources)
def raytrace(): #Raytraces the scene progessively while True: #random point in the image point = Point(random.uniform(0, 500), random.uniform(0, 500)) #pixel color pixel = 0 for source in sources: #calculates direction to light source dir = source - point #add jitter #dir.x += random.uniform(0, 25) #dir.y += random.uniform(0, 25) #distance between point and light source length = rt.length(dir) #normalized distance to source length2 = rt.length(rt.normalize(dir)) listSeg = [] free = True especularidadF = 0 especularidadT = 0 especularidadC = 0 refraccion = 0 colorB = 0 transparencia = 1 transparenciaR = 1 for seg in segments: #check if ray intersects with segment dist = rt.raySegmentIntersect(point, dir, seg[0], seg[1]) if seg[2] == 1: especularidadF = especularidadFalse(point, source, seg) if seg[2] == 2: #data = especularidadTrue(point, source, seg) especularidadT = especularidadTrue(point, source, seg) if seg[2] == 4: refraccion = rayRefraction(point, source, seg) #if intersection, or if intersection is closer than light source if dist > 0 and length2 > dist: if seg[2] == 3: transparencia = 3 else: free = False if seg[2] == 4 and refraccion != 0: free = True transparenciaR = 3 break dir = source - point for circle in circles: #if rt.inRadio(point, circle[0], circle[1]): #free = False #break distanciaFuente = point.distance(circle[0], source) dist = rt.rayCircleIntersect(point, dir, circle[0], circle[1]) dist -= circle[1] * distanciaFuente * 0.00001 if circle[2] == False: especularidadC = especularidadFalseCircle( point, source, circle) colorB = circle[3] if dist > 0 and length2 > dist: free = False break if free: intensity = (1 - (length / 500))**2 intensity /= transparencia intensity /= transparenciaR #intensity = max(0, min(intensity, 255)) values = (ref[int(point.y)][int(point.x)])[:3] #combine color, light source and light color values = values * intensity * light #add all light sources pixel += values if especularidadF != 0: intensity = (1 - (especularidadF / 500))**2 intensity /= 2 #print(len) #intensity = max(0, min(intensity, 255)) values = (ref[int(point.y)][int(point.x)])[:3] #combine color, light source and light color values = values * intensity * light #add all light sources pixel += values if especularidadT != 0: intensity = (1 - (especularidadT / 500))**2 intensity /= 2 #print(len) #intensity = max(0, min(intensity, 255)) values = (ref[int(point.y)][int(point.x)])[:3] #combine color, light source and light color values = values * intensity * light #add all light sources pixel += values if refraccion != 0: intensity = (1 - (refraccion / 500))**2 intensity /= 2 #print(len) #intensity = max(0, min(intensity, 255)) values = (ref[int(point.y)][int(point.x)])[:3] #combine color, light source and light color values = values * intensity * light #add all light sources pixel += values if especularidadC != 0: intensity = (1 - (especularidadC / 500))**2 intensity /= 2 #print(len) #intensity = max(0, min(intensity, 255)) values = (ref[int(point.y)][int(point.x)])[:3] #combine color, light source and light color if colorB != 0: intensity *= 2 values = np.asarray(colorB) * intensity * 2 * light else: values = values * intensity * light #add all light sources pixel += values #average pixel value and assign px[int(point.x)][int(point.y)] = pixel // len(sources)