]> git.localhorst.tv Git - blobs.git/blobdiff - src/math/glm.hpp
face direction
[blobs.git] / src / math / glm.hpp
index 46843db4ca2850603d4ca9001dab12b1cc302bae..7bb321f712c20b4018c5b9507d7568627a53f2fd 100644 (file)
@@ -36,6 +36,17 @@ inline bool anynan(const T &v) noexcept {
        return glm::any(glm::isnan(v));
 }
 
+template<class Vec>
+inline Vec limit(const Vec &v, typename Vec::value_type max) noexcept {
+       typename Vec::value_type len2 = glm::length2(v);
+       typename Vec::value_type max2 = max * max;
+       if (len2 > max2) {
+               return glm::normalize(v) * max;
+       } else {
+               return v;
+       }
+}
+
 template<class T>
 inline T rgb2hsl(const T &rgb) {
        using Vec4 = glm::tvec4<typename T::value_type>;
@@ -55,8 +66,8 @@ template<class T>
 inline T hsl2rgb(const T &hsl) {
        using Vec3 = glm::tvec3<typename T::value_type>;
        using Vec4 = glm::tvec4<typename T::value_type>;
-       const Vec4 K(0.0, -1.0/3.0, 2.0/3.0, -1.0);
-       const Vec3 p(glm::abs(glm::fract(Vec3(hsl.h) + Vec3(K)) * 6.0 - Vec3(K.w)));
+       const Vec4 K(1.0, 2.0/3.0, 1.0/3.0, 3.0);
+       const Vec3 p(glm::abs(glm::fract(Vec3(hsl.x) + Vec3(K)) * 6.0 - Vec3(K.w)));
        T rgb = hsl.z * glm::mix(Vec3(K.x), glm::clamp(p - Vec3(K.x), 0.0, 1.0), hsl.y);
        return rgb;
 }