-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathTraceIntegrator.py
More file actions
73 lines (57 loc) · 2.3 KB
/
PathTraceIntegrator.py
File metadata and controls
73 lines (57 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from Algebra import RGBColour
from Algebra import BLACK, Vector3D, Cross, Normalize, Length, Dot, local_color, Ray, flip_direction, random_direction, WHITE
from objetos import Objeto, Light, ObjectQuadric
import random
from math import pow
class PathTraceIntegrator:
background = BLACK # Cor do Background
ambient = 0.5
#Initializer - creates object list
def __init__(self):
self.obj_list = []
#trace light path
def trace_ray(self, ray, depth):
difuso = BLACK
especular = BLACK
# Checando interseções com cada objeto
dist = 100
hit = False
objeto = 1
hit_point = Vector3D(0.0, 0.0, 0.0)
normal = Vector3D(0.0, 0.0, 0.0)
for obj in self.obj_list:
inter = obj.intersect(ray)
tmp_hit = inter[0]
distance = inter[1]
if tmp_hit and distance < dist:
dist = distance
objeto = obj
hit = tmp_hit
hit_point = inter[2]
normal = inter[3]
if hit: ## Se o raio bateu no objeto calculamos a cor do ponto
if isinstance(objeto, Light):
return objeto.color
else:
result = local_color(objeto, normal, ray, self.ambient)
else:
return self.background
# Calculando os Raios Secúndarios
ktot = obj.kd + obj.ks + obj.kt
aleatorio = random.random()*ktot
if depth > 0:
if aleatorio < obj.kd: ## Raio Difuso
x = random.random()
y = random.random()
dir = random_direction(x, y, normal)
new_ray = Ray(hit_point, Normalize(dir))
difuso = self.trace_ray(new_ray, depth - 1)
elif aleatorio < obj.kd + obj.ks: # ## Raio especular
L = Normalize(flip_direction(ray.d))
N = objeto.normal
R = (N * (Dot(N, L)) - L) * 2.0
new_ray = Ray(hit_point, Normalize(R))
especular = self.trace_ray(new_ray, depth - 1)
else: ## Raio Transmitido
pass
return result + difuso*objeto.trans_difusa + especular*objeto.trans_especular