nuclear@1: #ifndef VMATH_H_ nuclear@1: #define VMATH_H_ nuclear@1: nuclear@1: #include nuclear@1: #include "vmathmat.h" nuclear@1: nuclear@15: #define DEG2RAD(x) (M_PI * (x) / 180.0) nuclear@15: #define RAD2DEG(x) (180.0 * (x) / M_PI) nuclear@15: nuclear@1: class Vector3 { nuclear@1: public: nuclear@1: float x, y, z; nuclear@1: nuclear@1: Vector3() : x(0), y(0), z(0) {} nuclear@1: Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {} nuclear@1: nuclear@1: float length_sq() const { return x * x + y * y + z * z; } nuclear@1: float length() const { return sqrt(x * x + y * y + z * z); } nuclear@1: nuclear@1: void normalize() nuclear@1: { nuclear@1: float len = length(); nuclear@1: if(len != 0.0) { nuclear@1: x /= len; nuclear@1: y /= len; nuclear@1: z /= len; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); } nuclear@1: const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); } nuclear@1: }; nuclear@1: nuclear@1: inline Vector3 normalize(const Vector3 &v) nuclear@1: { nuclear@1: float len = v.length(); nuclear@1: if(len != 0.0) { nuclear@1: return Vector3(v.x / len, v.y / len, v.z / len); nuclear@1: } nuclear@1: return v; nuclear@1: } nuclear@1: nuclear@1: inline Vector3 operator +(const Vector3 &a, const Vector3 &b) nuclear@1: { nuclear@1: return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); nuclear@1: } nuclear@1: nuclear@1: inline Vector3 operator -(const Vector3 &a, const Vector3 &b) nuclear@1: { nuclear@1: return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); nuclear@1: } nuclear@1: nuclear@17: inline Vector3 operator -(const Vector3 &v) nuclear@17: { nuclear@17: return Vector3(-v.x, -v.y, -v.z); nuclear@17: } nuclear@17: nuclear@12: inline Vector3 operator *(const Vector3 &a, const Vector3 &b) nuclear@12: { nuclear@12: return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); nuclear@12: } nuclear@12: nuclear@1: inline Vector3 operator *(const Vector3 &v, float s) nuclear@1: { nuclear@1: return Vector3(v.x * s, v.y * s, v.z * s); nuclear@1: } nuclear@1: nuclear@1: inline Vector3 operator /(const Vector3 &v, float s) nuclear@1: { nuclear@1: return Vector3(v.x / s, v.y / s, v.z / s); nuclear@1: } nuclear@1: nuclear@1: inline float dot(const Vector3 &a, const Vector3 &b) nuclear@1: { nuclear@1: return a.x * b.x + a.y * b.y + a.z * b.z; nuclear@1: } nuclear@1: nuclear@1: inline Vector3 cross(const Vector3 &a, const Vector3 &b) nuclear@1: { nuclear@1: return Vector3(a.y * b.z - a.z * b.y, nuclear@12: a.z * b.x - a.x * b.z, nuclear@1: a.x * b.y - a.y * b.x); nuclear@1: } nuclear@1: nuclear@1: inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v) nuclear@1: { nuclear@1: 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: 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: 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: return Vector3(x, y, z); nuclear@1: } nuclear@1: nuclear@17: inline Vector3 lerp(const Vector3 &a, const Vector3 &b, float t) nuclear@17: { nuclear@17: return Vector3(a.x + (b.x - a.x) * t, nuclear@17: a.y + (b.y - a.y) * t, nuclear@17: a.z + (b.z - a.z) * t); nuclear@17: } nuclear@17: nuclear@19: inline Vector3 reflect(const Vector3 &v, const Vector3 &n) nuclear@19: { nuclear@19: float vdotn = dot(v, n); nuclear@19: return n * vdotn * 2.0 - v; nuclear@19: } nuclear@19: nuclear@19: inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior) nuclear@19: { nuclear@19: float cos_inc = dot(v, -n); nuclear@19: float radical = 1.0 + ior * ior * (cos_inc * cos_inc - 1.0); nuclear@19: nuclear@19: if(radical < 0.0) { // total internal reflection nuclear@19: return -reflect(v, n); nuclear@19: } nuclear@19: nuclear@19: float beta = ior * cos_inc - sqrt(radical); nuclear@19: return v * ior + n * beta; nuclear@19: } nuclear@19: nuclear@1: // ---- Vector4 ---- nuclear@1: nuclear@1: class Vector4 { nuclear@1: public: nuclear@1: float x, y, z, w; nuclear@1: nuclear@1: Vector4() : x(0), y(0), z(0), w(1.0) {} nuclear@1: Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {} nuclear@1: Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {} nuclear@1: nuclear@1: float length_sq() const { return x * x + y * y + z * z + w * w; } nuclear@1: float length() const { return sqrt(x * x + y * y + z * z + w * w); } nuclear@1: nuclear@1: void normalize() nuclear@1: { nuclear@1: float len = length(); nuclear@1: if(len != 0.0) { nuclear@1: x /= len; nuclear@1: y /= len; nuclear@1: z /= len; nuclear@1: w /= len; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: float &operator [](int idx) nuclear@1: { nuclear@1: return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x)); nuclear@1: } nuclear@1: const float &operator [](int idx) const nuclear@1: { nuclear@1: return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x)); nuclear@1: } nuclear@1: }; nuclear@1: nuclear@1: inline Vector4 operator +(const Vector4 &a, const Vector4 &b) nuclear@1: { nuclear@1: return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); nuclear@1: } nuclear@1: nuclear@1: inline Vector4 operator -(const Vector4 &a, const Vector4 &b) nuclear@1: { nuclear@1: return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); nuclear@1: } nuclear@1: nuclear@17: inline Vector4 operator -(const Vector4 &v) nuclear@17: { nuclear@17: return Vector4(-v.x, -v.y, -v.z, -v.w); nuclear@17: } nuclear@17: nuclear@1: inline Vector4 operator *(const Vector4 &v, float s) nuclear@1: { nuclear@1: return Vector4(v.x * s, v.y * s, v.z * s, v.w * s); nuclear@1: } nuclear@1: nuclear@1: inline Vector4 operator /(const Vector4 &v, float s) nuclear@1: { nuclear@1: return Vector4(v.x / s, v.y / s, v.z / s, v.w / s); nuclear@1: } nuclear@1: nuclear@1: inline float dot(const Vector4 &a, const Vector4 &b) nuclear@1: { nuclear@1: return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; nuclear@1: } nuclear@1: nuclear@1: inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v) nuclear@1: { nuclear@1: 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: 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: 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: 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: return Vector4(x, y, z, w); nuclear@1: } nuclear@1: nuclear@17: inline Vector4 lerp(const Vector4 &a, const Vector4 &b, float t) nuclear@17: { nuclear@17: return Vector4(a.x + (b.x - a.x) * t, nuclear@17: a.y + (b.y - a.y) * t, nuclear@17: a.z + (b.z - a.z) * t, nuclear@17: a.w + (b.w - a.w) * t); nuclear@17: } nuclear@17: nuclear@17: nuclear@1: #endif // VMATH_H_