-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterial.h
More file actions
94 lines (66 loc) · 2.84 KB
/
Material.h
File metadata and controls
94 lines (66 loc) · 2.84 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
#pragma once
#include "Utility.h"
#include "Hittable.h" //We shouldn't include this, but seems we need it aswell... ><
class HitInfo; //Tell the compiler : eh it exists somewhere else, trust me
class Material{
public:
virtual ~Material() = default; //Virtual default destuctor. Must me public
virtual bool Scatter(const Ray& in, const HitInfo& hit, Color& attenuation, Ray& scattered) const = 0;
};
class Lambertian : public Material {
public:
Lambertian(Color color) : albedo(color){}
bool Scatter(const Ray& in, const HitInfo& hit, Color& attenuation, Ray& scattered) const override{
auto compilerWarningThrower = in; //Just because the compiler is yelling at me because in is not used there.
auto scatterDirection = hit.normal + RandomOnUnitSphere(); //Dispersed direction
if (scatterDirection.NearZero()){
scatterDirection = hit.normal + compilerWarningThrower.Direction() - compilerWarningThrower.Direction();
}
scattered = Ray(hit.p, scatterDirection);
attenuation = albedo;
return true;
}
private:
Color albedo;
};
class Metallic : public Material{
public:
Metallic(Color color, double fuzzRadius) : albedo(color), fuzz(fuzzRadius < 1 ? fuzzRadius : 1) {}
bool Scatter(const Ray& in, const HitInfo& hit, Color& attenuation, Ray& scattered) const override{
Vec3 reflected = Reflect(Normalize(in.Direction()), hit.normal);
scattered = Ray(hit.p, reflected + fuzz * RandomOnUnitSphere());
attenuation = albedo;
return (Dot(scattered.Direction(), hit.normal) > 0);
}
private:
Color albedo;
double fuzz;
};
class Dielectric : public Material{
public:
Dielectric(double refractionIndice) : ir(refractionIndice) {}
bool Scatter(const Ray& in, const HitInfo& hit, Color& attenuation, Ray& scattered) const override{
attenuation = Color(1.0);
double refractionRatio = hit.frontFace ? (1.0/ir) : ir;
Vec3 unitDirection = Normalize(in.Direction());
double cosTheta = fmin(Dot(-unitDirection, hit.normal), 1.0);
double sinTheta = sqrt(1.0 - cosTheta * cosTheta);
bool cannotRefract = refractionRatio * sinTheta > 1.0;
Vec3 direction;;
if (cannotRefract || Reflectance(cosTheta, refractionRatio) > RandomDouble01()){
direction = Reflect(unitDirection, hit.normal);
} else{
direction = Refract(unitDirection, hit.normal, refractionRatio);
}
scattered = Ray(hit.p, direction);
return true;
}
private:
double ir;
static double Reflectance(double cosine, double refIdx){
//Using Shlick's approximation for reflectance, instead of a big and ugly equation
auto r0 = (1-refIdx) / (1+refIdx);
r0 = r0 * r0;
return r0 + (1 - r0) * pow((1 - cosine), 5);
}
};