graphene
changeset 0:8ab44b19895e
graphene!!!
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 20 Jul 2015 07:08:25 +0300 |
parents | |
children | f85a59195206 |
files | .hgignore gmath/vec.h |
diffstat | 2 files changed, 149 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Mon Jul 20 07:08:25 2015 +0300 1.3 @@ -0,0 +1,3 @@ 1.4 +\.o$ 1.5 +\.d$ 1.6 +\.swp$
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/gmath/vec.h Mon Jul 20 07:08:25 2015 +0300 2.3 @@ -0,0 +1,146 @@ 2.4 +#ifndef GMATH_VEC_H_ 2.5 +#define GMATH_VEC_H_ 2.6 + 2.7 +#include <math.h> 2.8 + 2.9 +namespace gmath { 2.10 + 2.11 +class Vec3 { 2.12 +public: 2.13 + float x, y, z; 2.14 + 2.15 + Vec3() : x(0), y(0), z(0) {} 2.16 + Vec3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {} 2.17 + 2.18 + void normalize() 2.19 + { 2.20 + float len = length(*this); 2.21 + if(len != 0.0f) { 2.22 + x /= len; 2.23 + y /= len; 2.24 + z /= len; 2.25 + } 2.26 + } 2.27 +}; 2.28 + 2.29 +inline Vec3 operator +(const Vec3 &a, const Vec3 &b) 2.30 +{ 2.31 + return Vec3(a.x + b.x, a.y + b.y, a.z + b.z); 2.32 +} 2.33 + 2.34 +inline Vec3 operator -(const Vec3 &a, const Vec3 &b) 2.35 +{ 2.36 + return Vec3(a.x - b.x, a.y - b.y, a.z - b.z); 2.37 +} 2.38 + 2.39 +inline Vec3 operator *(const Vec3 &a, const Vec3 &b) 2.40 +{ 2.41 + return Vec3(a.x * b.x, a.y * b.y, a.z * b.z); 2.42 +} 2.43 + 2.44 +inline Vec3 operator /(const Vec3 &a, const Vec3 &b) 2.45 +{ 2.46 + return Vec3(a.x / b.x, a.y / b.y, a.z / b.z); 2.47 +} 2.48 + 2.49 +inline Vec3 operator *(const Vec3 &v, float s) 2.50 +{ 2.51 + return Vec3(v.x * s, v.y * s, v.z * s); 2.52 +} 2.53 + 2.54 +inline Vec3 operator *(float s, const Vec3 &v) 2.55 +{ 2.56 + return Vec3(s * v.x, s * v.y, s * v.z); 2.57 +} 2.58 + 2.59 +inline Vec3 operator /(const Vec3 &v, float s) 2.60 +{ 2.61 + return Vec3(v.x / s, v.y / s, v.z / s); 2.62 +} 2.63 + 2.64 +inline Vec3 operator /(float s, const Vec3 &v) 2.65 +{ 2.66 + return Vec3(s / v.x, s / v.y, s / v.z); 2.67 +} 2.68 + 2.69 +inline Vec3 &operator +=(Vec3 &a, const Vec3 &b) 2.70 +{ 2.71 + a.x += b.x; 2.72 + a.y += b.y; 2.73 + a.z += b.z; 2.74 + return *this; 2.75 +} 2.76 + 2.77 +inline Vec3 &operator -=(Vec3 &a, const Vec3 &b) 2.78 +{ 2.79 + a.x -= b.x; 2.80 + a.y -= b.y; 2.81 + a.z -= b.z; 2.82 + return *this; 2.83 +} 2.84 + 2.85 +inline Vec3 &operator *=(Vec3 &a, const Vec3 &b) 2.86 +{ 2.87 + a.x *= b.x; 2.88 + a.y *= b.y; 2.89 + a.z *= b.z; 2.90 + return *this; 2.91 +} 2.92 + 2.93 +inline Vec3 &operator /=(Vec3 &a, const Vec3 &b) 2.94 +{ 2.95 + a.x /= b.x; 2.96 + a.y /= b.y; 2.97 + a.z /= b.z; 2.98 + return *this; 2.99 +} 2.100 + 2.101 +inline Vec3 &operator *=(Vec3 &v, float s) 2.102 +{ 2.103 + v.x *= s; 2.104 + v.y *= s; 2.105 + v.z *= s; 2.106 + return *this; 2.107 +} 2.108 + 2.109 +inline Vec3 &operator /=(Vec3 &v, float s) 2.110 +{ 2.111 + v.x /= s; 2.112 + v.y /= s; 2.113 + v.z /= s; 2.114 + return *this; 2.115 +} 2.116 + 2.117 +inline float dot(const Vec3 &a, const Vec3 &b) 2.118 +{ 2.119 + return a.x * b.x + a.y * b.y + a.z * b.z; 2.120 +} 2.121 + 2.122 +inline Vec3 cross(const Vec3 &a, const Vec3 &b) 2.123 +{ 2.124 + return Vec3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); 2.125 +} 2.126 + 2.127 +inline float length(const Vec3 &v) 2.128 +{ 2.129 + return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 2.130 +} 2.131 + 2.132 +inline float length_sq(const Vec3 &v) 2.133 +{ 2.134 + return v.x * v.x + v.y * v.y + v.z * v.z; 2.135 +} 2.136 + 2.137 +inline Vec3 normalize(const Vec3 &v) 2.138 +{ 2.139 + float len = length(v); 2.140 + if(len == 0.0f) { 2.141 + return v; 2.142 + } 2.143 + 2.144 + return Vec3(v.x / len, v.y / len, v.z / len); 2.145 +} 2.146 + 2.147 +} 2.148 + 2.149 +#endif /* GMATH_VEC_H_ */