75357b330c
Now using GLM instead of the custom math libraries. Sadly, this did not 100% fix the problem at hand but it does give some closure that the math is not the problem. Also it will be nice to have a general math library to not have to deal with creating every math function every time.
212 lines
3.1 KiB
C++
212 lines
3.1 KiB
C++
#pragma once
|
|
|
|
#include "vec2.h"
|
|
#include "vec3.h"
|
|
|
|
namespace egm
|
|
{
|
|
|
|
template <typename T>
|
|
struct tvec4
|
|
{
|
|
T x;
|
|
T y;
|
|
T z;
|
|
T w;
|
|
|
|
tvec4() : x(0), y(0), z(0), w(0) {}
|
|
tvec4(const tvec4<T>& v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
|
|
template <typename U>
|
|
tvec4(const tvec4<U>& v) : x((T)v.x), y((T)v.y), z((T)v.z), w((T)v.w) {}
|
|
tvec4(const tvec3<T>& a, T b) : x(a.x), y(a.y), z(a.z), w(b) {}
|
|
tvec4(T a, const tvec3<T>& b) : x(a), y(b.x), z(b.y), w(b.z) {}
|
|
tvec4(const tvec2<T>& a, const tvec2<T>& b) : x(a.x), y(a.y), z(b.x), w(b.y) {}
|
|
tvec4(const T& scalar) : x(scalar), y(scalar), z(scalar), w(scalar) {}
|
|
tvec4(const T& scalar1, const T& scalar2, const T& scalar3, const T& scalar4) : x(scalar1), y(scalar2), z(scalar3), w(scalar4) {}
|
|
|
|
T& operator[](const unsigned int& index)
|
|
{
|
|
switch (index)
|
|
{
|
|
case 0:
|
|
return x;
|
|
case 1:
|
|
return y;
|
|
case 2:
|
|
return z;
|
|
case 3:
|
|
return w;
|
|
default:
|
|
throw "Index Out Of Bounds";
|
|
}
|
|
}
|
|
|
|
bool operator==(const tvec4<T>& o)
|
|
{
|
|
return (o.x == x && o.y == y && o.z == z && o.w == w);
|
|
}
|
|
|
|
tvec4<T>& operator=(const tvec4<T>& o)
|
|
{
|
|
x = o.x;
|
|
y = o.y;
|
|
z = o.z;
|
|
w = o.w;
|
|
return *this;
|
|
}
|
|
|
|
tvec4<T> operator-() const
|
|
{
|
|
return {
|
|
-x,
|
|
-y,
|
|
-z,
|
|
-w,
|
|
};
|
|
}
|
|
|
|
tvec4<T> operator+(const tvec4<T>& o) const
|
|
{
|
|
return {
|
|
x + o.x,
|
|
y + o.y,
|
|
z + o.z,
|
|
w + o.w
|
|
};
|
|
}
|
|
|
|
tvec4<T>& operator+=(const tvec4<T>& o)
|
|
{
|
|
x += o.x;
|
|
y += o.y;
|
|
z += o.z;
|
|
w += o.w;
|
|
return *this;
|
|
}
|
|
|
|
tvec4<T> operator-(const tvec4<T>& o) const
|
|
{
|
|
return {
|
|
x - o.x,
|
|
y - o.y,
|
|
z - o.z,
|
|
w - o.w
|
|
};
|
|
}
|
|
|
|
tvec4<T>& operator-=(const tvec4<T>& o)
|
|
{
|
|
x -= o.x;
|
|
y -= o.y;
|
|
z -= o.z;
|
|
w -= o.w;
|
|
return *this;
|
|
}
|
|
|
|
tvec4<T> operator*(const tvec4<T>& o) const
|
|
{
|
|
return {
|
|
x * o.x,
|
|
y * o.y,
|
|
z * o.z,
|
|
w * o.w
|
|
};
|
|
}
|
|
|
|
tvec4<T>& operator*=(const tvec4<T>& o)
|
|
{
|
|
x *= o.x;
|
|
y *= o.y;
|
|
z *= o.z;
|
|
w *= o.w;
|
|
return *this;
|
|
}
|
|
|
|
tvec4<T> operator*(const T& scalar) const
|
|
{
|
|
return {
|
|
x * scalar,
|
|
y * scalar,
|
|
z * scalar,
|
|
w * scalar
|
|
};
|
|
}
|
|
|
|
tvec4<T>& operator*=(const T& scalar)
|
|
{
|
|
x *= scalar;
|
|
y *= scalar;
|
|
z *= scalar;
|
|
w *= scalar;
|
|
return *this;
|
|
}
|
|
|
|
tvec4<T> operator/(const tvec4<T>& o) const
|
|
{
|
|
return {
|
|
x / o.x,
|
|
y / o.y,
|
|
z / o.z,
|
|
w / o.w
|
|
};
|
|
}
|
|
|
|
tvec4<T>& operator/=(const tvec4<T>& o)
|
|
{
|
|
x /= o.x;
|
|
y /= o.y;
|
|
z /= o.z;
|
|
w /= o.w;
|
|
return *this;
|
|
}
|
|
|
|
tvec4<T> operator/(const T& scalar) const
|
|
{
|
|
return {
|
|
x / scalar,
|
|
y / scalar,
|
|
z / scalar,
|
|
w / scalar
|
|
};
|
|
}
|
|
|
|
tvec4<T>& operator/=(const T& scalar)
|
|
{
|
|
x /= scalar;
|
|
y /= scalar;
|
|
z /= scalar;
|
|
w /= scalar;
|
|
return *this;
|
|
}
|
|
|
|
T length() const
|
|
{
|
|
return sqrt(x * x + y * y + z * z);
|
|
}
|
|
|
|
T length_squared() const
|
|
{
|
|
return x * x + y * y + z * z;
|
|
}
|
|
|
|
void normalize()
|
|
{
|
|
T l = length();
|
|
x /= l;
|
|
y /= l;
|
|
z /= l;
|
|
}
|
|
|
|
tvec4<T> normalized() const
|
|
{
|
|
tvec4<T> ret(*this);
|
|
ret.normalize();
|
|
return ret;
|
|
}
|
|
};
|
|
|
|
typedef tvec4<float> vec4;
|
|
typedef tvec4<double> dvec4;
|
|
typedef tvec4<int> ivec4;
|
|
}
|