Binary operators for swizzle/vec combinations

This commit is contained in:
athile 2011-09-21 12:42:35 -04:00
parent 5d3c6fb4ca
commit fe9a5562c1
2 changed files with 50 additions and 16 deletions

View File

@ -196,23 +196,35 @@ namespace detail
operator P () { return cast(); }
};
template <typename T, typename P, int N, typename S0, int E0, int E1, int E2, int E3, int D0, typename S1,int F0, int F1, int F2, int F3, int D1>
typename P operator+ (
const glm::detail::swizzle_base<S0,T,P,N,E0,E1,E2,E3,D0>& a,
const glm::detail::swizzle_base<S1,T,P,N,F0,F1,F2,F3,D1>& b)
{
return static_cast<const S0&>(a).cast() + static_cast<const S1&>(b).cast();
#define _GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(OPERAND)\
template <typename T, typename P, int N, typename S0, int E0, int E1, int E2, int E3, int D0, typename S1,int F0, int F1, int F2, int F3, int D1> \
typename P operator OPERAND ( \
const glm::detail::swizzle_base<S0,T,P,N,E0,E1,E2,E3,D0>& a, \
const glm::detail::swizzle_base<S1,T,P,N,F0,F1,F2,F3,D1>& b) \
{ \
return static_cast<const S0&>(a).cast() OPERAND static_cast<const S1&>(b).cast(); \
} \
\
template <typename T, typename P, int N, typename S0, int E0, int E1, int E2, int E3, int D0> \
typename P operator OPERAND ( \
const glm::detail::swizzle_base<S0,T,P,N,E0,E1,E2,E3,D0>& a, \
const typename P& b) \
{ \
return static_cast<const S0&>(a).cast() OPERAND b; \
} \
\
template <typename T, typename P, int N, typename S0, int E0, int E1, int E2, int E3, int D0> \
typename P operator OPERAND ( \
const typename P& a, \
const glm::detail::swizzle_base<S0,T,P,N,E0,E1,E2,E3,D0>& b) \
{ \
return a OPERAND static_cast<const S0&>(b).cast(); \
}
template <typename T, typename P, int N, typename S0, int E0, int E1, int E2, int E3, int D0, typename S1,int F0, int F1, int F2, int F3, int D1>
typename P operator- (
const glm::detail::swizzle_base<S0,T,P,N,E0,E1,E2,E3,D0>& a,
const glm::detail::swizzle_base<S1,T,P,N,F0,F1,F2,F3,D1>& b)
{
return static_cast<const S0&>(a).cast() - static_cast<const S1&>(b).cast();
}
_GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(+)
_GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(-)
_GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(*)
_GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(/)
}//namespace detail
}//namespace glm

View File

@ -167,9 +167,31 @@ int test_vec3_swizzle_operators()
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;
}