added vector/matrix typedefs and 3x3 matrix inversion

This commit is contained in:
myT 2024-03-29 01:58:35 +01:00
parent ef68bbd44e
commit 15d8ccf982
4 changed files with 48 additions and 14 deletions

View file

@ -685,6 +685,12 @@ const char* QDECL va( PRINTF_FORMAT_STRING const char* format, ... )
} }
const char* v2tos( const vec2_t v )
{
return va( "%g, %g", v[0], v[1] );
}
const char* v3tos( const vec3_t v ) const char* v3tos( const vec3_t v )
{ {
return va( "%g, %g, %g", v[0], v[1], v[2] ); return va( "%g, %g, %g", v[0], v[1], v[2] );

View file

@ -262,10 +262,17 @@ MATHLIB
*/ */
typedef int32_t ivec2_t[2];
typedef int32_t ivec3_t[3];
typedef int32_t ivec4_t[4];
typedef uint32_t uvec2_t[2];
typedef uint32_t uvec3_t[3];
typedef uint32_t uvec4_t[4];
typedef float vec_t; typedef float vec_t;
typedef vec_t vec2_t[2]; typedef vec_t vec2_t[2];
typedef vec_t vec3_t[3]; typedef vec_t vec3_t[3];
typedef vec_t vec4_t[4]; typedef vec_t vec4_t[4];
typedef vec_t matrix3x3_t[9];
typedef vec_t matrix4x4_t[16]; typedef vec_t matrix4x4_t[16];
extern const vec3_t vec2_zero; extern const vec3_t vec2_zero;
extern const vec3_t vec2_one; extern const vec3_t vec2_one;
@ -550,6 +557,7 @@ typedef enum {
} extErrorLevel_t; } extErrorLevel_t;
const char* QDECL va( PRINTF_FORMAT_STRING const char* format, ... ); const char* QDECL va( PRINTF_FORMAT_STRING const char* format, ... );
const char* v2tos( const vec3_t v );
const char* v3tos( const vec3_t v ); const char* v3tos( const vec3_t v );
const char* v4tos( const vec4_t v ); const char* v4tos( const vec4_t v );

View file

@ -1646,13 +1646,14 @@ void RE_TakeVideoFrame( int width, int height,
void R_EndScene( const viewParms_t* viewParms ); void R_EndScene( const viewParms_t* viewParms );
void R_MultMatrix( const float *a, const float *b, float *out ); void R_MultMatrix( const matrix4x4_t a, const matrix4x4_t b, matrix4x4_t out );
void R_InvMatrix( const float* in, float* out ); void R_InvMatrix( const matrix4x4_t in, matrix4x4_t out );
void R_TransposeMatrix( const float* in, float* out ); void R_InvMatrix3x3( const matrix3x3_t in, matrix3x3_t out );
void R_CameraPositionFromMatrix( const float* modelView, vec3_t cameraPos ); void R_TransposeMatrix( const matrix4x4_t in, matrix4x4_t out );
void R_CameraAxisVectorsFromMatrix( const float* modelView, vec3_t axisX, vec3_t axisY, vec3_t axisZ ); void R_CameraPositionFromMatrix( const matrix4x4_t modelView, vec3_t cameraPos );
void R_MakeIdentityMatrix( float* m ); void R_CameraAxisVectorsFromMatrix( const matrix4x4_t modelView, vec3_t axisX, vec3_t axisY, vec3_t axisZ );
void R_MakeOrthoProjectionMatrix( float* m, float w, float h ); void R_MakeIdentityMatrix( matrix4x4_t m );
void R_MakeOrthoProjectionMatrix( matrix4x4_t m, float w, float h );
// LinearDepth(depthZW, A, B) -> B / (depthZW - A) // LinearDepth(depthZW, A, B) -> B / (depthZW - A)
void R_LinearDepthConstantsFromProjectionMatrix( const float* projMatrix, float* A, float* B ); void R_LinearDepthConstantsFromProjectionMatrix( const float* projMatrix, float* A, float* B );

View file

@ -202,7 +202,7 @@ void R_TransformClipToWindow( const vec4_t clip, const viewParms_t *view, vec4_t
} }
void R_MultMatrix( const float *a, const float *b, float *out ) void R_MultMatrix( const matrix4x4_t a, const matrix4x4_t b, matrix4x4_t out )
{ {
for ( int i = 0 ; i < 4 ; i++ ) { for ( int i = 0 ; i < 4 ; i++ ) {
for ( int j = 0 ; j < 4 ; j++ ) { for ( int j = 0 ; j < 4 ; j++ ) {
@ -216,7 +216,7 @@ void R_MultMatrix( const float *a, const float *b, float *out )
} }
void R_InvMatrix( const float* m, float* invOut ) void R_InvMatrix( const matrix4x4_t m, matrix4x4_t invOut )
{ {
float inv[16]; float inv[16];
@ -344,7 +344,26 @@ void R_InvMatrix( const float* m, float* invOut )
} }
void R_TransposeMatrix( const float* in, float* out ) void R_InvMatrix3x3( const matrix3x3_t in, matrix3x3_t out )
{
const float det =
in[0] * (in[4] * in[8] - in[7] * in[5]) -
in[1] * (in[3] * in[8] - in[5] * in[6]) +
in[2] * (in[3] * in[7] - in[4] * in[6]);
const float invDet = det == 0.0f ? 1.0f : (1.0f / det);
out[0] = (in[4] * in[8] - in[7] * in[5]) * invDet;
out[1] = (in[2] * in[7] - in[1] * in[8]) * invDet;
out[2] = (in[1] * in[5] - in[2] * in[4]) * invDet;
out[3] = (in[5] * in[6] - in[3] * in[8]) * invDet;
out[4] = (in[0] * in[8] - in[2] * in[6]) * invDet;
out[5] = (in[3] * in[2] - in[0] * in[5]) * invDet;
out[6] = (in[3] * in[7] - in[6] * in[4]) * invDet;
out[7] = (in[6] * in[1] - in[0] * in[7]) * invDet;
out[8] = (in[0] * in[4] - in[3] * in[1]) * invDet;
}
void R_TransposeMatrix( const matrix4x4_t in, matrix4x4_t out )
{ {
out[ 0] = in[ 0]; out[ 0] = in[ 0];
out[ 4] = in[ 1]; out[ 4] = in[ 1];
@ -368,7 +387,7 @@ void R_TransposeMatrix( const float* in, float* out )
} }
void R_CameraPositionFromMatrix( const float* modelView, vec3_t cameraPos ) void R_CameraPositionFromMatrix( const matrix4x4_t modelView, vec3_t cameraPos )
{ {
float modelViewT[16]; float modelViewT[16];
R_TransposeMatrix( modelView, modelViewT ); R_TransposeMatrix( modelView, modelViewT );
@ -400,7 +419,7 @@ void R_CameraPositionFromMatrix( const float* modelView, vec3_t cameraPos )
} }
void R_CameraAxisVectorsFromMatrix( const float* modelView, vec3_t axisX, vec3_t axisY, vec3_t axisZ ) void R_CameraAxisVectorsFromMatrix( const matrix4x4_t modelView, vec3_t axisX, vec3_t axisY, vec3_t axisZ )
{ {
axisX[0] = modelView[ 0]; axisX[0] = modelView[ 0];
axisX[1] = modelView[ 4]; axisX[1] = modelView[ 4];
@ -416,7 +435,7 @@ void R_CameraAxisVectorsFromMatrix( const float* modelView, vec3_t axisX, vec3_t
} }
void R_MakeIdentityMatrix( float* m ) void R_MakeIdentityMatrix( matrix4x4_t m )
{ {
m[ 0] = 1.0f; m[ 0] = 1.0f;
m[ 1] = 0.0f; m[ 1] = 0.0f;
@ -437,7 +456,7 @@ void R_MakeIdentityMatrix( float* m )
} }
void R_MakeOrthoProjectionMatrix( float* m, float w, float h ) void R_MakeOrthoProjectionMatrix( matrix4x4_t m, float w, float h )
{ {
// 2/(r-l) 0 0 0 // 2/(r-l) 0 0 0
// 0 2/(t-b) 0 0 // 0 2/(t-b) 0 0