143 lines
3.5 KiB
C++
143 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include "Defines.h"
|
|
#include <cmath>
|
|
|
|
namespace td {
|
|
namespace maths {
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
// Vectors //
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
template<typename T>
|
|
Vec2<T> operator+(const Vec2<T>& vect, const Vec2<T>& other) {
|
|
return {vect.x + other.x, vect.y + other.y};
|
|
}
|
|
|
|
template<typename T>
|
|
Vec3<T> operator- (const Vec3<T>& vect) {
|
|
return { -vect.x, -vect.y, -vect.z };
|
|
}
|
|
|
|
template<typename T>
|
|
Vec4<T> operator- (const Vec4<T>& vect) {
|
|
return { -vect.x, -vect.y, -vect.z, -vect.w };
|
|
}
|
|
|
|
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>
|
|
Vec4<T> Normalize(const Vec4<T>& vect) {
|
|
T length = std::sqrt(vect.x * vect.x + vect.y * vect.y + vect.z * vect.z + vect.w * vect.w);
|
|
|
|
return { vect.x / length, vect.y / length, vect.z / length, vect.w / 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,
|
|
};
|
|
}
|
|
|
|
template<typename T>
|
|
T Dot(const Vec4<T>& vect, const Vec4<T>& other) {
|
|
return vect.x * other.x + vect.y * other.y + vect.z * other.z + vect.w * other.w;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
// Matricies //
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
template<typename T>
|
|
Vec4<T> Dot(const Mat4<T>& mat, const Vec4<T>& vect) {
|
|
return {
|
|
mat.x0 * vect.x + mat.x1 * vect.y + mat.x2 * vect.z + mat.x3 * vect.w,
|
|
mat.y0 * vect.x + mat.y1 * vect.y + mat.y2 * vect.z + mat.y3 * vect.w,
|
|
mat.z0 * vect.x + mat.z1 * vect.y + mat.z2 * vect.z + mat.z3 * vect.w,
|
|
mat.w0 * vect.x + mat.w1 * vect.y + mat.w2 * vect.z + mat.w3 * vect.w
|
|
};
|
|
}
|
|
|
|
template<typename T>
|
|
Mat4<T> Dot(const Mat4<T>& mat, const Mat4<T>& other) {
|
|
Mat4<T> result {};
|
|
|
|
for (std::size_t i = 0; i < Mat4<T>::MATRIX_SIZE; i++) {
|
|
for (std::size_t j = 0; j < Mat4<T>::MATRIX_SIZE; j++) {
|
|
for (std::size_t k = 0; k < Mat4<T>::MATRIX_SIZE; k++) {
|
|
result.at(i, j) += mat.at(i, k) * other.at(k, j);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
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;
|
|
result.x0 = mat.x0;
|
|
result.y1 = mat.y1;
|
|
result.z2 = mat.z2;
|
|
result.w3 = mat.w3;
|
|
|
|
return result;
|
|
}
|
|
|
|
Mat4f Perspective(float fovY, float aspectRatio, float zNear, float zFar);
|
|
Mat4f Look(const Vec3f& eye, const Vec3f& center, const Vec3f& up);
|
|
|
|
Mat4f Inverse(const Mat4f& mat);
|
|
|
|
} // namespace maths
|
|
} // namespace td
|