Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

refactor(wwmath): drop D3DX8 dependency from Matrix3D::Get_Inverse

Olcay Seygan776cd73f7fab
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WWMath/matrix3d.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WWMath/matrix3d.cpp
index f10c5a0ad..6c0e01eca 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WWMath/matrix3d.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WWMath/matrix3d.cpp
@@ -65,7 +65,6 @@
#include "matrix3.h"
#include "matrix4.h"
#include "quat.h"
-#include "D3dx8math.h"
// some static matrices which are sometimes useful
const Matrix3D Matrix3D::Identity
@@ -513,29 +512,17 @@ void Matrix3D::Obj_Look_At(const Vector3 &p,const Vector3 &t,float roll)
*=============================================================================================*/
void Matrix3D::Get_Inverse(Matrix3D & inv) const
{
- // TODO: Implement the general purpose inverse function here (once we need it :-)
- //Get_Orthogonal_Inverse(inv);
-
- Matrix4x4 mat4(*this);
- Matrix4x4 mat4Inv;
-
- float det;
- D3DXMatrixInverse((D3DXMATRIX *)&mat4Inv, &det, (D3DXMATRIX*)&mat4);
-
- inv.Row[0][0]=mat4Inv[0][0];
- inv.Row[0][1]=mat4Inv[0][1];
- inv.Row[0][2]=mat4Inv[0][2];
- inv.Row[0][3]=mat4Inv[0][3];
-
- inv.Row[1][0]=mat4Inv[1][0];
- inv.Row[1][1]=mat4Inv[1][1];
- inv.Row[1][2]=mat4Inv[1][2];
- inv.Row[1][3]=mat4Inv[1][3];
-
- inv.Row[2][0]=mat4Inv[2][0];
- inv.Row[2][1]=mat4Inv[2][1];
- inv.Row[2][2]=mat4Inv[2][2];
- inv.Row[2][3]=mat4Inv[2][3];
+ // Matrix3D is affine: [A | t]. Its inverse is [A^-1 | -A^-1 * t], so the 3x3
+ // Gauss-Jordan inverse we already have is all this needs (was D3DXMatrixInverse).
+ Matrix3x3 a_inv = Matrix3x3(*this).Inverse();
+ Vector3 inv_t = -(a_inv * Get_Translation());
+
+ for (int r = 0; r < 3; r++) {
+ inv.Row[r][0] = a_inv[r][0];
+ inv.Row[r][1] = a_inv[r][1];
+ inv.Row[r][2] = a_inv[r][2];
+ inv.Row[r][3] = inv_t[r];
+ }
}
/***********************************************************************************************
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WWMath/test_inverse.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WWMath/test_inverse.cpp
new file mode 100644
index 000000000..80968e7ce
--- /dev/null
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WWMath/test_inverse.cpp
@@ -0,0 +1,51 @@
+// Self-check for Matrix3D::Get_Inverse, which was reimplemented when the D3DX8
+// dependency was dropped. Run the 'wwmath_selfcheck' target; "OK" means pass.
+
+#include "matrix3d.h"
+#include <assert.h>
+#include <stdio.h>
+
+static void check_identity(const Matrix3D & m, const char * what)
+{
+ for (int r = 0; r < 3; r++) {
+ for (int c = 0; c < 4; c++) {
+ float expected = (r == c) ? 1.0f : 0.0f;
+ float diff = m[r][c] - expected;
+ if (diff < 0.0f) diff = -diff;
+ if (diff > 0.0001f) {
+ printf("FAIL %s: [%d][%d] = %f, expected %f\n", what, r, c, m[r][c], expected);
+ assert(0);
+ }
+ }
+ }
+}
+
+static void check_roundtrip(const Matrix3D & m, const char * what)
+{
+ Matrix3D inv, product;
+ m.Get_Inverse(inv);
+
+ Matrix3D::Multiply(m, inv, &product);
+ check_identity(product, what);
+
+ Matrix3D::Multiply(inv, m, &product);
+ check_identity(product, what);
+}
+
+int main(void)
+{
+ // Rotation about an arbitrary axis plus a translation - the general affine case.
+ Matrix3D rot(Vector3(0.3f, -0.7f, 0.65f), 1.1f);
+ rot.Set_Translation(Vector3(12.0f, -4.5f, 3.25f));
+ check_roundtrip(rot, "rotate+translate");
+
+ // Non-uniform scale: the orthogonal-inverse shortcut gets this one wrong.
+ Matrix3D scale(Vector3(2.0f, 0.0f, 0.0f),
+ Vector3(0.0f, 0.5f, 0.0f),
+ Vector3(0.0f, 0.0f, 4.0f),
+ Vector3(1.0f, 2.0f, 3.0f));
+ check_roundtrip(scale, "scale+translate");
+
+ printf("Matrix3D::Get_Inverse OK\n");
+ return 0;
+}