From eab7b922856b236943985ca53ee08f01aebf3a11 Mon Sep 17 00:00:00 2001 From: Yoel <s73017@beuth-hochschule.de> Date: Fri, 22 Jan 2021 11:49:50 +0100 Subject: [PATCH] Added base for reflective material. Currently same as diffuse --- RayTracer/material/ReflectiveMaterial.cpp | 60 +++++++++++++++++++++++ RayTracer/material/ReflectiveMaterial.h | 30 ++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 RayTracer/material/ReflectiveMaterial.cpp create mode 100644 RayTracer/material/ReflectiveMaterial.h diff --git a/RayTracer/material/ReflectiveMaterial.cpp b/RayTracer/material/ReflectiveMaterial.cpp new file mode 100644 index 0000000..d32cb33 --- /dev/null +++ b/RayTracer/material/ReflectiveMaterial.cpp @@ -0,0 +1,60 @@ +#define _USE_MATH_DEFINES +#include "ReflectiveMaterial.h" + +#include <cassert> + +#include "../tools/Random.h" +#include "texture/Constant.h" + +namespace material { +ReflectiveMaterial::ReflectiveMaterial( + const std::shared_ptr<util::Sampler>& albedo_texture, + const std::shared_ptr<util::Sampler>& emission_texture, float clearness) + : albedo_texture(albedo_texture), + emission_texture(emission_texture), + clearness(clearness) { +} +ReflectiveMaterial::ReflectiveMaterial( + const std::shared_ptr<util::Sampler>& albedo_texture, float clearness) + : albedo_texture(albedo_texture), + emission_texture( + std::make_shared<Constant>(Constant(util::Vec3(0, 0, 0)))), + clearness(clearness) { +} +ReflectiveMaterial::ReflectiveMaterial(const util::Vec3& color, float clearness) + : albedo_texture(std::make_shared<Constant>(Constant(color))), + emission_texture( + std::make_shared<Constant>(Constant(util::Vec3(0, 0, 0)))), + clearness(clearness) { +} +util::Vec3 ReflectiveMaterial::albedo(float texel_x, float texel_y) const { + return albedo_texture->color(texel_x, texel_y); +} +util::Vec3 ReflectiveMaterial::emission(float texel_x, float texel_y) const { + return emission_texture->color(texel_x, texel_y); +} +util::Vec3 ReflectiveMaterial::scattered_d(const util::Vec3& d, + const util::Vec3& n) const { + util::Vec3 rand = util::rand_vec3_in_circle(clearness); + util::Vec3 reflected = reflection(d, n); + return reflected + rand; +} +bool ReflectiveMaterial::scatter(const util::Vec3& d, + const util::Vec3& n) const { + return util::dot(d, n) > 0; +} +float ReflectiveMaterial::calculateLightMultiplier(const util::Vec3& d_in, + const util::Vec3& d_out, + const util::Vec3& n) const { + util::Vec3 reflected = reflection(-d_out, n).normalize(); + /*if (std::pow(util::dot(reflected, -d_in), 1) < 0.7) { + std::cout << reflected << " " << -d_in << " " + << util::dot(reflected, -d_in) << std::endl; + assert(false); + }*/ + auto dot = util::dot(reflected, -d_in); + auto theta = std::acos(dot); + + return std::cos(std::min<float>(theta * 3, M_PI / 2)); +} +} // namespace material \ No newline at end of file diff --git a/RayTracer/material/ReflectiveMaterial.h b/RayTracer/material/ReflectiveMaterial.h new file mode 100644 index 0000000..8c005e5 --- /dev/null +++ b/RayTracer/material/ReflectiveMaterial.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Material.h" + +namespace material { +class ReflectiveMaterial : public Material { + public: + ReflectiveMaterial(const std::shared_ptr<util::Sampler>& albedo_texture, + const std::shared_ptr<util::Sampler>& emission_texture, + float clearness); + ReflectiveMaterial(const std::shared_ptr<util::Sampler>& albedo, + float clearness); + ReflectiveMaterial(const util::Vec3& color, float clearness); + + util::Vec3 albedo(float texel_x, float texel_y) const override; + util::Vec3 emission(float texel_x, float texel_y) const override; + util::Vec3 scattered_d(const util::Vec3& d, + const util::Vec3& n) const override; + bool scatter(const util::Vec3& d, const util::Vec3& n) const override; + + float calculateLightMultiplier(const util::Vec3& d_in, + const util::Vec3& d_out, + const util::Vec3& n) const override; + + private: + std::shared_ptr<util::Sampler> albedo_texture; + std::shared_ptr<util::Sampler> emission_texture; + float clearness; +}; +} // namespace material -- GitLab