fix matrix stuff (finally)

This commit is contained in:
Luca Conte 2024-05-15 13:55:31 +02:00
parent 5a59d16b42
commit 93369795d3
4 changed files with 103 additions and 25 deletions

View File

@ -89,6 +89,11 @@ void rotationZ(mat4* out, GLfloat angle) {
} }
// CREATE 4x4 ROTATION MATRIX AROUND Y AXIS // CREATE 4x4 ROTATION MATRIX AROUND Y AXIS
/* cos a 0 sin a 0
* 0 1 0 0
* -sin a 0 cos a 0
* 0 0 0 1
*/
void rotationY(mat4* out, GLfloat angle) { void rotationY(mat4* out, GLfloat angle) {
identity(out); identity(out);
out->m00 = cos(angle); out->m00 = cos(angle);
@ -209,7 +214,16 @@ void mat3From4(mat3* out, mat4* in) {
memcpy(&out->m02, &in->m02, sizeof(vec3)); memcpy(&out->m02, &in->m02, sizeof(vec3));
} }
void mat3Adjoint(mat3* out, mat3* in) { void mat3Transpose(mat3* out, mat3* in) {
transposeAny((GLfloat*)out, (GLfloat*)in, 3, 3);
}
/**
* a - m00 b - m01 c - m02
* d - m10 e - m11 f - m12
* g - m20 h - m21 i - m22
*/
void mat3Minor(mat3* out, mat3* in) {
mat3 result; mat3 result;
result.m00 = in->m11 * in->m22 - in->m21 * in->m12; result.m00 = in->m11 * in->m22 - in->m21 * in->m12;
@ -227,6 +241,21 @@ void mat3Adjoint(mat3* out, mat3* in) {
memcpy(out, &result, sizeof(mat3)); memcpy(out, &result, sizeof(mat3));
} }
void mat3Cofactor(mat3* out, mat3* in) {
mat3 result;
mat3Minor(out, in);
out->m01 *= -1;
out->m10 *= -1;
out->m21 *= -1;
out->m12 *= -1;
}
void mat3Adjoint(mat3* out, mat3* in) {
mat3Cofactor(out, in);
mat3Transpose(out, out);
}
void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x) { void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x) {
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
((GLfloat*)out)[i] = ((GLfloat*)in)[i] * x; ((GLfloat*)out)[i] = ((GLfloat*)in)[i] * x;
@ -245,10 +274,6 @@ GLfloat mat3Determinant(mat3* M) {
; ;
} }
void mat3Transpose(mat3* out, mat3* in) {
transposeAny((GLfloat*)out, (GLfloat*)in, 3, 3);
}
void mat3Inverse(mat3* out, mat3* in) { void mat3Inverse(mat3* out, mat3* in) {
mat3 result; mat3 result;
mat3Adjoint(&result, in); mat3Adjoint(&result, in);
@ -260,7 +285,7 @@ void mat3Inverse(mat3* out, mat3* in) {
GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) { GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) {
GLfloat result = 0; GLfloat result = 0;
for (int i = 0; i < w * h; i++) { for (int i = 0; i < w * h; i++) {
result += abs(A[i] - B[i]); result += fabs(A[i] - B[i]);
} }
return result; return result;
} }

View File

@ -86,10 +86,12 @@ extern void mat4Print(mat4* m);
extern void mat3Print(mat3* m); extern void mat3Print(mat3* m);
extern void mat3From4(mat3* out, mat4* in); extern void mat3From4(mat3* out, mat4* in);
extern void mat3Adjoint(mat3* out, mat3* in);
extern void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x); extern void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x);
extern GLfloat mat3Determinant(mat3* m); extern GLfloat mat3Determinant(mat3* m);
extern void mat3Transpose(mat3* out, mat3* in); extern void mat3Transpose(mat3* out, mat3* in);
extern void mat3Minor(mat3* out, mat3* in);
extern void mat3Cofactor(mat3* out, mat3* in);
extern void mat3Adjoint(mat3* out, mat3* in);
extern void mat3Inverse(mat3* out, mat3* in); extern void mat3Inverse(mat3* out, mat3* in);
extern GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h); extern GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h);

View File

@ -5,22 +5,28 @@
#include "matrixMath.h" #include "matrixMath.h"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KNRM "\x1B[0m"
#define EPSILON 0.001f #define EPSILON 0.001f
int exitCode = 0;
void printTest(char* name, bool result) { void printTest(char* name, bool result) {
if (result) { if (result) {
printf(" PASSED - "); printf("%s PASSED%s - %s\n", KGRN, KNRM, name);
} else { } else {
printf("!!! FAILED - "); printf("%s!!! FAILED%s - %s\n", KRED, KNRM, name);
exitCode = 1;
} }
printf("%s", name);
printf("\n");
} }
void testSumDiff() { void testSumDiff() {
mat3 A = {1.0f, 0.0f, 0.0f, -1.0f, 0.0f, -1.0f, 1.0f, -1.0f, 9.0f}; mat3 A = {1, 0, 0, -1, 0, -1, 1, -1, 9};
mat3 B = {0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 8.0f}; mat3 B = {0, 1, -1, 0, 0, 1, 1, -1, 8};
GLfloat target = 7.0f; GLfloat target = 7;
GLfloat value = mat3SumDiff(&A, &B); GLfloat value = mat3SumDiff(&A, &B);
@ -38,9 +44,28 @@ void testSumDiff() {
printTest("mat3SumDiff", result); printTest("mat3SumDiff", result);
} }
void testMat3Minor() {
mat3 M = {2, 0, 1, -1, 5, -1, 3, 2, -2};
mat3 target = {-8, 5, -17, -2, -7, 4, -5, -1, 10};
mat3Minor(&M, &M);
printTest("mat3Minor", mat3SumDiff(&M, &target) < EPSILON);
}
void testMat3Cofactor() {
mat3 M = {2, 0, 1, -1, 5, -1, 3, 2, -2};
mat3 target = {-8, -5, -17, 2, -7, -4, -5, 1, 10};
mat3Cofactor(&M, &M);
printTest("mat3Cofactor", mat3SumDiff(&M, &target) < EPSILON);
}
void testMat3Adjoint() { void testMat3Adjoint() {
mat3 M = {2.0f, 0.0f, 1.0f, -1.0f, 5.0f, -1.0f, 3.0f, 2.0f, -2.0f}; mat3 M = {1, 2, -1, 2, 1, 2, -1, 2, 1};
mat3 target = {-8.0f, 5.0f, -17.0f, -2.0f, -7.0f, 4.0f, -5.0f, -1.0f, 10.0f}; mat3 target = {-3, -4, 5, -4, 0, -4, 5, -4, -3};
mat3Adjoint(&M, &M); mat3Adjoint(&M, &M);
@ -48,9 +73,9 @@ void testMat3Adjoint() {
} }
void testMat3MultiplyScalar() { void testMat3MultiplyScalar() {
mat3 M = {1.0f, 2.0f, 3.0f, 4.0f, 0.0f, -1.5f, -2.5f, -3.5f, -4.5f}; mat3 M = {1, 2, 3, 4, 0, -1.5f, -2.5f, -3.5f, -4.5f};
GLfloat x = 0.9f; GLfloat x = 0.9f;
mat3 target = {0.9f, 1.8f, 2.7f, 3.6f, 0.0f, -1.35f, -2.25f, -3.15f, -4.05f}; mat3 target = {0.9f, 1.8f, 2.7f, 3.6f, 0, -1.35f, -2.25f, -3.15f, -4.05f};
mat3MultiplyScalar(&M, &M, x); mat3MultiplyScalar(&M, &M, x);
@ -58,25 +83,48 @@ void testMat3MultiplyScalar() {
} }
void testMat3Determinant() { void testMat3Determinant() {
mat3 M = {1.0f, -3.0f, 2.0f, 3.0f, -1.0f, 3.0f, 2.0f, -3.0f, 1.0f}; mat3 M = {1, -3, 2, 3, -1, 3, 2, -3, 1};
GLfloat target = -15.0f; GLfloat target = -15;
printTest("mat3Determinant", fabs(mat3Determinant(&M) - target) < EPSILON); printTest("mat3Determinant", fabs(mat3Determinant(&M) - target) < EPSILON);
} }
void testMat3Inverse() { void testMat3Inverse() {
mat3 M = {1.0f, 2.0f, -1.0f, 2.0f, 1.0f, 2.0f, -1.0f, 2.0f, 1.0f}; mat3 M = {1, 2, -1, 2, 1, 2, -1, 2, 1};
mat3 target = {0.1875f, 0.25f, -0.3125f, 0.2f, 0.0f, 0.25f, -0.3125f, 0.25f, 0.1875f}; mat3 target = {0.1875f, 0.25f, -0.3125f, 0.25f, 0, 0.25f, -0.3125f, 0.25f, 0.1875f};
mat3Inverse(&M, &M); mat3Inverse(&M, &M);
printTest("mat3Inverse", mat3SumDiff(&M, &target) < EPSILON); bool result = mat3SumDiff(&M, &target) < EPSILON;
if (!result) {
printf("\nM:\n");
mat3Print(&M);
printf("\ntarget:\n");
mat3Print(&target);
}
printTest("mat3Inverse", result);
}
void testMat3Transpose() {
mat3 M = {1, 2, 3, 4, 5, 6, 7, 8, 9};
mat3 target = {1, 4, 7, 2, 5, 8, 3, 6, 9};
mat3Transpose(&M, &M);
printTest("mat3Transpose", mat3SumDiff(&M, &target) < EPSILON);
} }
int main(void) { int main(void) {
testSumDiff(); testSumDiff();
testMat3Minor();
testMat3Cofactor();
testMat3Adjoint(); testMat3Adjoint();
testMat3MultiplyScalar(); testMat3MultiplyScalar();
testMat3Determinant(); testMat3Determinant();
testMat3Inverse(); testMat3Inverse();
testMat3Transpose();
return exitCode;
} }

View File

@ -12,6 +12,9 @@ out vec3 fragmentPosition;
void main() { void main() {
normal = normalModelView * aNormal; normal = normalModelView * aNormal;
gl_Position = projection * modelView * vec4(aPosition, 1.0); vec4 modelViewPos = modelView * vec4(aPosition, 1.0);
fragmentPosition = vec3(modelView * vec4(aPosition, 1.0));
gl_Position = projection * modelViewPos;
fragmentPosition = vec3(modelViewPos);
} }