-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmaterial.cpp
156 lines (139 loc) · 5.41 KB
/
material.cpp
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
152
153
154
155
156
/**
* material.cpp
*
* By Sebastian Raaphorst, 2018.
*/
#include <memory>
#include "material.h"
#include "patterns/pattern.h"
#include "pointlight.h"
#include "shape.h"
#include "patterns/solidpattern.h"
#include "vec.h"
using namespace raytracer::shapes;
namespace raytracer {
Material::Material() noexcept:
pattern{std::make_shared<SolidPattern>(DEFAULT_COLOUR)},
ambient{DEFAULT_AMBIENT},
diffuse{DEFAULT_DIFFUSE},
specular{DEFAULT_SPECULAR},
shininess{DEFAULT_SHININESS},
reflectivity{DEFAULT_REFLECTIVITY},
transparency{DEFAULT_TRANSPARENCY},
refractive_index{DEFAULT_REFRACTIVE_INDEX} {}
Material::Material(const Colour &colour,
double ambient,
double diffuse,
double specular,
double shininess,
double reflectivity,
double transparency,
double refractive_index) noexcept:
pattern{std::make_shared<SolidPattern>(colour)},
ambient{ambient},
diffuse{diffuse},
specular{specular},
shininess{shininess},
reflectivity{reflectivity},
transparency{transparency},
refractive_index{refractive_index} {}
Material::Material(const std::shared_ptr<Pattern> &pattern,
double ambient,
double diffuse,
double specular,
double shininess,
double reflectivity,
double transparency,
double refractive_index) noexcept:
pattern{pattern},
ambient{ambient},
diffuse{diffuse},
specular{specular},
shininess{shininess},
reflectivity{reflectivity},
transparency{transparency},
refractive_index{refractive_index} {}
bool Material::operator==(const Material &other) const {
return *pattern == *other.pattern
&& ALMOST_EQUALS(ambient, other.ambient)
&& ALMOST_EQUALS(diffuse, other.diffuse)
&& ALMOST_EQUALS(specular, other.specular)
&& ALMOST_EQUALS(shininess, other.shininess)
&& ALMOST_EQUALS(reflectivity, other.reflectivity)
&& ALMOST_EQUALS(transparency, other.transparency)
&& ALMOST_EQUALS(refractive_index, other.refractive_index);
}
bool Material::operator!=(const Material &other) const noexcept {
return !(*this == other);
}
const std::shared_ptr<Pattern> &Material::getPattern() const noexcept {
return pattern;
}
double Material::getAmbient() const noexcept {
return ambient;
}
double Material::getDiffuse() const noexcept {
return diffuse;
}
double Material::getSpecular() const noexcept {
return specular;
}
double Material::getShininess() const noexcept {
return shininess;
}
double Material::getReflectivity() const noexcept {
return reflectivity;
}
double Material::getTransparency() const noexcept {
return transparency;
}
double Material::getRefractiveIndex() const noexcept {
return refractive_index;
}
void Material::setPattern(const std::shared_ptr<Pattern> &p) noexcept {
pattern = p;
}
void Material::setAmbient(double a) noexcept {
ambient = a;
}
void Material::setDiffuse(double d) noexcept {
diffuse = d;
}
void Material::setSpecular(double s) noexcept {
specular = s;
}
void Material::setShininess(double s) noexcept {
shininess = s;
}
void Material::setReflectivity(double r) noexcept {
reflectivity = r;
}
void Material::setTransparency(double t) noexcept {
transparency = t;
}
void Material::setRefractiveIndex(double r) noexcept {
refractive_index = r;
}
Colour Material::lighting(const PointLight &light,
const std::shared_ptr<const Shape> &shape,
const Tuple &point,
const Tuple &eyev, const Tuple &normalv, bool in_shadow) const noexcept {
const auto colour = pattern->colourAtObject(shape, point);
const auto effective_colour = colour * light.getIntensity();
const auto lightv = (light.getPosition() - point).normalize();
const auto ambient_component = ambient * effective_colour;
// If the point is in shadow, we only use the ambient component.
if (in_shadow)
return ambient_component;
const auto light_dot_normal = lightv.dot_product(normalv);
const auto diffuse_component = (light_dot_normal < 0)
? predefined_colours::black
: diffuse * effective_colour * light_dot_normal;
const auto reflectv = -lightv.reflect(normalv);
const auto reflect_dot_eye = std::pow(reflectv.dot_product(eyev), shininess);
const auto specular_component = (light_dot_normal < 0 || reflect_dot_eye <= 0)
? predefined_colours::black
: reflect_dot_eye * specular * light.getIntensity();
return ambient_component + diffuse_component + specular_component;
}
}