-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial.h
More file actions
61 lines (48 loc) · 1.25 KB
/
Copy pathmaterial.h
File metadata and controls
61 lines (48 loc) · 1.25 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
#ifndef MATERIAL_H
#define MATERIAL_H
#include "rtweekend.h"
class hit_record;
class material
{
public:
virtual ~material() = default;
virtual bool scatter(
const ray &r_in, const hit_record &rec, color &attenuation, ray &scattered) const
{
return false;
}
};
class lambertian : public material
{
public:
lambertian(const color &albedo) : albedo(albedo) {}
bool scatter(const ray &r_in, const hit_record &rec, color &attenuation, ray &scattered)
const override
{
auto scatter_direction = rec.normal + random_unit_vector();
// Catch degenerate scatter direction
if (scatter_direction.near_zero())
scatter_direction = rec.normal;
scattered = ray(rec.p, scatter_direction);
attenuation = albedo;
return true;
}
private:
color albedo;
};
class metal : public material
{
public:
metal(const color &albedo) : albedo(albedo) {}
bool scatter(const ray &r_in, const hit_record &rec, color &attenuation, ray &scattered)
const override
{
vec3 reflected = reflect(r_in.direction(), rec.normal);
scattered = ray(rec.p, reflected);
attenuation = albedo;
return true;
}
private:
color albedo;
};
#endif