Newer
Older
#include "../tools/Random.h"
Sphere::Sphere(float radius,
const std::shared_ptr<material::Material>& material)
: radius(radius), material(material) {
util::Vec3 d = r.d;
util::Vec3 x0 = r.x0;
float a = util::dot(d, d);
float b = 2 * util::dot(x0, d);
float c = util::dot(x0, x0) - (radius * radius);
float discrim = b * b - 4 * a * c;
float t1 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
float theta = acos(t1HitPoint.y() / radius);
float phi = M_PI + atan2(t1HitPoint.x(), t1HitPoint.z());
float t2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
float theta = acos(t2HitPoint.y() / radius);
float phi = M_PI + atan2(t2HitPoint.x(), t2HitPoint.z());
util::AxisAlignedBoundingBox Sphere::bounds() const {
return util::AxisAlignedBoundingBox(util::Vec3(-radius),
util::Vec3(radius));
}
// TODO: Rework this function to be yours. THIS IS COPIED!!!! HIT IS A WRONG
// RETURN VALUE hit's n is wrong and bad
util::SurfacePoint Sphere::sampleLight() const {
float u[2] = {(float)util::dis(util::gen), (float)util::dis(util::gen)};
float z = u[0];
float r = std::sqrt(std::max((float)0, (float)1. - z * z));
float phi = 2 * M_PI * u[1];
util::Vec3 point(r * std::cos(phi), r * std::sin(phi), z);
return util::SurfacePoint(point, point, material);
}