-
Notifications
You must be signed in to change notification settings - Fork 3
/
dielectric.m
41 lines (37 loc) · 1.33 KB
/
dielectric.m
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
classdef dielectric
properties
ref_idx;
end
methods
%% Constructor
function obj = dielectric(ri)
obj.ref_idx = ri;
end
%% Calculate scatter of the ray
function [flag, attenuation, scattered] = scatter(obj, ray_in, rec)
reflected = reflect(ray_in.direction, rec.normal);
attenuation = ones(3, 1);
if(ray_in.direction'*rec.normal > 0)
outward_normal = -rec.normal;
ni_over_nt = obj.ref_idx;
cosine = obj.ref_idx * (ray_in.direction'*rec.normal) / norm(ray_in.direction);
else
outward_normal = rec.normal;
ni_over_nt = 1./obj.ref_idx;
cosine = -(ray_in.direction'*rec.normal) / norm(ray_in.direction);
end
[flag_ref, refracted] = refract(ray_in.direction, outward_normal, ni_over_nt);
if(flag_ref)
reflect_prob = schlick(cosine, obj.ref_idx);
else
reflect_prob = 1;
end
if(rand(1) < reflect_prob)
scattered = ray(rec.p, reflected);
else
scattered = ray(rec.p, refracted);
end
flag = true;
end
end
end