add maths utils
This commit is contained in:
113
include/misc/Maths.h
Normal file
113
include/misc/Maths.h
Normal file
@@ -0,0 +1,113 @@
|
||||
#pragma once
|
||||
|
||||
#include "Defines.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace td {
|
||||
namespace maths {
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// Vectors //
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
template<typename T>
|
||||
Vec3<T> operator- (const Vec3<T>& vect) {
|
||||
return { -vect.x, -vect.y, -vect.z };
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec3<T> operator+ (const Vec3<T>& vect, const Vec3<T>& other) {
|
||||
return { vect.x + other.x, vect.y + other.y, vect.z + other.y };
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec3<T> operator- (const Vec3<T>& vect, const Vec3<T>& other) {
|
||||
return vect + (-other);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec3<T> Normalize(const Vec3<T>& vect) {
|
||||
T length = std::sqrt(vect.x * vect.x + vect.y * vect.y + vect.z * vect.z);
|
||||
|
||||
return { vect.x / length, vect.y / length, vect.z / length };
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T Dot(const Vec3<T>& vect, const Vec3<T>& other) {
|
||||
return vect.x * other.x + vect.y * other.y + vect.z * other.z;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec3<T> Cross(const Vec3<T>& vect, const Vec3<T>& other) {
|
||||
return {
|
||||
vect.y * other.z - vect.z * other.y,
|
||||
vect.z * other.x - vect.x * other.z,
|
||||
vect.x * other.y - vect.y * other.x,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// Matricies //
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
template<typename T>
|
||||
struct Mat4 {
|
||||
T x0, x1, x2, x3;
|
||||
T y0, y1, y2, y3;
|
||||
T z0, z1, z2, z3;
|
||||
T w0, w1, w2, w3;
|
||||
|
||||
T operator[] (std::size_t offset) const {
|
||||
return reinterpret_cast<const T*>(this)[offset];
|
||||
}
|
||||
|
||||
T& operator[] (std::size_t offset) {
|
||||
return reinterpret_cast<T*>(this)[offset];
|
||||
}
|
||||
};
|
||||
|
||||
typedef Mat4<float> Mat4f;
|
||||
typedef Mat4<int> Mat4i;
|
||||
typedef Mat4<double> Mat4d;
|
||||
|
||||
Mat4f Perspective(float fovY, float aspectRatio, float zNear, float zFar);
|
||||
Mat4f LookAt(const Vec3f& eye, const Vec3f& center, const Vec3f& up);
|
||||
|
||||
Mat4f Dot(const Mat4f& mat, const Mat4f& other);
|
||||
|
||||
template<typename T>
|
||||
Mat4<T> Identity() {
|
||||
Mat4<T> result{};
|
||||
|
||||
result.x0 = static_cast<T>(1);
|
||||
result.y1 = static_cast<T>(1);
|
||||
result.z2 = static_cast<T>(1);
|
||||
result.w3 = static_cast<T>(1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Mat4<T> Transpose(const Mat4<T>& mat) {
|
||||
Mat4<T> result;
|
||||
|
||||
result.x1 = mat.y0;
|
||||
result.x2 = mat.z0;
|
||||
result.x3 = mat.w0;
|
||||
result.y0 = mat.x1;
|
||||
result.y2 = mat.z1;
|
||||
result.y3 = mat.w1;
|
||||
result.z0 = mat.x2;
|
||||
result.z1 = mat.y2;
|
||||
result.z3 = mat.w2;
|
||||
result.w0 = mat.x3;
|
||||
result.w1 = mat.y3;
|
||||
result.w2 = mat.z3;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace maths
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user