graphene
diff src/gmath/vector.inl @ 5:2ce58d5309f0
swizzling
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 25 Jul 2015 17:19:27 +0300 |
parents | d30e24132b6e |
children | 9fbbc96e6fbe |
line diff
1.1 --- a/src/gmath/vector.inl Sat Jul 25 07:42:30 2015 +0300 1.2 +++ b/src/gmath/vector.inl Sat Jul 25 17:19:27 2015 +0300 1.3 @@ -2,6 +2,13 @@ 1.4 1.5 namespace gph { 1.6 1.7 +#undef GPH_SWIZZLE2 1.8 +#undef GPH_SWIZZLE3 1.9 +#undef GPH_SWIZZLE4 1.10 +#define GPH_SWIZZLE2(T, a, b) inline Vector2 T::a##b() const { return Vector2(a, b); } 1.11 +#define GPH_SWIZZLE3(T, a, b, c) inline Vector3 T::a##b##c() const { return Vector3(a, b, c); } 1.12 +#define GPH_SWIZZLE4(T, a, b, c, d) inline Vector4 T::a##b##c##d() const { return Vector4(a, b, c, d); } 1.13 + 1.14 // ---- Vector3 ---- 1.15 1.16 inline void Vector3::normalize() 1.17 @@ -190,6 +197,8 @@ 1.18 return dot(ng, i) < 0.0f ? n : -n; 1.19 } 1.20 1.21 +GPH_VEC3_SWIZZLE 1.22 + 1.23 // ---- Vector4 ---- 1.24 1.25 1.26 @@ -214,4 +223,29 @@ 1.27 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w)); 1.28 } 1.29 1.30 +GPH_VEC4_SWIZZLE 1.31 + 1.32 +// ---- Vector2 ---- 1.33 + 1.34 +inline void Vector2::normalize() 1.35 +{ 1.36 + float len = (float)sqrt(x * x + y * y); 1.37 + if(len != 0.0f) { 1.38 + x /= len; 1.39 + y /= len; 1.40 + } 1.41 +} 1.42 + 1.43 +inline float &Vector2::operator[] (int idx) 1.44 +{ 1.45 + return idx == 0 ? x : y; 1.46 +} 1.47 + 1.48 +inline const float &Vector2::operator[] (int idx) const 1.49 +{ 1.50 + return idx == 0 ? x : y; 1.51 +} 1.52 + 1.53 +GPH_VEC2_SWIZZLE 1.54 + 1.55 } // namespace gph