more maths
This commit is contained in:
@@ -46,7 +46,10 @@ Vec3<T> Cross(const Vec3<T>& vect, const Vec3<T>& other) {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
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 //
|
||||
@@ -54,6 +57,8 @@ Vec3<T> Cross(const Vec3<T>& vect, const Vec3<T>& other) {
|
||||
|
||||
template<typename T>
|
||||
struct Mat4 {
|
||||
static const std::size_t MATRIX_SIZE = 4;
|
||||
|
||||
T x0, x1, x2, x3;
|
||||
T y0, y1, y2, y3;
|
||||
T z0, z1, z2, z3;
|
||||
@@ -66,16 +71,44 @@ struct Mat4 {
|
||||
T& operator[] (std::size_t offset) {
|
||||
return reinterpret_cast<T*>(this)[offset];
|
||||
}
|
||||
|
||||
T at(std::size_t row, std::size_t column) const {
|
||||
return operator[](row * MATRIX_SIZE + column);
|
||||
}
|
||||
|
||||
T& at(std::size_t row, std::size_t column) {
|
||||
return operator[](row * MATRIX_SIZE + column);
|
||||
}
|
||||
};
|
||||
|
||||
typedef Mat4<float> Mat4f;
|
||||
typedef Mat4<int> Mat4i;
|
||||
typedef Mat4<double> Mat4d;
|
||||
|
||||
Mat4f Perspective(float fovY, float aspectRatio, float zNear, float zFar);
|
||||
Mat4f Look(const Vec3f& eye, const Vec3f& center, const Vec3f& up);
|
||||
template<typename T>
|
||||
Vec4<T> Dot(const Mat4<T>& mat, const Vec4<T>& vect) {
|
||||
return {
|
||||
Dot(*reinterpret_cast<const Vec4<T>*>(&mat), vect),
|
||||
Dot(*reinterpret_cast<const Vec4<T>*>(&mat[Mat4<T>::MATRIX_SIZE]), vect),
|
||||
Dot(*reinterpret_cast<const Vec4<T>*>(&mat[2 * Mat4<T>::MATRIX_SIZE]), vect),
|
||||
Dot(*reinterpret_cast<const Vec4<T>*>(&mat[3 * Mat4<T>::MATRIX_SIZE]), vect),
|
||||
};
|
||||
}
|
||||
|
||||
Mat4f Dot(const Mat4f& mat, const Mat4f& other);
|
||||
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() {
|
||||
@@ -109,5 +142,10 @@ Mat4<T> Transpose(const Mat4<T>& mat) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user