From c7c4f8f82c66cfe89b72d176ee51dab8bd30dc95 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 20 Dec 2020 21:59:33 +0100 Subject: Add hittable interface and hit infos on hit --- include/hittable.hpp | 23 +++++++++++++++++++++++ include/hittable_list.hpp | 21 +++++++++++++++++++++ include/ray.hpp | 2 +- include/rt.hpp | 18 ++++++++++++++++++ include/sphere.hpp | 16 ++++++++++++++++ include/vec3.hpp | 8 ++++---- 6 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 include/hittable.hpp create mode 100644 include/hittable_list.hpp create mode 100644 include/rt.hpp create mode 100644 include/sphere.hpp (limited to 'include') diff --git a/include/hittable.hpp b/include/hittable.hpp new file mode 100644 index 0000000..4978e57 --- /dev/null +++ b/include/hittable.hpp @@ -0,0 +1,23 @@ +#ifndef __HITTABLE_HPP_ +#define __HITTABLE_HPP_ + +#include "ray.hpp" + +struct hit_info { + double t; + point3 point; + vec3 normal; + bool front_face; + + inline void set_face_normal(const ray& r, const vec3& outward_normal) { + front_face = r.direction.dot(outward_normal) < 0.0; + normal = front_face ? outward_normal : -outward_normal; + } +}; + +class hittable { + public: + virtual bool hit(const ray& r, double t_min, double t_max, struct hit_info& info) const = 0; +}; + +#endif // !__HITTABLE_HPP_ diff --git a/include/hittable_list.hpp b/include/hittable_list.hpp new file mode 100644 index 0000000..41d8f4a --- /dev/null +++ b/include/hittable_list.hpp @@ -0,0 +1,21 @@ +#ifndef __HITTABLE_LIST_HPP_ +#define __HITTABLE_LIST_HPP_ + +#include +#include + +#include "hittable.hpp" + +class hittable_list : public hittable { + public: + hittable_list(); + + void add(std::shared_ptr hittable); + void clear(); + + virtual bool hit(const ray&r, double t_min, double t_max, struct hit_info& info) const; + + std::vector> hittables; +}; + +#endif // !__HITTABLE_LIST_HPP_ diff --git a/include/ray.hpp b/include/ray.hpp index 3f8542b..b3d88ae 100644 --- a/include/ray.hpp +++ b/include/ray.hpp @@ -8,7 +8,7 @@ class ray { ray(const point3& origin, const vec3& direction); - point3 at(double t); + point3 at(double t) const; point3 origin; vec3 direction; diff --git a/include/rt.hpp b/include/rt.hpp new file mode 100644 index 0000000..60170ac --- /dev/null +++ b/include/rt.hpp @@ -0,0 +1,18 @@ +#ifndef __RT_HPP_ +#define __RT_HPP_ + +#include + +const double pi = 3.1415926535897932385; +const double infinity = std::numeric_limits::infinity(); + +inline double deg_to_rad(double deg) { + return deg * pi / 180.0; +} + +#include "vec3.hpp" +#include "ray.hpp" +#include "color.hpp" +#include "sphere.hpp" + +#endif // !__RT_HPP_ diff --git a/include/sphere.hpp b/include/sphere.hpp new file mode 100644 index 0000000..5bf0ab9 --- /dev/null +++ b/include/sphere.hpp @@ -0,0 +1,16 @@ +#ifndef __SPHERE_HPP_ +#define __SPHERE_HPP_ + +#include "hittable.hpp" + +class sphere : public hittable { + public: + sphere(point3 center, double radius = 1.0); + + virtual bool hit(const ray& r, double t_min, double t_max, struct hit_info& info) const; + + point3 center; + double radius; +}; + +#endif // !__SPHERE_HPP_ diff --git a/include/vec3.hpp b/include/vec3.hpp index 48f0ae3..e658e71 100644 --- a/include/vec3.hpp +++ b/include/vec3.hpp @@ -10,7 +10,7 @@ class vec3 { vec3(double x, double y, double z); - void operator-(); + vec3 operator-() const; vec3& operator+=(vec3& vec); @@ -18,15 +18,15 @@ class vec3 { vec3& operator/=(double numerator); - double length(); + double length() const; - double length_sq(); + double length_sq() const; double dot(const vec3& vec) const; vec3 cross(const vec3& vec) const; - vec3 unit(); + vec3 unit() const; double x, y, z; }; -- cgit v1.3