2019-01-29 22:05:51 +00:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "Common/Matrix.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2019-01-29 23:12:06 +00:00
|
|
|
#include <cmath>
|
2019-01-29 22:05:51 +00:00
|
|
|
|
2019-01-29 23:12:06 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void MatrixMul(int n, const float* a, const float* b, float* result)
|
2019-01-29 22:05:51 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < n; ++j)
|
|
|
|
{
|
|
|
|
float temp = 0;
|
|
|
|
for (int k = 0; k < n; ++k)
|
|
|
|
{
|
|
|
|
temp += a[i * n + k] * b[k * n + j];
|
|
|
|
}
|
|
|
|
result[i * n + j] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 23:12:06 +00:00
|
|
|
} // namespace
|
2019-01-29 22:05:51 +00:00
|
|
|
|
2019-01-29 23:12:06 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2019-01-29 22:05:51 +00:00
|
|
|
Matrix33 Matrix33::Identity()
|
|
|
|
{
|
|
|
|
Matrix33 mtx = {};
|
|
|
|
mtx.data[0] = 1.0f;
|
|
|
|
mtx.data[4] = 1.0f;
|
|
|
|
mtx.data[8] = 1.0f;
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix33 Matrix33::RotateX(float rad)
|
|
|
|
{
|
2019-01-29 23:12:06 +00:00
|
|
|
const float s = sin(rad);
|
|
|
|
const float c = cos(rad);
|
2019-01-29 22:05:51 +00:00
|
|
|
Matrix33 mtx = {};
|
|
|
|
mtx.data[0] = 1;
|
|
|
|
mtx.data[4] = c;
|
|
|
|
mtx.data[5] = -s;
|
|
|
|
mtx.data[7] = s;
|
|
|
|
mtx.data[8] = c;
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix33 Matrix33::RotateY(float rad)
|
|
|
|
{
|
2019-01-29 23:12:06 +00:00
|
|
|
const float s = sin(rad);
|
|
|
|
const float c = cos(rad);
|
2019-01-29 22:05:51 +00:00
|
|
|
Matrix33 mtx = {};
|
|
|
|
mtx.data[0] = c;
|
|
|
|
mtx.data[2] = s;
|
|
|
|
mtx.data[4] = 1;
|
|
|
|
mtx.data[6] = -s;
|
|
|
|
mtx.data[8] = c;
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix33 Matrix33::RotateZ(float rad)
|
|
|
|
{
|
2019-01-29 23:12:06 +00:00
|
|
|
const float s = sin(rad);
|
|
|
|
const float c = cos(rad);
|
2019-01-29 22:05:51 +00:00
|
|
|
Matrix33 mtx = {};
|
|
|
|
mtx.data[0] = c;
|
|
|
|
mtx.data[1] = -s;
|
|
|
|
mtx.data[3] = s;
|
|
|
|
mtx.data[4] = c;
|
|
|
|
mtx.data[8] = 1;
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix33 Matrix33::Scale(const Vec3& vec)
|
|
|
|
{
|
|
|
|
Matrix33 mtx = {};
|
|
|
|
mtx.data[0] = vec.x;
|
|
|
|
mtx.data[4] = vec.y;
|
|
|
|
mtx.data[8] = vec.z;
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Matrix33::Multiply(const Matrix33& a, const Matrix33& b, Matrix33* result)
|
|
|
|
{
|
|
|
|
MatrixMul(3, a.data.data(), b.data.data(), result->data.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Matrix33::Multiply(const Matrix33& a, const Vec3& vec, Vec3* result)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
result->data[i] = 0;
|
|
|
|
|
|
|
|
for (int k = 0; k < 3; ++k)
|
|
|
|
{
|
|
|
|
result->data[i] += a.data[i * 3 + k] * vec.data[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix44 Matrix44::Identity()
|
|
|
|
{
|
|
|
|
Matrix44 mtx = {};
|
|
|
|
mtx.data[0] = 1.0f;
|
|
|
|
mtx.data[5] = 1.0f;
|
|
|
|
mtx.data[10] = 1.0f;
|
|
|
|
mtx.data[15] = 1.0f;
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix44 Matrix44::FromMatrix33(const Matrix33& m33)
|
|
|
|
{
|
|
|
|
Matrix44 mtx;
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 3; ++j)
|
|
|
|
{
|
|
|
|
mtx.data[i * 4 + j] = m33.data[i * 3 + j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
mtx.data[i * 4 + 3] = 0;
|
|
|
|
mtx.data[i + 12] = 0;
|
|
|
|
}
|
|
|
|
mtx.data[15] = 1.0f;
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
2019-01-29 23:12:06 +00:00
|
|
|
Matrix44 Matrix44::FromArray(const std::array<float, 16>& arr)
|
2019-01-29 22:05:51 +00:00
|
|
|
{
|
|
|
|
Matrix44 mtx;
|
2019-01-29 23:12:06 +00:00
|
|
|
mtx.data = arr;
|
2019-01-29 22:05:51 +00:00
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix44 Matrix44::Translate(const Vec3& vec)
|
|
|
|
{
|
|
|
|
Matrix44 mtx = Matrix44::Identity();
|
|
|
|
mtx.data[3] = vec.x;
|
|
|
|
mtx.data[7] = vec.y;
|
2019-01-29 23:12:06 +00:00
|
|
|
mtx.data[11] = vec.z;
|
2019-01-29 22:05:51 +00:00
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix44 Matrix44::Shear(const float a, const float b)
|
|
|
|
{
|
|
|
|
Matrix44 mtx = Matrix44::Identity();
|
|
|
|
mtx.data[2] = a;
|
|
|
|
mtx.data[6] = b;
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
2019-01-29 23:12:06 +00:00
|
|
|
void Matrix44::Multiply(const Matrix44& a, const Matrix44& b, Matrix44* result)
|
2019-01-29 22:05:51 +00:00
|
|
|
{
|
2019-01-29 23:12:06 +00:00
|
|
|
MatrixMul(4, a.data.data(), b.data.data(), result->data.data());
|
2019-01-29 22:05:51 +00:00
|
|
|
}
|
2019-01-29 23:12:06 +00:00
|
|
|
} // namespace Common
|