From 56bed8e38f5b6d9effd77371ad781ae71601205b Mon Sep 17 00:00:00 2001 From: ladevieq Date: Mon, 21 Dec 2020 13:51:18 +0100 Subject: Add lambertian and metallic materials --- include/hittable.hpp | 5 +++++ include/lambertian.hpp | 15 +++++++++++++++ include/material.hpp | 13 +++++++++++++ include/metal.hpp | 16 ++++++++++++++++ include/ray.hpp | 1 + include/rt.hpp | 3 +++ include/sphere.hpp | 5 ++++- include/vec3.hpp | 4 ++++ 8 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 include/lambertian.hpp create mode 100644 include/material.hpp create mode 100644 include/metal.hpp (limited to 'include') diff --git a/include/hittable.hpp b/include/hittable.hpp index 4978e57..aa55034 100644 --- a/include/hittable.hpp +++ b/include/hittable.hpp @@ -1,13 +1,18 @@ #ifndef __HITTABLE_HPP_ #define __HITTABLE_HPP_ +#include + #include "ray.hpp" +class material; + struct hit_info { double t; point3 point; vec3 normal; bool front_face; + std::shared_ptr mat_ptr; inline void set_face_normal(const ray& r, const vec3& outward_normal) { front_face = r.direction.dot(outward_normal) < 0.0; diff --git a/include/lambertian.hpp b/include/lambertian.hpp new file mode 100644 index 0000000..b9b7509 --- /dev/null +++ b/include/lambertian.hpp @@ -0,0 +1,15 @@ +#ifndef __LAMBERTIAN_HPP_ +#define __LAMBERTIAN_HPP_ + +#include "material.hpp" + +class lambertian : public material { + public: + lambertian(const color& albedo); + + virtual bool scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const; + + color albedo; +}; + +#endif // !__LAMBERTIAN_HPP_ diff --git a/include/material.hpp b/include/material.hpp new file mode 100644 index 0000000..5e6f095 --- /dev/null +++ b/include/material.hpp @@ -0,0 +1,13 @@ +#ifndef __MATERIAL_HPP_ +#define __MATERIAL_HPP_ + +#include "ray.hpp" + +struct hit_info; + +class material { + public: + virtual bool scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scaterred) const = 0; +}; + +#endif // !__MATERIAL_HPP_ diff --git a/include/metal.hpp b/include/metal.hpp new file mode 100644 index 0000000..5516890 --- /dev/null +++ b/include/metal.hpp @@ -0,0 +1,16 @@ +#ifndef __METAL_HPP_ +#define __METAL_HPP_ + +#include "material.hpp" + +class metal : public material { + public: + metal(const color& albedo, double f); + + virtual bool scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const; + + color albedo; + double fuzz; +}; + +#endif // !__METAL_HPP_ diff --git a/include/ray.hpp b/include/ray.hpp index b3d88ae..e18812e 100644 --- a/include/ray.hpp +++ b/include/ray.hpp @@ -6,6 +6,7 @@ class ray { public: + ray(); ray(const point3& origin, const vec3& direction); point3 at(double t) const; diff --git a/include/rt.hpp b/include/rt.hpp index a0c912e..2cf3ce2 100644 --- a/include/rt.hpp +++ b/include/rt.hpp @@ -31,5 +31,8 @@ inline double clamp(double x, double min, double max) { #include "color.hpp" #include "sphere.hpp" #include "camera.hpp" +#include "material.hpp" +#include "lambertian.hpp" +#include "metal.hpp" #endif // !__RT_HPP_ diff --git a/include/sphere.hpp b/include/sphere.hpp index 5bf0ab9..c33a796 100644 --- a/include/sphere.hpp +++ b/include/sphere.hpp @@ -2,15 +2,18 @@ #define __SPHERE_HPP_ #include "hittable.hpp" +#include "material.hpp" class sphere : public hittable { public: - sphere(point3 center, double radius = 1.0); + sphere(point3 center, double radius, std::shared_ptr material); virtual bool hit(const ray& r, double t_min, double t_max, struct hit_info& info) const; point3 center; double radius; + + std::shared_ptr mat_ptr; }; #endif // !__SPHERE_HPP_ diff --git a/include/vec3.hpp b/include/vec3.hpp index 7b0e469..317f99b 100644 --- a/include/vec3.hpp +++ b/include/vec3.hpp @@ -28,6 +28,8 @@ class vec3 { vec3 unit() const; + bool near_zero() const; + static vec3 random(); static vec3 random(double min, double max); @@ -49,6 +51,8 @@ vec3 operator*(double scale, vec3 u); vec3 operator/(vec3 u, double scale); +vec3 reflect(vec3 v, vec3 n); + std::ostream& operator<<(std::ostream& out, vec3 vec); vec3 random_in_unit_sphere(); -- cgit v1.3