2020-06-03 21:49:20 +00:00
|
|
|
#ifndef HITTABLE_H
|
|
|
|
#define HITTABLE_H
|
|
|
|
|
2020-06-06 21:21:21 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2020-06-03 21:49:20 +00:00
|
|
|
#include "ray.h"
|
2020-06-06 21:21:21 +00:00
|
|
|
#include "material.h"
|
2020-06-08 21:37:10 +00:00
|
|
|
#include "aabb.h"
|
2020-06-06 21:21:21 +00:00
|
|
|
|
|
|
|
class Material;
|
2020-06-03 21:49:20 +00:00
|
|
|
|
|
|
|
struct hit_record {
|
|
|
|
Point3 p;
|
|
|
|
Vec3 normal;
|
2020-06-06 21:21:21 +00:00
|
|
|
std::shared_ptr<Material> mat_ptr;
|
2020-06-03 21:49:20 +00:00
|
|
|
double t;
|
2020-06-09 21:15:59 +00:00
|
|
|
double u;
|
|
|
|
double v;
|
2020-06-03 22:15:32 +00:00
|
|
|
bool front_face;
|
|
|
|
|
|
|
|
inline void set_face_normal(const Ray& r, const Vec3& outward_normal) {
|
|
|
|
front_face = dot(r.direction(), outward_normal) < 0;
|
|
|
|
normal = front_face ? outward_normal : -outward_normal;
|
|
|
|
}
|
2020-06-03 21:49:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Hittable {
|
|
|
|
public:
|
2020-06-03 22:15:32 +00:00
|
|
|
virtual bool hit(const Ray &r, double tmin, double tmax, hit_record &rec) const = 0;
|
2020-06-08 21:37:10 +00:00
|
|
|
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const = 0;
|
2020-06-03 22:15:32 +00:00
|
|
|
};
|
2020-06-03 21:49:20 +00:00
|
|
|
|
2020-06-13 20:02:26 +00:00
|
|
|
class Flip_face : public Hittable {
|
|
|
|
public:
|
|
|
|
Flip_face(std::shared_ptr<Hittable> p) : ptr(p) {}
|
|
|
|
|
|
|
|
virtual bool hit(const Ray &r, double tmin, double tmax, hit_record &rec) const {
|
|
|
|
if (!ptr->hit(r, tmin, tmax, rec))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
rec.front_face = !rec.front_face;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const {
|
|
|
|
return ptr->bounding_box(t0, t1, output_box);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<Hittable> ptr;
|
|
|
|
};
|
|
|
|
|
2020-06-03 21:49:20 +00:00
|
|
|
#endif // HITTABLE_H
|