diff --git a/shader/refractionshader.cpp b/shader/refractionshader.cpp index e13916a..5312e30 100644 --- a/shader/refractionshader.cpp +++ b/shader/refractionshader.cpp @@ -1,8 +1,8 @@ #include "scene/scene.h" #include "shader/refractionshader.h" -RefractionShader::RefractionShader(float indexInside, float indexOutside, Color const &objectColor) : indexInside( - indexInside), indexOutside(indexOutside), objectColor(objectColor) +RefractionShader::RefractionShader(float indexInside, float indexOutside, Color const &objectColor, float lightLoss) : indexInside( + indexInside), indexOutside(indexOutside), objectColor(objectColor), lightLoss(lightLoss) {} Color RefractionShader::shade(Scene const &scene, Ray const &ray) const @@ -51,7 +51,12 @@ Color RefractionShader::shade(Scene const &scene, Ray const &ray) const } // Send out a new refracted ray into the scene - return scene.traceRay(refractionRay) * objectColor; + Color hitColor = scene.traceRay(refractionRay); + + // Calculate light lost + float lightRemaining = 1; + if (refractionRay.primitive == ray.primitive) lightRemaining = 1 - lightLoss + exp(-refractionRay.length / 10) * lightLoss; + return hitColor * objectColor * lightRemaining; } return Color(0.0f, 0.0f, 0.0f); } diff --git a/shader/refractionshader.h b/shader/refractionshader.h index 7b489da..1da09c0 100644 --- a/shader/refractionshader.h +++ b/shader/refractionshader.h @@ -8,7 +8,7 @@ class RefractionShader : public Shader public: // Constructor - RefractionShader(float indexInside, float indexOutside, Color const &objectColor = Color(1, 1, 1)); + RefractionShader(float indexInside, float indexOutside, Color const &objectColor = Color(1, 1, 1), float lightLoss = 0); // Shader functions Color shade(Scene const &scene, Ray const &ray) const override; @@ -18,6 +18,7 @@ public: private: float indexInside; float indexOutside; + float lightLoss; Color objectColor; };