diff options
author | vimene <vincent.menegaux@gmail.com> | 2024-12-31 03:40:14 +0100 |
---|---|---|
committer | vimene <vincent.menegaux@gmail.com> | 2024-12-31 03:40:14 +0100 |
commit | cc6fb8c33637566a7b116d46440e6063f016deea (patch) | |
tree | 5cc83f84525507994add57df7dd974e6611d675a /src/math/vector.h | |
parent | 6b765a85cf81bf4b7162e4c9280dd4054581c611 (diff) | |
download | engine-cc6fb8c33637566a7b116d46440e6063f016deea.tar.gz |
various improvements
- added quaternions and rewrote all rotations to use them
- added transforms to put all object transforms in a single place
- added Wavefront .obj file parser
- removed frame buffer's abstract class
- improved vectors, matrices, triangles, vertices and vertices data by putting all code in header file
- added vector's operations
- changed from NULL to nullptr
- miscellaneous improvements
Diffstat (limited to 'src/math/vector.h')
-rw-r--r-- | src/math/vector.h | 266 |
1 files changed, 208 insertions, 58 deletions
diff --git a/src/math/vector.h b/src/math/vector.h index 82e1513..b26c5fe 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -1,72 +1,222 @@ #ifndef MATH_VECTOR_H #define MATH_VECTOR_H +#include <cmath> +#include "math/quat.h" + namespace engine::math { -class Vector2 { - public: - float x, y; - - Vector2(); - Vector2(float x, float y); - bool operator==(Vector2 other) const; - bool operator!=(Vector2 other) const; - Vector2 operator+() const; - Vector2 operator-() const; - Vector2 operator+(Vector2 other) const; - Vector2 operator-(Vector2 other) const; - float det(Vector2 other) const; - Vector2 round() const; +struct Vector2 { + float x, y; + + constexpr bool operator==(const Vector2& other) const & { + return x == other.x && y == other.y; + } + + constexpr bool operator!=(const Vector2& other) const & { + return !(*this == other); + } + + constexpr Vector2 operator+() const & { + return *this; + } + + constexpr Vector2 operator-() const & { + return { -x, -y }; + } + + constexpr Vector2 operator+(const Vector2& other) const & { + return { x + other.x, y + other.y }; + } + + constexpr Vector2 operator-(const Vector2& other) const & { + return *this + (-other); + } + + constexpr float det(const Vector2& other) const & { + return this->x * other.y - other.x * this->y; + } + + constexpr Vector2 round() const & { + return { std::round(x), std::round(y) }; + } + + constexpr Vector2 mul_term(const Vector2& other) const & { + return { x * other.x, y * other.y }; + } }; -Vector2 operator*(float n, Vector2 other); -Vector2 operator*(Vector2 other, float n); -Vector2 operator/(Vector2 other, float n); - -class Vector3 { - public: - float x, y, z; - - Vector3(); - Vector3(float x, float y, float z); - bool operator==(Vector3 other) const; - bool operator!=(Vector3 other) const; - Vector3 operator+() const; - Vector3 operator-() const; - Vector3 operator+(Vector3 other) const; - Vector3 operator-(Vector3 other) const; - Vector3 round() const; - Vector3 cross(Vector3 other) const; +constexpr Vector2 operator*(float n, const Vector2& other) { + return { n * other.x, n * other.y }; +} + +constexpr Vector2 operator*(const Vector2& other, float n) { + return n * other; +} + +constexpr Vector2 operator/(const Vector2& other, float n) { + return { other.x / n, other.y / n }; +} + +constexpr Vector2 operator+(const Vector2& other, float n) { + return { other.x + n, other.y + n }; +} + +constexpr Vector2 operator-(const Vector2& other, float n) { + return { other.x - n, other.y - n }; +} + +struct Vector3; +constexpr Vector3 operator*(float n, const Vector3& other); +constexpr Vector3 operator/(const Vector3& other, float n); + +struct Vector3 { + static constexpr Vector3 bilerp(const Vector3& v1, const Vector3& v2, const Vector3& v3, float b0, float b1) { + return b0 * v1 + b1 * v2 + (1.f - b0 - b1) * v3; + } + + float x, y, z; + + constexpr bool operator==(const Vector3& other) const & { + return x == other.x && y == other.y && z == other.z; + } + + constexpr bool operator!=(const Vector3& other) const & { + return !(*this == other); + } + + constexpr Vector3 operator+() const & { + return *this; + } + + constexpr Vector3 operator-() const & { + return { -x, -y, -z }; + } + + constexpr Vector3 operator+(const Vector3& other) const & { + return { x + other.x, y + other.y, z + other.z }; + } + + constexpr Vector3 operator-(const Vector3& other) const & { + return *this + (-other); + } + + constexpr Vector3 round() { + return { std::round(x), std::round(y), std::round(z) }; + } + + constexpr Vector3 cross(const Vector3& other) const & { + return { + y * other.z - z * other.y, + z * other.x - x * other.z, + x * other.y - y * other.x + }; + } + + constexpr Vector3 rot(const Quaternion& q) const & { + return { + (2.f * (q.w * q.w + q.x * q.x) - 1.f) * x + (2.f * (q.x * q.y - q.w * q.z) ) * y + (2.f * (q.x * q.z + q.w * q.y) ) * z, + (2.f * (q.x * q.y + q.w * q.z) ) * x + (2.f * (q.w * q.w + q.y * q.y) - 1.f) * y + (2.f * (q.y * q.z - q.w * q.x) ) * z, + (2.f * (q.x * q.z - q.w * q.y) ) * x + (2.f * (q.y * q.z + q.w * q.x) ) * y + (2.f * (q.w * q.w + q.z * q.z) - 1.f) * z, + }; + } + + constexpr float dot(const Vector3& other) const & { + return x * other.x + y * other.y + z * other.z; + } + + constexpr float length_squared() const & { + return dot(*this); + } + + constexpr float length() const & { + return std::sqrt(length_squared()); + } + + constexpr Vector3 normalize() const & { + return *this / length(); + } }; -Vector3 operator*(float n, Vector3 other); -Vector3 operator*(Vector3 other, float n); -Vector3 operator/(Vector3 other, float n); - -class Vector4 { - public: - float x, y, z, w; - - Vector4(); - Vector4(float x, float y, float z, float w); - Vector4(float x, float y, float z); - Vector4(Vector3 v, float w); - Vector4(Vector3 v); - bool operator==(Vector4 other) const; - bool operator!=(Vector4 other) const; - Vector4 operator+() const; - Vector4 operator-() const; - Vector4 operator+(Vector4 other) const; - Vector4 operator-(Vector4 other) const; - Vector4 round() const; - Vector2 xy() const; - Vector3 xyz() const; - Vector4 div_by_w() const; +constexpr Vector3 operator*(float n, const Vector3& other) { + return { n * other.x, n * other.y, n * other.z }; +} + +constexpr Vector3 operator*(const Vector3& other, float n) { + return n * other; +} + +constexpr Vector3 operator/(float n, const Vector3& other) { + return { n / other.x, n / other.y, n / other.z }; +} + +constexpr Vector3 operator/(const Vector3& other, float n) { + return { other.x / n, other.y / n, other.z / n }; +} + +struct Vector4 { + float x, y, z, w; + + constexpr Vector4() {} + constexpr Vector4(float x, float y, float z, float w) : x{x}, y{y}, z{z}, w{w} {} + constexpr Vector4(float x, float y, float z) : x{x}, y{y}, z{z}, w{1.f} {} + constexpr Vector4(const Vector2& v, float z, float w) : x{v.x}, y{v.y}, z{z}, w{w} {} + constexpr Vector4(const Vector2& v, float z) : x{v.x}, y{v.y}, z{z}, w{1.f} {} + constexpr Vector4(const Vector3& v, float w) : x{v.x}, y{v.y}, z{v.z}, w{w} {} + constexpr Vector4(const Vector3& v) : x{v.x}, y{v.y}, z{v.z}, w{1.f} {} + + constexpr bool operator==(const Vector4& other) const & { + return x == other.x && y == other.y && z == other.z && w == other.w; + } + + constexpr bool operator!=(const Vector4& other) const & { + return !(*this == other); + } + + constexpr Vector4 operator+() const & { + return *this; + } + + constexpr Vector4 operator-() const & { + return { -x, -y, -z, -w }; + } + + constexpr Vector4 operator+(const Vector4& other) const & { + return { x + other.x, y + other.y, z + other.z, w + other.w }; + } + + constexpr Vector4 operator-(const Vector4& other) const & { + return *this + (-other); + } + + constexpr Vector4 round() const & { + return { std::round(x), std::round(y), std::round(z), std::round(w) }; + } + + constexpr Vector2 xy() const & { + return { x, y }; + } + + constexpr Vector3 xyz() const & { + return { x, y, z }; + } + + constexpr Vector4 div_by_w() const & { + return {x / w, y / w, z / w, w}; + } }; -Vector4 operator*(float n, Vector4 other); -Vector4 operator*(Vector4 other, float n); -Vector4 operator/(Vector4 other, float n); +constexpr Vector4 operator*(float n, const Vector4& other) { + return { n * other.x, n * other.y, n * other.z, n * other.w }; +} + +constexpr Vector4 operator*(const Vector4& other, float n) { + return n * other; +} + +constexpr Vector4 operator/(const Vector4& other, float n) { + return { other.x / n, other.y / n, other.z / n, other.w / n }; +} } |