Skip to content
Permalink
Browse files

WiimoteEmu: Remove redundant Matrix library and use the one in Common.

  • Loading branch information...
jordan-woyak committed Jan 29, 2019
1 parent b9a00a4 commit 69b617ce766e5b1b8956a29f7a8caeed21f6024d
@@ -4,10 +4,12 @@

#include "Common/Matrix.h"

#include <cmath>
#include <algorithm>
#include <cmath>

inline void MatrixMul(int n, const float* a, const float* b, float* result)
namespace
{
void MatrixMul(int n, const float* a, const float* b, float* result)
{
for (int i = 0; i < n; ++i)
{
@@ -22,7 +24,10 @@ inline void MatrixMul(int n, const float* a, const float* b, float* result)
}
}
}
} // namespace

namespace Common
{
Matrix33 Matrix33::Identity()
{
Matrix33 mtx = {};
@@ -34,8 +39,8 @@ Matrix33 Matrix33::Identity()

Matrix33 Matrix33::RotateX(float rad)
{
float s = sin(rad);
float c = cos(rad);
const float s = sin(rad);
const float c = cos(rad);
Matrix33 mtx = {};
mtx.data[0] = 1;
mtx.data[4] = c;
@@ -47,8 +52,8 @@ Matrix33 Matrix33::RotateX(float rad)

Matrix33 Matrix33::RotateY(float rad)
{
float s = sin(rad);
float c = cos(rad);
const float s = sin(rad);
const float c = cos(rad);
Matrix33 mtx = {};
mtx.data[0] = c;
mtx.data[2] = s;
@@ -60,8 +65,8 @@ Matrix33 Matrix33::RotateY(float rad)

Matrix33 Matrix33::RotateZ(float rad)
{
float s = sin(rad);
float c = cos(rad);
const float s = sin(rad);
const float c = cos(rad);
Matrix33 mtx = {};
mtx.data[0] = c;
mtx.data[1] = -s;
@@ -128,10 +133,10 @@ Matrix44 Matrix44::FromMatrix33(const Matrix33& m33)
return mtx;
}

Matrix44 Matrix44::FromArray(const float mtxArray[16])
Matrix44 Matrix44::FromArray(const std::array<float, 16>& arr)
{
Matrix44 mtx;
std::copy_n(mtxArray, 16, std::begin(mtx.data));
mtx.data = arr;
return mtx;
}

@@ -140,7 +145,7 @@ Matrix44 Matrix44::Translate(const Vec3& vec)
Matrix44 mtx = Matrix44::Identity();
mtx.data[3] = vec.x;
mtx.data[7] = vec.y;
mtx.data[11] = vec.y;
mtx.data[11] = vec.z;
return mtx;
}

@@ -152,7 +157,8 @@ Matrix44 Matrix44::Shear(const float a, const float b)
return mtx;
}

void Matrix44::Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result)
void Matrix44::Multiply(const Matrix44& a, const Matrix44& b, Matrix44* result)
{
MatrixMul(4, a.data.data(), b.data.data(), result.data.data());
MatrixMul(4, a.data.data(), b.data.data(), result->data.data());
}
} // namespace Common
@@ -9,11 +9,21 @@
// Tiny matrix/vector library.
// Used for things like Free-Look in the gfx backend.

namespace Common
{
union Vec3
{
Vec3() {}
Vec3() = default;
Vec3(float _x, float _y, float _z) : data{_x, _y, _z} {}

Vec3& operator+=(const Vec3& rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}

std::array<float, 3> data = {};

struct
@@ -24,6 +34,11 @@ union Vec3
};
};

inline Vec3 operator+(Vec3 lhs, const Vec3& rhs)
{
return lhs += rhs;
}

class Matrix33
{
public:
@@ -66,16 +81,16 @@ class Matrix44
public:
static Matrix44 Identity();
static Matrix44 FromMatrix33(const Matrix33& m33);
static Matrix44 FromArray(const float mtxArray[16]);
static Matrix44 FromArray(const std::array<float, 16>& arr);

static Matrix44 Translate(const Vec3& vec);
static Matrix44 Shear(const float a, const float b = 0);

static void Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result);
static void Multiply(const Matrix44& a, const Matrix44& b, Matrix44* result);

Matrix44& operator*=(const Matrix44& rhs)
{
Multiply(Matrix44(*this), rhs, *this);
Multiply(Matrix44(*this), rhs, this);
return *this;
}

@@ -85,4 +100,5 @@ class Matrix44
inline Matrix44 operator*(Matrix44 lhs, const Matrix44& rhs)
{
return lhs *= rhs;
}
}
} // namespace Common
@@ -451,7 +451,6 @@
<ClInclude Include="HW\WiimoteEmu\Extension\Nunchuk.h" />
<ClInclude Include="HW\WiimoteEmu\Extension\Turntable.h" />
<ClInclude Include="HW\WiimoteEmu\I2CBus.h" />
<ClInclude Include="HW\WiimoteEmu\MatrixMath.h" />
<ClInclude Include="HW\WiimoteEmu\MotionPlus.h" />
<ClInclude Include="HW\WiimoteEmu\Speaker.h" />
<ClInclude Include="HW\WiimoteEmu\WiimoteEmu.h" />
@@ -616,4 +615,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
@@ -1221,9 +1221,6 @@
<ClInclude Include="HW\WiimoteEmu\Encryption.h">
<Filter>HW %28Flipper/Hollywood%29\Wiimote\Emu</Filter>
</ClInclude>
<ClInclude Include="HW\WiimoteEmu\MatrixMath.h">
<Filter>HW %28Flipper/Hollywood%29\Wiimote\Emu</Filter>
</ClInclude>
<ClInclude Include="HW\WiimoteEmu\WiimoteEmu.h">
<Filter>HW %28Flipper/Hollywood%29\Wiimote\Emu</Filter>
</ClInclude>
@@ -4,10 +4,12 @@

#include "Core/HW/WiimoteEmu/Camera.h"

#include <cmath>

#include "Common/BitUtils.h"
#include "Common/ChunkFile.h"
#include "Common/Matrix.h"
#include "Core/HW/WiimoteCommon/WiimoteReport.h"
#include "Core/HW/WiimoteEmu/MatrixMath.h"

namespace WiimoteEmu
{
@@ -40,30 +42,25 @@ int CameraLogic::BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in)
void CameraLogic::Update(const ControllerEmu::Cursor::StateData& cursor,
const NormalizedAccelData& accel, bool sensor_bar_on_top)
{
double nsin, ncos;
double nsin;

// Ugly code to figure out the wiimote's current angle.
// TODO: Kill this.
double ax = accel.x;
double az = accel.z;
const double len = sqrt(ax * ax + az * az);
const double len = std::sqrt(ax * ax + az * az);

if (len)
{
ax /= len;
az /= len; // normalizing the vector
nsin = ax;
ncos = az;
}
else
{
nsin = 0;
ncos = 1;
}

const double ir_sin = nsin;
const double ir_cos = ncos;

static constexpr int camWidth = 1024;
static constexpr int camHeight = 768;
static constexpr double bndleft = 0.78820266;
@@ -73,7 +70,7 @@ void CameraLogic::Update(const ControllerEmu::Cursor::StateData& cursor,

constexpr int NUM_POINTS = 4;

std::array<Vertex, NUM_POINTS> v;
std::array<Common::Vec3, NUM_POINTS> v;

for (auto& vtx : v)
{
@@ -95,23 +92,17 @@ void CameraLogic::Update(const ControllerEmu::Cursor::StateData& cursor,
v[2].x -= (cursor.z * 0.5 + 1) * dist2;
v[3].x += (cursor.z * 0.5 + 1) * dist2;

#define printmatrix(m) \
PanicAlert("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n", m[0][0], m[0][1], m[0][2], \
m[0][3], m[1][0], m[1][1], m[1][2], m[1][3], m[2][0], m[2][1], m[2][2], m[2][3], \
m[3][0], m[3][1], m[3][2], m[3][3])
Matrix rot, tot;
static Matrix scale;
MatrixScale(scale, 1, camWidth / camHeight, 1);
MatrixRotationByZ(rot, ir_sin, ir_cos);
MatrixMultiply(tot, scale, rot);
const auto scale = Common::Matrix33::Scale({1, camWidth / camHeight, 1});
const auto rot = Common::Matrix33::RotateZ(std::asin(nsin));
const auto tot = scale * rot;

u16 x[NUM_POINTS], y[NUM_POINTS];
memset(x, 0xFF, sizeof(x));
memset(y, 0xFF, sizeof(y));

for (std::size_t i = 0; i < v.size(); i++)
{
MatrixTransformVertex(tot, v[i]);
v[i] = tot * v[i];

if ((v[i].x < -1) || (v[i].x > 1) || (v[i].y < -1) || (v[i].y > 1))
continue;

0 comments on commit 69b617c

Please sign in to comment.
You can’t perform that action at this time.