mirror of
https://github.com/g-truc/glm.git
synced 2024-11-10 12:41:54 +00:00
Start implementation for swizzle operaators. operator+ and operator- have been added.
This commit is contained in:
parent
d942261f23
commit
5d3c6fb4ca
@ -61,9 +61,11 @@ namespace detail
|
|||||||
N = number of components in the vector (e.g. 3)
|
N = number of components in the vector (e.g. 3)
|
||||||
E0...3 = what index the n-th element of this swizzle refers to
|
E0...3 = what index the n-th element of this swizzle refers to
|
||||||
*/
|
*/
|
||||||
template <typename Type, typename Class, int N, int E0, int E1, int E2, int E3, int DUPLICATE_ELEMENTS>
|
template <typename Derived, typename Type, typename Class, int N, int E0, int E1, int E2, int E3, int DUPLICATE_ELEMENTS>
|
||||||
struct swizzle_base
|
struct swizzle_base
|
||||||
{
|
{
|
||||||
|
typedef Derived derived_type;
|
||||||
|
|
||||||
swizzle_base& operator= (const Class& that)
|
swizzle_base& operator= (const Class& that)
|
||||||
{
|
{
|
||||||
static const int offset_dst[4] = { E0, E1, E2, E3 };
|
static const int offset_dst[4] = { E0, E1, E2, E3 };
|
||||||
@ -88,101 +90,136 @@ namespace detail
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Type& elem (size_t i) { return (reinterpret_cast<Type*>(_buffer))[i]; }
|
Type& elem (size_t i) { return (reinterpret_cast<Type*>(_buffer))[i]; }
|
||||||
|
const Type& elem (size_t i) const { return (reinterpret_cast<const Type*>(_buffer))[i]; }
|
||||||
|
|
||||||
// Use an opaque buffer to *ensure* the compiler doesn't call a constructor.
|
// Use an opaque buffer to *ensure* the compiler doesn't call a constructor.
|
||||||
// Otherwise, a vec4 containg all swizzles might end up with 1000s of
|
// Otherwise, a vec4 containing all swizzles might end up with 1000s of
|
||||||
// constructor calls
|
// constructor calls
|
||||||
char _buffer[sizeof(Type) * N];
|
char _buffer[sizeof(Type) * N];
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Type, typename Class, int N, int E0, int E1, int E2, int E3>
|
template <typename Derived, typename Type, typename Class, int N, int E0, int E1, int E2, int E3>
|
||||||
struct swizzle_base<Type,Class,N,E0,E1,E2,E3,1>
|
struct swizzle_base<Derived, Type,Class,N,E0,E1,E2,E3,1>
|
||||||
{
|
{
|
||||||
|
typedef Derived derived_type;
|
||||||
|
|
||||||
struct Stub {};
|
struct Stub {};
|
||||||
swizzle_base& operator= (const Stub& that) {}
|
swizzle_base& operator= (const Stub& that) {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Type& elem (size_t i) { return (reinterpret_cast<Type*>(_buffer))[i]; }
|
Type& elem (size_t i) { return (reinterpret_cast<Type*>(_buffer))[i]; }
|
||||||
|
const Type& elem (size_t i) const { return (reinterpret_cast<const Type*>(_buffer))[i]; }
|
||||||
|
|
||||||
char _buffer[sizeof(Type) * N];
|
char _buffer[sizeof(Type) * N];
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Internal class for implementing swizzle operators
|
//! Internal class for implementing swizzle operators
|
||||||
template <typename T, typename P, int E0, int E1>
|
template <typename T, typename P, int E0, int E1>
|
||||||
struct swizzle2 : public swizzle_base<T,P,2,E0,E1,0,0,(E0 == E1)>
|
struct swizzle2 : public swizzle_base<swizzle2<T,P,E0,E1>, T,P,2,E0,E1,0,0,(E0 == E1)>
|
||||||
{
|
{
|
||||||
using swizzle_base<T,P,2,E0,E1,0,0,(E0 == E1)>::operator=;
|
using swizzle_base<swizzle2<T,P,E0,E1>,T,P,2,E0,E1,0,0,(E0 == E1)>::operator=;
|
||||||
operator P () { return P(this->elem(E0), this->elem(E1)); }
|
P cast() const { return P(this->elem(E0), this->elem(E1)); }
|
||||||
|
operator P () const { return cast(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Internal class for implementing swizzle operators
|
//! Internal class for implementing swizzle operators
|
||||||
template <typename T, typename P, int E0, int E1, int E2>
|
template <typename T, typename P, int E0, int E1, int E2>
|
||||||
struct swizzle2_3 : public swizzle_base<T,P,2,E0,E1,E2,0,1>
|
struct swizzle2_3 : public swizzle_base<swizzle2_3<T,P,E0,E1,E2>,T,P,2,E0,E1,E2,0,1>
|
||||||
{
|
{
|
||||||
using swizzle_base<T,P,2,E0,E1,E2,0,1>::operator=;
|
using swizzle_base<swizzle2_3<T,P,E0,E1,E2>,T,P,2,E0,E1,E2,0,1>::operator=;
|
||||||
operator P () { 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 () const { return cast(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Internal class for implementing swizzle operators
|
//! Internal class for implementing swizzle operators
|
||||||
template <typename T, typename P, int E0, int E1, int E2, int E3>
|
template <typename T, typename P, int E0, int E1, int E2, int E3>
|
||||||
struct swizzle2_4 : public swizzle_base<T,P,2,E0,E1,E2,E3,1>
|
struct swizzle2_4 : public swizzle_base<swizzle2_4<T,P,E0,E1,E2,E3>,T,P,2,E0,E1,E2,E3,1>
|
||||||
{
|
{
|
||||||
using swizzle_base<T,P,2,E0,E1,E2,E3,1>::operator=;
|
using swizzle_base<swizzle2_4<T,P,E0,E1,E2,E3>,T,P,2,E0,E1,E2,E3,1>::operator=;
|
||||||
operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); }
|
P cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); }
|
||||||
|
operator P () const { return cast(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Internal class for implementing swizzle operators
|
//! Internal class for implementing swizzle operators
|
||||||
template <typename T, typename P, int E0, int E1, int E2>
|
template <typename T, typename P, int E0, int E1, int E2>
|
||||||
struct swizzle3 : public swizzle_base<T,P,3,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>
|
struct swizzle3 : public swizzle_base<swizzle3<T,P,E0,E1,E2>,T,P,3,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>
|
||||||
{
|
{
|
||||||
using swizzle_base<T,P,3,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>::operator=;
|
using swizzle_base<swizzle3<T,P,E0,E1,E2>,T,P,3,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>::operator=;
|
||||||
operator P () { 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 () const { return cast(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Internal class for implementing swizzle operators
|
//! Internal class for implementing swizzle operators
|
||||||
template <typename T, typename P, int E0, int E1>
|
template <typename T, typename P, int E0, int E1>
|
||||||
struct swizzle3_2 : public swizzle_base<T,P,2,E0,E1,0,0,(E0==E1)>
|
struct swizzle3_2 : public swizzle_base<swizzle3_2<T,P,E0,E1>,T,P,2,E0,E1,0,0,(E0==E1)>
|
||||||
{
|
{
|
||||||
using swizzle_base<T,P,2,E0,E1,0,0,(E0==E1)>::operator=;
|
using swizzle_base<swizzle3_2<T,P,E0,E1>,T,P,2,E0,E1,0,0,(E0==E1)>::operator=;
|
||||||
operator P () { return P(this->elem(E0), this->elem(E1)); }
|
P cast() const { return P(this->elem(E0), this->elem(E1)); }
|
||||||
|
operator P () const { return cast(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Internal class for implementing swizzle operators
|
//! Internal class for implementing swizzle operators
|
||||||
template <typename T, typename P, int E0, int E1, int E2, int E3>
|
template <typename T, typename P, int E0, int E1, int E2, int E3>
|
||||||
struct swizzle3_4 : public swizzle_base<T,P,3,E0,E1,E2,E3,1>
|
struct swizzle3_4 : public swizzle_base<swizzle3_4<T,P,E0,E1,E2,E3>,T,P,3,E0,E1,E2,E3,1>
|
||||||
{
|
{
|
||||||
using swizzle_base<T,P,3,E0,E1,E2,E3,1>::operator=;
|
using swizzle_base<swizzle3_4<T,P,E0,E1,E2,E3>,T,P,3,E0,E1,E2,E3,1>::operator=;
|
||||||
operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); }
|
P cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); }
|
||||||
|
operator P () const { return cast(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Internal class for implementing swizzle operators
|
//! Internal class for implementing swizzle operators
|
||||||
template <typename T, typename P, int E0, int E1, int E2, int E3>
|
template <typename T, typename P, int E0, int E1, int E2, int E3>
|
||||||
struct swizzle4 : public swizzle_base<T,P,4,E0,E1,E2,E3,(E0==E1||E0==E2||E0==E3||E1==E2||E1==E3||E2==E3)>
|
struct swizzle4 : public swizzle_base<swizzle4<T,P,E0,E1,E2,E3>,T,P,4,E0,E1,E2,E3,(E0==E1||E0==E2||E0==E3||E1==E2||E1==E3||E2==E3)>
|
||||||
{
|
{
|
||||||
using swizzle_base<T,P,4,E0,E1,E2,E3,(E0==E1||E0==E2||E0==E3||E1==E2||E1==E3||E2==E3)>::operator=;
|
using swizzle_base<swizzle4<T,P,E0,E1,E2,E3>,T,P,4,E0,E1,E2,E3,(E0==E1||E0==E2||E0==E3||E1==E2||E1==E3||E2==E3)>::operator=;
|
||||||
operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); }
|
P cast() const { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); }
|
||||||
|
operator P () const { return cast(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Internal class for implementing swizzle operators
|
//! Internal class for implementing swizzle operators
|
||||||
template <typename T, typename P, int E0, int E1>
|
template <typename T, typename P, int E0, int E1>
|
||||||
struct swizzle4_2 : public swizzle_base<T,P,2,E0,E1,0,0,(E0==E1)>
|
struct swizzle4_2 : public swizzle_base<swizzle4_2<T,P,E0,E1>,T,P,2,E0,E1,0,0,(E0==E1)>
|
||||||
{
|
{
|
||||||
using swizzle_base<T,P,2,E0,E1,0,0,(E0==E1)>::operator=;
|
using swizzle_base<swizzle4_2<T,P,E0,E1>,T,P,2,E0,E1,0,0,(E0==E1)>::operator=;
|
||||||
operator P () { return P(this->elem(E0), this->elem(E1)); }
|
P cast() const { return P(this->elem(E0), this->elem(E1)); }
|
||||||
|
operator P () const { return cast(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//! Internal class for implementing swizzle operators
|
//! Internal class for implementing swizzle operators
|
||||||
template <typename T, typename P, int E0, int E1, int E2>
|
template <typename T, typename P, int E0, int E1, int E2>
|
||||||
struct swizzle4_3 : public swizzle_base<T,P,4,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>
|
struct swizzle4_3 : public swizzle_base<swizzle4_3<T,P,E0,E1,E2>,T,P,4,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>
|
||||||
{
|
{
|
||||||
using swizzle_base<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=;
|
||||||
operator P () { 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(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}//namespace detail
|
}//namespace detail
|
||||||
}//namespace glm
|
}//namespace glm
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define _GLM_SWIZZLE2_2_MEMBERS(T,P,E0,E1) \
|
#define _GLM_SWIZZLE2_2_MEMBERS(T,P,E0,E1) \
|
||||||
struct { glm::detail::swizzle2<T,P,0,0> E0 ## E0; }; \
|
struct { glm::detail::swizzle2<T,P,0,0> E0 ## E0; }; \
|
||||||
struct { glm::detail::swizzle2<T,P,0,1> E0 ## E1; }; \
|
struct { glm::detail::swizzle2<T,P,0,1> E0 ## E1; }; \
|
||||||
|
@ -103,29 +103,19 @@ int test_vec3_swizzle3_3()
|
|||||||
glm::vec3 v(1, 2, 3);
|
glm::vec3 v(1, 2, 3);
|
||||||
glm::vec3 u;
|
glm::vec3 u;
|
||||||
|
|
||||||
u = v;
|
u = v; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1;
|
||||||
Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1;
|
|
||||||
|
|
||||||
u = v.xyz;
|
u = v.xyz; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1;
|
||||||
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 = v.zyx;
|
u.zyx = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;
|
||||||
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;
|
u = v.rgb; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1;
|
||||||
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 = v.bgr;
|
u.bgr = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;
|
||||||
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;
|
u = v.stp; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1;
|
||||||
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 = v.pts;
|
u.pts = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;
|
||||||
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;
|
return Error;
|
||||||
}
|
}
|
||||||
@ -140,12 +130,8 @@ int test_vec3_swizzle_half()
|
|||||||
glm::hvec3 v(a1, b1, c1);
|
glm::hvec3 v(a1, b1, c1);
|
||||||
glm::hvec3 u;
|
glm::hvec3 u;
|
||||||
|
|
||||||
float c = v.x;
|
|
||||||
float d = v.y;
|
|
||||||
u = v;
|
u = v;
|
||||||
|
|
||||||
float a = u.x;
|
|
||||||
float b = u.y;
|
|
||||||
Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1;
|
Error += (u.x.toFloat() == 1.0f && u.y.toFloat() == 2.0f && u.z.toFloat() == 3.0f) ? 0 : 1;
|
||||||
|
|
||||||
u = v.xyz;
|
u = v.xyz;
|
||||||
@ -172,6 +158,22 @@ int test_vec3_swizzle_half()
|
|||||||
return Error;
|
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);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
return Error;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int Error = 0;
|
int Error = 0;
|
||||||
@ -181,6 +183,7 @@ int main()
|
|||||||
Error += test_vec3_swizzle3_2();
|
Error += test_vec3_swizzle3_2();
|
||||||
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();
|
||||||
|
|
||||||
return Error;
|
return Error;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user