-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmaterial.hpp
More file actions
133 lines (112 loc) · 4.37 KB
/
Copy pathmaterial.hpp
File metadata and controls
133 lines (112 loc) · 4.37 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef RT_SYCL_MATERIAL_HPP
#define RT_SYCL_MATERIAL_HPP
#include <iostream>
#include "hit_record.hpp"
#include "texture.hpp"
#include "vec.hpp"
#include "visit.hpp"
struct lambertian_material {
lambertian_material() = default;
lambertian_material(const color& a)
: albedo { solid_texture { a } } {}
lambertian_material(const texture_t& a)
: albedo { a } {}
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation,
ray& scattered) const {
LocalPseudoRNG rng { toseed(r_in.direction(), r_in.origin()) };
vec scatter_direction = rec.normal + rng.unit_vec();
scattered = ray(rec.p, scatter_direction, r_in.time());
// Attenuation of the ray hitting the object is modified based on the color
// at hit point
attenuation *= dev_visit([&](auto&& t) { return t.value(rec); }, albedo);
return true;
}
color emitted(const hit_record& rec) { return color(0.f, 0.f, 0.f); }
texture_t albedo;
};
struct metal_material {
metal_material() = default;
metal_material(const color& a, real_t f)
: albedo { a }
, fuzz { std::clamp(f, 0.0f, 1.0f) } {}
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation,
ray& scattered) const {
LocalPseudoRNG rng { toseed(r_in.direction(), r_in.origin()) };
vec reflected = reflect(unit_vector(r_in.direction()), rec.normal);
scattered = ray(rec.p, reflected + fuzz * rng.in_unit_ball(), r_in.time());
// Attenuation of the ray hitting the object is modified based on the color
// at hit point
attenuation *= albedo;
return (dot(scattered.direction(), rec.normal) > 0);
}
color emitted(const hit_record& rec) { return color(0, 0, 0); }
color albedo;
real_t fuzz;
};
struct dielectric_material {
dielectric_material() = default;
dielectric_material(real_t ri, const color& albedo)
: ref_idx { ri }
, albedo { albedo } {}
// Schlick's approximation for reflectance
real_t reflectance(real_t cosine, real_t ref_idx) const {
auto r0 = (1 - ref_idx) / (1 + ref_idx);
r0 *= r0;
return r0 + (1 - r0) * sycl::pow((1 - cosine), 5.0f);
}
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation,
ray& scattered) const {
// Attenuation of the ray hitting the object is modified based on the color
// at hit point
LocalPseudoRNG rng { toseed(r_in.direction(), r_in.origin()) };
attenuation *= albedo;
real_t refraction_ratio = rec.front_face ? (1.0f / ref_idx) : ref_idx;
vec unit_direction = unit_vector(r_in.direction());
real_t cos_theta = sycl::fmin(-sycl::dot(unit_direction, rec.normal), 1.0f);
real_t sin_theta = sycl::sqrt(1.0f - cos_theta * cos_theta);
bool cannot_refract = refraction_ratio * sin_theta > 1.0f;
vec direction;
if (cannot_refract || reflectance(cos_theta, refraction_ratio) > rng.real())
direction = reflect(unit_direction, rec.normal);
else
direction = refract(unit_direction, rec.normal, refraction_ratio);
scattered = ray(rec.p, direction, r_in.time());
return true;
}
color emitted(const hit_record& rec) { return color(0, 0, 0); }
// Refractive index of the glass
real_t ref_idx;
// Color of the glass
color albedo;
};
struct lightsource_material {
lightsource_material() = default;
lightsource_material(const texture_t& a)
: emit { a } {}
lightsource_material(const color& a)
: emit { solid_texture { a } } {}
template <typename... T> bool scatter(T&...) const { return false; }
color emitted(const hit_record& rec) {
return dev_visit([&](auto&& t) { return t.value(rec); }, emit);
}
texture_t emit;
};
struct isotropic_material {
isotropic_material(const color& a)
: albedo { solid_texture { a } } {}
isotropic_material(texture_t& a)
: albedo { a } {}
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation,
ray& scattered) const {
LocalPseudoRNG rng { toseed(r_in.direction(), r_in.origin()) };
scattered = ray(rec.p, rng.in_unit_ball(), r_in.time());
attenuation *= dev_visit([&](auto&& t) { return t.value(rec); }, albedo);
return true;
}
color emitted(const hit_record& rec) { return color(0, 0, 0); }
texture_t albedo;
};
using material_t =
std::variant<lambertian_material, metal_material, dielectric_material,
lightsource_material, isotropic_material>;
#endif