-
Notifications
You must be signed in to change notification settings - Fork 0
/
material.h
151 lines (127 loc) · 4.71 KB
/
material.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef MATERIAL_H
#define MATERIAL_H
#include "rtweekend.h"
#include "hittable.h"
#include "texture.h"
#include "onb.h"
// struct hit_record;
class material {
public:
virtual bool scatter(const ray& r_in, const hit_record& rec,
color& attenuation, ray& scattered, double& pdf) const {
return false;
};
virtual double scattering_pdf(const ray& r_in, const hit_record& rec,
const ray& scaterred) const {
return 0;
}
virtual color emitted(const ray& r_in, const hit_record& rec,
double u, double v, const point3& p) const {
return color(0, 0, 0);
}
};
// diffuse
class lambertian : public material {
public:
shared_ptr<texture> albedo;
public:
lambertian(shared_ptr<texture> a) : albedo(a) {}
lambertian(const color& a) : albedo(make_shared<solid_color>(a)) {}
virtual bool scatter(const ray& r_in, const hit_record& rec,
color& attenuation, ray& scattered, double& pdf) const override {
auto scatter_direction = random_in_hemisphere(rec.normal);
// onb uvw;
// uvw.build_from_w(rec.normal);
// auto scatter_direction = uvw.local(random_cosine_direction());
// Catch degenerate scatter direction
if (scatter_direction.near_zero())
scatter_direction = rec.normal;
// scatter_direction = uvw.w();
scattered = ray(rec.p, unit_vector(scatter_direction), r_in.time());
attenuation = albedo->value(rec.u, rec.v, rec.p);
pdf = 0.5 / pi;
// pdf = dot(uvw.w(), scattered.direction()) / pi;
return true;
}
double scattering_pdf(const ray& r_in, const hit_record& rec,
const ray& scattered) const {
auto cosine = dot(rec.normal, unit_vector(scattered.direction()));
return cosine < 0 ? 0 : cosine / pi;
}
};
class metal : public material {
public:
color albedo;
double fuzz;
public:
metal(const color& a, double f) : albedo(a), fuzz(f < 1 ? f : 1) {}
virtual bool scatter(const ray& r_in, const hit_record& rec,
color& attenuation, ray& scattered, double& pdf) const override {
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
scattered = ray(rec.p, reflected + fuzz * random_in_unit_sphere(),
r_in.time());
attenuation = albedo;
return (dot(scattered.direction(), rec.normal) > 0);
}
};
class dielectric : public material {
private:
static double reflectance(double cosine, double ref_idx) {
// Use Schlick's approximation for reflectance.
auto r0 = (1 - ref_idx) / (1 + ref_idx);
r0 = r0 * r0;
return r0 + (1 - r0) * pow((1 - cosine), 5);
}
public:
double ir; // Index of Refraction
public:
dielectric(double index_of_refraction) : ir(index_of_refraction) {}
virtual bool scatter(const ray& r_in, const hit_record& rec,
color& attenuation, ray& scattered, double& pdf) const override {
attenuation = color(1.0, 1.0, 1.0);
double refraction_ratio = rec.front_face ? (1.0 / ir) : ir;
vec3 unit_direction = unit_vector(r_in.direction());
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
double sin_theta = sqrt(1 - cos_theta * cos_theta);
bool cannot_refract = refraction_ratio * sin_theta > 1.0;
vec3 direction;
if (cannot_refract || random_double() < reflectance(cos_theta, refraction_ratio))
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;
}
};
class diffuse_light : public material {
public:
shared_ptr<texture> emit;
public:
diffuse_light(shared_ptr<texture> a) : emit(a) {}
diffuse_light(color c) : emit(make_shared<solid_color>(c)) {}
virtual bool scatter(const ray& r_in, const hit_record& rec,
color& attenuation, ray& scattered, double& pdf) const override {
return false;
}
virtual color emitted(const ray& r_in, const hit_record& rec,
double u, double v, const point3& p) const override {
if (rec.front_face)
return emit->value(u, v, p);
else
return color(0, 0, 0);
}
};
class isotropic : public material {
public:
shared_ptr<texture> albedo;
public:
isotropic(color c) : albedo(make_shared<solid_color>(c)) {}
isotropic(shared_ptr<texture> a) : albedo(a) {}
virtual bool scatter(const ray& r_in, const hit_record& rec,
color& attenuation, ray& scattered, double& pdf) const override {
scattered = ray(rec.p, random_in_unit_sphere(), r_in.time());
attenuation = albedo->value(rec.u, rec.v, rec.p);
return true;
}
};
#endif