diff --git a/RayTracer/shape/RectanglePlane.cpp b/RayTracer/shape/RectanglePlane.cpp index 04e5ba212a98fc0301a3c0588361507add26d03a..faf890f6d16d610f9f67848bd641749aafa9cd3e 100644 --- a/RayTracer/shape/RectanglePlane.cpp +++ b/RayTracer/shape/RectanglePlane.cpp @@ -30,11 +30,18 @@ std::optional<cam::Hit> RectanglePlane::intersect(const cam::Ray& r) const { if (r.in_range(t) && std::abs(t_hitpoint.x()) <= width / 2 && std::abs(t_hitpoint.z()) <= depth / 2) { - return std::optional<cam::Hit>({t_hitpoint, n, t, material}); + return std::optional<cam::Hit>( + {t_hitpoint, n, texture_coordinates(t_hitpoint), t, material}); } else { return std::nullopt; } } + +std::pair<float, float> RectanglePlane::texture_coordinates( + const util::Vec3& pos) const { + return std::pair<float, float>( + {pos.x() / width + 0.5, pos.z() / depth + 0.5}); +} util::AxisAlignedBoundingBox RectanglePlane::bounds() const { return util::AxisAlignedBoundingBox(util::Vec3(-width / 2, 0, -depth / 2), util::Vec3(width / 2, 0, depth / 2)); @@ -44,8 +51,9 @@ util::SurfacePoint RectanglePlane::sampleLight() const { float x = util::disMinus1To1(util::gen) * width / 2; // Z coord of the sampled point. float z = util::disMinus1To1(util::gen) * depth / 2; - return util::SurfacePoint(util::Vec3(x, 0, z), util::Vec3(0, 1, 0), - material); + util::Vec3 pos(x, 0, z); + return util::SurfacePoint(pos, util::Vec3(0, 1, 0), + texture_coordinates(pos), material); // The sampled point will be in local coordinates. } util::Vec3 RectanglePlane::calculateLightEmission(const util::SurfacePoint& p, diff --git a/RayTracer/shape/RectanglePlane.h b/RayTracer/shape/RectanglePlane.h index 900c2ff78b224e23d6a3daec2a05c8d33f6218e6..c749a031731e7e085be544716b6cf90ea3ad5836 100644 --- a/RayTracer/shape/RectanglePlane.h +++ b/RayTracer/shape/RectanglePlane.h @@ -9,6 +9,8 @@ class RectanglePlane : public Light, public Shape { RectanglePlane(float width, float depth, bool twofaced, const std::shared_ptr<material::Material>& material); 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::SurfacePoint sampleLight() const override;