rayzor

annotate src/vmath.h @ 22:5380ff64e83f

minor changes from dos, and line endings cleanup
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 02 May 2014 14:32:58 +0300
parents 79609d482762
children
rev   line source
nuclear@1 1 #ifndef VMATH_H_
nuclear@1 2 #define VMATH_H_
nuclear@1 3
nuclear@1 4 #include <math.h>
nuclear@1 5 #include "vmathmat.h"
nuclear@1 6
nuclear@15 7 #define DEG2RAD(x) (M_PI * (x) / 180.0)
nuclear@15 8 #define RAD2DEG(x) (180.0 * (x) / M_PI)
nuclear@15 9
nuclear@1 10 class Vector3 {
nuclear@1 11 public:
nuclear@1 12 float x, y, z;
nuclear@1 13
nuclear@1 14 Vector3() : x(0), y(0), z(0) {}
nuclear@1 15 Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
nuclear@1 16
nuclear@1 17 float length_sq() const { return x * x + y * y + z * z; }
nuclear@1 18 float length() const { return sqrt(x * x + y * y + z * z); }
nuclear@1 19
nuclear@1 20 void normalize()
nuclear@1 21 {
nuclear@1 22 float len = length();
nuclear@1 23 if(len != 0.0) {
nuclear@1 24 x /= len;
nuclear@1 25 y /= len;
nuclear@1 26 z /= len;
nuclear@1 27 }
nuclear@1 28 }
nuclear@1 29
nuclear@1 30 float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); }
nuclear@1 31 const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); }
nuclear@1 32 };
nuclear@1 33
nuclear@1 34 inline Vector3 normalize(const Vector3 &v)
nuclear@1 35 {
nuclear@1 36 float len = v.length();
nuclear@1 37 if(len != 0.0) {
nuclear@1 38 return Vector3(v.x / len, v.y / len, v.z / len);
nuclear@1 39 }
nuclear@1 40 return v;
nuclear@1 41 }
nuclear@1 42
nuclear@1 43 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
nuclear@1 44 {
nuclear@1 45 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
nuclear@1 46 }
nuclear@1 47
nuclear@1 48 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
nuclear@1 49 {
nuclear@1 50 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
nuclear@1 51 }
nuclear@1 52
nuclear@17 53 inline Vector3 operator -(const Vector3 &v)
nuclear@17 54 {
nuclear@17 55 return Vector3(-v.x, -v.y, -v.z);
nuclear@17 56 }
nuclear@17 57
nuclear@12 58 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
nuclear@12 59 {
nuclear@12 60 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
nuclear@12 61 }
nuclear@12 62
nuclear@1 63 inline Vector3 operator *(const Vector3 &v, float s)
nuclear@1 64 {
nuclear@1 65 return Vector3(v.x * s, v.y * s, v.z * s);
nuclear@1 66 }
nuclear@1 67
nuclear@1 68 inline Vector3 operator /(const Vector3 &v, float s)
nuclear@1 69 {
nuclear@1 70 return Vector3(v.x / s, v.y / s, v.z / s);
nuclear@1 71 }
nuclear@1 72
nuclear@1 73 inline float dot(const Vector3 &a, const Vector3 &b)
nuclear@1 74 {
nuclear@1 75 return a.x * b.x + a.y * b.y + a.z * b.z;
nuclear@1 76 }
nuclear@1 77
nuclear@1 78 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
nuclear@1 79 {
nuclear@1 80 return Vector3(a.y * b.z - a.z * b.y,
nuclear@12 81 a.z * b.x - a.x * b.z,
nuclear@1 82 a.x * b.y - a.y * b.x);
nuclear@1 83 }
nuclear@1 84
nuclear@1 85 inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v)
nuclear@1 86 {
nuclear@1 87 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
nuclear@1 88 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
nuclear@1 89 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
nuclear@1 90 return Vector3(x, y, z);
nuclear@1 91 }
nuclear@1 92
nuclear@17 93 inline Vector3 lerp(const Vector3 &a, const Vector3 &b, float t)
nuclear@17 94 {
nuclear@17 95 return Vector3(a.x + (b.x - a.x) * t,
nuclear@17 96 a.y + (b.y - a.y) * t,
nuclear@17 97 a.z + (b.z - a.z) * t);
nuclear@17 98 }
nuclear@17 99
nuclear@19 100 inline Vector3 reflect(const Vector3 &v, const Vector3 &n)
nuclear@19 101 {
nuclear@19 102 float vdotn = dot(v, n);
nuclear@19 103 return n * vdotn * 2.0 - v;
nuclear@19 104 }
nuclear@19 105
nuclear@19 106 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior)
nuclear@19 107 {
nuclear@19 108 float cos_inc = dot(v, -n);
nuclear@19 109 float radical = 1.0 + ior * ior * (cos_inc * cos_inc - 1.0);
nuclear@19 110
nuclear@19 111 if(radical < 0.0) { // total internal reflection
nuclear@19 112 return -reflect(v, n);
nuclear@19 113 }
nuclear@19 114
nuclear@19 115 float beta = ior * cos_inc - sqrt(radical);
nuclear@19 116 return v * ior + n * beta;
nuclear@19 117 }
nuclear@19 118
nuclear@1 119 // ---- Vector4 ----
nuclear@1 120
nuclear@1 121 class Vector4 {
nuclear@1 122 public:
nuclear@1 123 float x, y, z, w;
nuclear@1 124
nuclear@1 125 Vector4() : x(0), y(0), z(0), w(1.0) {}
nuclear@1 126 Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {}
nuclear@1 127 Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {}
nuclear@1 128
nuclear@1 129 float length_sq() const { return x * x + y * y + z * z + w * w; }
nuclear@1 130 float length() const { return sqrt(x * x + y * y + z * z + w * w); }
nuclear@1 131
nuclear@1 132 void normalize()
nuclear@1 133 {
nuclear@1 134 float len = length();
nuclear@1 135 if(len != 0.0) {
nuclear@1 136 x /= len;
nuclear@1 137 y /= len;
nuclear@1 138 z /= len;
nuclear@1 139 w /= len;
nuclear@1 140 }
nuclear@1 141 }
nuclear@1 142
nuclear@1 143 float &operator [](int idx)
nuclear@1 144 {
nuclear@1 145 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
nuclear@1 146 }
nuclear@1 147 const float &operator [](int idx) const
nuclear@1 148 {
nuclear@1 149 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
nuclear@1 150 }
nuclear@1 151 };
nuclear@1 152
nuclear@1 153 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
nuclear@1 154 {
nuclear@1 155 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
nuclear@1 156 }
nuclear@1 157
nuclear@1 158 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
nuclear@1 159 {
nuclear@1 160 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
nuclear@1 161 }
nuclear@1 162
nuclear@17 163 inline Vector4 operator -(const Vector4 &v)
nuclear@17 164 {
nuclear@17 165 return Vector4(-v.x, -v.y, -v.z, -v.w);
nuclear@17 166 }
nuclear@17 167
nuclear@1 168 inline Vector4 operator *(const Vector4 &v, float s)
nuclear@1 169 {
nuclear@1 170 return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
nuclear@1 171 }
nuclear@1 172
nuclear@1 173 inline Vector4 operator /(const Vector4 &v, float s)
nuclear@1 174 {
nuclear@1 175 return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
nuclear@1 176 }
nuclear@1 177
nuclear@1 178 inline float dot(const Vector4 &a, const Vector4 &b)
nuclear@1 179 {
nuclear@1 180 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
nuclear@1 181 }
nuclear@1 182
nuclear@1 183 inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v)
nuclear@1 184 {
nuclear@1 185 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3] * v.w;
nuclear@1 186 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3] * v.w;
nuclear@1 187 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3] * v.w;
nuclear@1 188 float w = m.m[3][0] * v.x + m.m[3][1] * v.y + m.m[3][2] * v.z + m.m[3][3] * v.w;
nuclear@1 189 return Vector4(x, y, z, w);
nuclear@1 190 }
nuclear@1 191
nuclear@17 192 inline Vector4 lerp(const Vector4 &a, const Vector4 &b, float t)
nuclear@17 193 {
nuclear@17 194 return Vector4(a.x + (b.x - a.x) * t,
nuclear@17 195 a.y + (b.y - a.y) * t,
nuclear@17 196 a.z + (b.z - a.z) * t,
nuclear@17 197 a.w + (b.w - a.w) * t);
nuclear@17 198 }
nuclear@17 199
nuclear@17 200
nuclear@1 201 #endif // VMATH_H_