def checkIntersection(point, dir, source): reboundPoint = Point( w + 1, h + 1) #Punto considerado como infinito, se va a reemplazar free = True segWhereRebound = 0 length = ray.length(dir) length2 = ray.length(ray.normalize(dir)) for seg in segments: # La idea es en vez de revisar todos los segmentos escoger el más cercano if (ray.length(seg[0] - point) <= 350 and ray.length(seg[1] - point) <= 350): #Radio de illuminación dist = ray.raySegmentIntersect( point, dir, seg[0], seg[1]) #Compruebo si hay intersección if dist[0] != -1 and dist[0] < length2: #Hay rebote #print("Rebotó en" + str(dist[1].__str__() ) ) free = False #Solo cambiarlo si es el punto intersecado es el más cercano al punto de todos los que han habido if ((dist[1].x - point.x) <= reboundPoint.x and (dist[1].y - point.y) <= reboundPoint.y): reboundPoint = dist[1] segWhereRebound = seg if free == False: return [ True, reboundPoint, segWhereRebound ] #Choca con un segmento, por ende hay que devolver que sí chocó y en dónde. #print(reboundPoint) else: return [False, 0, 0] #En caso opuesto, el rayo no choca.
def raytrace(luces): #Raytraces the scene progessively sources = luces while True: #MONTE CARLO start = timeit.default_timer( ) #Registra el tiempo cuando se cálcula un píxel point = Point(random.uniform(0, w), random.uniform(0, h)) pixel = 0 #pixel color for source in sources: #calculates direction to light source dir = source - point length = ray.length(dir) intensity = (1 - (length / h))**2 #print(intensity) if intensity > 0: if ray.distanceBetweenPoints(point, source) <= radioDeIlluminacion: color = PathTraycing(dir, point, 0, source) if color[0] != 0: if color[2] > 0: #Luz directa sin rebotes values = (ref[int(point.y)][int(point.x)])[:3] #print(values) values = values * intensity * light else: #Luz con rebota colorSegmento = (ref[int(color[1].y)][int( color[1].x)])[:3] values = colorSegmento * intensity * light pixel += values #average pixel value and assign px[int(point.x)][int(point.y)] = pixel // len(sources) stop = timeit.default_timer( ) # Registra el tiempo cuando finalizan los cálculos de un píxel. print('Time (s): ', (stop - start) * 1000) #Imprime el tiempo de cálculo para un píxel
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 sources = [Point(10, 10)] if (pygame.mouse.get_pressed()[0] and sources[0].x == pygame.mouse.get_pos()[0] and sources[0].y == pygame.mouse.get_pos()[1]): mouse_x, mouse_y = pygame.mouse.get_pos() sources = [Point(mouse_x, mouse_y)] #point = Point (100, 100) 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 = ray.length(dir) free = True for seg in segments: #check if ray intersects with segment dist = ray.raySegmentIntersect(point, dir, seg[0], seg[1]) #if intersection, or if intersection is closer than light source if dist != -1 and dist < length: 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 checkIntersection(point, dir, source): reboundPoint = Point( 501, 501) #Punto considerado como infinito, se va a reemplazar free = True segWhereRebound = 0 material = 0 length = ray.length(dir) length2 = ray.length(ray.normalize(dir)) for seg in segments: # La idea es en vez de revisar todos los segmentos escoger el más cercano typeOfSegment = seg[1] segmentPoints = seg[0] materialType = seg[2] if (typeOfSegment == "square"): dist = ray.raySegmentIntersect(point, dir, segmentPoints[0], segmentPoints[1]) if dist[0] != -1 and dist[0] < length2: #Hay rebote #print("Rebotó en" + str(dist[1].__str__() ) ) free = False #Solo cambiarlo si es el punto intersecado es el más cercano al punto de todos los que han habido if ((dist[1].x - point.x) <= reboundPoint.x and (dist[1].y - point.y) <= reboundPoint.y): reboundPoint = dist[1] segWhereRebound = segmentPoints material = materialType else: #Es un círculo r = segmentPoints[1] center = segmentPoints[0] if ray.length(center - point) <= 80: dist = ray.rayCircleIntersection(point, center, r, source) t = ray.length(source - point) if dist != -1 and t > length2: if ((dist[0].x - point.x) <= reboundPoint.x and (dist[0].y - point.y) <= reboundPoint.y and len(dist) > 1): free = False reboundPoint = center #print("Punto: ", point, "Luz: ", source) if free == False: #print(reboundPoint) if reboundPoint.x == 96 and reboundPoint.y == 409: return [True, reboundPoint] reboundPoint = ray.movePoint(segWhereRebound[0], segWhereRebound[1], dir, reboundPoint) return [ True, reboundPoint, segWhereRebound, material ] #Choca con un segmento, por ende hay que devolver que sí chocó y en dónde. else: return [False, 0, 0] #En caso opuesto, el rayo no choca.
def raytrace(): #Raytraces the scene progessively while True: #MONTE CARLO point = Point(random.uniform(0, w), random.uniform(0, h)) pixel = 0 #pixel color for source in sources: if point.x > 386: px[int(point.x)][int(point.y)] = pixel // len(sources) break #calculates direction to light source if ray.distanceBetweenPoints(point, source[0]) <= radioDeIlluminacion: dir = source[0] - point length = ray.length(dir) color = PathTraycing(dir, point, 0, source[0]) if color[0] != 0: if color[2] > 0: #Luz directa sin rebotes intensity = ((1 - (length / w))**2) * 0.5 values = (ref[int(point.y)][int(point.x)])[:3] colorSeg = (ref[int(color[1].y)][int(color[1].x)])[:3] #print(distance) if point.x > 296 and point.x < 306: values = (colorSeg * intensity) * source[1] else: values = values * intensity * source[1] else: #Luz sin rebote intensity = ((1 - (length / w))**2) colorSeg = (ref[int(color[1].y)][int(color[1].x)])[:3] #values = (ref[int(point.y)][int(point.x)])[:3] #print(colorSeg, color[1]) values = colorSeg * intensity * source[1] pixel += values #average pixel value and assign px[int(point.x)][int(point.y)] = pixel // len(sources)