-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterial.h
94 lines (79 loc) · 2.76 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
#ifndef MATERIAL_H_
#define MATERIAL_H_
#include <Eigen/Core>
#include "util.h"
class Material {
protected:
using Vec3f = Eigen::Vector3f;
public:
virtual bool Scatter(const Ray& ray_in, const HitResult& hit,
Vec3f* attenuation, Ray* scattered_ray) const = 0;
};
class Lambertian : public Material {
public:
Lambertian(const Vec3f& albedo) : albedo_(albedo) {}
bool Scatter(const Ray& ray_in, const HitResult& hit,
Vec3f* attenuation, Ray* scattered_ray) const override {
Vec3f direction = hit.normal + GetRandomVecInUnitSphere();
*scattered_ray = Ray(hit.point, direction);
*attenuation = albedo_;
return true;
}
Vec3f albedo_;
};
class Metal : public Material {
public:
Metal(const Vec3f& albedo, float fuzz) : albedo_(albedo) {
fuzz_ = std::max(std::min(fuzz, 1.f), 0.f);
}
bool Scatter(const Ray& ray_in, const HitResult& hit,
Vec3f* attenuation, Ray* scattered_ray) const override {
Vec3f reflected = Reflect(ray_in.direction().normalized(), hit.normal);
if (fuzz_ > 0) {
reflected += fuzz_ * GetRandomVecInUnitSphere();
}
*scattered_ray = Ray(hit.point, reflected);
*attenuation = albedo_;
return (scattered_ray->direction().dot(hit.normal) > 0);
}
Vec3f albedo_;
float fuzz_;
};
class Dielectric : public Material {
public:
Dielectric(float refractive_index, float fuzz)
: refractive_index_(refractive_index), fuzz_(fuzz) {
fuzz_ = std::max(std::min(fuzz, 1.f), 0.f);
}
bool Scatter(const Ray& ray_in, const HitResult& hit,
Vec3f* attenuation, Ray* scattered_ray) const override {
float ni_over_nt;
float cosine;
Vec3f outward_normal;
if (ray_in.direction().dot(hit.normal) > 0) {
outward_normal = -hit.normal;
ni_over_nt = refractive_index_;
cosine = refractive_index_ * ray_in.direction().dot(hit.normal) / ray_in.direction().norm();
} else {
outward_normal = hit.normal;
ni_over_nt = 1.0 / refractive_index_;
cosine = -ray_in.direction().dot(hit.normal) / ray_in.direction().norm();
}
Vec3f refracted_direction;
bool did_refract = Refract(ray_in.direction(), outward_normal, ni_over_nt,
&refracted_direction);
float reflect_probability = did_refract ?
ComputeSchlick(cosine, refractive_index_) : 1.0f;
*attenuation = Vec3f(1.0, 1.0, 1.0);
if (drand48() < reflect_probability) {
Vec3f reflected_direction = Reflect(ray_in.direction(), hit.normal);
*scattered_ray = Ray(hit.point, reflected_direction);
} else {
*scattered_ray = Ray(hit.point, refracted_direction);
}
return true;
}
float refractive_index_;
float fuzz_;
};
#endif // MATERIAL_H_