WIP swizzle. Note: glm::dot() now working for swizzled vec3, but mysteriously not for vec2 or vec4.

This commit is contained in:
athile 2011-09-21 20:09:34 -04:00
parent 2851081b66
commit 6dee4eabc4
2 changed files with 85 additions and 46 deletions

View File

@ -242,51 +242,9 @@ namespace detail
{ {
using swizzle_base<swizzle4_3<T,P,E0,E1,E2>,T,P,4,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>::operator=; using swizzle_base<swizzle4_3<T,P,E0,E1,E2>,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 cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2)); }
operator P () { return cast(); } operator P () const { return 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(); \
}
#define _GLM_SWIZZLE_SCALAR_BINARY_OPERATOR_IMPLEMENTATION(OPERAND)\
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 T& 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 T& 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(); \
}
// //
// To prevent the C++ syntax from getting *completely* overwhelming, define some alias macros // To prevent the C++ syntax from getting *completely* overwhelming, define some alias macros
// //
@ -295,6 +253,52 @@ namespace detail
#define _GLM_SWIZZLE_TYPE1 glm::detail::swizzle_base<S0,T,P,N,E0,E1,E2,E3,D0> #define _GLM_SWIZZLE_TYPE1 glm::detail::swizzle_base<S0,T,P,N,E0,E1,E2,E3,D0>
#define _GLM_SWIZZLE_TYPE2 glm::detail::swizzle_base<S1,T,P,N,F0,F1,F2,F3,D1> #define _GLM_SWIZZLE_TYPE2 glm::detail::swizzle_base<S1,T,P,N,F0,F1,F2,F3,D1>
#define _GLM_SWIZZLE_BINARY_OPERATOR_IMPLEMENTATION(OPERAND)\
_GLM_SWIZZLE_TEMPLATE2 \
typename P operator OPERAND ( const _GLM_SWIZZLE_TYPE1& a, const _GLM_SWIZZLE_TYPE2& b) \
{ \
return static_cast<const S0&>(a).cast() OPERAND static_cast<const S1&>(b).cast(); \
} \
_GLM_SWIZZLE_TEMPLATE1 \
typename P operator OPERAND ( const _GLM_SWIZZLE_TYPE1& a, const typename P& b) \
{ \
return static_cast<const S0&>(a).cast() OPERAND b; \
} \
_GLM_SWIZZLE_TEMPLATE1 \
typename P operator OPERAND ( const typename P& a, const _GLM_SWIZZLE_TYPE1& b) \
{ \
return a OPERAND static_cast<const S0&>(b).cast(); \
}
#define _GLM_SWIZZLE_SCALAR_BINARY_OPERATOR_IMPLEMENTATION(OPERAND)\
_GLM_SWIZZLE_TEMPLATE1 \
typename P operator OPERAND ( const _GLM_SWIZZLE_TYPE1& a, const typename T& b) \
{ \
return static_cast<const S0&>(a).cast() OPERAND b; \
} \
_GLM_SWIZZLE_TEMPLATE1 \
typename P operator OPERAND ( const typename T& a, const _GLM_SWIZZLE_TYPE1& b) \
{ \
return a OPERAND static_cast<const S0&>(b).cast(); \
}
#define _GLM_SWIZZLE_FUNCTION2(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<const S0&>(a).cast(), static_cast<const S1&>(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<const S0&>(a).cast(), b);\
}\
_GLM_SWIZZLE_TEMPLATE1\
typename S0::RETURN_TYPE FUNCTION(const typename S0::vec_type& a, const typename _GLM_SWIZZLE_TYPE1& b)\
{\
return FUNCTION(a, static_cast<const S0&>(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_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(+)
@ -309,9 +313,10 @@ namespace detail
namespace glm namespace glm
{ {
/*_GLM_SWIZZLE_TEMPLATE2 typename S0::value_type dot (const _GLM_SWIZZLE_TYPE1& a, const _GLM_SWIZZLE_TYPE2& b) { return dot(a.cast(), b.cast()); }
_GLM_SWIZZLE_TEMPLATE1 typename S0::value_type dot (const _GLM_SWIZZLE_TYPE1& a, const typename S0::vec_type& b) { return dot(a.cast(), b); }
_GLM_SWIZZLE_TEMPLATE1 typename S0::value_type dot (const typename S0::vec_type& a, const _GLM_SWIZZLE_TYPE1& b) { return dot(a, b.cast()); }*/ _GLM_SWIZZLE_FUNCTION2(value_type, dot);
_GLM_SWIZZLE_FUNCTION2(vec_type, abs);
} }

View File

@ -196,6 +196,39 @@ int test_vec3_swizzle_operators()
return Error; 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 #if 0
using namespace glm; using namespace glm;
@ -345,6 +378,7 @@ int main()
Error += test_vec3_swizzle3_3(); Error += test_vec3_swizzle3_3();
Error += test_vec3_swizzle_half(); Error += test_vec3_swizzle_half();
Error += test_vec3_swizzle_operators(); Error += test_vec3_swizzle_operators();
Error += test_vec3_swizzle_functions();
return Error; return Error;
} }