From 4193adbbff4a5b633f59f88367fe1b74aec7789b Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 15 Apr 2022 12:32:42 +0800 Subject: Code Refactor: - Add material class. - Move diffuse routine into separate material classes. --- material.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 material.cpp (limited to 'material.cpp') diff --git a/material.cpp b/material.cpp new file mode 100644 index 0000000..f92b02c --- /dev/null +++ b/material.cpp @@ -0,0 +1,61 @@ +// +// Created by Keuin on 2022/4/15. +// + +#include "material.h" + + +bool material_diffuse_lambertian::scatter(ray3d &r, const object &hit_obj, double hit_t, random_uv_gen_3d &ruvg) const { + const auto hit_point = r.at(hit_t); // hit point, on the surface + auto nv = hit_obj.normal_vector(hit_point); + if (dot(nv, r.direction()) > 0) return false; // discard rays from inner (or invert nv) + auto d = nv + ruvg.normalized(); + if (d.is_zero()) d = nv; + vec3d diffuse_target = hit_point + d; + r.decay(albedo); // lose some light when diffused + r.source(hit_point); + r.direction((diffuse_target - hit_point).unit_vec()); // the new diffused ray we trace on + return true; +} + +material_diffuse_lambertian::material_diffuse_lambertian(double albedo) : albedo(albedo) { + assert(albedo >= 0); + assert(albedo <= 1); +} + +material_diffuse_simple::material_diffuse_simple(double albedo) : albedo(albedo) { + assert(albedo >= 0); + assert(albedo <= 1); +} + +bool material_diffuse_simple::scatter(ray3d &r, const object &hit_obj, double hit_t, random_uv_gen_3d &ruvg) const { + const auto hit_point = r.at(hit_t); // hit point, on the surface + auto nv = hit_obj.normal_vector(hit_point); + if (dot(nv, r.direction()) > 0) return false; // discard rays from inner (or invert nv) + auto d = nv + ruvg.normalized(); + if (d.is_zero()) d = nv; + vec3d diffuse_target = hit_point + nv + ruvg.range01(); + r.decay(albedo); // lose some light when diffused + r.source(hit_point); + r.direction((diffuse_target - hit_point).unit_vec()); // the new diffused ray we trace on + return true; +} + +material_diffuse_hemispherical::material_diffuse_hemispherical(double albedo) : albedo(albedo) { + assert(albedo >= 0); + assert(albedo <= 1); +} + +bool +material_diffuse_hemispherical::scatter(ray3d &r, const object &hit_obj, double hit_t, random_uv_gen_3d &ruvg) const { + const auto hit_point = r.at(hit_t); // hit point, on the surface + auto nv = hit_obj.normal_vector(hit_point); + if (dot(nv, r.direction()) > 0) return false; // discard rays from inner (or invert nv) + auto d = nv + ruvg.normalized(); + if (d.is_zero()) d = nv; + vec3d diffuse_target = hit_point + ruvg.hemisphere(nv); + r.decay(albedo); // lose some light when diffused + r.source(hit_point); + r.direction((diffuse_target - hit_point).unit_vec()); // the new diffused ray we trace on + return true; +} -- cgit v1.2.3