123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- #include "math/Matrix.hpp"
- #include "math/Math.hpp"
- Core::Matrix::Matrix() {
- unit();
- }
- Core::Matrix& Core::Matrix::unit() {
- return translateTo(Vector3(0.0f, 0.0f, 0.0f));
- }
- Core::Matrix& Core::Matrix::set(int index, const Vector4& v) {
- data[index] = v;
- return *this;
- }
- Core::Matrix Core::Matrix::transpose() {
- Matrix m;
- for(int x = 0; x < 4; x++) {
- for(int y = 0; y < 4; y++) {
- m.data[x][y] = data[y][x];
- }
- }
- return m;
- }
- const float* Core::Matrix::getValues() const {
- return &(data[0][0]);
- }
- Core::Matrix& Core::Matrix::operator*=(const Matrix& m) {
- data[0] = data[0][0] * m.data[0] + data[0][1] * m.data[1] +
- data[0][2] * m.data[2] + data[0][3] * m.data[3];
- data[1] = data[1][0] * m.data[0] + data[1][1] * m.data[1] +
- data[1][2] * m.data[2] + data[1][3] * m.data[3];
- data[2] = data[2][0] * m.data[0] + data[2][1] * m.data[1] +
- data[2][2] * m.data[2] + data[2][3] * m.data[3];
- data[3] = data[3][0] * m.data[0] + data[3][1] * m.data[1] +
- data[3][2] * m.data[2] + data[3][3] * m.data[3];
- return *this;
- }
- Core::Matrix Core::Matrix::operator*(const Matrix& other) const {
- Matrix m = *this;
- m *= other;
- return m;
- }
- Core::Vector3 Core::Matrix::operator*(const Vector3& v) const {
- Vector4 v4(v[0], v[1], v[2], 1.0f);
- return Vector3(data[0].dot(v4), data[1].dot(v4), data[2].dot(v4)) *
- (1.0f / data[3].dot(v4));
- }
- Core::Matrix& Core::Matrix::scale(const Vector3& v) {
- data[0] *= v[0];
- data[1] *= v[1];
- data[2] *= v[2];
- return *this;
- }
- Core::Matrix& Core::Matrix::scale(float s) {
- return scale(Vector3(s, s, s));
- }
- Core::Matrix& Core::Matrix::translate(const Vector3& v) {
- return translateX(v[0]).translateY(v[1]).translateZ(v[2]);
- }
- Core::Matrix& Core::Matrix::translateX(float tx) {
- data[0] += data[3] * tx;
- return *this;
- }
- Core::Matrix& Core::Matrix::translateY(float ty) {
- data[1] += data[3] * ty;
- return *this;
- }
- Core::Matrix& Core::Matrix::translateZ(float tz) {
- data[2] += data[3] * tz;
- return *this;
- }
- Core::Matrix& Core::Matrix::translateTo(const Vector3& v) {
- data[0] = Vector4(1.0f, 0.0f, 0.0f, v[0]);
- data[1] = Vector4(0.0f, 1.0f, 0.0f, v[1]);
- data[2] = Vector4(0.0f, 0.0f, 1.0f, v[2]);
- data[3] = Vector4(0.0f, 0.0f, 0.0f, 1.0f);
- return *this;
- }
- Core::Matrix& Core::Matrix::rotate(float degrees, int a, int b) {
- float sin = 0.0f;
- float cos = 0.0f;
- Math::sinCos(Core::Math::degreeToRadian(degrees), sin, cos);
- Vector4 v = data[a];
- data[a] = cos * data[a] - sin * data[b];
- data[b] = sin * v + cos * data[b];
- return *this;
- }
- Core::Matrix& Core::Matrix::rotateX(float degrees) {
- return rotate(degrees, 1, 2);
- }
- Core::Matrix& Core::Matrix::rotateY(float degrees) {
- return rotate(-degrees, 0, 2);
- }
- Core::Matrix& Core::Matrix::rotateZ(float degrees) {
- return rotate(degrees, 0, 1);
- }
- Core::Matrix& Core::Matrix::rotate(const Quaternion& q) {
- Vector3 a = q * Vector3(data[0][0], data[1][0], data[2][0]);
- Vector3 b = q * Vector3(data[0][1], data[1][1], data[2][1]);
- Vector3 c = q * Vector3(data[0][2], data[1][2], data[2][2]);
- Vector3 d = q * Vector3(data[0][3], data[1][3], data[2][3]);
- set(0, Vector4(a[0], b[0], c[0], d[0]));
- set(1, Vector4(a[1], b[1], c[1], d[1]));
- set(2, Vector4(a[2], b[2], c[2], d[2]));
- return *this;
- }
|