mirror of
https://github.com/Manoj-HV30/multithreaded-raytracer.git
synced 2026-05-16 19:35:24 +00:00
47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
#ifndef SPHERE_H
|
|
#define SPHERE_H
|
|
|
|
#include "hittable.h"
|
|
#include "vec3.h"
|
|
|
|
class sphere : public hittable {
|
|
public:
|
|
sphere(const point3& center, double radius, shared_ptr<material> mat) : center(center), radius(std::fmax(0,radius)), mat(mat) {}
|
|
|
|
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
|
|
vec3 oc = center - r.origin();
|
|
auto a = r.direction().length_squared();
|
|
auto h = dot(r.direction(), oc);
|
|
auto c = oc.length_squared() - radius*radius;
|
|
|
|
auto discriminant = h*h - a*c;
|
|
if (discriminant < 0)
|
|
return false;
|
|
|
|
auto sqrtd = std::sqrt(discriminant);
|
|
|
|
// Find the nearest root that lies in the acceptable range.
|
|
auto root = (h - sqrtd) / a;
|
|
if (!ray_t.surrounds(root)){
|
|
root = (h + sqrtd) / a;
|
|
if (!ray_t.surrounds(root))
|
|
return false;
|
|
}
|
|
|
|
rec.t = root;
|
|
rec.p = r.at(rec.t);
|
|
vec3 outward_normal = (rec.p - center) / radius;
|
|
rec.set_face_normal(r, outward_normal);
|
|
rec.mat = mat;
|
|
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
point3 center;
|
|
double radius;
|
|
shared_ptr<material>mat;
|
|
};
|
|
|
|
#endif
|