aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/object.cpp19
-rw-r--r--src/object.hpp6
-rw-r--r--src/render.cpp2
3 files changed, 21 insertions, 6 deletions
diff --git a/src/object.cpp b/src/object.cpp
index 1449cbb..15fc267 100644
--- a/src/object.cpp
+++ b/src/object.cpp
@@ -10,15 +10,28 @@ void Color::clamp() {
if (m_z > 1) { m_z = 1; }
}
-Material::Material(Color color, double defuse, double emissive) {
+Material::Material(Color color, double defuse, double spectral, double spectral_pow, double emissive) {
m_color = color;
m_defuse = defuse;
m_emissive = emissive;
+ m_spectral = spectral;
+ m_spectral_pow = spectral_pow;
}
Color Material::reflect(const Vec3d &normal, const Vec3d &in, const Vec3d &out, const Color &incol) const {
- return Vec3d(m_color) * m_emissive +
- (Vec3d(m_color) * Vec3d(incol)) * (out.dot(normal) * m_defuse);
+ // Emissive
+ Color c = Vec3d(m_color) * m_emissive;
+
+ // Defuse
+ c += (Vec3d(m_color) * Vec3d(incol)) * (in.dot(normal) * m_defuse);
+
+ // Spectral
+ if (m_spectral > 0) {
+ auto R = normal * (2 * normal.dot(in)) - in;
+ c += Vec3d(incol) * pow(out.dot(R) * m_spectral, m_spectral_pow);
+ }
+
+ return c;
}
Sphere::Sphere(const Material &mat, Vec3d center, double radius) : Shape(mat) {
diff --git a/src/object.hpp b/src/object.hpp
index e697ba2..3b108fb 100644
--- a/src/object.hpp
+++ b/src/object.hpp
@@ -25,7 +25,7 @@ class Color : public Vec3d {
// Implements phong BRDF
class Material {
public:
- Material(Color color, double defuse, double emissive=0);
+ Material(Color color, double defuse, double spectral=0, double spectral_pow=0, double emissive=0);
Color reflect(const Vec3d &normal, const Vec3d &in, const Vec3d &out, const Color &incol) const;
@@ -34,11 +34,13 @@ class Material {
}
// Whether the material is reflective
- bool reflects() const { return m_defuse > 0; }
+ bool reflects() const { return m_defuse+m_spectral > 0; }
private:
Color m_color;
double m_defuse;
double m_emissive;
+ double m_spectral;
+ double m_spectral_pow;
};
class Shape {
diff --git a/src/render.cpp b/src/render.cpp
index c278797..3f2280d 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -124,7 +124,7 @@ Color Renderer::pathtrace_sample(const Ray &r, unsigned hop) {
auto newray = Ray(end, randdir, true);
auto incol = pathtrace_sample(newray, hop+1);
- col += res->m_mat.reflect(norm, r.m_direction, newray.m_direction, incol);
+ col += res->m_mat.reflect(norm, newray.m_direction, r.m_direction, incol);
}
return col;