toytracer/hittable.h

78 lines
1.8 KiB
C++

#ifndef HITTABLE_H
#define HITTABLE_H
#include <memory>
#include "ray.h"
#include "material.h"
#include "aabb.h"
class Material;
struct hit_record {
Point3 p;
Vec3 normal;
std::shared_ptr<Material> mat_ptr;
double t;
double u;
double v;
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;
}
};
class Hittable {
public:
virtual bool hit(const Ray &r, double tmin, double tmax, hit_record &rec) const = 0;
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const = 0;
};
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;
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;
};
class Translate : public Hittable {
public:
Translate(std::shared_ptr<Hittable> p, const Vec3& displacement) : ptr(p), offset(displacement) {}
virtual bool hit(const Ray &r, double tmin, double tmax, hit_record &rec) const;
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const;
private:
std::shared_ptr<Hittable> ptr;
Vec3 offset;
};
class Rotate_y : public Hittable {
public:
Rotate_y(std::shared_ptr<Hittable> p, double angle);
virtual bool hit(const Ray& r, double tmin, double tmax, hit_record& rec) const;
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const {
output_box = bbox;
return hasbox;
}
private:
std::shared_ptr<Hittable> ptr;
double sin_theta;
double cos_theta;
bool hasbox;
Aabb bbox;
};
#endif // HITTABLE_H