diff --git a/RayTracer/tools/Vec3.cpp b/RayTracer/tools/Vec3.cpp index 5cd548bdb33e741179d7c1488d150d930300c51b..61a9117d9bdcb737af0767fc0db79d92ce29f772 100644 --- a/RayTracer/tools/Vec3.cpp +++ b/RayTracer/tools/Vec3.cpp @@ -93,6 +93,9 @@ bool Vec3::operator!=(const Vec3& rhs) const { Vec3 Vec3::normalize() const { Vec3 x(*this); float l = x.length(); + if (l == 0) { + return x; + } return Vec3(x[0] / l, x[1] / l, x[2] / l); } float Vec3::length() const { @@ -123,4 +126,8 @@ std::ostream& operator<<(std::ostream& os, const util::Vec3& rhs) { os << "(" << rhs[0] << " ," << rhs[1] << " ," << rhs[2] << ")"; return os; } + +Vec3 operator*(float s, const Vec3& rhs) { + return rhs * s; +} } // namespace util \ No newline at end of file diff --git a/RayTracer/tools/Vec3.h b/RayTracer/tools/Vec3.h index f469b3de683dec5368fcbb88b0a6332e80c73d80..5b927e266be52f939668d0e596d4d7a3b65facdb 100644 --- a/RayTracer/tools/Vec3.h +++ b/RayTracer/tools/Vec3.h @@ -20,6 +20,7 @@ class Vec3 { Vec3 operator+(const Vec3& rhs) const; Vec3 operator*(const Vec3& rhs) const; Vec3 operator*(float s) const; + friend Vec3 operator*(float s, const Vec3& rhs); Vec3 operator/(const Vec3& rhs) const; Vec3 operator/(const float div) const; void operator-=(const Vec3& rhs);