toytracer/aarect.cpp
2020-06-13 22:00:05 +02:00

80 lines
2.3 KiB
C++

#include "aarect.h"
bool Xy_rect::bounding_box(double t0, double t1, Aabb& output_box) const {
output_box = Aabb(Point3(x0, y0, k - 0.0001), Point3(x1, y1, k + 0.0001));
return true;
}
bool Xz_rect::bounding_box(double t0, double t1, Aabb& output_box) const {
output_box = Aabb(Point3(x0, k - 0.0001, z0), Point3(x1, k + 0.0001, z1));
return true;
}
bool Yz_rect::bounding_box(double t0, double t1, Aabb& output_box) const {
output_box = Aabb(Point3(k - 0.0001, y0, z0), Point3(k + 0.0001, y1, z1));
return true;
}
bool Xy_rect::hit(const Ray& r, double t0, double t1, hit_record& rec) const {
auto t = (k - r.origin().z()) / r.direction().z();
if (t < t0 || t > t1)
return false;
auto x = r.origin().x() + t * r.direction().x();
auto y = r.origin().y() + t * r.direction().y();
if (x < x0 || x > x1 || y < y0 || y > y1)
return false;
rec.u = (x - x0) / (x1 - x0);
rec.v = (y - y0) / (y1 - y0);
rec.t = t;
auto outward_normal = Vec3(0, 0, 1);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mat_ptr;
rec.p = r.at(t);
return true;
}
bool Xz_rect::hit(const Ray& r, double t0, double t1, hit_record& rec) const {
auto t = (k - r.origin().y()) / r.direction().y();
if (t < t0 || t > t1)
return false;
auto x = r.origin().x() + t * r.direction().x();
auto z = r.origin().z() + t * r.direction().z();
if (x < x0 || x > x1 || z < z0 || z > z1)
return false;
rec.u = (x - x0) / (x1 - x0);
rec.v = (z - z0) / (z1 - z0);
rec.t = t;
auto outward_normal = Vec3(0, 1, 0);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mat_ptr;
rec.p = r.at(t);
return true;
}
bool Yz_rect::hit(const Ray& r, double t0, double t1, hit_record& rec) const {
auto t = (k - r.origin().x()) / r.direction().x();
if (t < t0 || t > t1)
return false;
auto y = r.origin().y() + t * r.direction().y();
auto z = r.origin().z() + t * r.direction().z();
if (y < y0 || y > y1 || z < z0 || z > z1)
return false;
rec.u = (y - y0) / (y1 - y0);
rec.v = (z - z0) / (z1 - z0);
rec.t = t;
auto outward_normal = Vec3(1, 0, 0);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mat_ptr;
rec.p = r.at(t);
return true;
}