# HG changeset patch # User John Tsiombikas # Date 1437799350 -10800 # Node ID d30e24132b6eeb4a570b5e526c75f87bd5a5df0e # Parent d71b4e899e08c711b3a9741065a4d8044088b3c5 more gmath diff -r d71b4e899e08 -r d30e24132b6e src/gmath/matrix.h --- a/src/gmath/matrix.h Sat Jul 25 05:52:39 2015 +0300 +++ b/src/gmath/matrix.h Sat Jul 25 07:42:30 2015 +0300 @@ -2,7 +2,7 @@ #define GMATH_MATRIX_H_ #include -#include "vec.h" +#include "vector.h" namespace gph { diff -r d71b4e899e08 -r d30e24132b6e src/gmath/vector.cc --- a/src/gmath/vector.cc Sat Jul 25 05:52:39 2015 +0300 +++ b/src/gmath/vector.cc Sat Jul 25 07:42:30 2015 +0300 @@ -1,4 +1,5 @@ -#include "vec.h" +#include "vector.h" +#include "matrix.h" namespace gph { @@ -7,6 +8,22 @@ { } +Vector3 operator *(const Vector3 &v, const Matrix4x4 &m) +{ + float x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0]; + float y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1]; + float z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2]; + return Vector3(x, y, z); +} + +Vector3 operator *(const Matrix4x4 &m, const Vector3 &v) +{ + float x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3]; + float y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3]; + float z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]; + return Vector3(x, y, z); +} + Vector4::Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) { diff -r d71b4e899e08 -r d30e24132b6e src/gmath/vector.h --- a/src/gmath/vector.h Sat Jul 25 05:52:39 2015 +0300 +++ b/src/gmath/vector.h Sat Jul 25 07:42:30 2015 +0300 @@ -6,6 +6,7 @@ namespace gph { class Vector4; +class Matrix4x4; class Vector3 { public: @@ -15,25 +16,9 @@ Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {} Vector3(const Vector4 &v); - inline void normalize() - { - float len = (float)sqrt(x * x + y * y + z * z); - if(len != 0.0f) { - x /= len; - y /= len; - z /= len; - } - } - - inline float &operator[] (int idx) - { - return idx == 0 ? x : (idx == 1 ? y : z); - } - - inline const float &operator[] (int idx) const - { - return idx == 0 ? x : (idx == 1 ? y : z); - } + inline void normalize(); + inline float &operator[] (int idx); + inline const float &operator[] (int idx) const; }; @@ -45,149 +30,46 @@ Vector4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {} Vector4(const Vector3 &v); - inline void normalize() - { - float len = (float)sqrt(x * x + y * y + z * z + w * w); - if(len != 0.0f) { - x /= len; - y /= len; - z /= len; - w /= len; - } - } - - inline float &operator[] (int idx) - { - return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w)); - } - - inline const float &operator[] (int idx) const - { - return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w)); - } + inline void normalize(); + inline float &operator[] (int idx); + inline const float &operator[] (int idx) const; }; // ---- Vector3 functions ---- +inline Vector3 operator +(const Vector3 &a, const Vector3 &b); +inline Vector3 operator -(const Vector3 &a, const Vector3 &b); +inline Vector3 operator *(const Vector3 &a, const Vector3 &b); +inline Vector3 operator /(const Vector3 &a, const Vector3 &b); +inline Vector3 operator *(const Vector3 &v, float s); +inline Vector3 operator *(float s, const Vector3 &v); +inline Vector3 operator /(const Vector3 &v, float s); +inline Vector3 operator /(float s, const Vector3 &v); +inline Vector3 &operator +=(Vector3 &a, const Vector3 &b); +inline Vector3 &operator -=(Vector3 &a, const Vector3 &b); +inline Vector3 &operator *=(Vector3 &a, const Vector3 &b); +inline Vector3 &operator /=(Vector3 &a, const Vector3 &b); +inline Vector3 &operator *=(Vector3 &v, float s); +inline Vector3 &operator /=(Vector3 &v, float s); -inline Vector3 operator +(const Vector3 &a, const Vector3 &b) -{ - return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); -} +Vector3 operator *(const Vector3 &v, const Matrix4x4 &m); +Vector3 operator *(const Matrix4x4 &m, const Vector3 &v); -inline Vector3 operator -(const Vector3 &a, const Vector3 &b) -{ - return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); -} +inline bool operator ==(const Vector3 &a, const Vector3 &b); +inline bool operator !=(const Vector3 &a, const Vector3 &b); -inline Vector3 operator *(const Vector3 &a, const Vector3 &b) -{ - return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); -} +inline float dot(const Vector3 &a, const Vector3 &b); +inline Vector3 cross(const Vector3 &a, const Vector3 &b); +inline float length(const Vector3 &v); +inline float length_sq(const Vector3 &v); +inline Vector3 normalize(const Vector3 &v); -inline Vector3 operator /(const Vector3 &a, const Vector3 &b) -{ - return Vector3(a.x / b.x, a.y / b.y, a.z / b.z); -} +inline Vector3 reflect(const Vector3 &v, const Vector3 &n); +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior); +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior); -inline Vector3 operator *(const Vector3 &v, float s) -{ - return Vector3(v.x * s, v.y * s, v.z * s); -} - -inline Vector3 operator *(float s, const Vector3 &v) -{ - return Vector3(s * v.x, s * v.y, s * v.z); -} - -inline Vector3 operator /(const Vector3 &v, float s) -{ - return Vector3(v.x / s, v.y / s, v.z / s); -} - -inline Vector3 operator /(float s, const Vector3 &v) -{ - return Vector3(s / v.x, s / v.y, s / v.z); -} - -inline Vector3 &operator +=(Vector3 &a, const Vector3 &b) -{ - a.x += b.x; - a.y += b.y; - a.z += b.z; - return a; -} - -inline Vector3 &operator -=(Vector3 &a, const Vector3 &b) -{ - a.x -= b.x; - a.y -= b.y; - a.z -= b.z; - return a; -} - -inline Vector3 &operator *=(Vector3 &a, const Vector3 &b) -{ - a.x *= b.x; - a.y *= b.y; - a.z *= b.z; - return a; -} - -inline Vector3 &operator /=(Vector3 &a, const Vector3 &b) -{ - a.x /= b.x; - a.y /= b.y; - a.z /= b.z; - return a; -} - -inline Vector3 &operator *=(Vector3 &v, float s) -{ - v.x *= s; - v.y *= s; - v.z *= s; - return v; -} - -inline Vector3 &operator /=(Vector3 &v, float s) -{ - v.x /= s; - v.y /= s; - v.z /= s; - return v; -} - -inline float dot(const Vector3 &a, const Vector3 &b) -{ - return a.x * b.x + a.y * b.y + a.z * b.z; -} - -inline Vector3 cross(const Vector3 &a, const Vector3 &b) -{ - return Vector3(a.y * b.z - a.z * b.y, - a.z * b.x - a.x * b.z, - a.x * b.y - a.y * b.x); -} - -inline float length(const Vector3 &v) -{ - return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z); -} - -inline float length_sq(const Vector3 &v) -{ - return v.x * v.x + v.y * v.y + v.z * v.z; -} - -inline Vector3 normalize(const Vector3 &v) -{ - float len = length(v); - if(len == 0.0f) { - return v; - } - - return Vector3(v.x / len, v.y / len, v.z / len); -} +inline Vector3 distance(const Vector3 &a, const Vector3 &b); +inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b); +inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng); } diff -r d71b4e899e08 -r d30e24132b6e src/gmath/vector.inl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gmath/vector.inl Sat Jul 25 07:42:30 2015 +0300 @@ -0,0 +1,217 @@ +#include + +namespace gph { + +// ---- Vector3 ---- + +inline void Vector3::normalize() +{ + float len = (float)sqrt(x * x + y * y + z * z); + if(len != 0.0f) { + x /= len; + y /= len; + z /= len; + } +} + +inline float &Vector3::operator[] (int idx) +{ + return idx == 0 ? x : (idx == 1 ? y : z); +} + +inline const float &Vector3::operator[] (int idx) const +{ + return idx == 0 ? x : (idx == 1 ? y : z); +} + +inline Vector3 operator +(const Vector3 &a, const Vector3 &b) +{ + return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); +} + +inline Vector3 operator -(const Vector3 &a, const Vector3 &b) +{ + return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); +} + +inline Vector3 operator *(const Vector3 &a, const Vector3 &b) +{ + return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); +} + +inline Vector3 operator /(const Vector3 &a, const Vector3 &b) +{ + return Vector3(a.x / b.x, a.y / b.y, a.z / b.z); +} + +inline Vector3 operator *(const Vector3 &v, float s) +{ + return Vector3(v.x * s, v.y * s, v.z * s); +} + +inline Vector3 operator *(float s, const Vector3 &v) +{ + return Vector3(s * v.x, s * v.y, s * v.z); +} + +inline Vector3 operator /(const Vector3 &v, float s) +{ + return Vector3(v.x / s, v.y / s, v.z / s); +} + +inline Vector3 operator /(float s, const Vector3 &v) +{ + return Vector3(s / v.x, s / v.y, s / v.z); +} + +inline Vector3 &operator +=(Vector3 &a, const Vector3 &b) +{ + a.x += b.x; + a.y += b.y; + a.z += b.z; + return a; +} + +inline Vector3 &operator -=(Vector3 &a, const Vector3 &b) +{ + a.x -= b.x; + a.y -= b.y; + a.z -= b.z; + return a; +} + +inline Vector3 &operator *=(Vector3 &a, const Vector3 &b) +{ + a.x *= b.x; + a.y *= b.y; + a.z *= b.z; + return a; +} + +inline Vector3 &operator /=(Vector3 &a, const Vector3 &b) +{ + a.x /= b.x; + a.y /= b.y; + a.z /= b.z; + return a; +} + +inline Vector3 &operator *=(Vector3 &v, float s) +{ + v.x *= s; + v.y *= s; + v.z *= s; + return v; +} + +inline Vector3 &operator /=(Vector3 &v, float s) +{ + v.x /= s; + v.y /= s; + v.z /= s; + return v; +} + +inline bool operator ==(const Vector3 &a, const Vector3 &b) +{ + return a.x == b.x && a.y == b.y && a.z == b.z; +} + +inline bool operator !=(const Vector3 &a, const Vector3 &b) +{ + return !(a == b); +} + +inline float dot(const Vector3 &a, const Vector3 &b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} + +inline Vector3 cross(const Vector3 &a, const Vector3 &b) +{ + return Vector3(a.y * b.z - a.z * b.y, + a.z * b.x - a.x * b.z, + a.x * b.y - a.y * b.x); +} + +inline float length(const Vector3 &v) +{ + return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z); +} + +inline float length_sq(const Vector3 &v) +{ + return v.x * v.x + v.y * v.y + v.z * v.z; +} + +inline Vector3 normalize(const Vector3 &v) +{ + float len = length(v); + if(len == 0.0f) { + return v; + } + + return Vector3(v.x / len, v.y / len, v.z / len); +} + +inline Vector3 reflect(const Vector3 &v, const Vector3 &n) +{ + return v - n * dot(n, v) * 2.0; +} + +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior) +{ + float ndotv = dot(n, v); + float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv); + if(k < 0.0f) { + return Vector3(); + } + return ior * v - (ior * ndotv + sqrt(k)) * n; +} + +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior) +{ + if(to_ior == 0.0f) to_ior = 1.0f; + return refract(v, n, from_ior / to_ior); +} + +inline Vector3 distance(const Vector3 &a, const Vector3 &b) +{ + return length(a - b); +} + +inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b) +{ + return length_sq(a - b); +} + +inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng) +{ + return dot(ng, i) < 0.0f ? n : -n; +} + +// ---- Vector4 ---- + + +inline void Vector4::normalize() +{ + float len = (float)sqrt(x * x + y * y + z * z + w * w); + if(len != 0.0f) { + x /= len; + y /= len; + z /= len; + w /= len; + } +} + +inline float &Vector4::operator[] (int idx) +{ + return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w)); +} + +inline const float &Vector4::operator[] (int idx) const +{ + return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w)); +} + +} // namespace gph