aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-09-16 20:52:12 +0200
committerJulian T <julian@jtle.dk>2020-12-06 22:44:10 +0100
commit0f9e88ccf0510ab4d830529fa539ef6db715f988 (patch)
tree863b5f466d3e81e16e04aad56ef7a0d860a143f9
parenta38e6014ea5441e9d29fcb3b5607cd94e4061cff (diff)
Added first draft of spectral lightningold_spectral
-rw-r--r--app/main.cpp5
-rw-r--r--src/object.cpp19
-rw-r--r--src/object.hpp6
-rw-r--r--src/render.cpp2
4 files changed, 24 insertions, 8 deletions
diff --git a/app/main.cpp b/app/main.cpp
index e53b8ad..16de1f6 100644
--- a/app/main.cpp
+++ b/app/main.cpp
@@ -24,12 +24,13 @@ int main(int argc, char *argv[])
Material blue(Color(0.3, 0.3, 1), 1);
+ Material green(Color(0.3, 1, 0.3), 0, 1, 50);
Material red(Color(1, 0.3, 0.3), 1);
Material white(Color(1, 1, 1), 1);
- Material em(Color(1, 1, 1), 0, 2);
+ Material em(Color(1, 1, 1), 0, 0, 0, 2);
scn.addShape(new Sphere(red, Vec3d(2, 6, -1), 1));
- scn.addShape(new Sphere(white, Vec3d(0, 4, -1), 1.3));
+ scn.addShape(new Sphere(green, Vec3d(0, 4, -1), 1.3));
scn.addShape(new Sphere(white, Vec3d(-2, 5, -2), 1.3));
scn.addShape(new Sphere(blue, Vec3d(0, 7, 0), 0.5));
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;