diff --git a/glm/core/_swizzle.hpp b/glm/core/_swizzle.hpp index 5e943a88..439ac028 100644 --- a/glm/core/_swizzle.hpp +++ b/glm/core/_swizzle.hpp @@ -48,7 +48,6 @@ namespace glm }; }//namespace glm - namespace glm{ namespace detail { @@ -58,8 +57,8 @@ namespace detail ValueType = type of scalar values (e.g. float, double) VecType = class the swizzle is applies to (e.g. vector3f) - N = number of components in the vector (e.g. 3) - E0...3 = what index the n-th element of this swizzle refers to + N = number of components in the vector (e.g. 3) + E0...3 = what index the n-th element of this swizzle refers to in the unswizzled vec */ template struct swizzle_base @@ -68,75 +67,71 @@ namespace detail typedef VecType vec_type; typedef ValueType value_type; - swizzle_base& operator= (const VecType& that) + swizzle_base& operator= (const ValueType& t) { static const int offset_dst[4] = { E0, E1, E2, E3 }; - // Make a copy of the data in this == &that - ValueType t[N]; - for (int i = 0; i < N; ++i) - t[i] = that[i]; for (int i = 0; i < N; ++i) - elem(offset_dst[i]) = t[i]; + elem(offset_dst[i]) = t; return *this; } - swizzle_base& operator= (const ValueType& t) + swizzle_base& operator= (const VecType& that) { - static const int offset_dst[4] = { E0, E1, E2, E3 }; - - for (int i = 0; i < N; ++i) - elem(offset_dst[i]) = t; - + struct op { + void operator() (value_type& e, value_type& t) { e = t; } + }; + _apply_op(that, op()); return *this; } void operator -= (const VecType& that) { - static const int offset_dst[4] = { E0, E1, E2, E3 }; - - ValueType t[N]; - for (int i = 0; i < N; ++i) - t[i] = that[i]; - for (int i = 0; i < N; ++i) - elem(offset_dst[i]) -= t[i]; + struct op { + void operator() (value_type& e, value_type& t) { e -= t; } + }; + _apply_op(that, op()); } void operator += (const VecType& that) { - static const int offset_dst[4] = { E0, E1, E2, E3 }; - - ValueType t[N]; - for (int i = 0; i < N; ++i) - t[i] = that[i]; - for (int i = 0; i < N; ++i) - elem(offset_dst[i]) += t[i]; + struct op { + void operator() (value_type& e, value_type& t) { e += t; } + }; + _apply_op(that, op()); } void operator *= (const VecType& that) { - static const int offset_dst[4] = { E0, E1, E2, E3 }; - - ValueType t[N]; - for (int i = 0; i < N; ++i) - t[i] = that[i]; - for (int i = 0; i < N; ++i) - elem(offset_dst[i]) *= t[i]; + struct op { + void operator() (value_type& e, value_type& t) { e *= t; } + }; + _apply_op(that, op()); } void operator /= (const VecType& that) + { + struct op { + void operator() (value_type& e, value_type& t) { e /= t; } + }; + _apply_op(that, op()); + } + + protected: + template + void _apply_op(const VecType& that, T op) { static const int offset_dst[4] = { E0, E1, E2, E3 }; + // Make a copy of the data in this == &that ValueType t[N]; for (int i = 0; i < N; ++i) t[i] = that[i]; for (int i = 0; i < N; ++i) - elem(offset_dst[i]) /= t[i]; + op( elem(offset_dst[i]), t[i] ); } - protected: value_type& elem (size_t i) { return (reinterpret_cast(_buffer))[i]; } const value_type& elem (size_t i) const { return (reinterpret_cast(_buffer))[i]; } @@ -169,6 +164,7 @@ namespace detail { using swizzle_base,T,P,2,E0,E1,0,0,(E0 == E1)>::operator=; P cast() const { return P(this->elem(E0), this->elem(E1)); } + P operator ()() const { return cast(); } operator P () const { return cast(); } }; @@ -178,6 +174,7 @@ namespace detail { using swizzle_base,T,P,2,E0,E1,E2,0,1>::operator=; P cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2)); } + P operator ()() const { return cast(); } operator P () const { return cast(); } }; @@ -187,6 +184,7 @@ namespace detail { using swizzle_base,T,P,2,E0,E1,E2,E3,1>::operator=; P cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); } + P operator ()() const { return cast(); } operator P () const { return cast(); } }; @@ -196,6 +194,7 @@ namespace detail { using swizzle_base,T,P,3,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>::operator=; P cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2)); } + P operator ()() const { return cast(); } operator P () const { return cast(); } }; @@ -205,6 +204,7 @@ namespace detail { using swizzle_base,T,P,2,E0,E1,0,0,(E0==E1)>::operator=; P cast() const { return P(this->elem(E0), this->elem(E1)); } + P operator ()() const { return cast(); } operator P () const { return cast(); } }; @@ -214,6 +214,7 @@ namespace detail { using swizzle_base,T,P,3,E0,E1,E2,E3,1>::operator=; P cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); } + P operator ()() const { return cast(); } operator P () const { return cast(); } }; @@ -223,6 +224,7 @@ namespace detail { using swizzle_base,T,P,4,E0,E1,E2,E3,(E0==E1||E0==E2||E0==E3||E1==E2||E1==E3||E2==E3)>::operator=; P cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); } + P operator ()() const { return cast(); } operator P () const { return cast(); } }; @@ -232,6 +234,7 @@ namespace detail { using swizzle_base,T,P,2,E0,E1,0,0,(E0==E1)>::operator=; P cast() const { return P(this->elem(E0), this->elem(E1)); } + P operator ()() const { return cast(); } operator P () const { return cast(); } }; @@ -242,18 +245,19 @@ namespace detail { using swizzle_base,T,P,4,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>::operator=; P cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2)); } + P operator ()() const { return cast(); } operator P () const { return cast(); } }; // -// To prevent the C++ syntax from getting *completely* overwhelming, define some alias macros +// To prevent the C++ syntax from getting entirely overwhelming, define some alias macros // #define _GLM_SWIZZLE_TEMPLATE1 template #define _GLM_SWIZZLE_TEMPLATE2 template #define _GLM_SWIZZLE_TYPE1 glm::detail::swizzle_base #define _GLM_SWIZZLE_TYPE2 glm::detail::swizzle_base -#define _GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(OPERAND)\ +#define _GLM_SWIZZLE_VECTOR_BINARY_OPERATOR_IMPLEMENTATION(OPERAND)\ _GLM_SWIZZLE_TEMPLATE2 \ typename P operator OPERAND ( const _GLM_SWIZZLE_TYPE1& a, const _GLM_SWIZZLE_TYPE2& b) \ { \ @@ -282,13 +286,65 @@ namespace detail return a OPERAND static_cast(b).cast(); \ } -#define _GLM_SWIZZLE_FUNCTION2(RETURN_TYPE,FUNCTION)\ +#define _GLM_SWIZZLE_FUNCTION_1_ARGS(RETURN_TYPE,FUNCTION)\ + template \ + typename glm::detail::swizzle2::RETURN_TYPE FUNCTION(const glm::detail::swizzle2& a) \ + { \ + return FUNCTION(a.cast()); \ + } \ + template \ + typename glm::detail::swizzle2_3::RETURN_TYPE FUNCTION(const glm::detail::swizzle2_3& a) \ + { \ + return FUNCTION(a.cast()); \ + } \ + template \ + typename glm::detail::swizzle2_4::RETURN_TYPE FUNCTION(const glm::detail::swizzle2_4& a) \ + { \ + return FUNCTION(a.cast()); \ + } \ + template \ + typename glm::detail::swizzle3_2::RETURN_TYPE FUNCTION(const glm::detail::swizzle3_2& a) \ + { \ + return FUNCTION(a.cast()); \ + } \ + template \ + typename glm::detail::swizzle3::RETURN_TYPE FUNCTION(const glm::detail::swizzle3& a) \ + { \ + return FUNCTION(a.cast()); \ + } \ + template \ + typename glm::detail::swizzle3_4::RETURN_TYPE FUNCTION(const glm::detail::swizzle3_4& a) \ + { \ + return FUNCTION(a.cast()); \ + } \ + template \ + typename glm::detail::swizzle4_2::RETURN_TYPE FUNCTION(const glm::detail::swizzle4_2& a) \ + { \ + return FUNCTION(a.cast()); \ + } \ + template \ + typename glm::detail::swizzle4_3::RETURN_TYPE FUNCTION(const glm::detail::swizzle4_3& a) \ + { \ + return FUNCTION(a.cast()); \ + } \ + template \ + typename glm::detail::swizzle4::RETURN_TYPE FUNCTION(const glm::detail::swizzle4& a) \ + { \ + return FUNCTION(a.cast()); \ + } + +#define _GLM_SWIZZLE_FUNCTION_2_ARGS(RETURN_TYPE,FUNCTION)\ _GLM_SWIZZLE_TEMPLATE2\ typename S0::RETURN_TYPE FUNCTION(const typename _GLM_SWIZZLE_TYPE1& a, const typename _GLM_SWIZZLE_TYPE2& b)\ {\ return FUNCTION(static_cast(a).cast(), static_cast(b).cast());\ }\ _GLM_SWIZZLE_TEMPLATE1\ + typename S0::RETURN_TYPE FUNCTION(const typename _GLM_SWIZZLE_TYPE1& a, const typename _GLM_SWIZZLE_TYPE1& b)\ + {\ + return FUNCTION(static_cast(a).cast(), static_cast(b).cast());\ + }\ + _GLM_SWIZZLE_TEMPLATE1\ typename S0::RETURN_TYPE FUNCTION(const typename _GLM_SWIZZLE_TYPE1& a, const typename S0::vec_type& b)\ {\ return FUNCTION(static_cast(a).cast(), b);\ @@ -297,26 +353,146 @@ namespace detail typename S0::RETURN_TYPE FUNCTION(const typename S0::vec_type& a, const typename _GLM_SWIZZLE_TYPE1& b)\ {\ return FUNCTION(a, static_cast(b).cast());\ + } \ + template \ + typename glm::detail::swizzle2::RETURN_TYPE FUNCTION(const glm::detail::swizzle2& a, const glm::detail::swizzle2& b) \ + { \ + return FUNCTION(a.cast(), b.cast()); \ + } \ + template \ + typename glm::detail::swizzle2_3::RETURN_TYPE FUNCTION(const glm::detail::swizzle2_3& a, const glm::detail::swizzle2_3& b) \ + { \ + return FUNCTION(a.cast(), b.cast()); \ + } \ + template \ + typename glm::detail::swizzle2_4::RETURN_TYPE FUNCTION(const glm::detail::swizzle2_4& a, const glm::detail::swizzle2_4& b) \ + { \ + return FUNCTION(a.cast(), b.cast()); \ + } \ + template \ + typename glm::detail::swizzle3_2::RETURN_TYPE FUNCTION(const glm::detail::swizzle3_2& a, const glm::detail::swizzle3_2& b) \ + { \ + return FUNCTION(a.cast(), b.cast()); \ + } \ + template \ + typename glm::detail::swizzle3::RETURN_TYPE FUNCTION(const glm::detail::swizzle3& a, const glm::detail::swizzle3& b) \ + { \ + return FUNCTION(a.cast(), b.cast()); \ + } \ + template \ + typename glm::detail::swizzle3_4::RETURN_TYPE FUNCTION(const glm::detail::swizzle3_4& a, const glm::detail::swizzle3_4& b) \ + { \ + return FUNCTION(a.cast(), b.cast()); \ + } \ + template \ + typename glm::detail::swizzle4_2::RETURN_TYPE FUNCTION(const glm::detail::swizzle4_2& a, const glm::detail::swizzle4_2& b) \ + { \ + return FUNCTION(a.cast(), b.cast()); \ + } \ + template \ + typename glm::detail::swizzle4_3::RETURN_TYPE FUNCTION(const glm::detail::swizzle4_3& a, const glm::detail::swizzle4_3& b) \ + { \ + return FUNCTION(a.cast(), b.cast()); \ + } \ + template \ + typename glm::detail::swizzle4::RETURN_TYPE FUNCTION(const glm::detail::swizzle4& a, const glm::detail::swizzle4& b) \ + { \ + return FUNCTION(a.cast(), b.cast()); \ } - _GLM_SWIZZLE_TEMPLATE1 typename S0::vec_type operator- (typename S0::value_type a, const _GLM_SWIZZLE_TYPE1& b) { return a - b; } - - _GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(+) - _GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(-) - _GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(*) - _GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(/) - - _GLM_SWIZZLE_SCALAR_BINARY_OPERATOR_IMPLEMENTATION(*) +#define _GLM_SWIZZLE_FUNCTION_2_ARGS_SCALAR(RETURN_TYPE,FUNCTION)\ + _GLM_SWIZZLE_TEMPLATE2\ + typename S0::RETURN_TYPE FUNCTION(const typename _GLM_SWIZZLE_TYPE1& a, const typename _GLM_SWIZZLE_TYPE2& b, const typename S0::value_type& c)\ + {\ + return FUNCTION(static_cast(a).cast(), static_cast(b).cast(), c);\ + }\ + _GLM_SWIZZLE_TEMPLATE1\ + typename S0::RETURN_TYPE FUNCTION(const typename _GLM_SWIZZLE_TYPE1& a, const typename _GLM_SWIZZLE_TYPE1& b, const typename S0::value_type& c)\ + {\ + return FUNCTION(static_cast(a).cast(), static_cast(b).cast(), c);\ + }\ + _GLM_SWIZZLE_TEMPLATE1\ + typename S0::RETURN_TYPE FUNCTION(const typename _GLM_SWIZZLE_TYPE1& a, const typename S0::vec_type& b, const typename S0::value_type& c)\ + {\ + return FUNCTION(static_cast(a).cast(), b, c);\ + }\ + _GLM_SWIZZLE_TEMPLATE1\ + typename S0::RETURN_TYPE FUNCTION(const typename S0::vec_type& a, const typename _GLM_SWIZZLE_TYPE1& b, const typename S0::value_type& c)\ + {\ + return FUNCTION(a, static_cast(b).cast(), c);\ + } \ + template \ + typename glm::detail::swizzle2::RETURN_TYPE FUNCTION(const glm::detail::swizzle2& a, const glm::detail::swizzle2& b, const T& c) \ + { \ + return FUNCTION(a.cast(), b.cast(), c); \ + } \ + template \ + typename glm::detail::swizzle2_3::RETURN_TYPE FUNCTION(const glm::detail::swizzle2_3& a, const glm::detail::swizzle2_3& b, const T& c) \ + { \ + return FUNCTION(a.cast(), b.cast(), c); \ + } \ + template \ + typename glm::detail::swizzle2_4::RETURN_TYPE FUNCTION(const glm::detail::swizzle2_4& a, const glm::detail::swizzle2_4& b, const T& c) \ + { \ + return FUNCTION(a.cast(), b.cast(), c); \ + } \ + template \ + typename glm::detail::swizzle3_2::RETURN_TYPE FUNCTION(const glm::detail::swizzle3_2& a, const glm::detail::swizzle3_2& b, const T& c) \ + { \ + return FUNCTION(a.cast(), b.cast(), c); \ + } \ + template \ + typename glm::detail::swizzle3::RETURN_TYPE FUNCTION(const glm::detail::swizzle3& a, const glm::detail::swizzle3& b, const T& c) \ + { \ + return FUNCTION(a.cast(), b.cast(), c); \ + } + template \ + typename glm::detail::swizzle3_4::RETURN_TYPE FUNCTION(const glm::detail::swizzle3_4& a, const glm::detail::swizzle3_4& b, const T& c) \ + { \ + return FUNCTION(a.cast(), b.cast(), c); \ + } \ + template \ + typename glm::detail::swizzle4_2::RETURN_TYPE FUNCTION(const glm::detail::swizzle4_2& a, const glm::detail::swizzle4_2& b, const T& c) \ + { \ + return FUNCTION(a.cast(), b.cast(), c); \ + } \ + template \ + typename glm::detail::swizzle4_3::RETURN_TYPE FUNCTION(const glm::detail::swizzle4_3& a, const glm::detail::swizzle4_3& b, const T& c) \ + { \ + return FUNCTION(a.cast(), b.cast(), c); \ + } \ + template \ + typename glm::detail::swizzle4::RETURN_TYPE FUNCTION(const glm::detail::swizzle4& a, const glm::detail::swizzle4& b, const T& c) \ + { \ + return FUNCTION(a.cast(), b.cast(), c); \ + } }//namespace detail }//namespace glm namespace glm { - + namespace detail + { + _GLM_SWIZZLE_SCALAR_BINARY_OPERATOR_IMPLEMENTATION(-) + _GLM_SWIZZLE_SCALAR_BINARY_OPERATOR_IMPLEMENTATION(*) - _GLM_SWIZZLE_FUNCTION2(value_type, dot); - _GLM_SWIZZLE_FUNCTION2(vec_type, abs); + _GLM_SWIZZLE_VECTOR_BINARY_OPERATOR_IMPLEMENTATION(+) + _GLM_SWIZZLE_VECTOR_BINARY_OPERATOR_IMPLEMENTATION(-) + _GLM_SWIZZLE_VECTOR_BINARY_OPERATOR_IMPLEMENTATION(*) + _GLM_SWIZZLE_VECTOR_BINARY_OPERATOR_IMPLEMENTATION(/) + } + + _GLM_SWIZZLE_FUNCTION_1_ARGS(vec_type, abs); + _GLM_SWIZZLE_FUNCTION_1_ARGS(vec_type, acos); + _GLM_SWIZZLE_FUNCTION_1_ARGS(vec_type, acosh); + _GLM_SWIZZLE_FUNCTION_1_ARGS(vec_type, all); + _GLM_SWIZZLE_FUNCTION_1_ARGS(vec_type, any); + + _GLM_SWIZZLE_FUNCTION_2_ARGS(value_type, dot); + _GLM_SWIZZLE_FUNCTION_2_ARGS(vec_type, cross); + _GLM_SWIZZLE_FUNCTION_2_ARGS(vec_type, step); + _GLM_SWIZZLE_FUNCTION_2_ARGS_SCALAR(vec_type, mix); } diff --git a/glm/core/func_geometric.inl b/glm/core/func_geometric.inl index f83b590c..26a6b64e 100644 --- a/glm/core/func_geometric.inl +++ b/glm/core/func_geometric.inl @@ -132,6 +132,7 @@ namespace glm ( genType const & x, genType const & y + ) { GLM_STATIC_ASSERT(detail::type::is_float, "'dot' only accept floating-point inputs"); diff --git a/glm/core/type_vec2.hpp b/glm/core/type_vec2.hpp index a28fa6e7..c93157bb 100644 --- a/glm/core/type_vec2.hpp +++ b/glm/core/type_vec2.hpp @@ -114,6 +114,22 @@ namespace detail tvec2(tref2 const & r); + template + GLM_FUNC_DECL tvec2(glm::detail::swizzle2,E0,E1>& that) + { + *this = that(); + } + template + GLM_FUNC_DECL tvec2(glm::detail::swizzle3_2,E0,E1>& that) + { + *this = that(); + } + template + GLM_FUNC_DECL tvec2(glm::detail::swizzle4_2,E0,E1>& that) + { + *this = that(); + } + ////////////////////////////////////// // Convertion constructors diff --git a/glm/core/type_vec3.hpp b/glm/core/type_vec3.hpp index 8a6ababc..e65463f2 100644 --- a/glm/core/type_vec3.hpp +++ b/glm/core/type_vec3.hpp @@ -116,6 +116,22 @@ namespace detail GLM_FUNC_DECL tvec3(tref3 const & r); + template + GLM_FUNC_DECL tvec3(glm::detail::swizzle2_3,E0,E1,E2>& that) + { + *this = that(); + } + template + GLM_FUNC_DECL tvec3(glm::detail::swizzle3,E0,E1,E2>& that) + { + *this = that(); + } + template + GLM_FUNC_DECL tvec3(glm::detail::swizzle4_3,E0,E1,E2>& that) + { + *this = that(); + } + ////////////////////////////////////// // Convertion scalar constructors diff --git a/glm/core/type_vec4.hpp b/glm/core/type_vec4.hpp index 5761bcdd..296e1641 100644 --- a/glm/core/type_vec4.hpp +++ b/glm/core/type_vec4.hpp @@ -118,6 +118,22 @@ namespace detail GLM_FUNC_DECL tvec4(tref4 const & r); + template + GLM_FUNC_DECL tvec4(glm::detail::swizzle2_4,E0,E1,E2,E3>& that) + { + *this = that(); + } + template + GLM_FUNC_DECL tvec4(glm::detail::swizzle3_4,E0,E1,E2,E3>& that) + { + *this = that(); + } + template + GLM_FUNC_DECL tvec4(glm::detail::swizzle4,E0,E1,E2,E3>& that) + { + *this = that(); + } + ////////////////////////////////////// // Convertion scalar constructors diff --git a/test/core/core_type_vec3.cpp b/test/core/core_type_vec3.cpp index 87572df3..fb7add27 100644 --- a/test/core/core_type_vec3.cpp +++ b/test/core/core_type_vec3.cpp @@ -1,260 +1,264 @@ -/////////////////////////////////////////////////////////////////////////////////////////////////// -// OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net) -/////////////////////////////////////////////////////////////////////////////////////////////////// -// Created : 2008-08-31 -// Updated : 2011-09-19 -// Licence : This source is under MIT License -// File : test/core/type_vec3.cpp -/////////////////////////////////////////////////////////////////////////////////////////////////// - -#include -#include - -static int test_vec3_operators() -{ - glm::vec3 A(1.0f); - glm::vec3 B(1.0f); - bool R = A != B; - bool S = A == B; - - return (S && !R) ? 0 : 1; -} - -int test_vec3_size() -{ - int Error = 0; - - Error += sizeof(glm::vec3) == sizeof(glm::mediump_vec3) ? 0 : 1; - Error += 12 == sizeof(glm::mediump_vec3) ? 0 : 1; - Error += sizeof(glm::dvec3) == sizeof(glm::highp_vec3) ? 0 : 1; - Error += 24 == sizeof(glm::highp_vec3) ? 0 : 1; - Error += glm::vec3().length() == 3 ? 0 : 1; - Error += glm::dvec3().length() == 3 ? 0 : 1; - - return Error; -} - -int test_vec3_swizzle3_2() -{ - int Error = 0; - - glm::vec3 v(1, 2, 3); - glm::vec2 u; - - // Can not assign a vec3 swizzle to a vec2 - //u = v.xyz; //Illegal - //u = v.rgb; //Illegal - //u = v.stp; //Illegal - - u = v.xx; Error += (u.x == 1.0f && u.y == 1.0f) ? 0 : 1; - u = v.xy; Error += (u.x == 1.0f && u.y == 2.0f) ? 0 : 1; - u = v.xz; Error += (u.x == 1.0f && u.y == 3.0f) ? 0 : 1; - u = v.yx; Error += (u.x == 2.0f && u.y == 1.0f) ? 0 : 1; - u = v.yy; Error += (u.x == 2.0f && u.y == 2.0f) ? 0 : 1; - u = v.yz; Error += (u.x == 2.0f && u.y == 3.0f) ? 0 : 1; - u = v.zx; Error += (u.x == 3.0f && u.y == 1.0f) ? 0 : 1; - u = v.zy; Error += (u.x == 3.0f && u.y == 2.0f) ? 0 : 1; - u = v.zz; Error += (u.x == 3.0f && u.y == 3.0f) ? 0 : 1; - - u = v.rr; Error += (u.r == 1.0f && u.g == 1.0f) ? 0 : 1; - u = v.rg; Error += (u.r == 1.0f && u.g == 2.0f) ? 0 : 1; - u = v.rb; Error += (u.r == 1.0f && u.g == 3.0f) ? 0 : 1; - u = v.gr; Error += (u.r == 2.0f && u.g == 1.0f) ? 0 : 1; - u = v.gg; Error += (u.r == 2.0f && u.g == 2.0f) ? 0 : 1; - u = v.gb; Error += (u.r == 2.0f && u.g == 3.0f) ? 0 : 1; - u = v.br; Error += (u.r == 3.0f && u.g == 1.0f) ? 0 : 1; - u = v.bg; Error += (u.r == 3.0f && u.g == 2.0f) ? 0 : 1; - u = v.bb; Error += (u.r == 3.0f && u.g == 3.0f) ? 0 : 1; - - u = v.ss; Error += (u.s == 1.0f && u.t == 1.0f) ? 0 : 1; - u = v.st; Error += (u.s == 1.0f && u.t == 2.0f) ? 0 : 1; - u = v.sp; Error += (u.s == 1.0f && u.t == 3.0f) ? 0 : 1; - u = v.ts; Error += (u.s == 2.0f && u.t == 1.0f) ? 0 : 1; - u = v.tt; Error += (u.s == 2.0f && u.t == 2.0f) ? 0 : 1; - u = v.tp; Error += (u.s == 2.0f && u.t == 3.0f) ? 0 : 1; - u = v.ps; Error += (u.s == 3.0f && u.t == 1.0f) ? 0 : 1; - u = v.pt; Error += (u.s == 3.0f && u.t == 2.0f) ? 0 : 1; - u = v.pp; Error += (u.s == 3.0f && u.t == 3.0f) ? 0 : 1; - - // Mixed member aliases are not valid - //u = v.rx; //Illegal - //u = v.sy; //Illegal - - - u = glm::vec2(1, 2); - v = glm::vec3(1, 2, 3); - //v.xx = u; //Illegal - v.xy = u; Error += (v.x == 1.0f && v.y == 2.0f && v.z == 3.0f) ? 0 : 1; - v.xz = u; Error += (v.x == 1.0f && v.y == 2.0f && v.z == 2.0f) ? 0 : 1; - v.yx = u; Error += (v.x == 2.0f && v.y == 1.0f && v.z == 2.0f) ? 0 : 1; - //v.yy = u; //Illegal - v.yz = u; Error += (v.x == 2.0f && v.y == 1.0f && v.z == 2.0f) ? 0 : 1; - v.zx = u; Error += (v.x == 2.0f && v.y == 1.0f && v.z == 1.0f) ? 0 : 1; - v.zy = u; Error += (v.x == 2.0f && v.y == 2.0f && v.z == 1.0f) ? 0 : 1; - //v.zz = u; //Illegal - - return Error; -} - -int test_vec3_swizzle3_3() -{ - int Error = 0; - - glm::vec3 v(1, 2, 3); - glm::vec3 u; - - u = v; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; - - u = v.xyz; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; - u = v.zyx; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; - u.zyx = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; - - u = v.rgb; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; - u = v.bgr; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; - u.bgr = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; - - u = v.stp; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; - u = v.pts; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; - u.pts = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; - - return Error; -} - -int test_vec3_swizzle_half() -{ - int Error = 0; - - glm::half a1(1); - glm::half b1(2); - glm::half c1(3); - glm::hvec3 v(a1, b1, c1); - glm::hvec3 u; - - u = v; - - Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1; - - u = v.xyz; - Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1; - u = v.zyx; - Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; - u.zyx = v; - Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; - - u = v.rgb; - Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1; - u = v.bgr; - Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; - u.bgr = v; - Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; - - u = v.stp; - Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1; - u = v.pts; - Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; - u.pts = v; - Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; - - return Error; -} - -int test_vec3_swizzle_operators() -{ - int Error = 0; - - glm::vec3 q, u, v; - - u = glm::vec3(1, 2, 3); - v = glm::vec3(10, 20, 30); - - // Swizzle, swizzle binary operators - q = u.xyz + v.xyz; Error += (q == (u + v)) ? 0 : 1; - q = (u.zyx + v.zyx).zyx; Error += (q == (u + v)) ? 0 : 1; - q = (u.xyz - v.xyz); Error += (q == (u - v)) ? 0 : 1; - q = (u.xyz * v.xyz); Error += (q == (u * v)) ? 0 : 1; - q = (u.xxx * v.xxx); Error += (q == glm::vec3(u.x * v.x)) ? 0 : 1; - q = (u.xyz / v.xyz); Error += (q == (u / v)) ? 0 : 1; - - // vec, swizzle binary operators - q = u + v.xyz; Error += (q == (u + v)) ? 0 : 1; - q = (u - v.xyz); Error += (q == (u - v)) ? 0 : 1; - q = (u * v.xyz); Error += (q == (u * v)) ? 0 : 1; - q = (u * v.xxx); Error += (q == v.x * u) ? 0 : 1; - q = (u / v.xyz); Error += (q == (u / v)) ? 0 : 1; - - // swizzle,vec binary operators - q = u.xyz + v; Error += (q == (u + v)) ? 0 : 1; - q = (u.xyz - v); Error += (q == (u - v)) ? 0 : 1; - q = (u.xyz * v); Error += (q == (u * v)) ? 0 : 1; - q = (u.xxx * v); Error += (q == u.x * v) ? 0 : 1; - q = (u.xyz / v); Error += (q == (u / v)) ? 0 : 1; - - // Compile errors - //q = (u.yz * v.xyz); - //q = (u * v.xy); - - return Error; -} - -int test_vec3_swizzle_functions() -{ - int Error = 0; - - // vec3 - working as expected - glm::vec3 q, u, v; - u = glm::vec3(1, 2, 3); - v = glm::vec3(10, 20, 30); - glm::dot(u, v); - glm::dot(u.xyz, v.zyz); - glm::dot(u, v.zyx); - glm::dot(u.xyz, v); - - // vec2 - not working! how is vec3 working and not vec2? - glm::vec2 a, b; - glm::dot(a, b); - glm::dot(a.xy, b.xy); - //glm::dot(u.xy, v.xy); - - glm::dot(glm::vec4(1,2,3,4).xyz, v); - - glm::vec4 r, s, t; - - r = glm::vec4(1, 2, 3, 4); - s = glm::vec4(10, 20, 30, 40); - - glm::dot(r, s); - //glm::dot(r.xyzw, s.xyzw); - //glm::dot(r.xyz, s.xyz); - - return Error; -} - -#if 0 -using namespace glm; - +/////////////////////////////////////////////////////////////////////////////////////////////////// +// OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net) +/////////////////////////////////////////////////////////////////////////////////////////////////// +// Created : 2008-08-31 +// Updated : 2011-09-19 +// Licence : This source is under MIT License +// File : test/core/type_vec3.cpp +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include + +static int test_vec3_operators() +{ + glm::vec3 A(1.0f); + glm::vec3 B(1.0f); + bool R = A != B; + bool S = A == B; + + return (S && !R) ? 0 : 1; +} + +int test_vec3_size() +{ + int Error = 0; + + Error += sizeof(glm::vec3) == sizeof(glm::mediump_vec3) ? 0 : 1; + Error += 12 == sizeof(glm::mediump_vec3) ? 0 : 1; + Error += sizeof(glm::dvec3) == sizeof(glm::highp_vec3) ? 0 : 1; + Error += 24 == sizeof(glm::highp_vec3) ? 0 : 1; + Error += glm::vec3().length() == 3 ? 0 : 1; + Error += glm::dvec3().length() == 3 ? 0 : 1; + + return Error; +} + +int test_vec3_swizzle3_2() +{ + int Error = 0; + + glm::vec3 v(1, 2, 3); + glm::vec2 u; + + // Can not assign a vec3 swizzle to a vec2 + //u = v.xyz; //Illegal + //u = v.rgb; //Illegal + //u = v.stp; //Illegal + + u = v.xx; Error += (u.x == 1.0f && u.y == 1.0f) ? 0 : 1; + u = v.xy; Error += (u.x == 1.0f && u.y == 2.0f) ? 0 : 1; + u = v.xz; Error += (u.x == 1.0f && u.y == 3.0f) ? 0 : 1; + u = v.yx; Error += (u.x == 2.0f && u.y == 1.0f) ? 0 : 1; + u = v.yy; Error += (u.x == 2.0f && u.y == 2.0f) ? 0 : 1; + u = v.yz; Error += (u.x == 2.0f && u.y == 3.0f) ? 0 : 1; + u = v.zx; Error += (u.x == 3.0f && u.y == 1.0f) ? 0 : 1; + u = v.zy; Error += (u.x == 3.0f && u.y == 2.0f) ? 0 : 1; + u = v.zz; Error += (u.x == 3.0f && u.y == 3.0f) ? 0 : 1; + + u = v.rr; Error += (u.r == 1.0f && u.g == 1.0f) ? 0 : 1; + u = v.rg; Error += (u.r == 1.0f && u.g == 2.0f) ? 0 : 1; + u = v.rb; Error += (u.r == 1.0f && u.g == 3.0f) ? 0 : 1; + u = v.gr; Error += (u.r == 2.0f && u.g == 1.0f) ? 0 : 1; + u = v.gg; Error += (u.r == 2.0f && u.g == 2.0f) ? 0 : 1; + u = v.gb; Error += (u.r == 2.0f && u.g == 3.0f) ? 0 : 1; + u = v.br; Error += (u.r == 3.0f && u.g == 1.0f) ? 0 : 1; + u = v.bg; Error += (u.r == 3.0f && u.g == 2.0f) ? 0 : 1; + u = v.bb; Error += (u.r == 3.0f && u.g == 3.0f) ? 0 : 1; + + u = v.ss; Error += (u.s == 1.0f && u.t == 1.0f) ? 0 : 1; + u = v.st; Error += (u.s == 1.0f && u.t == 2.0f) ? 0 : 1; + u = v.sp; Error += (u.s == 1.0f && u.t == 3.0f) ? 0 : 1; + u = v.ts; Error += (u.s == 2.0f && u.t == 1.0f) ? 0 : 1; + u = v.tt; Error += (u.s == 2.0f && u.t == 2.0f) ? 0 : 1; + u = v.tp; Error += (u.s == 2.0f && u.t == 3.0f) ? 0 : 1; + u = v.ps; Error += (u.s == 3.0f && u.t == 1.0f) ? 0 : 1; + u = v.pt; Error += (u.s == 3.0f && u.t == 2.0f) ? 0 : 1; + u = v.pp; Error += (u.s == 3.0f && u.t == 3.0f) ? 0 : 1; + + // Mixed member aliases are not valid + //u = v.rx; //Illegal + //u = v.sy; //Illegal + + + u = glm::vec2(1, 2); + v = glm::vec3(1, 2, 3); + //v.xx = u; //Illegal + v.xy = u; Error += (v.x == 1.0f && v.y == 2.0f && v.z == 3.0f) ? 0 : 1; + v.xz = u; Error += (v.x == 1.0f && v.y == 2.0f && v.z == 2.0f) ? 0 : 1; + v.yx = u; Error += (v.x == 2.0f && v.y == 1.0f && v.z == 2.0f) ? 0 : 1; + //v.yy = u; //Illegal + v.yz = u; Error += (v.x == 2.0f && v.y == 1.0f && v.z == 2.0f) ? 0 : 1; + v.zx = u; Error += (v.x == 2.0f && v.y == 1.0f && v.z == 1.0f) ? 0 : 1; + v.zy = u; Error += (v.x == 2.0f && v.y == 2.0f && v.z == 1.0f) ? 0 : 1; + //v.zz = u; //Illegal + + return Error; +} + +int test_vec3_swizzle3_3() +{ + int Error = 0; + + glm::vec3 v(1, 2, 3); + glm::vec3 u; + + u = v; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; + + u = v.xyz; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; + u = v.zyx; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + u.zyx = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + + u = v.rgb; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; + u = v.bgr; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + u.bgr = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + + u = v.stp; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; + u = v.pts; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + u.pts = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + + return Error; +} + +int test_vec3_swizzle_half() +{ + int Error = 0; + + glm::half a1(1); + glm::half b1(2); + glm::half c1(3); + glm::hvec3 v(a1, b1, c1); + glm::hvec3 u; + + u = v; + + Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1; + + u = v.xyz; + Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1; + u = v.zyx; + Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; + u.zyx = v; + Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; + + u = v.rgb; + Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1; + u = v.bgr; + Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; + u.bgr = v; + Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; + + u = v.stp; + Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1; + u = v.pts; + Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; + u.pts = v; + Error += (u.x.toFloat() == 3.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 1.0f) ? 0 : 1; + + return Error; +} + +int test_vec3_swizzle_operators() +{ + int Error = 0; + + glm::vec3 q, u, v; + + u = glm::vec3(1, 2, 3); + v = glm::vec3(10, 20, 30); + + // Swizzle, swizzle binary operators + q = u.xyz + v.xyz; Error += (q == (u + v)) ? 0 : 1; + q = (u.zyx + v.zyx).zyx; Error += (q == (u + v)) ? 0 : 1; + q = (u.xyz - v.xyz); Error += (q == (u - v)) ? 0 : 1; + q = (u.xyz * v.xyz); Error += (q == (u * v)) ? 0 : 1; + q = (u.xxx * v.xxx); Error += (q == glm::vec3(u.x * v.x)) ? 0 : 1; + q = (u.xyz / v.xyz); Error += (q == (u / v)) ? 0 : 1; + + // vec, swizzle binary operators + q = u + v.xyz; Error += (q == (u + v)) ? 0 : 1; + q = (u - v.xyz); Error += (q == (u - v)) ? 0 : 1; + q = (u * v.xyz); Error += (q == (u * v)) ? 0 : 1; + q = (u * v.xxx); Error += (q == v.x * u) ? 0 : 1; + q = (u / v.xyz); Error += (q == (u / v)) ? 0 : 1; + + // swizzle,vec binary operators + q = u.xyz + v; Error += (q == (u + v)) ? 0 : 1; + q = (u.xyz - v); Error += (q == (u - v)) ? 0 : 1; + q = (u.xyz * v); Error += (q == (u * v)) ? 0 : 1; + q = (u.xxx * v); Error += (q == u.x * v) ? 0 : 1; + q = (u.xyz / v); Error += (q == (u / v)) ? 0 : 1; + + // Compile errors + //q = (u.yz * v.xyz); + //q = (u * v.xy); + + return Error; +} + +int test_vec3_swizzle_functions() +{ + int Error = 0; + + // vec3 - working as expected + glm::vec3 q, u, v; + u = glm::vec3(1, 2, 3); + v = glm::vec3(10, 20, 30); + glm::dot(u, v); + glm::dot(u.xyz, v.zyz); + glm::dot(u, v.zyx); + glm::dot(u.xyz, v); + + // vec2 - not working! how is vec3 working and not vec2? + glm::vec2 a, b; + glm::dot(a, b); + glm::dot(a.xy, b.yy); + glm::dot(a.xy, b.xy); + glm::dot(u.xy, v.xy); + + glm::dot(glm::vec4(1,2,3,4).xyz, v); + + glm::vec4 r, s, t; + + r = glm::vec4(1, 2, 3, 4); + s = glm::vec4(10, 20, 30, 40); + + glm::dot(r, s); + glm::dot(r.xyzw, s.xyzw); + glm::dot(r.xyz, s.xyz); + + glm::cross(u, v); + glm::cross(u.zyx, v); + glm::cross(u.xxz, v.yyx); + + return Error; +} + +#if 1 +using namespace glm; + // -// Description : Array and textureless GLSL 2D/3D/4D simplex -// noise functions. -// Author : Ian McEwan, Ashima Arts. -// Maintainer : ijm -// Lastmod : 20110822 (ijm) -// License : Copyright (C) 2011 Ashima Arts. All rights reserved. -// Distributed under the MIT License. See LICENSE file. -// https://github.com/ashima/webgl-noise -// - -vec4 mod289(vec4 x) { - return x - floor(x * (1.0 / 289.0)) * 289.0; } - -float mod289(float x) { - return x - floor(x * (1.0 / 289.0)) * 289.0; } - -vec4 permute(vec4 x) { - return mod289(((x*34.0)+1.0)*x); +// GLSL textureless classic 4D noise "cnoise", +// with an RSL-style periodic variant "pnoise". +// Author: Stefan Gustavson (stefan.gustavson@liu.se) +// Version: 2011-08-22 +// +// Many thanks to Ian McEwan of Ashima Arts for the +// ideas for permutation and gradient selection. +// +// Copyright (c) 2011 Stefan Gustavson. All rights reserved. +// Distributed under the MIT license. See LICENSE file. +// https://github.com/ashima/webgl-noise +// + +vec4 mod289(vec4 x) +{ + return x - floor(x * (1.0 / 289.0)) * 289.0; } -float permute(float x) { - return mod289(((x*34.0)+1.0)*x); +vec4 permute(vec4 x) +{ + return mod289(((x*34.0)+1.0)*x); } vec4 taylorInvSqrt(vec4 r) @@ -262,123 +266,292 @@ vec4 taylorInvSqrt(vec4 r) return 1.79284291400159 - 0.85373472095314 * r; } -float taylorInvSqrt(float r) +vec4 fade(vec4 t) { + return t*t*t*(t*(t*6.0-15.0)+10.0); +} + +// Classic Perlin noise +float cnoise(vec4 P) { - return 1.79284291400159 - 0.85373472095314 * r; + vec4 Pi0 = floor(P); // Integer part for indexing + vec4 Pi1 = Pi0 + 1.0; // Integer part + 1 + Pi0 = mod289(Pi0); + Pi1 = mod289(Pi1); + vec4 Pf0 = fract(P); // Fractional part for interpolation + vec4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0 + vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); + vec4 iy = vec4(Pi0.yy(), Pi1.yy()); + vec4 iz0 = vec4(Pi0.zzzz()); + vec4 iz1 = vec4(Pi1.zzzz()); + vec4 iw0 = vec4(Pi0.wwww()); + vec4 iw1 = vec4(Pi1.wwww); + + vec4 ixy = permute(permute(ix) + iy); + vec4 ixy0 = permute(ixy + iz0); + vec4 ixy1 = permute(ixy + iz1); + vec4 ixy00 = permute(ixy0 + iw0); + vec4 ixy01 = permute(ixy0 + iw1); + vec4 ixy10 = permute(ixy1 + iw0); + vec4 ixy11 = permute(ixy1 + iw1); + + vec4 gx00 = ixy00 * (1.0 / 7.0); + vec4 gy00 = floor(gx00) * (1.0 / 7.0); + vec4 gz00 = floor(gy00) * (1.0 / 6.0); + gx00 = fract(gx00) - 0.5; + gy00 = fract(gy00) - 0.5; + gz00 = fract(gz00) - 0.5; + vec4 gw00 = vec4(0.75) - abs(gx00) - abs(gy00) - abs(gz00); + vec4 sw00 = step(gw00, vec4(0.0)); + gx00 -= sw00 * (step(0.0, gx00) - 0.5); + gy00 -= sw00 * (step(0.0, gy00) - 0.5); + + vec4 gx01 = ixy01 * (1.0 / 7.0); + vec4 gy01 = floor(gx01) * (1.0 / 7.0); + vec4 gz01 = floor(gy01) * (1.0 / 6.0); + gx01 = fract(gx01) - 0.5; + gy01 = fract(gy01) - 0.5; + gz01 = fract(gz01) - 0.5; + vec4 gw01 = vec4(0.75) - abs(gx01) - abs(gy01) - abs(gz01); + vec4 sw01 = step(gw01, vec4(0.0)); + gx01 -= sw01 * (step(0.0, gx01) - 0.5); + gy01 -= sw01 * (step(0.0, gy01) - 0.5); + + vec4 gx10 = ixy10 * (1.0 / 7.0); + vec4 gy10 = floor(gx10) * (1.0 / 7.0); + vec4 gz10 = floor(gy10) * (1.0 / 6.0); + gx10 = fract(gx10) - 0.5; + gy10 = fract(gy10) - 0.5; + gz10 = fract(gz10) - 0.5; + vec4 gw10 = vec4(0.75) - abs(gx10) - abs(gy10) - abs(gz10); + vec4 sw10 = step(gw10, vec4(0.0)); + gx10 -= sw10 * (step(0.0, gx10) - 0.5); + gy10 -= sw10 * (step(0.0, gy10) - 0.5); + + vec4 gx11 = ixy11 * (1.0 / 7.0); + vec4 gy11 = floor(gx11) * (1.0 / 7.0); + vec4 gz11 = floor(gy11) * (1.0 / 6.0); + gx11 = fract(gx11) - 0.5; + gy11 = fract(gy11) - 0.5; + gz11 = fract(gz11) - 0.5; + vec4 gw11 = vec4(0.75) - abs(gx11) - abs(gy11) - abs(gz11); + vec4 sw11 = step(gw11, vec4(0.0)); + gx11 -= sw11 * (step(0.0, gx11) - 0.5); + gy11 -= sw11 * (step(0.0, gy11) - 0.5); + + vec4 g0000 = vec4(gx00.x,gy00.x,gz00.x,gw00.x); + vec4 g1000 = vec4(gx00.y,gy00.y,gz00.y,gw00.y); + vec4 g0100 = vec4(gx00.z,gy00.z,gz00.z,gw00.z); + vec4 g1100 = vec4(gx00.w,gy00.w,gz00.w,gw00.w); + vec4 g0010 = vec4(gx10.x,gy10.x,gz10.x,gw10.x); + vec4 g1010 = vec4(gx10.y,gy10.y,gz10.y,gw10.y); + vec4 g0110 = vec4(gx10.z,gy10.z,gz10.z,gw10.z); + vec4 g1110 = vec4(gx10.w,gy10.w,gz10.w,gw10.w); + vec4 g0001 = vec4(gx01.x,gy01.x,gz01.x,gw01.x); + vec4 g1001 = vec4(gx01.y,gy01.y,gz01.y,gw01.y); + vec4 g0101 = vec4(gx01.z,gy01.z,gz01.z,gw01.z); + vec4 g1101 = vec4(gx01.w,gy01.w,gz01.w,gw01.w); + vec4 g0011 = vec4(gx11.x,gy11.x,gz11.x,gw11.x); + vec4 g1011 = vec4(gx11.y,gy11.y,gz11.y,gw11.y); + vec4 g0111 = vec4(gx11.z,gy11.z,gz11.z,gw11.z); + vec4 g1111 = vec4(gx11.w,gy11.w,gz11.w,gw11.w); + + vec4 norm00 = taylorInvSqrt(vec4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100))); + g0000 *= norm00.x; + g0100 *= norm00.y; + g1000 *= norm00.z; + g1100 *= norm00.w; + + vec4 norm01 = taylorInvSqrt(vec4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101))); + g0001 *= norm01.x; + g0101 *= norm01.y; + g1001 *= norm01.z; + g1101 *= norm01.w; + + vec4 norm10 = taylorInvSqrt(vec4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110))); + g0010 *= norm10.x; + g0110 *= norm10.y; + g1010 *= norm10.z; + g1110 *= norm10.w; + + vec4 norm11 = taylorInvSqrt(vec4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111))); + g0011 *= norm11.x; + g0111 *= norm11.y; + g1011 *= norm11.z; + g1111 *= norm11.w; + + float n0000 = dot(g0000, Pf0); + float n1000 = dot(g1000, vec4(Pf1.x, Pf0.yzw())); + float n0100 = dot(g0100, vec4(Pf0.x, Pf1.y, Pf0.zw())); + float n1100 = dot(g1100, vec4(Pf1.xy(), Pf0.zw())); + float n0010 = dot(g0010, vec4(Pf0.xy(), Pf1.z, Pf0.w)); + float n1010 = dot(g1010, vec4(Pf1.x, Pf0.y, Pf1.z, Pf0.w)); + float n0110 = dot(g0110, vec4(Pf0.x, Pf1.yz(), Pf0.w)); + float n1110 = dot(g1110, vec4(Pf1.xyz(), Pf0.w)); + float n0001 = dot(g0001, vec4(Pf0.xyz(), Pf1.w)); + float n1001 = dot(g1001, vec4(Pf1.x, Pf0.yz(), Pf1.w)); + float n0101 = dot(g0101, vec4(Pf0.x, Pf1.y, Pf0.z, Pf1.w)); + float n1101 = dot(g1101, vec4(Pf1.xy(), Pf0.z, Pf1.w)); + float n0011 = dot(g0011, vec4(Pf0.xy(), Pf1.zw())); + float n1011 = dot(g1011, vec4(Pf1.x, Pf0.y, Pf1.zw())); + float n0111 = dot(g0111, vec4(Pf0.x, Pf1.yzw())); + float n1111 = dot(g1111, Pf1); + + vec4 fade_xyzw = fade(Pf0); + vec4 n_0w = mix(vec4(n0000, n1000, n0100, n1100), vec4(n0001, n1001, n0101, n1101), fade_xyzw.w); + vec4 n_1w = mix(vec4(n0010, n1010, n0110, n1110), vec4(n0011, n1011, n0111, n1111), fade_xyzw.w); + vec4 n_zw = mix(n_0w, n_1w, fade_xyzw.z); + vec2 n_yzw = mix(n_zw.xy, n_zw.zw, fade_xyzw.y); + float n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x); + return 2.2 * n_xyzw; } -vec4 grad4(float j, vec4 ip) - { - const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0); - vec4 p,s; +// Classic Perlin noise, periodic version +float pnoise(vec4 P, vec4 rep) +{ + vec4 Pi0 = mod(floor(P), rep); // Integer part modulo rep + vec4 Pi1 = mod(Pi0 + 1.0, rep); // Integer part + 1 mod rep + Pi0 = mod289(Pi0); + Pi1 = mod289(Pi1); + vec4 Pf0 = fract(P); // Fractional part for interpolation + vec4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0 + vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); + vec4 iy = vec4(Pi0.yy(), Pi1.yy()); + vec4 iz0 = vec4(Pi0.zzzz()); + vec4 iz1 = vec4(Pi1.zzzz()); + vec4 iw0 = vec4(Pi0.wwww()); + vec4 iw1 = vec4(Pi1.wwww()); + + vec4 ixy = permute(permute(ix) + iy); + vec4 ixy0 = permute(ixy + iz0); + vec4 ixy1 = permute(ixy + iz1); + vec4 ixy00 = permute(ixy0 + iw0); + vec4 ixy01 = permute(ixy0 + iw1); + vec4 ixy10 = permute(ixy1 + iw0); + vec4 ixy11 = permute(ixy1 + iw1); + + vec4 gx00 = ixy00 * (1.0 / 7.0); + vec4 gy00 = floor(gx00) * (1.0 / 7.0); + vec4 gz00 = floor(gy00) * (1.0 / 6.0); + gx00 = fract(gx00) - 0.5; + gy00 = fract(gy00) - 0.5; + gz00 = fract(gz00) - 0.5; + vec4 gw00 = vec4(0.75) - abs(gx00) - abs(gy00) - abs(gz00); + vec4 sw00 = step(gw00, vec4(0.0)); + gx00 -= sw00 * (step(0.0, gx00) - 0.5); + gy00 -= sw00 * (step(0.0, gy00) - 0.5); + + vec4 gx01 = ixy01 * (1.0 / 7.0); + vec4 gy01 = floor(gx01) * (1.0 / 7.0); + vec4 gz01 = floor(gy01) * (1.0 / 6.0); + gx01 = fract(gx01) - 0.5; + gy01 = fract(gy01) - 0.5; + gz01 = fract(gz01) - 0.5; + vec4 gw01 = vec4(0.75) - abs(gx01) - abs(gy01) - abs(gz01); + vec4 sw01 = step(gw01, vec4(0.0)); + gx01 -= sw01 * (step(0.0, gx01) - 0.5); + gy01 -= sw01 * (step(0.0, gy01) - 0.5); + + vec4 gx10 = ixy10 * (1.0 / 7.0); + vec4 gy10 = floor(gx10) * (1.0 / 7.0); + vec4 gz10 = floor(gy10) * (1.0 / 6.0); + gx10 = fract(gx10) - 0.5; + gy10 = fract(gy10) - 0.5; + gz10 = fract(gz10) - 0.5; + vec4 gw10 = vec4(0.75) - abs(gx10) - abs(gy10) - abs(gz10); + vec4 sw10 = step(gw10, vec4(0.0)); + gx10 -= sw10 * (step(0.0, gx10) - 0.5); + gy10 -= sw10 * (step(0.0, gy10) - 0.5); + + vec4 gx11 = ixy11 * (1.0 / 7.0); + vec4 gy11 = floor(gx11) * (1.0 / 7.0); + vec4 gz11 = floor(gy11) * (1.0 / 6.0); + gx11 = fract(gx11) - 0.5; + gy11 = fract(gy11) - 0.5; + gz11 = fract(gz11) - 0.5; + vec4 gw11 = vec4(0.75) - abs(gx11) - abs(gy11) - abs(gz11); + vec4 sw11 = step(gw11, vec4(0.0)); + gx11 -= sw11 * (step(0.0, gx11) - 0.5); + gy11 -= sw11 * (step(0.0, gy11) - 0.5); + + vec4 g0000 = vec4(gx00.x,gy00.x,gz00.x,gw00.x); + vec4 g1000 = vec4(gx00.y,gy00.y,gz00.y,gw00.y); + vec4 g0100 = vec4(gx00.z,gy00.z,gz00.z,gw00.z); + vec4 g1100 = vec4(gx00.w,gy00.w,gz00.w,gw00.w); + vec4 g0010 = vec4(gx10.x,gy10.x,gz10.x,gw10.x); + vec4 g1010 = vec4(gx10.y,gy10.y,gz10.y,gw10.y); + vec4 g0110 = vec4(gx10.z,gy10.z,gz10.z,gw10.z); + vec4 g1110 = vec4(gx10.w,gy10.w,gz10.w,gw10.w); + vec4 g0001 = vec4(gx01.x,gy01.x,gz01.x,gw01.x); + vec4 g1001 = vec4(gx01.y,gy01.y,gz01.y,gw01.y); + vec4 g0101 = vec4(gx01.z,gy01.z,gz01.z,gw01.z); + vec4 g1101 = vec4(gx01.w,gy01.w,gz01.w,gw01.w); + vec4 g0011 = vec4(gx11.x,gy11.x,gz11.x,gw11.x); + vec4 g1011 = vec4(gx11.y,gy11.y,gz11.y,gw11.y); + vec4 g0111 = vec4(gx11.z,gy11.z,gz11.z,gw11.z); + vec4 g1111 = vec4(gx11.w,gy11.w,gz11.w,gw11.w); - auto t1 = abs(p.xyz); - auto t2 = ones.xyz; - auto t3 = dot(t1, t2); - auto t0 = dot(abs(p.xyz), ones.xyz); + vec4 norm00 = taylorInvSqrt(vec4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100))); + g0000 *= norm00.x; + g0100 *= norm00.y; + g1000 *= norm00.z; + g1100 *= norm00.w; - p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0f) * ip.z - 1.0f; - p.w = 1.5f - dot(abs(p.xyz), ones.xyz); - s = vec4(lessThan(p, vec4(0.0))); - p.xyz = p.xyz + (s.xyz*2.0f - 1.0f) * s.www; + vec4 norm01 = taylorInvSqrt(vec4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101))); + g0001 *= norm01.x; + g0101 *= norm01.y; + g1001 *= norm01.z; + g1101 *= norm01.w; - return p; + vec4 norm10 = taylorInvSqrt(vec4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110))); + g0010 *= norm10.x; + g0110 *= norm10.y; + g1010 *= norm10.z; + g1110 *= norm10.w; + + vec4 norm11 = taylorInvSqrt(vec4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111))); + g0011 *= norm11.x; + g0111 *= norm11.y; + g1011 *= norm11.z; + g1111 *= norm11.w; + + float n0000 = dot(g0000, Pf0); + float n1000 = dot(g1000, vec4(Pf1.x, Pf0.yzw())); + float n0100 = dot(g0100, vec4(Pf0.x, Pf1.y, Pf0.zw())); + float n1100 = dot(g1100, vec4(Pf1.xy(), Pf0.zw())); + float n0010 = dot(g0010, vec4(Pf0.xy(), Pf1.z, Pf0.w)); + float n1010 = dot(g1010, vec4(Pf1.x, Pf0.y, Pf1.z, Pf0.w)); + float n0110 = dot(g0110, vec4(Pf0.x, Pf1.yz(), Pf0.w)); + float n1110 = dot(g1110, vec4(Pf1.xyz(), Pf0.w)); + float n0001 = dot(g0001, vec4(Pf0.xyz(), Pf1.w)); + float n1001 = dot(g1001, vec4(Pf1.x, Pf0.yz(), Pf1.w)); + float n0101 = dot(g0101, vec4(Pf0.x, Pf1.y, Pf0.z, Pf1.w)); + float n1101 = dot(g1101, vec4(Pf1.xy(), Pf0.z, Pf1.w)); + float n0011 = dot(g0011, vec4(Pf0.xy(), Pf1.zw())); + float n1011 = dot(g1011, vec4(Pf1.x, Pf0.y, Pf1.zw())); + float n0111 = dot(g0111, vec4(Pf0.x, Pf1.yzw())); + float n1111 = dot(g1111, Pf1); + + vec4 fade_xyzw = fade(Pf0); + vec4 n_0w = mix(vec4(n0000, n1000, n0100, n1100), vec4(n0001, n1001, n0101, n1101), fade_xyzw.w); + vec4 n_1w = mix(vec4(n0010, n1010, n0110, n1110), vec4(n0011, n1011, n0111, n1111), fade_xyzw.w); + vec4 n_zw = mix(n_0w, n_1w, fade_xyzw.z); + vec2 n_yzw = mix(n_zw.xy, n_zw.zw, fade_xyzw.y); + float n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x); + return 2.2 * n_xyzw; } +#endif +int main() +{ + int Error = 0; -float snoise(vec4 v) - { - const vec4 C = vec4( 0.138196601125011, // (5 - sqrt(5))/20 G4 - 0.276393202250021, // 2 * G4 - 0.414589803375032, // 3 * G4 - -0.447213595499958); // -1 + 4 * G4 - -// (sqrt(5) - 1)/4 = F4, used once below -#define F4 0.309016994374947451 - -// First corner - vec4 i = floor(v + dot(v, vec4(F4)) ); - vec4 x0 = v - i + dot(i, C.xxxx); - -// Other corners - -// Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI) - vec4 i0; - vec3 isX = step( x0.yzw, x0.xxx ); - vec3 isYZ = step( x0.zww, x0.yyz ); -// i0.x = dot( isX, vec3( 1.0 ) ); - i0.x = isX.x + isX.y + isX.z; - i0.yzw = 1.0f - isX; -// i0.y += dot( isYZ.xy, vec2( 1.0 ) ); - i0.y += isYZ.x + isYZ.y; - i0.zw += 1.0f - isYZ.xy; - i0.z += isYZ.z; - i0.w += 1.0f - isYZ.z; - - // i0 now contains the unique values 0,1,2,3 in each channel - vec4 i3 = clamp( i0, 0.0, 1.0 ); - vec4 i2 = clamp( i0-1.0, 0.0, 1.0 ); - vec4 i1 = clamp( i0-2.0, 0.0, 1.0 ); - - // x0 = x0 - 0.0 + 0.0 * C.xxxx - // x1 = x0 - i1 + 1.0 * C.xxxx - // x2 = x0 - i2 + 2.0 * C.xxxx - // x3 = x0 - i3 + 3.0 * C.xxxx - // x4 = x0 - 1.0 + 4.0 * C.xxxx - vec4 x1 = x0 - i1 + C.xxxx; - vec4 x2 = x0 - i2 + C.yyyy; - vec4 x3 = x0 - i3 + C.zzzz; - vec4 x4 = x0 + C.wwww; - -// Permutations - i = mod289(i); - float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x); - vec4 j1 = permute( permute( permute( permute ( - i.w + vec4(i1.w, i2.w, i3.w, 1.0 )) - + i.z + vec4(i1.z, i2.z, i3.z, 1.0 )) - + i.y + vec4(i1.y, i2.y, i3.y, 1.0 )) - + i.x + vec4(i1.x, i2.x, i3.x, 1.0 )); - -// Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope -// 7*7*6 = 294, which is close to the ring size 17*17 = 289. - vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ; - - vec4 p0 = grad4(j0, ip); - vec4 p1 = grad4(j1.x, ip); - vec4 p2 = grad4(j1.y, ip); - vec4 p3 = grad4(j1.z, ip); - vec4 p4 = grad4(j1.w, ip); - -// Normalise gradients - vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); - p0 *= norm.x; - p1 *= norm.y; - p2 *= norm.z; - p3 *= norm.w; - p4 *= taylorInvSqrt(dot(p4,p4)); - -// Mix contributions from the five corners - vec3 m0 = max(0.6f - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0); - vec2 m1 = max(0.6f - vec2(dot(x3,x3), dot(x4,x4) ), 0.0); - m0 = m0 * m0; - m1 = m1 * m1; - return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 ))) - + dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ; - - } -#endif - -int main() -{ - int Error = 0; - - Error += test_vec3_operators(); - Error += test_vec3_size(); - Error += test_vec3_swizzle3_2(); - Error += test_vec3_swizzle3_3(); - Error += test_vec3_swizzle_half(); - Error += test_vec3_swizzle_operators(); - Error += test_vec3_swizzle_functions(); - - return Error; -} + Error += test_vec3_operators(); + Error += test_vec3_size(); + Error += test_vec3_swizzle3_2(); + Error += test_vec3_swizzle3_3(); + Error += test_vec3_swizzle_half(); + Error += test_vec3_swizzle_operators(); + Error += test_vec3_swizzle_functions(); + + return Error; +}