Started to use matricies to handle transformations and the camera Added Vector3 and Matrix3x3 classes and improved Vector2
103 lines
3.9 KiB
C++
103 lines
3.9 KiB
C++
#pragma once
|
|
|
|
#include "vector2.h"
|
|
|
|
#include <ostream>
|
|
|
|
template <typename T>
|
|
class Vector3 {
|
|
public:
|
|
Vector3() = default;
|
|
|
|
Vector3(T x, T y, T z) : m_x(x), m_y(y), m_z(z) {}
|
|
template <typename X, typename Y, typename Z>
|
|
Vector3(X x, Y y, Z z) :
|
|
m_x(static_cast<T>(x)),
|
|
m_y(static_cast<T>(y)),
|
|
m_z(static_cast<T>(z)) {}
|
|
|
|
template <typename A, typename B>
|
|
Vector3(Vector2<A> xy, B z) :
|
|
m_x(static_cast<T>(xy[0])),
|
|
m_y(static_cast<T>(xy[1])),
|
|
m_z(static_cast<T>(z)) {}
|
|
template <typename A, typename B>
|
|
Vector3(A x, Vector2<B> yz) :
|
|
m_x(static_cast<T>(x)),
|
|
m_y(static_cast<T>(yz[0])),
|
|
m_z(static_cast<T>(yz[1])) {}
|
|
|
|
Vector3(const Vector3<T> &other) = default;
|
|
template <typename A>
|
|
Vector3(const Vector3<A> &other) :
|
|
m_x(static_cast<T>(other[0])),
|
|
m_y(static_cast<T>(other[1])),
|
|
m_z(static_cast<T>(other[2])) {}
|
|
|
|
T x() { return m_x; }
|
|
T y() { return m_y; }
|
|
T z() { return m_z; }
|
|
|
|
void set_x(T x) { m_x = x; }
|
|
void set_y(T y) { m_y = y; }
|
|
void set_z(T z) { m_z = z; }
|
|
|
|
void set(T x, T y, T z) { m_x = x; m_y = y; m_z = z; }
|
|
|
|
Vector3<T> operator-() const { return Vector3<T>(-m_x, -m_y, -m_z); }
|
|
|
|
Vector3<T> &operator=(const Vector3<T> &other) { m_x = other.m_x; m_y = other.m_y; m_z = other.m_z; return *this; }
|
|
|
|
Vector3<T> operator+(const Vector3<T> &other) const { return Vector3(m_x + other.m_x, m_y + other.m_y, m_z + other.m_z); }
|
|
Vector3<T> &operator+=(const Vector3<T> &other) { m_x += other.m_x; m_y += other.m_y; m_z += other.m_z; return *this; }
|
|
|
|
Vector3<T> operator-(const Vector3<T> &other) const { return Vector3(m_x - other.m_x, m_y - other.m_y, m_z - other.m_z); }
|
|
Vector3<T> &operator-=(const Vector3<T> &other) { m_x -= other.m_x; m_y -= other.m_y; m_z -= other.m_z; return *this; }
|
|
|
|
Vector3<T> operator*(const Vector3<T> &other) const { return Vector3(m_x * other.m_x, m_y * other.m_y, m_z * other.m_z); }
|
|
Vector3<T> &operator*=(const Vector3<T> &other) { m_x *= other.m_x; m_y *= other.m_y; m_z *= other.m_z; return *this; }
|
|
Vector3<T> operator*(const T &s) const { return Vector3(m_x * s, m_y * s, m_z * s); }
|
|
Vector3<T> &operator*=(const T &s) { m_x *= s; m_y *= s; m_z *= s; return *this; }
|
|
|
|
Vector3<T> operator/(const Vector3<T> &other) const { return Vector3(m_x / other.m_x, m_y / other.m_y, m_z / other.m_z); }
|
|
Vector3<T> &operator/=(const Vector3<T> &other) { m_x /= other.m_x; m_y /= other.m_y; m_z /= other.m_z; return *this; }
|
|
Vector3<T> operator/(const T &s) const { return Vector3(m_x / s, m_y / s, m_z / s); }
|
|
Vector3<T> &operator/=(const T &s) { m_x /= s; m_y /= s; m_z /= s; return *this; }
|
|
|
|
bool operator==(const Vector3<T> &other) const { return m_x == other.m_x && m_y == other.m_y && m_z == other.m_z; }
|
|
bool operator!=(const Vector3<T> &other) const { return m_x == other.m_x || m_y != other.m_y || m_z != other.m_z; }
|
|
|
|
T &operator[](const int idx) {
|
|
switch(idx) {
|
|
default:
|
|
case 0:
|
|
return m_x;
|
|
case 1:
|
|
return m_y;
|
|
case 2:
|
|
return m_z;
|
|
}
|
|
}
|
|
const T &operator[](const int idx) const {
|
|
switch(idx) {
|
|
default:
|
|
case 0:
|
|
return m_x;
|
|
case 1:
|
|
return m_y;
|
|
case 2:
|
|
return m_z;
|
|
}
|
|
}
|
|
|
|
friend std::ostream &operator<<(std::ostream &os, const Vector3<T> &vec) {
|
|
os << "Vector3(" << vec.m_x << ", " << vec.m_y << ", " << vec.m_z << ")";
|
|
return os;
|
|
}
|
|
|
|
private:
|
|
T m_x = 0;
|
|
T m_y = 0;
|
|
T m_z = 0;
|
|
};
|
|
|