|
81 | 81 | return orig + t*dir;
|
82 | 82 | }
|
83 | 83 |
|
| 84 | + public: |
84 | 85 | vec3 orig;
|
85 | 86 | vec3 dir;
|
86 | 87 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
|
143 | 144 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
144 | 145 | }
|
145 | 146 |
|
| 147 | + public: |
146 | 148 | vec3 origin;
|
147 | 149 | vec3 lower_left_corner;
|
148 | 150 | vec3 horizontal;
|
|
204 | 206 | auto a = r.direction().length_squared();
|
205 | 207 | auto half_b = dot(oc, r.direction());
|
206 | 208 | auto c = oc.length_squared() - radius*radius;
|
| 209 | + |
207 | 210 | auto discriminant = half_b*half_b - a*c;
|
208 | 211 |
|
209 | 212 | if (discriminant > 0) {
|
210 | 213 | auto root = sqrt(discriminant);
|
| 214 | + |
211 | 215 | auto temp = (-half_b - root)/a;
|
212 | 216 | if (temp < t_max && temp > t_min) {
|
213 | 217 | rec.t = temp;
|
|
219 | 223 | rec.mat_ptr = mat_ptr;
|
220 | 224 | return true;
|
221 | 225 | }
|
| 226 | + |
222 | 227 | temp = (-half_b + root) / a;
|
223 | 228 | if (temp < t_max && temp > t_min) {
|
224 | 229 | rec.t = temp;
|
|
884 | 889 | public:
|
885 | 890 | constant_texture() {}
|
886 | 891 | constant_texture(vec3 c) : color(c) {}
|
| 892 | + |
887 | 893 | virtual vec3 value(double u, double v, const vec3& p) const {
|
888 | 894 | return color;
|
889 | 895 | }
|
| 896 | + |
| 897 | + public: |
890 | 898 | vec3 color;
|
891 | 899 | };
|
892 | 900 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
2038 | 2046 | flip_face(shared_ptr<hittable> p) : ptr(p) {}
|
2039 | 2047 |
|
2040 | 2048 | virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
|
2041 |
| - if (ptr->hit(r, t_min, t_max, rec)) { |
2042 |
| - rec.front_face = !rec.front_face; |
2043 |
| - return true; |
2044 |
| - } |
2045 |
| - else |
| 2049 | + if (!ptr->hit(r, t_min, t_max, rec)) |
2046 | 2050 | return false;
|
| 2051 | + |
| 2052 | + rec.front_face = !rec.front_face; |
| 2053 | + return true; |
2047 | 2054 | }
|
2048 | 2055 |
|
2049 | 2056 | virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
|
|
2197 | 2204 |
|
2198 | 2205 | bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
|
2199 | 2206 | ray moved_r(r.origin() - offset, r.direction(), r.time());
|
2200 |
| - if (ptr->hit(moved_r, t_min, t_max, rec)) { |
2201 |
| - rec.p += offset; |
2202 |
| - rec.set_face_normal(moved_r, rec.normal); |
2203 |
| - return true; |
2204 |
| - } |
2205 |
| - else |
| 2207 | + if (!ptr->hit(moved_r, t_min, t_max, rec)) |
2206 | 2208 | return false;
|
| 2209 | + |
| 2210 | + rec.p += offset; |
| 2211 | + rec.set_face_normal(moved_r, rec.normal); |
| 2212 | + |
| 2213 | + return true; |
2207 | 2214 | }
|
2208 | 2215 |
|
2209 | 2216 | bool translate::bounding_box(double t0, double t1, aabb& output_box) const {
|
2210 |
| - if (ptr->bounding_box(t0, t1, output_box)) { |
2211 |
| - output_box = aabb( |
2212 |
| - output_box.min() + offset, |
2213 |
| - output_box.max() + offset); |
2214 |
| - return true; |
2215 |
| - } |
2216 |
| - else |
| 2217 | + if (!ptr->bounding_box(t0, t1, output_box)) |
2217 | 2218 | return false;
|
| 2219 | + |
| 2220 | + output_box = aabb( |
| 2221 | + output_box.min() + offset, |
| 2222 | + output_box.max() + offset); |
| 2223 | + |
| 2224 | + return true; |
2218 | 2225 | }
|
2219 | 2226 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2220 | 2227 | [Listing [translate-class]: <kbd>[hittable.h]</kbd> Hittable translation class]
|
|
2295 | 2302 | sin_theta = sin(radians);
|
2296 | 2303 | cos_theta = cos(radians);
|
2297 | 2304 | hasbox = ptr->bounding_box(0, 1, bbox);
|
2298 |
| - vec3 min(infinity, infinity, infinity); |
| 2305 | + |
| 2306 | + vec3 min( infinity, infinity, infinity); |
2299 | 2307 | vec3 max(-infinity, -infinity, -infinity);
|
| 2308 | + |
2300 | 2309 | for (int i = 0; i < 2; i++) {
|
2301 | 2310 | for (int j = 0; j < 2; j++) {
|
2302 | 2311 | for (int k = 0; k < 2; k++) {
|
2303 | 2312 | auto x = i*bbox.max().x() + (1-i)*bbox.min().x();
|
2304 | 2313 | auto y = j*bbox.max().y() + (1-j)*bbox.min().y();
|
2305 | 2314 | auto z = k*bbox.max().z() + (1-k)*bbox.min().z();
|
2306 |
| - auto newx = cos_theta*x + sin_theta*z; |
| 2315 | + |
| 2316 | + auto newx = cos_theta*x + sin_theta*z; |
2307 | 2317 | auto newz = -sin_theta*x + cos_theta*z;
|
| 2318 | + |
2308 | 2319 | vec3 tester(newx, y, newz);
|
| 2320 | + |
2309 | 2321 | for (int c = 0; c < 3; c++) {
|
2310 |
| - if (tester[c] > max[c]) |
2311 |
| - max[c] = tester[c]; |
2312 |
| - if (tester[c] < min[c]) |
2313 |
| - min[c] = tester[c]; |
| 2322 | + min[c] = ffmin(min[c], tester[c]); |
| 2323 | + max[c] = ffmax(max[c], tester[c]); |
2314 | 2324 | }
|
2315 | 2325 | }
|
2316 | 2326 | }
|
2317 | 2327 | }
|
| 2328 | + |
2318 | 2329 | bbox = aabb(min, max);
|
2319 | 2330 | }
|
2320 | 2331 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
2328 | 2339 | bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
|
2329 | 2340 | vec3 origin = r.origin();
|
2330 | 2341 | vec3 direction = r.direction();
|
| 2342 | + |
2331 | 2343 | origin[0] = cos_theta*r.origin()[0] - sin_theta*r.origin()[2];
|
2332 |
| - origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2]; |
| 2344 | + origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2]; |
| 2345 | + |
2333 | 2346 | direction[0] = cos_theta*r.direction()[0] - sin_theta*r.direction()[2];
|
2334 | 2347 | direction[2] = sin_theta*r.direction()[0] + cos_theta*r.direction()[2];
|
| 2348 | + |
2335 | 2349 | ray rotated_r(origin, direction, r.time());
|
2336 |
| - if (ptr->hit(rotated_r, t_min, t_max, rec)) { |
2337 |
| - vec3 p = rec.p; |
2338 |
| - vec3 normal = rec.normal; |
2339 |
| - p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2]; |
2340 |
| - p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2]; |
2341 |
| - normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2]; |
2342 |
| - normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2]; |
2343 |
| - rec.p = p; |
2344 |
| - rec.set_face_normal(rotated_r, normal); |
2345 |
| - return true; |
2346 |
| - } |
2347 |
| - else |
| 2350 | + |
| 2351 | + if (!ptr->hit(rotated_r, t_min, t_max, rec)) |
2348 | 2352 | return false;
|
| 2353 | + |
| 2354 | + vec3 p = rec.p; |
| 2355 | + vec3 normal = rec.normal; |
| 2356 | + |
| 2357 | + p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2]; |
| 2358 | + p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2]; |
| 2359 | + |
| 2360 | + normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2]; |
| 2361 | + normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2]; |
| 2362 | + |
| 2363 | + rec.p = p; |
| 2364 | + rec.set_face_normal(rotated_r, normal); |
| 2365 | + |
| 2366 | + return true; |
2349 | 2367 | }
|
2350 | 2368 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2351 | 2369 | [Listing [rot-y-hit]: <kbd>[hittable.h]</kbd> Hittable Y-rotate hit function]
|
|
0 commit comments