From 5d3c6fb4caa76c592693523123e1c688c4cb1198 Mon Sep 17 00:00:00 2001 From: athile Date: Wed, 21 Sep 2011 12:19:27 -0400 Subject: [PATCH] Start implementation for swizzle operaators. operator+ and operator- have been added. --- glm/core/_swizzle.hpp | 103 ++++++++++++++++++++++++----------- test/core/core_type_vec3.cpp | 53 +++++++++--------- 2 files changed, 98 insertions(+), 58 deletions(-) diff --git a/glm/core/_swizzle.hpp b/glm/core/_swizzle.hpp index aaf18caa..fd27d8c1 100644 --- a/glm/core/_swizzle.hpp +++ b/glm/core/_swizzle.hpp @@ -61,9 +61,11 @@ namespace detail N = number of components in the vector (e.g. 3) E0...3 = what index the n-th element of this swizzle refers to */ - template + template struct swizzle_base { + typedef Derived derived_type; + swizzle_base& operator= (const Class& that) { static const int offset_dst[4] = { E0, E1, E2, E3 }; @@ -88,101 +90,136 @@ namespace detail } protected: - Type& elem (size_t i) { return (reinterpret_cast(_buffer))[i]; } + Type& elem (size_t i) { return (reinterpret_cast(_buffer))[i]; } + const Type& elem (size_t i) const { return (reinterpret_cast(_buffer))[i]; } // 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 char _buffer[sizeof(Type) * N]; }; - template - struct swizzle_base + template + struct swizzle_base { + typedef Derived derived_type; + struct Stub {}; swizzle_base& operator= (const Stub& that) {} protected: - Type& elem (size_t i) { return (reinterpret_cast(_buffer))[i]; } + Type& elem (size_t i) { return (reinterpret_cast(_buffer))[i]; } + const Type& elem (size_t i) const { return (reinterpret_cast(_buffer))[i]; } + char _buffer[sizeof(Type) * N]; }; //! Internal class for implementing swizzle operators template - struct swizzle2 : public swizzle_base + struct swizzle2 : public swizzle_base, T,P,2,E0,E1,0,0,(E0 == E1)> { - using swizzle_base::operator=; - operator P () { return P(this->elem(E0), this->elem(E1)); } + using swizzle_base,T,P,2,E0,E1,0,0,(E0 == E1)>::operator=; + P cast() const { return P(this->elem(E0), this->elem(E1)); } + operator P () const { return cast(); } }; //! Internal class for implementing swizzle operators template - struct swizzle2_3 : public swizzle_base + struct swizzle2_3 : public swizzle_base,T,P,2,E0,E1,E2,0,1> { - using swizzle_base::operator=; - operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2)); } + 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)); } + operator P () const { return cast(); } }; //! Internal class for implementing swizzle operators template - struct swizzle2_4 : public swizzle_base + struct swizzle2_4 : public swizzle_base,T,P,2,E0,E1,E2,E3,1> { - using swizzle_base::operator=; - operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); } + 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)); } + operator P () const { return cast(); } }; //! Internal class for implementing swizzle operators template - struct swizzle3 : public swizzle_base + struct swizzle3 : public swizzle_base,T,P,3,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)> { - using swizzle_base::operator=; - operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2)); } + 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)); } + operator P () const { return cast(); } }; //! Internal class for implementing swizzle operators template - struct swizzle3_2 : public swizzle_base + struct swizzle3_2 : public swizzle_base,T,P,2,E0,E1,0,0,(E0==E1)> { - using swizzle_base::operator=; - operator P () { return P(this->elem(E0), this->elem(E1)); } + using swizzle_base,T,P,2,E0,E1,0,0,(E0==E1)>::operator=; + P cast() const { return P(this->elem(E0), this->elem(E1)); } + operator P () const { return cast(); } }; //! Internal class for implementing swizzle operators template - struct swizzle3_4 : public swizzle_base + struct swizzle3_4 : public swizzle_base,T,P,3,E0,E1,E2,E3,1> { - using swizzle_base::operator=; - operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); } + 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)); } + operator P () const { return cast(); } }; //! Internal class for implementing swizzle operators template - struct swizzle4 : public swizzle_base + struct swizzle4 : public swizzle_base,T,P,4,E0,E1,E2,E3,(E0==E1||E0==E2||E0==E3||E1==E2||E1==E3||E2==E3)> { - using swizzle_base::operator=; - operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); } + 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)); } + operator P () const { return cast(); } }; //! Internal class for implementing swizzle operators template - struct swizzle4_2 : public swizzle_base + struct swizzle4_2 : public swizzle_base,T,P,2,E0,E1,0,0,(E0==E1)> { - using swizzle_base::operator=; - operator P () { return P(this->elem(E0), this->elem(E1)); } + using swizzle_base,T,P,2,E0,E1,0,0,(E0==E1)>::operator=; + P cast() const { return P(this->elem(E0), this->elem(E1)); } + operator P () const { return cast(); } }; //! Internal class for implementing swizzle operators template - struct swizzle4_3 : public swizzle_base + struct swizzle4_3 : public swizzle_base,T,P,4,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)> { - using swizzle_base::operator=; - operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2)); } + 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)); } + operator P () { return cast(); } }; + + + template + typename P operator+ ( + const glm::detail::swizzle_base& a, + const glm::detail::swizzle_base& b) + { + return static_cast(a).cast() + static_cast(b).cast(); + } + + template + typename P operator- ( + const glm::detail::swizzle_base& a, + const glm::detail::swizzle_base& b) + { + return static_cast(a).cast() - static_cast(b).cast(); + } + }//namespace detail }//namespace glm + + + #define _GLM_SWIZZLE2_2_MEMBERS(T,P,E0,E1) \ struct { glm::detail::swizzle2 E0 ## E0; }; \ struct { glm::detail::swizzle2 E0 ## E1; }; \ diff --git a/test/core/core_type_vec3.cpp b/test/core/core_type_vec3.cpp index 97467329..fe94a735 100644 --- a/test/core/core_type_vec3.cpp +++ b/test/core/core_type_vec3.cpp @@ -103,30 +103,20 @@ int test_vec3_swizzle3_3() 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; 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.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.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; - + 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; } @@ -140,12 +130,8 @@ int test_vec3_swizzle_half() glm::hvec3 v(a1, b1, c1); glm::hvec3 u; - float c = v.x; - float d = v.y; 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; u = v.xyz; @@ -172,6 +158,22 @@ int test_vec3_swizzle_half() 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 Error = 0; @@ -181,6 +183,7 @@ int main() Error += test_vec3_swizzle3_2(); Error += test_vec3_swizzle3_3(); Error += test_vec3_swizzle_half(); + Error += test_vec3_swizzle_operators(); return Error; }