-
Notifications
You must be signed in to change notification settings - Fork 0
/
RT_integrator.py
66 lines (49 loc) · 2.4 KB
/
RT_integrator.py
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
# integrator class such as whitted ray tracing / path tracing
# It is the rendering equation solver.
import RT_utility as rtu
import RT_ray as rtr
class Integrator():
def __init__(self) -> None:
pass
def compute_scattering(self, rGen_ray, maxDepth, scene):
pass
class Whitted():
def __init__(self) -> None:
pass
class PathTracing():
def __init__(self, bDlight=True) -> None:
self.bool_direct_lighting = bDlight
pass
def compute_scattering(self, rGen_ray, scene, maxDepth):
if maxDepth <= 0:
return rtu.Color()
found_hit = scene.find_intersection(rGen_ray, rtu.Interval(0.000001, rtu.infinity_number))
if found_hit == True:
hinfo = scene.getHitList()
hmat = hinfo.getMaterial()
sinfo = hmat.scattering(rGen_ray, hinfo)
if sinfo is None: # if no scattering (light)
return hmat.emitting() # emit the light color
Le = rtu.Color()
if self.bool_direct_lighting:
for light in scene.point_light_list: # for now handle only point lights
tolight_dir = light.center - hinfo.getP()
tolight_ray = rtr.Ray(hinfo.getP(), tolight_dir)
max_distance = tolight_dir.len()
occlusion_hit = scene.find_occlusion(tolight_ray, rtu.Interval(0.000001, max_distance))
# if not occluded.
if not occlusion_hit:
# accumulate all unoccluded light
Le = Le + (light.material.emitting() * (1/max_distance))
return Le + self.compute_scattering(rtr.Ray(hinfo.getP(), sinfo.scattered_ray.getDirection()), scene, maxDepth-1) * sinfo.attenuation_color
return scene.getBackgroundColor()
# def background_color(self, rGen_ray):
# unit_direction = rtu.Vec3.unit_vector(rGen_ray.getDirection())
# a = (unit_direction.y() + 1.0)*0.5
# return rtu.Color(1,1,1)*(1.0-a) + rtu.Color(0.5, 0.7, 1.0)*a
# def get_color(self, rGen_ray, scene):
# found_hit = scene.find_intersection(rGen_ray, rtu.Interval(0.000001, rtu.infinity_number))
# if found_hit == True:
# tmpN = scene.getHitList().getNormal()
# return (rtu.Color(tmpN.x(), tmpN.y(), tmpN.z()) + rtu.Color(1,1,1))*0.5
# return self.background_color(rGen_ray)