math classes refactor

This commit is contained in:
h4570
2022-08-10 20:51:19 +02:00
parent 0df43e5a08
commit 10e0f52d44
8 changed files with 286 additions and 144 deletions
+29 -110
View File
@@ -20,39 +20,28 @@ class M4x4 {
public:
MATRIX data alignas(sizeof(float) * 16);
static M4x4 Identity;
static const M4x4 Identity;
/** Create matrix with random values */
M4x4() {}
/**
* Create matrix with random values
*/
explicit M4x4();
M4x4(const M4x4& v) { copy(this, v); }
explicit M4x4(const float* v) { copy(this, v); }
inline M4x4(const float& m11, const float& m12, const float& m13,
const float& m14, const float& m21, const float& m22,
const float& m23, const float& m24, const float& m31,
const float& m32, const float& m33, const float& m34,
const float& m41, const float& m42, const float& m43,
const float& m44) {
set(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42,
m43, m44);
}
M4x4(const float& m11, const float& m12, const float& m13, const float& m14,
const float& m21, const float& m22, const float& m23, const float& m24,
const float& m31, const float& m32, const float& m33, const float& m34,
const float& m41, const float& m42, const float& m43, const float& m44);
static void copy(M4x4* out, const float* in);
static void copy(M4x4* out, const M4x4& in) { copy(out, in.data); }
static void copy(M4x4* out, const M4x4& in);
void operator=(const M4x4& v);
Vec4 operator*(const Vec4& v) const;
M4x4 operator*(const M4x4& v) const {
M4x4 result;
cross(result.data, this->data, v.data);
return result;
}
void operator*=(const M4x4& v) { cross(this->data, this->data, v.data); }
float& operator[](const u8& index) { return data[index]; }
M4x4 operator*(const M4x4& v) const;
void operator*=(const M4x4& v);
float& operator[](const u8& index);
/**
* 1 0 0 0
@@ -62,13 +51,7 @@ class M4x4 {
*/
void identity();
/**
* 1 0 0 0
* 0 1 0 0
* 0 0 1 0
* 0 0 0 1
*/
void unit() { identity(); }
inline void unit() { identity(); }
void set(const float& m11, const float& m12, const float& m13,
const float& m14, const float& m21, const float& m22,
@@ -77,86 +60,22 @@ class M4x4 {
const float& m41, const float& m42, const float& m43,
const float& m44);
void translate(const Vec4& v) {
M4x4 temp = M4x4::Identity;
temp.translationX(v.x);
temp.translationY(v.y);
temp.translationZ(v.z);
cross(this->data, temp.data, this->data);
}
void translate(const Vec4& v);
void translateX(const float& v);
void translateY(const float& v);
void translateZ(const float& v);
void translateX(const float& v) {
M4x4 temp = M4x4::Identity;
temp.translationX(v);
cross(this->data, temp.data, this->data);
}
void rotateByAngle(const float& angle, const Vec4& axis);
void rotate(const Vec4& v);
void rotateX(const float& radians);
void rotateY(const float& radians);
void rotateZ(const float& radians);
void translateY(const float& v) {
M4x4 temp = M4x4::Identity;
temp.translationY(v);
cross(this->data, temp.data, this->data);
}
void translateZ(const float& v) {
M4x4 temp = M4x4::Identity;
temp.translationZ(v);
cross(this->data, temp.data, this->data);
}
/** Rotate M4x4. */
void rotate(const Vec4& v) {
M4x4 temp = M4x4::Identity;
temp.rotationZ(v.z);
cross(this->data, temp.data, this->data);
temp.identity();
temp.rotationY(v.y);
cross(this->data, temp.data, this->data);
temp.identity();
temp.rotationX(v.x);
cross(this->data, temp.data, this->data);
}
/** Rotate M4x4 by X. */
void rotateX(const float& radians) {
M4x4 temp = M4x4::Identity;
temp.rotationX(radians);
cross(this->data, temp.data, this->data);
}
/** Rotate M4x4 by Y. */
void rotateY(const float& radians) {
M4x4 temp = M4x4::Identity;
temp.rotationY(radians);
cross(this->data, temp.data, this->data);
}
/** Rotate M4x4 by Z. */
void rotateZ(const float& radians) {
M4x4 temp = M4x4::Identity;
temp.rotationZ(radians);
cross(this->data, temp.data, this->data);
}
/** Rotate M4x4 by angle. */
void rotateByAngle(const float& angle, const Vec4& axis) {
M4x4 temp;
temp.rotationByAngle(angle, axis);
cross(this->data, temp.data, this->data);
}
void scale(const float& v) { scale(Vec4(v, v, v, 1.0F)); }
void scaleX(const float& v) { scale(Vec4(v, 1.0F, 1.0F, 1.0F)); }
void scaleY(const float& v) { scale(Vec4(1.0F, v, 1.0F, 1.0F)); }
void scaleZ(const float& v) { scale(Vec4(1.0F, 1.0F, v, 1.0F)); }
void scale(const Vec4& v) {
M4x4 temp = M4x4::Identity;
temp.setScale(v);
cross(this->data, temp.data, this->data);
}
void scale(const float& v);
void scale(const Vec4& v);
void scaleX(const float& v);
void scaleY(const float& v);
void scaleZ(const float& v);
/** Create perspective projection M4x4 (gluPerspective) */
static M4x4 perspective(const float& fov, const float& width,
+4 -2
View File
@@ -26,9 +26,11 @@ class Math {
constexpr static float HALF_PI = 1.5707963267948966192313216916398F;
static float cos(float x);
static float asin(float x);
static float atan2(float y, float x);
static float mod(float x, float y);
static inline float sin(float x) { return cos(x - HALF_PI); }
static inline float tan(float x) { return sin(x) / cos(x); }
static float acos(float x);
static float sin(float x);
static float tan(float x);
static float invSqrt(float x);
static bool equalf(const float& a, const float& b,
const float& epsilon = 0.00001F);
+2 -3
View File
@@ -25,9 +25,8 @@ class Plane {
~Plane();
void update(const Vec4& a, const Vec4& b, const Vec4& c);
inline float distanceTo(const Vec4& t_vec) const {
return this->distance + this->normal.innerProduct(t_vec);
}
float distanceTo(const Vec4& t_vec) const;
void print() const;
void print(const char* name) const;
+5 -13
View File
@@ -63,6 +63,9 @@ class Vec4 {
void operator*=(const Vec4& v);
void operator/=(const float& v);
/** (0,0,0,1) */
void unit();
inline void set(const Vec4& v) { copy(this, v.xyzw); }
inline void set(const float* v) { copy(this, v); }
void set(const float& x, const float& y = 0.0F, const float& z = 0.0F,
@@ -94,13 +97,7 @@ class Vec4 {
* @param max Opposite max vertex (ex. far, right, up vertex of bounding
* box)
*/
u8 collidesBox(const Vec4& min, const Vec4& max) const {
return ((this->x <= max.x && this->x >= min.x) &&
(this->y < max.y && this->y >= min.y) &&
(this->z <= max.z && this->z >= min.z))
? 1
: 0;
}
bool collidesBox(const Vec4& min, const Vec4& max) const;
/**
* Checks if this vector is on given box (this->y >= box.y)
@@ -109,12 +106,7 @@ class Vec4 {
* @param max Opposite max vertex (ex. far, right, up vertex of bounding
* box)
*/
u8 isOnBox(const Vec4& min, const Vec4& max) const {
return ((this->x <= max.x && this->x >= min.x) && (this->y >= max.y) &&
(this->z <= max.z && this->z >= min.z))
? 1
: 0;
}
bool isOnBox(const Vec4& min, const Vec4& max) const;
/** Check if given triangle should be backface culled */
static u8 shouldBeBackfaceCulled(const Vec4* cameraPos, const Vec4* v0,