From 10a936f55d91c332d9d42295412ea85ef9119b84 Mon Sep 17 00:00:00 2001
From: Zeldaboy14 <zeldaboy1999@gmail.com>
Date: Sun, 30 Mar 2025 20:53:40 -0500
Subject: [PATCH] Fix Lightmappedgeneric and vbsp matrix functions

Shiro assisted in this fix, and now we don't need the custom matrixinvert.h functions anymore, because we use built-in engine functions for inversing Parallax corrected cubemaps. LightmappedGeneric has also been updated so mat_specular 0 doesn't break brushs anymore
---
 .../lightmappedgeneric_dx9_helper.cpp         |   4 +-
 sp/src/utils/vbsp/map.cpp                     |  23 ++--
 sp/src/utils/vbsp/matrixinvert.h              | 117 ------------------
 3 files changed, 14 insertions(+), 130 deletions(-)
 delete mode 100644 sp/src/utils/vbsp/matrixinvert.h

diff --git a/sp/src/materialsystem/stdshaders/lightmappedgeneric_dx9_helper.cpp b/sp/src/materialsystem/stdshaders/lightmappedgeneric_dx9_helper.cpp
index 2273db49..edd6f05c 100644
--- a/sp/src/materialsystem/stdshaders/lightmappedgeneric_dx9_helper.cpp
+++ b/sp/src/materialsystem/stdshaders/lightmappedgeneric_dx9_helper.cpp
@@ -805,8 +805,8 @@ void DrawLightmappedGeneric_DX9_Internal(CBaseVSShader *pShader, IMaterialVar**
 			(params[info.m_nBlendModulateTexture]->IsTexture() );
 		bool hasNormalMapAlphaEnvmapMask = IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
 #ifdef PARALLAX_CORRECTED_CUBEMAPS
-		// Parallax cubemaps
-		bool hasParallaxCorrection = params[info.m_nEnvmapParallax]->GetIntValue() > 0;
+		// Parallax cubemaps. Check for envmap because if we don't, white splotchs can appear at certain viewing angles when mat_specular is 0.
+		bool hasParallaxCorrection = params[info.m_nEnvmap]->IsDefined() && params[info.m_nEnvmapParallax]->GetIntValue() > 0;
 #endif
 
 		if ( hasFlashlight && !IsX360() )				
diff --git a/sp/src/utils/vbsp/map.cpp b/sp/src/utils/vbsp/map.cpp
index c479b279..d8bce641 100644
--- a/sp/src/utils/vbsp/map.cpp
+++ b/sp/src/utils/vbsp/map.cpp
@@ -15,9 +15,6 @@
 #include "materialsub.h"
 #include "fgdlib/fgdlib.h"
 #include "manifest.h"
-#ifdef PARALLAX_CORRECTED_CUBEMAPS
-#include "matrixinvert.h"
-#endif
 #ifdef MAPBASE_VSCRIPT
 #include "vscript_vbsp.h"
 #endif
@@ -1657,9 +1654,11 @@ ChunkFileResult_t CMapFile::LoadEntityCallback(CChunkFile *pFile, int nParam)
 		//
 		if (!strcmp("parallax_obb", pClassName))
 		{
-			matrix3x4_t obbMatrix, invObbMatrix;
-			SetIdentityMatrix(obbMatrix);
-			SetIdentityMatrix(invObbMatrix);
+			//Originally was matrix3x2. Now we use built-in functions to the engine instead of a custom invert matrix
+			VMatrix obbMatrix, invObbMatrix;
+			MatrixSetIdentity(obbMatrix);
+			MatrixSetIdentity(invObbMatrix);
+			
 
 			// Get corner and its 3 edges (scaled, local x, y, and z axes)
 			mapbrush_t *brush = &mapbrushes[mapent->firstbrush];
@@ -1714,13 +1713,15 @@ ChunkFileResult_t CMapFile::LoadEntityCallback(CChunkFile *pFile, int nParam)
 				x *= abs(DotProduct(diag, x));
 
 				// Build transformation matrix (what is needed to turn a [0,0,0] - [1,1,1] cube into this brush)
-				MatrixSetColumn(x, 0, obbMatrix);
-				MatrixSetColumn(y, 1, obbMatrix);
-				MatrixSetColumn(z, 2, obbMatrix);
-				MatrixSetColumn(corner, 3, obbMatrix);
+				//Originally was MatrixSetColum. Since we use VMatrix now, changed to obbMatrix
+				obbMatrix.SetForward(x);
+				obbMatrix.SetLeft(y);
+				obbMatrix.SetUp(z);
+				obbMatrix.SetTranslation(corner);
 
 				//find inverse (we need the world to local matrix, "transformationmatrix" is kind of a misnomer)
-				MatrixInversion(obbMatrix, invObbMatrix);
+				//Originally was MatrixInversion. This is now using the built in functions, not relying on MatrixInversion and matrixinvert.h anymore
+				MatrixInverseGeneral(obbMatrix, invObbMatrix);
 				break;
 			}
 			
diff --git a/sp/src/utils/vbsp/matrixinvert.h b/sp/src/utils/vbsp/matrixinvert.h
deleted file mode 100644
index 6e676ad3..00000000
--- a/sp/src/utils/vbsp/matrixinvert.h
+++ /dev/null
@@ -1,117 +0,0 @@
-// By Jason Yu-Tseh Chi
-// From http://chi3x10.wordpress.com/2008/05/28/calculate-matrix-inversion-in-c/
-// Modified to work with valve's matrix_3x4_t
-
-#include "mathlib\mathlib.h"
-
-// Calculate the cofactor of element (row,col)
-int GetMatrixMinor(float **src, float **dest, int row, int col, int order)
-{
-	// Indicate which col and row is being copied to dest
-	int colCount = 0, rowCount = 0;
-
-	for (int i = 0; i < order; i++)
-	{
-		if (i != row)
-		{
-			colCount = 0;
-			for (int j = 0; j < order; j++)
-			{
-				// When j is not the element
-				if (j != col)
-				{
-					dest[rowCount][colCount] = src[i][j];
-					colCount++;
-				}
-			}
-			rowCount++;
-		}
-	}
-
-	return 1;
-}
-
-// Calculate the determinant recursively.
-double CalcMatrixDeterminant(float **mat, int order)
-{
-	// Order must be >= 0
-	// Stop the recursion when matrix is a single element
-	if (order == 1)
-		return mat[0][0];
-
-	// The determinant value
-	float det = 0;
-
-	// Allocate the cofactor matrix
-	float **minor;
-	minor = new float*[order - 1];
-	for (int i = 0; i<order - 1; i++)
-		minor[i] = new float[order - 1];
-
-	for (int i = 0; i < order; i++)
-	{
-		// Get minor of element (0,i)
-		GetMatrixMinor(mat, minor, 0, i, order);
-		// The recusion is here!
-
-		det += (i % 2 == 1 ? -1.0 : 1.0) * mat[0][i] * CalcMatrixDeterminant(minor, order - 1);
-		//det += pow( -1.0, i ) * mat[0][i] * CalcMatrixDeterminant( minor,order-1 );
-	}
-
-	// Release memory
-	for (int i = 0; i<order - 1; i++)
-		delete[] minor[i];
-	delete[] minor;
-
-	return det;
-}
-
-// Matrix inversion
-void MatrixInversion(matrix3x4_t &in, matrix3x4_t &out)
-{
-	float **A;
-	A = new float*[4];
-	for (int i = 0; i < 4; i++)
-		A[i] = new float[4];
-	int order = 4;
-
-	// Load in into A
-	for (int i = 0; i < 3; i++)
-	{
-		for (int j = 0; j < 4; j++)
-		{
-			A[i][j] = in[i][j];
-		}
-	}
-	A[3][0] = A[3][1] = A[3][2] = 0;
-	A[3][3] = 1;
-
-	// Get the determinant of a
-	double det = 1.0 / CalcMatrixDeterminant((float**)A, order);
-
-	// Memory allocation
-	float *temp = new float[(order - 1)*(order - 1)];
-	float **minor = new float*[order - 1];
-	for (int i = 0; i<order - 1; i++)
-		minor[i] = temp + (i*(order - 1));
-
-	for (int j = 0; j<order; j++)
-	{
-		for (int i = 0; i<order; i++)
-		{
-			// Get the co-factor (matrix) of A(j,i)
-			GetMatrixMinor((float**)A, minor, j, i, order);
-			out[i][j] = det*CalcMatrixDeterminant(minor, order - 1);
-			if ((i + j) % 2 == 1)
-				out[i][j] = -out[i][j];
-		}
-	}
-
-	// Release memory
-	for (int i = 0; i < 4; i++)
-		delete A[i];
-	delete A;
-	//delete [] minor[0];
-	delete[] temp;
-	delete[] minor;
-}
\ No newline at end of file