Colored refraction shader

This commit is contained in:
Maximilian Giller 2023-01-24 19:59:13 +01:00
parent 7ec5a8a5f4
commit 3b76969ff2
2 changed files with 63 additions and 51 deletions

View file

@ -1,18 +1,23 @@
#include "scene/scene.h"
#include "shader/refractionshader.h"
RefractionShader::RefractionShader(float indexInside, float indexOutside) : indexInside(indexInside), indexOutside(indexOutside) {}
RefractionShader::RefractionShader(float indexInside, float indexOutside, Color const &objectColor) : indexInside(
indexInside), indexOutside(indexOutside), objectColor(objectColor)
{}
Color RefractionShader::shade(Scene const &scene, Ray const &ray) const {
Color RefractionShader::shade(Scene const &scene, Ray const &ray) const
{
// Circumvent getting environment map color into the mix
if (ray.getRemainingBounces() > 0) {
if (ray.getRemainingBounces() > 0)
{
// Get the normal of the primitive which was hit
Vector3d normalVector = ray.normal;
// Calculate the index of refraction
float refractiveIndex = indexOutside / indexInside;
// What if we are already inside the object?
if (dotProduct(normalVector, ray.direction) > 0) {
if (dotProduct(normalVector, ray.direction) > 0)
{
normalVector = -normalVector;
refractiveIndex = indexInside / indexOutside;
}
@ -30,22 +35,26 @@ Color RefractionShader::shade(Scene const &scene, Ray const &ray) const {
refractionRay.primitive = nullptr;
// Check whether it is a refraction.
if (dotProduct(t, normalVector) <= 0.0) {
if (dotProduct(t, normalVector) <= 0.0)
{
refractionRay.origin = ray.origin + (ray.length + REFR_EPS) * ray.direction;
refractionRay.direction = normalized(t);
} else { // Otherwise, it is a total reflection.
} else
{ // Otherwise, it is a total reflection.
refractionRay.origin = ray.origin + (ray.length - REFR_EPS) * ray.direction;
// Next we get the reflection vector
Vector3d const reflectionVector = ray.direction - 2.0f * dotProduct(normalVector, ray.direction) * normalVector;
Vector3d const reflectionVector =
ray.direction - 2.0f * dotProduct(normalVector, ray.direction) * normalVector;
// Change the ray direction and origin
refractionRay.direction = normalized(reflectionVector);
}
// Send out a new refracted ray into the scene
return scene.traceRay(refractionRay);
return scene.traceRay(refractionRay) * objectColor;
}
return Color(0.0f, 0.0f, 0.0f);
}
bool RefractionShader::isTransparent() const { return true; }
bool RefractionShader::isTransparent() const
{ return true; }

View file

@ -3,19 +3,22 @@
#include "shader/shader.h"
class RefractionShader : public Shader {
class RefractionShader : public Shader
{
public:
// Constructor
RefractionShader(float indexInside, float indexOutside);
RefractionShader(float indexInside, float indexOutside, Color const &objectColor = Color(1, 1, 1));
// Shader functions
Color shade(Scene const &scene, Ray const &ray) const override;
bool isTransparent() const override;
private:
float indexInside;
float indexOutside;
Color objectColor;
};
#endif