glide_test1
diff vmath.h @ 0:f3ddb2bb7024
first 3dfx glide test, initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 09 Mar 2014 06:27:58 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/vmath.h Sun Mar 09 06:27:58 2014 +0200 1.3 @@ -0,0 +1,261 @@ 1.4 +#ifndef VMATH_H_ 1.5 +#define VMATH_H_ 1.6 + 1.7 +#include <math.h> 1.8 +#include "matrix.h" 1.9 + 1.10 +class Vector4; 1.11 + 1.12 +class Vector3 { 1.13 +public: 1.14 + float x, y, z; 1.15 + 1.16 + Vector3() : x(0), y(0), z(0) {} 1.17 + Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {} 1.18 + explicit inline Vector3(const Vector4 &v); 1.19 + 1.20 + inline float &operator [](int idx) 1.21 + { 1.22 + switch(idx) { 1.23 + case 0: 1.24 + return x; 1.25 + case 1: 1.26 + return y; 1.27 + case 2: 1.28 + default: 1.29 + return z; 1.30 + } 1.31 + } 1.32 + 1.33 + inline const float &operator [](int idx) const 1.34 + { 1.35 + switch(idx) { 1.36 + case 0: 1.37 + return x; 1.38 + case 1: 1.39 + return y; 1.40 + case 2: 1.41 + default: 1.42 + return z; 1.43 + } 1.44 + } 1.45 + 1.46 + inline float length() const 1.47 + { 1.48 + return sqrt(x * x + y * y + z * z); 1.49 + } 1.50 + 1.51 + inline void normalize() 1.52 + { 1.53 + float len = length(); 1.54 + if(len != 0) { 1.55 + x /= len; 1.56 + y /= len; 1.57 + z /= len; 1.58 + } 1.59 + } 1.60 +}; 1.61 + 1.62 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b) 1.63 +{ 1.64 + return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); 1.65 +} 1.66 + 1.67 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b) 1.68 +{ 1.69 + return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); 1.70 +} 1.71 + 1.72 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b) 1.73 +{ 1.74 + return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); 1.75 +} 1.76 + 1.77 +inline Vector3 operator *(const Vector3 &v, float s) 1.78 +{ 1.79 + return Vector3(v.x * s, v.y * s, v.z * s); 1.80 +} 1.81 + 1.82 +inline Vector3 operator *(float s, const Vector3 &v) 1.83 +{ 1.84 + return Vector3(v.x * s, v.y * s, v.z * s); 1.85 +} 1.86 + 1.87 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b) 1.88 +{ 1.89 + return Vector3(a.x / b.x, a.y / b.y, a.z / b.z); 1.90 +} 1.91 + 1.92 +inline Vector3 operator /(const Vector3 &v, float s) 1.93 +{ 1.94 + return Vector3(v.x / s, v.y / s, v.z / s); 1.95 +} 1.96 + 1.97 +inline float dot(const Vector3 &a, const Vector3 &b) 1.98 +{ 1.99 + return a.x * b.x + a.y * b.y + a.z * b.z; 1.100 +} 1.101 + 1.102 +inline Vector3 cross(const Vector3 &a, const Vector3 &b) 1.103 +{ 1.104 + return Vector3(a.y * b.z - a.z * b.y, 1.105 + a.z * b.x - a.x * b.z, 1.106 + a.x * b.y - a.y * b.z); 1.107 +} 1.108 + 1.109 + 1.110 +inline float length(const Vector3 &v) 1.111 +{ 1.112 + return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 1.113 +} 1.114 + 1.115 +inline float length_sq(const Vector3 &v) 1.116 +{ 1.117 + return v.x * v.x + v.y * v.y + v.z * v.z; 1.118 +} 1.119 + 1.120 +inline Vector3 normalize(const Vector3 &v) 1.121 +{ 1.122 + float len = length(v); 1.123 + float s = len == 0 ? 1.0 : 1.0 / len; 1.124 + return v * s; 1.125 +} 1.126 + 1.127 +inline Vector3 transform(const Vector3 &v, const Matrix4x4 &m) 1.128 +{ 1.129 + return Vector3( 1.130 + m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3], 1.131 + m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3], 1.132 + m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]); 1.133 +} 1.134 + 1.135 + 1.136 +// ---- Vector4 ---- 1.137 +class Vector4 { 1.138 +public: 1.139 + float x, y, z, w; 1.140 + 1.141 + Vector4() : x(0), y(0), z(0), w(1) {} 1.142 + Vector4(float xx, float yy, float zz, float ww = 1.0f) : x(xx), y(yy), z(zz), w(ww) {} 1.143 + explicit Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) {} 1.144 + 1.145 + inline float &operator [](int idx) 1.146 + { 1.147 + switch(idx) { 1.148 + case 0: 1.149 + return x; 1.150 + case 1: 1.151 + return y; 1.152 + case 2: 1.153 + return z; 1.154 + default: 1.155 + case 3: 1.156 + return w; 1.157 + } 1.158 + } 1.159 + 1.160 + inline const float &operator [](int idx) const 1.161 + { 1.162 + switch(idx) { 1.163 + case 0: 1.164 + return x; 1.165 + case 1: 1.166 + return y; 1.167 + case 2: 1.168 + return z; 1.169 + case 3: 1.170 + default: 1.171 + return w; 1.172 + } 1.173 + } 1.174 + 1.175 + inline float length() const 1.176 + { 1.177 + return sqrt(x * x + y * y + z * z + w * w); 1.178 + } 1.179 + 1.180 + inline void normalize() 1.181 + { 1.182 + float len = length(); 1.183 + if(len != 0) { 1.184 + x /= len; 1.185 + y /= len; 1.186 + z /= len; 1.187 + w /= len; 1.188 + } 1.189 + } 1.190 +}; 1.191 + 1.192 +inline Vector4 operator +(const Vector4 &a, const Vector4 &b) 1.193 +{ 1.194 + return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); 1.195 +} 1.196 + 1.197 +inline Vector4 operator -(const Vector4 &a, const Vector4 &b) 1.198 +{ 1.199 + return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); 1.200 +} 1.201 + 1.202 +inline Vector4 operator *(const Vector4 &a, const Vector4 &b) 1.203 +{ 1.204 + return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); 1.205 +} 1.206 + 1.207 +inline Vector4 operator *(const Vector4 &v, float s) 1.208 +{ 1.209 + return Vector4(v.x * s, v.y * s, v.z * s, v.w * s); 1.210 +} 1.211 + 1.212 +inline Vector4 operator *(float s, const Vector4 &v) 1.213 +{ 1.214 + return Vector4(v.x * s, v.y * s, v.z * s, v.w * s); 1.215 +} 1.216 + 1.217 +inline Vector4 operator /(const Vector4 &a, const Vector4 &b) 1.218 +{ 1.219 + return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w); 1.220 +} 1.221 + 1.222 +inline Vector4 operator /(const Vector4 &v, float s) 1.223 +{ 1.224 + return Vector4(v.x / s, v.y / s, v.z / s, v.w / s); 1.225 +} 1.226 + 1.227 +inline float dot(const Vector4 &a, const Vector4 &b) 1.228 +{ 1.229 + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; 1.230 +} 1.231 + 1.232 +inline float length(const Vector4 &v) 1.233 +{ 1.234 + return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); 1.235 +} 1.236 + 1.237 +inline float length_sq(const Vector4 &v) 1.238 +{ 1.239 + return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w; 1.240 +} 1.241 + 1.242 +inline Vector4 normalize(const Vector4 &v) 1.243 +{ 1.244 + float len = length(v); 1.245 + float s = len == 0 ? 1.0 : 1.0 / len; 1.246 + return v * s; 1.247 +} 1.248 + 1.249 +inline Vector4 transform(const Vector4 &v, const Matrix4x4 &m) 1.250 +{ 1.251 + return Vector4( 1.252 + m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w, 1.253 + m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w, 1.254 + m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w, 1.255 + m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w); 1.256 +} 1.257 + 1.258 +inline Vector3::Vector3(const Vector4 &v) 1.259 + : x(v.x), y(v.y), z(v.z) 1.260 +{ 1.261 +} 1.262 + 1.263 + 1.264 +#endif // VMATH_H_ 1.265 \ No newline at end of file