graphene

view src/gmath/vector.h @ 5:2ce58d5309f0

swizzling
author John Tsiombikas Sat, 25 Jul 2015 17:19:27 +0300 d30e24132b6e 9fbbc96e6fbe
line source
1 #ifndef GMATH_VEC_H_
2 #define GMATH_VEC_H_
4 #include <math.h>
5 #include "swizzle.h"
7 namespace gph {
9 #define GPH_SWIZZLE2(T, a, b) inline Vector2 a##b() const;
10 #define GPH_SWIZZLE3(T, a, b, c) inline Vector3 a##b##c() const;
11 #define GPH_SWIZZLE4(T, a, b, c, d) inline Vector4 a##b##c##d() const;
13 class Vector3;
14 class Vector4;
15 class Matrix4x4;
17 class Vector2 {
18 public:
19 float x, y;
21 Vector2() : x(0), y(0) {}
22 Vector2(float x_, float y_) : x(x_), y(y_) {}
23 Vector2(const Vector3 &v);
25 inline void normalize();
26 inline float &operator[] (int idx);
27 inline const float &operator[] (int idx) const;
29 GPH_VEC2_SWIZZLE
30 };
32 class Vector3 {
33 public:
34 float x, y, z;
36 Vector3() : x(0), y(0), z(0) {}
37 Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
38 Vector3(const Vector4 &v);
40 inline void normalize();
41 inline float &operator[] (int idx);
42 inline const float &operator[] (int idx) const;
44 GPH_VEC3_SWIZZLE
45 };
48 class Vector4 {
49 public:
50 float x, y, z, w;
52 Vector4() : x(0), y(0), z(0), w(0) {}
53 Vector4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {}
54 Vector4(const Vector3 &v);
56 inline void normalize();
57 inline float &operator[] (int idx);
58 inline const float &operator[] (int idx) const;
60 GPH_VEC4_SWIZZLE
61 };
63 // ---- Vector3 functions ----
64 inline Vector3 operator +(const Vector3 &a, const Vector3 &b);
65 inline Vector3 operator -(const Vector3 &a, const Vector3 &b);
66 inline Vector3 operator *(const Vector3 &a, const Vector3 &b);
67 inline Vector3 operator /(const Vector3 &a, const Vector3 &b);
68 inline Vector3 operator *(const Vector3 &v, float s);
69 inline Vector3 operator *(float s, const Vector3 &v);
70 inline Vector3 operator /(const Vector3 &v, float s);
71 inline Vector3 operator /(float s, const Vector3 &v);
72 inline Vector3 &operator +=(Vector3 &a, const Vector3 &b);
73 inline Vector3 &operator -=(Vector3 &a, const Vector3 &b);
74 inline Vector3 &operator *=(Vector3 &a, const Vector3 &b);
75 inline Vector3 &operator /=(Vector3 &a, const Vector3 &b);
76 inline Vector3 &operator *=(Vector3 &v, float s);
77 inline Vector3 &operator /=(Vector3 &v, float s);
79 Vector3 operator *(const Vector3 &v, const Matrix4x4 &m);
80 Vector3 operator *(const Matrix4x4 &m, const Vector3 &v);
82 inline bool operator ==(const Vector3 &a, const Vector3 &b);
83 inline bool operator !=(const Vector3 &a, const Vector3 &b);
85 inline float dot(const Vector3 &a, const Vector3 &b);
86 inline Vector3 cross(const Vector3 &a, const Vector3 &b);
87 inline float length(const Vector3 &v);
88 inline float length_sq(const Vector3 &v);
89 inline Vector3 normalize(const Vector3 &v);
91 inline Vector3 reflect(const Vector3 &v, const Vector3 &n);
92 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior);
93 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior);
95 inline Vector3 distance(const Vector3 &a, const Vector3 &b);
96 inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b);
97 inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng);
99 }
101 #endif /* GMATH_VEC_H_ */