From 2edc75f7935573e6022be739b4058f56ae8665d2 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 23 Mar 2019 22:06:25 -1000 Subject: [PATCH] CColor clamp for all arithmetic ops --- include/zeus/CColor.hpp | 35 ++++++++++++++++++++++------------- include/zeus/CTransform.hpp | 6 ++---- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/include/zeus/CColor.hpp b/include/zeus/CColor.hpp index bdb0675..73bde95 100644 --- a/include/zeus/CColor.hpp +++ b/include/zeus/CColor.hpp @@ -133,59 +133,67 @@ public: bool operator!=(const CColor& rhs) const { return !(*this == rhs); } - CColor operator+(const CColor& rhs) const { return mSimd + rhs.mSimd; } + CColor operator+(const CColor& rhs) const { return CColor(mSimd + rhs.mSimd).Clamp(); } - CColor operator-(const CColor& rhs) const { return mSimd - rhs.mSimd; } + CColor operator-(const CColor& rhs) const { return CColor(mSimd - rhs.mSimd).Clamp(); } - CColor operator*(const CColor& rhs) const { return mSimd * rhs.mSimd; } + CColor operator*(const CColor& rhs) const { return CColor(mSimd * rhs.mSimd).Clamp(); } - CColor operator/(const CColor& rhs) const { return mSimd / rhs.mSimd; } + CColor operator/(const CColor& rhs) const { return CColor(mSimd / rhs.mSimd).Clamp(); } - CColor operator+(float val) const { return mSimd + simd(val); } + CColor operator+(float val) const { return CColor(mSimd + simd(val)).Clamp(); } - CColor operator-(float val) const { return mSimd - simd(val); } + CColor operator-(float val) const { return CColor(mSimd - simd(val)).Clamp(); } - CColor operator*(float val) const { return mSimd * simd(val); } + CColor operator*(float val) const { return CColor(mSimd * simd(val)).Clamp(); } - CColor operator/(float val) const { return mSimd / simd(val); } + CColor operator/(float val) const { return CColor(mSimd / simd(val)).Clamp(); } const CColor& operator+=(const CColor& rhs) { mSimd += rhs.mSimd; + Clamp(); return *this; } const CColor& operator-=(const CColor& rhs) { mSimd -= rhs.mSimd; + Clamp(); return *this; } const CColor& operator*=(const CColor& rhs) { mSimd *= rhs.mSimd; + Clamp(); return *this; } const CColor& operator/=(const CColor& rhs) { mSimd /= rhs.mSimd; + Clamp(); return *this; } const CColor& operator+=(float rhs) { mSimd += simd(rhs); + Clamp(); return *this; } const CColor& operator-=(float rhs) { mSimd -= simd(rhs); + Clamp(); return *this; } const CColor& operator*=(float rhs) { mSimd *= simd(rhs); + Clamp(); return *this; } const CColor& operator/=(float rhs) { mSimd /= simd(rhs); + Clamp(); return *this; } @@ -279,11 +287,12 @@ public: /** * @brief Clamps to GPU-safe RGBA values [0,1] */ - void Clamp() { + CColor& Clamp() { r() = std::min(1.f, std::max(0.f, float(r()))); g() = std::min(1.f, std::max(0.f, float(g()))); b() = std::min(1.f, std::max(0.f, float(b()))); a() = std::min(1.f, std::max(0.f, float(a()))); + return *this; } float r() const { return mSimd[0]; } @@ -314,11 +323,11 @@ constexpr CColor skYellow(1.f, 1.f, 0.f, 1.f); constexpr CColor skWhite(1.f, 1.f, 1.f, 1.f); constexpr CColor skClear(0.f, 0.f, 0.f, 0.f); -inline CColor operator+(float lhs, const CColor& rhs) { return simd(lhs) + rhs.mSimd; } +inline CColor operator+(float lhs, const CColor& rhs) { return CColor(simd(lhs) + rhs.mSimd).Clamp(); } -inline CColor operator-(float lhs, const CColor& rhs) { return simd(lhs) - rhs.mSimd; } +inline CColor operator-(float lhs, const CColor& rhs) { return CColor(simd(lhs) - rhs.mSimd).Clamp(); } -inline CColor operator*(float lhs, const CColor& rhs) { return simd(lhs) * rhs.mSimd; } +inline CColor operator*(float lhs, const CColor& rhs) { return CColor(simd(lhs) * rhs.mSimd).Clamp(); } -inline CColor operator/(float lhs, const CColor& rhs) { return simd(lhs) / rhs.mSimd; } +inline CColor operator/(float lhs, const CColor& rhs) { return CColor(simd(lhs) / rhs.mSimd).Clamp(); } } // namespace zeus diff --git a/include/zeus/CTransform.hpp b/include/zeus/CTransform.hpp index 2883d54..1e1d8ef 100644 --- a/include/zeus/CTransform.hpp +++ b/include/zeus/CTransform.hpp @@ -157,10 +157,8 @@ public: simd{0.f, 0.f, factor, 0.f})); } - CTransform multiplyIgnoreTranslation(const CTransform& xfrm) const { - CTransform ret; - ret.basis = basis * xfrm.basis; - return ret; + CTransform multiplyIgnoreTranslation(const CTransform& rhs) const { + return CTransform(basis * rhs.basis, origin + rhs.origin); } CTransform getRotation() const {