diff --git a/util/Transform.cc b/util/Transform.cc index 962a52d..1ca90b3 100644 --- a/util/Transform.cc +++ b/util/Transform.cc @@ -64,6 +64,11 @@ Vector Transform::transform_direction(Transform & t, const Vector & v) return t.getMatrix() % v; } +Vector Transform::transform_normal(Transform & t, const Vector & v) +{ + return (t.getMatrix() % v).normalize(); +} + Ray Transform::transform_ray(Transform & t, const Ray & r) { Vector newPosition = t.getMatrix() * r.getOrigin(); diff --git a/util/Transform.h b/util/Transform.h index 11af3be..d03e7d2 100644 --- a/util/Transform.h +++ b/util/Transform.h @@ -17,6 +17,7 @@ class Transform Matrix & getMatrix() { return m_matrix; } Vector transform_point(Transform & t, const Vector & v); Vector transform_direction(Transform & t, const Vector & v); + Vector transform_normal(Transform & t, const Vector & v); Ray transform_ray(Transform & t, const Ray & r); protected: diff --git a/util/Vector.cc b/util/Vector.cc index 2208d11..744214c 100644 --- a/util/Vector.cc +++ b/util/Vector.cc @@ -17,7 +17,7 @@ Vector::Vector(double x, double y, double z) m_array[2] = z; } -void Vector::normalize() +Vector & Vector::normalize() { double length = sqrt(m_array[0] * m_array[0] + m_array[1] * m_array[1] @@ -25,6 +25,7 @@ void Vector::normalize() m_array[0] /= length; m_array[1] /= length; m_array[2] /= length; + return *this; } std::ostream & operator<<(std::ostream & out, const Vector & v) diff --git a/util/Vector.h b/util/Vector.h index 0704d93..390e89c 100644 --- a/util/Vector.h +++ b/util/Vector.h @@ -11,7 +11,7 @@ class Vector Vector(double x, double y, double z); double & operator[](int idx) { return m_array[idx]; } double operator[](int idx) const { return m_array[idx]; } - void normalize(); + Vector & normalize(); private: double m_array[3];