blob: 61e34ec4330a16e89b4aa438d47319b4001b3d55 (
plain)
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
|
//
// Created by Keuin on 2022/4/15.
//
#include "material_dielectric.h"
#include "tracelog.h"
bool material_dielectric::scatter(ray3d &r, const object &hit_obj, double hit_t, random_uv_gen_3d &ruvg) const {
const auto hit_p = r.at(hit_t);
assert(hit_obj.is_on(hit_p));
auto n = hit_obj.normal_vector(hit_p);
auto ri_ = ri_inv;
auto cos1 = dot(r.direction(), n); // -cos(a1)
if (cos1 > 0) {
// the ray is started from the object's inner,
// use normal vector and ri on the surface's inner side
n = -n;
ri_ = 1.0 / ri_;
} else {
cos1 = -cos1;
}
vec3d r2;
// determine reflection or refraction using Schlick's Approximation.
if (reflectance(cos1, ri_) > ruvg.range01_scalar()) {
// reflect
TRACELOG(" reflect (dielectric material, schlick, ri=%-10f)\n", ri_);
r2 = n.reflect(r.direction());
} else {
// refract
TRACELOG(" refract (dielectric material, schlick, ri=%-10f)\n", ri_);
r2 = n.refract<true>(r.direction(), ri_);
}
r.direction(r2.unit_vec());
r.source(hit_p);
return true;
}
|