toytracer/sphere.h

34 lines
777 B
C++

#ifndef SPHERE_H
#define SPHERE_H
#include <cmath>
#include <memory>
#include "hittable.h"
#include "material.h"
#include "vec3.h"
#include "util.h"
class Sphere : public Hittable {
public:
Sphere() {}
Sphere(Point3 cen, double r, std::shared_ptr<Material> m) : center(cen), radius(r), mat_ptr(m) {}
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:
Point3 center;
double radius;
std::shared_ptr<Material> mat_ptr;
};
inline void compute_sphere_uv(const Vec3& p, double& u, double& v) {
auto phi = std::atan2(p.z(), p.x());
auto theta = asin(p.y());
u = 1 - (phi + pi) / (2 * pi);
v = (theta + pi/2) / pi;
}
#endif // SPHERE_H