|
| 1 | +#ifndef AARECTH |
| 2 | +#define AARECTH |
| 3 | + |
| 4 | +#include "hitable.h" |
| 5 | + |
| 6 | +class xy_rect: public hitable { |
| 7 | + public: |
| 8 | + xy_rect() {} |
| 9 | + xy_rect(float _x0, float _x1, float _y0, float _y1, float _k, material *mat) : x0(_x0), x1(_x1), y0(_y0), y1(_y1), k(_k), mp(mat) {}; |
| 10 | + virtual bool hit(const ray& r, float t0, float t1, hit_record& rec) const; |
| 11 | + virtual bool bounding_box(float t0, float t1, aabb& box) const { |
| 12 | + box = aabb(vec3(x0,y0, k-0.0001), vec3(x1, y1, k+0.0001)); |
| 13 | + return true; } |
| 14 | + material *mp; |
| 15 | + float x0, x1, y0, y1, k; |
| 16 | +}; |
| 17 | + |
| 18 | +class xz_rect: public hitable { |
| 19 | + public: |
| 20 | + xz_rect() {} |
| 21 | + xz_rect(float _x0, float _x1, float _z0, float _z1, float _k, material *mat) : x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mp(mat) {}; |
| 22 | + virtual bool hit(const ray& r, float t0, float t1, hit_record& rec) const; |
| 23 | + virtual bool bounding_box(float t0, float t1, aabb& box) const { |
| 24 | + box = aabb(vec3(x0,k-0.0001,z0), vec3(x1, k+0.0001, z1)); |
| 25 | + return true; |
| 26 | + } |
| 27 | + virtual float pdf_value(const vec3& o, const vec3& v) const { |
| 28 | + hit_record rec; |
| 29 | + if (this->hit(ray(o, v), 0.001, FLT_MAX, rec)) { |
| 30 | + float area = (x1-x0)*(z1-z0); |
| 31 | + float distance_squared = rec.t * rec.t * v.squared_length(); |
| 32 | + float cosine = fabs(dot(v, rec.normal) / v.length()); |
| 33 | + return distance_squared / (cosine * area); |
| 34 | + } |
| 35 | + else |
| 36 | + return 0; |
| 37 | + } |
| 38 | + virtual vec3 random(const vec3& o) const { |
| 39 | + vec3 random_point = vec3(x0 + drand48()*(x1-x0), k, z0 + drand48()*(z1-z0)); |
| 40 | + return random_point - o; |
| 41 | + } |
| 42 | + material *mp; |
| 43 | + float x0, x1, z0, z1, k; |
| 44 | +}; |
| 45 | + |
| 46 | +class yz_rect: public hitable { |
| 47 | + public: |
| 48 | + yz_rect() {} |
| 49 | + yz_rect(float _y0, float _y1, float _z0, float _z1, float _k, material *mat) : y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mp(mat) {}; |
| 50 | + virtual bool hit(const ray& r, float t0, float t1, hit_record& rec) const; |
| 51 | + virtual bool bounding_box(float t0, float t1, aabb& box) const { |
| 52 | + box = aabb(vec3(k-0.0001, y0, z0), vec3(k+0.0001, y1, z1)); |
| 53 | + return true; } |
| 54 | + material *mp; |
| 55 | + float y0, y1, z0, z1, k; |
| 56 | +}; |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +bool xy_rect::hit(const ray& r, float t0, float t1, hit_record& rec) const { |
| 62 | + float t = (k-r.origin().z()) / r.direction().z(); |
| 63 | + if (t < t0 || t > t1) |
| 64 | + return false; |
| 65 | + float x = r.origin().x() + t*r.direction().x(); |
| 66 | + float y = r.origin().y() + t*r.direction().y(); |
| 67 | + if (x < x0 || x > x1 || y < y0 || y > y1) |
| 68 | + return false; |
| 69 | + rec.u = (x-x0)/(x1-x0); |
| 70 | + rec.v = (y-y0)/(y1-y0); |
| 71 | + rec.t = t; |
| 72 | + rec.mat_ptr = mp; |
| 73 | + rec.p = r.point_at_parameter(t); |
| 74 | + rec.normal = vec3(0, 0, 1); |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +bool xz_rect::hit(const ray& r, float t0, float t1, hit_record& rec) const { |
| 80 | + float t = (k-r.origin().y()) / r.direction().y(); |
| 81 | + if (t < t0 || t > t1) |
| 82 | + return false; |
| 83 | + float x = r.origin().x() + t*r.direction().x(); |
| 84 | + float z = r.origin().z() + t*r.direction().z(); |
| 85 | + if (x < x0 || x > x1 || z < z0 || z > z1) |
| 86 | + return false; |
| 87 | + rec.u = (x-x0)/(x1-x0); |
| 88 | + rec.v = (z-z0)/(z1-z0); |
| 89 | + rec.t = t; |
| 90 | + rec.mat_ptr = mp; |
| 91 | + rec.p = r.point_at_parameter(t); |
| 92 | + rec.normal = vec3(0, 1, 0); |
| 93 | + return true; |
| 94 | +} |
| 95 | + |
| 96 | +bool yz_rect::hit(const ray& r, float t0, float t1, hit_record& rec) const { |
| 97 | + float t = (k-r.origin().x()) / r.direction().x(); |
| 98 | + if (t < t0 || t > t1) |
| 99 | + return false; |
| 100 | + float y = r.origin().y() + t*r.direction().y(); |
| 101 | + float z = r.origin().z() + t*r.direction().z(); |
| 102 | + if (y < y0 || y > y1 || z < z0 || z > z1) |
| 103 | + return false; |
| 104 | + rec.u = (y-y0)/(y1-y0); |
| 105 | + rec.v = (z-z0)/(z1-z0); |
| 106 | + rec.t = t; |
| 107 | + rec.mat_ptr = mp; |
| 108 | + rec.p = r.point_at_parameter(t); |
| 109 | + rec.normal = vec3(1, 0, 0); |
| 110 | + return true; |
| 111 | +} |
| 112 | + |
| 113 | +#endif |
0 commit comments