rayzor
diff src/vmath.h @ 0:2a5340a6eee4
rayzor first commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 05 Apr 2014 08:46:27 +0300 |
parents | |
children | a826bf0fb169 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/vmath.h Sat Apr 05 08:46:27 2014 +0300 1.3 @@ -0,0 +1,148 @@ 1.4 +#ifndef VMATH_H_ 1.5 +#define VMATH_H_ 1.6 + 1.7 +#include <math.h> 1.8 +#include "vmathmat.h" 1.9 + 1.10 +class Vector3 { 1.11 +public: 1.12 + float x, y, z; 1.13 + 1.14 + Vector3() : x(0), y(0), z(0) {} 1.15 + Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {} 1.16 + 1.17 + float length_sq() const { return x * x + y * y + z * z; } 1.18 + float length() const { return sqrt(x * x + y * y + z * z); } 1.19 + 1.20 + void normalize() 1.21 + { 1.22 + float len = length(); 1.23 + if(len != 0.0) { 1.24 + x /= len; 1.25 + y /= len; 1.26 + z /= len; 1.27 + } 1.28 + } 1.29 + 1.30 + float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); } 1.31 + const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); } 1.32 +}; 1.33 + 1.34 +inline Vector3 normalize(const Vector3 &v) 1.35 +{ 1.36 + float len = v.length(); 1.37 + if(len != 0.0) { 1.38 + return Vector3(v.x / len, v.y / len, v.z / len); 1.39 + } 1.40 + return v; 1.41 +} 1.42 + 1.43 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b) 1.44 +{ 1.45 + return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); 1.46 +} 1.47 + 1.48 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b) 1.49 +{ 1.50 + return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); 1.51 +} 1.52 + 1.53 +inline Vector3 operator *(const Vector3 &v, float s) 1.54 +{ 1.55 + return Vector3(v.x * s, v.y * s, v.z * s); 1.56 +} 1.57 + 1.58 +inline Vector3 operator /(const Vector3 &v, float s) 1.59 +{ 1.60 + return Vector3(v.x / s, v.y / s, v.z / s); 1.61 +} 1.62 + 1.63 +inline float dot(const Vector3 &a, const Vector3 &b) 1.64 +{ 1.65 + return a.x * b.x + a.y * b.y + a.z * b.z; 1.66 +} 1.67 + 1.68 +inline Vector3 cross(const Vector3 &a, const Vector3 &b) 1.69 +{ 1.70 + return Vector3(a.y * b.z - a.z * b.y, 1.71 + a.z * b.z - a.x * b.z, 1.72 + a.x * b.y - a.y * b.x); 1.73 +} 1.74 + 1.75 +inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v) 1.76 +{ 1.77 + float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3]; 1.78 + float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3]; 1.79 + float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3]; 1.80 + return Vector3(x, y, z); 1.81 +} 1.82 + 1.83 +// ---- Vector4 ---- 1.84 + 1.85 +class Vector4 { 1.86 +public: 1.87 + float x, y, z, w; 1.88 + 1.89 + Vector4() : x(0), y(0), z(0), w(1.0) {} 1.90 + Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {} 1.91 + Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {} 1.92 + 1.93 + float length_sq() const { return x * x + y * y + z * z + w * w; } 1.94 + float length() const { return sqrt(x * x + y * y + z * z + w * w); } 1.95 + 1.96 + void normalize() 1.97 + { 1.98 + float len = length(); 1.99 + if(len != 0.0) { 1.100 + x /= len; 1.101 + y /= len; 1.102 + z /= len; 1.103 + w /= len; 1.104 + } 1.105 + } 1.106 + 1.107 + float &operator [](int idx) 1.108 + { 1.109 + return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x)); 1.110 + } 1.111 + const float &operator [](int idx) const 1.112 + { 1.113 + return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x)); 1.114 + } 1.115 +}; 1.116 + 1.117 +inline Vector4 operator +(const Vector4 &a, const Vector4 &b) 1.118 +{ 1.119 + return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); 1.120 +} 1.121 + 1.122 +inline Vector4 operator -(const Vector4 &a, const Vector4 &b) 1.123 +{ 1.124 + return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); 1.125 +} 1.126 + 1.127 +inline Vector4 operator *(const Vector4 &v, float s) 1.128 +{ 1.129 + return Vector4(v.x * s, v.y * s, v.z * s, v.w * s); 1.130 +} 1.131 + 1.132 +inline Vector4 operator /(const Vector4 &v, float s) 1.133 +{ 1.134 + return Vector4(v.x / s, v.y / s, v.z / s, v.w / s); 1.135 +} 1.136 + 1.137 +inline float dot(const Vector4 &a, const Vector4 &b) 1.138 +{ 1.139 + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; 1.140 +} 1.141 + 1.142 +inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v) 1.143 +{ 1.144 + 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; 1.145 + 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; 1.146 + 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; 1.147 + 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; 1.148 + return Vector4(x, y, z, w); 1.149 +} 1.150 + 1.151 +#endif // VMATH_H_