|
|
|
@ -6,6 +6,9 @@
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <libgba-sprite-engine/matrixfx.h>
|
|
|
|
|
#include <libgba-sprite-engine/math.h>
|
|
|
|
|
#include <libgba-sprite-engine/mesh.h>
|
|
|
|
|
#include <libgba-sprite-engine/camera.h>
|
|
|
|
|
#include <libgba-sprite-engine/gba_engine.h>
|
|
|
|
|
|
|
|
|
|
class MatrixFxSuite : public ::testing::Test {
|
|
|
|
|
protected:
|
|
|
|
@ -21,7 +24,7 @@ INLINE float rnd2(float val) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void assertMatrix(MatrixFx expected, MatrixFx actual) {
|
|
|
|
|
void assertMatrix(MatrixFx expected, MatrixFx actual, std::string matrixName) {
|
|
|
|
|
for(int i = 0; i < MATRIX_DIMENSION; i++) {
|
|
|
|
|
auto expect = expected.mAt(i);
|
|
|
|
|
auto act = actual.mAt(i);
|
|
|
|
@ -30,7 +33,82 @@ void assertMatrix(MatrixFx expected, MatrixFx actual) {
|
|
|
|
|
float expectFl = rnd2(fx2float(expect));
|
|
|
|
|
float actFl = rnd2(fx2float(act));
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(expectFl, actFl) << "M[" << i << "] does not match: (exp, act) " << expect << ", " << act << " - floats: " << expectFl << ", " << actFl;
|
|
|
|
|
ASSERT_EQ(expectFl, actFl) << matrixName << "[" << i << "] does not match: (exp, act) " << expect << ", " << act << " - floats: " << expectFl << ", " << actFl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(MatrixFxSuite, RotationMatriches) {
|
|
|
|
|
auto result = MatrixFx::rotationZ(0);
|
|
|
|
|
auto expectedIdMatrix = MatrixFx(float2fx(1.0), 0, 0, 0, 0, float2fx(1.0), 0, 0, 0, 0, float2fx(1.0), 0, 0, 0, 0, float2fx(1.0));
|
|
|
|
|
assertMatrix(expectedIdMatrix, result, "rotz");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(MatrixFxSuite, RotationYawPitchRoll) {
|
|
|
|
|
auto expectedIdMatrix = MatrixFx(float2fx(1.0), 0, 0, 0, 0, float2fx(1.0), 0, 0, 0, 0, float2fx(1.0), 0, 0, 0, 0, float2fx(1.0));
|
|
|
|
|
auto rotYwaPitchRoll = MatrixFx::rotationYawPitchRoll(0, 0, 0);
|
|
|
|
|
assertMatrix(expectedIdMatrix, rotYwaPitchRoll, "rotywa");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(MatrixFxSuite, MeshToTransformMatrix_IntegrationTest) {
|
|
|
|
|
// source:
|
|
|
|
|
Mesh cube;
|
|
|
|
|
cube.add(VectorFx(-1, 1, 1));
|
|
|
|
|
cube.add(VectorFx(1, 1, 1));
|
|
|
|
|
cube.add(VectorFx(-1, -1, 1));
|
|
|
|
|
cube.add(VectorFx(-1, -1, -1));
|
|
|
|
|
cube.add(VectorFx(-1, 1, -1));
|
|
|
|
|
cube.add(VectorFx(1, 1, -1));
|
|
|
|
|
cube.add(VectorFx(1, -1, 1));
|
|
|
|
|
cube.add(VectorFx(-1, -1, -1));
|
|
|
|
|
|
|
|
|
|
auto currentCamera = Camera(VectorFx::fromInt(0, 0, 10), VectorFx::fromInt(0, 0, 0));
|
|
|
|
|
auto viewMatrix = MatrixFx::lookAtLH(currentCamera.getPosition(), currentCamera.getTarget(), VectorFx::up());
|
|
|
|
|
|
|
|
|
|
auto projectionMatrix = MatrixFx::perspectiveFovLH(float2fx(0.78), fxdiv(GBA_SCREEN_WIDTH_FX, GBA_SCREEN_HEIGHT_FX), float2fx(0.01), ONE);
|
|
|
|
|
auto expectedProjectionMatrix = MatrixFx(float2fx(1.64f), 0, 0, 0, 0, float2fx(2.55f), 0, 0, 0, 0, float2fx(1.05), float2fx(1.05), 0, 0, float2fx(-0.05), 0);
|
|
|
|
|
assertMatrix(expectedProjectionMatrix, projectionMatrix, "project");
|
|
|
|
|
|
|
|
|
|
auto expectedIdMatrix = MatrixFx(float2fx(1.0), 0, 0, 0, 0, float2fx(1.0), 0, 0, 0, 0, float2fx(1.0), 0, 0, 0, 0, float2fx(1.0));
|
|
|
|
|
auto rotYwaPitchRoll = MatrixFx::rotationYawPitchRoll(cube.roty(), cube.rotx(), cube.rotz());
|
|
|
|
|
assertMatrix(expectedIdMatrix, rotYwaPitchRoll, "rotywa");
|
|
|
|
|
auto translatedPos = MatrixFx::translation(cube.position());
|
|
|
|
|
assertMatrix(expectedIdMatrix, translatedPos, "translpos");
|
|
|
|
|
|
|
|
|
|
auto worldMatrix = rotYwaPitchRoll * translatedPos;
|
|
|
|
|
assertMatrix(expectedIdMatrix, worldMatrix, "worldmatrix");
|
|
|
|
|
|
|
|
|
|
auto transformMatrix = worldMatrix * viewMatrix * projectionMatrix;
|
|
|
|
|
auto expectedTransformMatrix = MatrixFx(float2fx(-1.67), 0, 0, 0, 0, float2fx(2.55), 0, 0, 0, 0, float2fx(-1.0), float2fx(-1.0), 0, 0, float2fx(9.85), float2fx(9.75));
|
|
|
|
|
assertMatrix(expectedTransformMatrix, transformMatrix, "transfomatrix");
|
|
|
|
|
|
|
|
|
|
auto coord = *cube.vertices()[0].get();
|
|
|
|
|
auto point = MatrixFx::transformCoordinates(coord, transformMatrix);
|
|
|
|
|
ASSERT_EQ(fx2float(point.x()), 0.125f);
|
|
|
|
|
ASSERT_EQ(fx2float(point.y()), 0.25f);
|
|
|
|
|
ASSERT_EQ(fx2float(point.z()), 1.00f);
|
|
|
|
|
|
|
|
|
|
auto x = fxmul(point.x(), GBA_SCREEN_WIDTH_FX) + fxdiv(GBA_SCREEN_WIDTH_FX, int2fx(2));
|
|
|
|
|
auto y = fxmul(-point.y(), GBA_SCREEN_HEIGHT_FX) + fxdiv(GBA_SCREEN_HEIGHT_FX, int2fx(2));
|
|
|
|
|
ASSERT_EQ(fx2float(x), 150);
|
|
|
|
|
ASSERT_EQ(fx2float(y), 40);
|
|
|
|
|
|
|
|
|
|
// dest in Babylon - dest according to for loop below (should print something roughly similar)
|
|
|
|
|
/*
|
|
|
|
|
* 163, 36 - 150,40
|
|
|
|
|
* 76, 36 - 60,40
|
|
|
|
|
* 163, 123 - 150,140
|
|
|
|
|
* 155, 115 - 93,26
|
|
|
|
|
* 155, 44 - 93,115
|
|
|
|
|
* 84, 44 - 172,115
|
|
|
|
|
* 76, 123 - 60,140
|
|
|
|
|
* 84, 115 - 93,26
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
for(auto& vertex : cube.vertices()) {
|
|
|
|
|
auto point = MatrixFx::transformCoordinates(*vertex.get(), transformMatrix);
|
|
|
|
|
auto x = fxmul(point.x(), GBA_SCREEN_WIDTH_FX) + fxdiv(GBA_SCREEN_WIDTH_FX, int2fx(2));
|
|
|
|
|
auto y = fxmul(-point.y(), GBA_SCREEN_HEIGHT_FX) + fxdiv(GBA_SCREEN_HEIGHT_FX, int2fx(2));
|
|
|
|
|
std::cout << "plotting (" << fx2int(x) << "," << fx2int(y) << ")" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -64,7 +142,7 @@ TEST_F(MatrixFxSuite, lookAtLH_TestData) {
|
|
|
|
|
auto result = MatrixFx::lookAtLH(eye, target, up);
|
|
|
|
|
auto expected = MatrixFx(-257, 0, 0, 0, 0, 256, 0, 0, 0, 0, -250, 0, 0, 0, 2500, 256);
|
|
|
|
|
|
|
|
|
|
assertMatrix(expected, result);
|
|
|
|
|
assertMatrix(expected, result, "M");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -92,5 +170,5 @@ TEST_F(MatrixFxSuite, PerspectiveFovLH_TestData) {
|
|
|
|
|
auto result = MatrixFx::perspectiveFovLH(float2fx(0.78), float2fx(1.6), float2fx(0.01), float2fx(1));
|
|
|
|
|
auto expected = MatrixFx::fromFloat(1.565f, 0, 0, 0, 0, 2.505f, 0, 0, 0, 0, 1.005f, 1, 0, 0, -0.005f, 0);
|
|
|
|
|
|
|
|
|
|
assertMatrix(expected, result);
|
|
|
|
|
assertMatrix(expected, result, "M");
|
|
|
|
|
}
|
|
|
|
|