# HG changeset patch # User John Tsiombikas # Date 1437792759 -10800 # Node ID d71b4e899e08c711b3a9741065a4d8044088b3c5 # Parent fb032d88839f6586e112f525910cbbc244c066d8 minimal matrix and vec4 diff -r fb032d88839f -r d71b4e899e08 src/gmath/gmath.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gmath/gmath.h Sat Jul 25 05:52:39 2015 +0300 @@ -0,0 +1,7 @@ +#ifndef GMATH_H_ +#define GMATH_H_ + +#include "vec.h" +#include "matrix.h" + +#endif // GMATH_H_ diff -r fb032d88839f -r d71b4e899e08 src/gmath/matrix.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gmath/matrix.h Sat Jul 25 05:52:39 2015 +0300 @@ -0,0 +1,58 @@ +#ifndef GMATH_MATRIX_H_ +#define GMATH_MATRIX_H_ + +#include +#include "vec.h" + +namespace gph { + +class Matrix4x4 { +private: + float m[4][4]; + +public: + static Matrix4x4 identity; + + Matrix4x4() + { + memcpy((float*)m, (const float*)identity.m, 16 * sizeof(float)); + } + + Matrix4x4(const float *m) + { + memcpy((float*)this->m, (const float*)m, 16 * sizeof(float)); + } + + Matrix4x4(float m00, float m01, float m02, float m03, + float m10, float m11, float m12, float m13, + float m20, float m21, float m22, float m23, + float m30, float m31, float m32, float m33) + { + m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03; + m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13; + m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; + m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33; + } + + Matrix4x4(const Vector4 &v0, const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) + { + m[0][0] = v0.x; m[0][1] = v0.y; m[0][2] = v0.z; m[0][3] = v0.w; + m[1][0] = v1.x; m[1][1] = v1.y; m[1][2] = v1.z; m[1][3] = v1.w; + m[2][0] = v2.x; m[2][1] = v2.y; m[2][2] = v2.z; m[2][3] = v2.w; + m[3][0] = v3.x; m[3][1] = v3.y; m[3][2] = v3.z; m[3][3] = v3.w; + } + + float *operator [](int idx) + { + return m[idx]; + } + + const float *operator [](int idx) const + { + return m[idx]; + } +}; + +} // namespace gph + +#endif // GMATH_MATRIX_H_ diff -r fb032d88839f -r d71b4e899e08 src/gmath/vec.h --- a/src/gmath/vec.h Sat Jul 25 05:08:22 2015 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,149 +0,0 @@ -#ifndef GMATH_VEC_H_ -#define GMATH_VEC_H_ - -#include - -namespace gph { - -class Vector3 { -public: - float x, y, z; - - Vector3() : x(0), y(0), z(0) {} - Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {} - - inline void normalize(); -}; - -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 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 void Vector3::normalize() -{ - float len = length(*this); - if(len != 0.0f) { - x /= len; - y /= len; - z /= len; - } -} - - -} - -#endif /* GMATH_VEC_H_ */ diff -r fb032d88839f -r d71b4e899e08 src/gmath/vector.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gmath/vector.cc Sat Jul 25 05:52:39 2015 +0300 @@ -0,0 +1,15 @@ +#include "vec.h" + +namespace gph { + +Vector3::Vector3(const Vector4 &v) + : x(v.x), y(v.y), z(v.z) +{ +} + +Vector4::Vector4(const Vector3 &v) + : x(v.x), y(v.y), z(v.z), w(1.0f) +{ +} + +} // namespace gph diff -r fb032d88839f -r d71b4e899e08 src/gmath/vector.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gmath/vector.h Sat Jul 25 05:52:39 2015 +0300 @@ -0,0 +1,194 @@ +#ifndef GMATH_VEC_H_ +#define GMATH_VEC_H_ + +#include + +namespace gph { + +class Vector4; + +class Vector3 { +public: + float x, y, z; + + Vector3() : x(0), y(0), z(0) {} + 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); + } +}; + + +class Vector4 { +public: + float x, y, z, w; + + Vector4() : x(0), y(0), z(0), w(0) {} + 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)); + } +}; + +// ---- Vector3 functions ---- + +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 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); +} + +} + +#endif /* GMATH_VEC_H_ */ diff -r fb032d88839f -r d71b4e899e08 src/graphene3d.h --- a/src/graphene3d.h Sat Jul 25 05:08:22 2015 +0300 +++ b/src/graphene3d.h Sat Jul 25 05:52:39 2015 +0300 @@ -2,6 +2,6 @@ #define GRAPHENE3D_H_ #include "opengl.h" -#include "gmath/vec.h" +#include "gmath/gmath.h" #endif // GRAPHENE3D_H_