Skip to content
Snippets Groups Projects
Commit 6e7017d9 authored by Yoel's avatar Yoel
Browse files

Sphere refactor: Texels working correctly

parent 56276277
No related branches found
No related tags found
No related merge requests found
...@@ -25,16 +25,18 @@ std::optional<cam::Hit> Sphere::intersect(const cam::Ray& r) const { ...@@ -25,16 +25,18 @@ std::optional<cam::Hit> Sphere::intersect(const cam::Ray& r) const {
util::Vec3 t1HitPoint = r(t1); util::Vec3 t1HitPoint = r(t1);
float theta = acos(t1HitPoint.y() / radius); float theta = acos(t1HitPoint.y() / radius);
float phi = M_PI + atan2(t1HitPoint.x(), t1HitPoint.z()); float phi = M_PI + atan2(t1HitPoint.x(), t1HitPoint.z());
return std::optional<cam::Hit>( return std::optional<cam::Hit>({t1HitPoint, t1HitPoint,
{t1HitPoint, t1HitPoint, t1, material}); texture_coordinates(t1HitPoint), t1,
material});
} else { } else {
float t2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); float t2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
if (r.in_range(t2)) { if (r.in_range(t2)) {
util::Vec3 t2HitPoint = r(t2); util::Vec3 t2HitPoint = r(t2);
float theta = acos(t2HitPoint.y() / radius); float theta = acos(t2HitPoint.y() / radius);
float phi = M_PI + atan2(t2HitPoint.x(), t2HitPoint.z()); float phi = M_PI + atan2(t2HitPoint.x(), t2HitPoint.z());
return std::optional<cam::Hit>( return std::optional<cam::Hit>({t2HitPoint, t2HitPoint,
{t2HitPoint, t2HitPoint, t2, material}); texture_coordinates(t2HitPoint),
t2, material});
} else { } else {
return std::nullopt; return std::nullopt;
} }
...@@ -43,6 +45,12 @@ std::optional<cam::Hit> Sphere::intersect(const cam::Ray& r) const { ...@@ -43,6 +45,12 @@ std::optional<cam::Hit> Sphere::intersect(const cam::Ray& r) const {
return std::nullopt; return std::nullopt;
} }
} }
std::pair<float, float> Sphere::texture_coordinates(
const util::Vec3& pos) const {
float theta = std::acos(pos.y() / radius);
float phi = M_PI + std::atan2(pos.x(), pos.z());
return std::pair<float, float>({phi / (2 * M_PI), theta / M_PI});
}
util::AxisAlignedBoundingBox Sphere::bounds() const { util::AxisAlignedBoundingBox Sphere::bounds() const {
return util::AxisAlignedBoundingBox(util::Vec3(-radius), return util::AxisAlignedBoundingBox(util::Vec3(-radius),
util::Vec3(radius)); util::Vec3(radius));
...@@ -56,7 +64,8 @@ util::SurfacePoint Sphere::sampleLight() const { ...@@ -56,7 +64,8 @@ util::SurfacePoint Sphere::sampleLight() const {
util::Vec3 point(radius * std::cos(theta) * std::sin(phi), util::Vec3 point(radius * std::cos(theta) * std::sin(phi),
radius * std::sin(theta) * std::sin(phi), radius * std::sin(theta) * std::sin(phi),
radius * std::cos(phi)); radius * std::cos(phi));
return util::SurfacePoint(point, point.normalize(), material); return util::SurfacePoint(point, point.normalize(),
texture_coordinates(point), material);
} }
util::Vec3 Sphere::calculateLightEmission(const util::SurfacePoint& p, util::Vec3 Sphere::calculateLightEmission(const util::SurfacePoint& p,
const util::Vec3& d) const { const util::Vec3& d) const {
......
...@@ -8,6 +8,8 @@ class Sphere : public Light, public Shape { ...@@ -8,6 +8,8 @@ class Sphere : public Light, public Shape {
public: public:
Sphere(float radius, const std::shared_ptr<material::Material>& material); Sphere(float radius, const std::shared_ptr<material::Material>& material);
std::optional<cam::Hit> intersect(const cam::Ray& r) const override; std::optional<cam::Hit> intersect(const cam::Ray& r) const override;
std::pair<float, float> texture_coordinates(
const util::Vec3& pos) const override;
util::AxisAlignedBoundingBox bounds() const override; util::AxisAlignedBoundingBox bounds() const override;
util::SurfacePoint sampleLight() const override; util::SurfacePoint sampleLight() const override;
util::Vec3 calculateLightEmission(const util::SurfacePoint& p, util::Vec3 calculateLightEmission(const util::SurfacePoint& p,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment