From c5f7407199a880743ff4b89f5f652a1b43543e6b Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Tue, 22 Apr 2025 19:29:18 +0200 Subject: [PATCH] maintenance and added vec3Set --- src/matrix-math.c | 132 ++++++++++++++++++++++++++++++++++------------ src/matrix-math.h | 1 + 2 files changed, 99 insertions(+), 34 deletions(-) diff --git a/src/matrix-math.c b/src/matrix-math.c index c9dc8b8..2e7f5e8 100644 --- a/src/matrix-math.c +++ b/src/matrix-math.c @@ -30,7 +30,7 @@ void mat4Empty(mat4 mat) { } /** - * mutliplies A with B and stores the result in results + * mutliplies A with B and stores the result in result */ void mat4Multiply(mat4 result, mat4 A, mat4 B) { // if result is one of the arguments, modify copy instead of result directly @@ -41,13 +41,19 @@ void mat4Multiply(mat4 result, mat4 A, mat4 B) { return; } + // loops over cells of the result matrix for (int i = 0; i < 16; i++) { int col = i / 4; int row = i % 4; + + // pointer to the current cell GLfloat* curr = &(result[i]); + // initialise current cell with 0 *curr = 0; + // loop over the row of A and column of B + // continuously adding the multiplication of the two values to the result cell for (int j = 0; j < 4; j++) { *curr += A[row + j * 4] * B[j + col * 4]; } @@ -66,6 +72,9 @@ void mat4Print(mat4 m) { } } +/** + * translates by the vector v + */ void mat4Translate(mat4 out, mat4 in, vec3 v) { mat4 T; mat4Identity(T); @@ -77,6 +86,9 @@ void mat4Translate(mat4 out, mat4 in, vec3 v) { mat4Multiply(out, T, in); } +/** + * scales by the vector v + */ void mat4Scale(mat4 out, mat4 in, vec3 v) { mat4 T; mat4Identity(T); @@ -88,6 +100,10 @@ void mat4Scale(mat4 out, mat4 in, vec3 v) { mat4Multiply(out, T, in); } + +/** + * rotates around the X axis by the angle a (in radians) + */ void mat4RotateX(mat4 out, mat4 in, GLfloat a) { mat4 T; mat4Identity(T); @@ -100,6 +116,9 @@ void mat4RotateX(mat4 out, mat4 in, GLfloat a) { mat4Multiply(out, T, in); } +/** + * rotates around the Y axis by the angle a (in radians) + */ void mat4RotateY(mat4 out, mat4 in, GLfloat a) { mat4 T; mat4Identity(T); @@ -112,6 +131,9 @@ void mat4RotateY(mat4 out, mat4 in, GLfloat a) { mat4Multiply(out, T, in); } +/** + * rotates around the Z axis by the angle a (in radians) + */ void mat4RotateZ(mat4 out, mat4 in, GLfloat a) { mat4 T; mat4Identity(T); @@ -124,38 +146,12 @@ void mat4RotateZ(mat4 out, mat4 in, GLfloat a) { mat4Multiply(out, T, in); } -void vec3Subtract(vec3 out, vec3 a, vec3 b) { - out[0] = a[0] - b[0]; - out[1] = a[1] - b[1]; - out[2] = a[2] - b[2]; -} - -void vec3CrossProduct(vec3 out, vec3 a, vec3 b) { - vec3 result; - result[0] = a[1] * b[2] - a[2] * b[1]; - result[1] = a[2] * b[0] - a[0] * b[2]; - result[2] = a[0] * b[1] - a[1] * b[0]; - - out[0] = result[0]; - out[1] = result[1]; - out[2] = result[2]; -} - -void vec3Normalise(vec3 out, vec3 in) { - GLfloat length = vec3Length(in); - out[0] = in[0] / length; - out[1] = in[1] / length; - out[2] = in[2] / length; -} - -GLfloat vec3Length(vec3 in) { - return sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]); -} - -GLfloat vec3DotProduct(vec3 a, vec3 b) { - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; -} - +/** + * builds a look-at matrix, overwriting out + * eye is the position of the camera + * look is the position of the target to be looked at + * up is the up vector + */ void mat4BuildLookAt(mat4 out, vec3 eye, vec3 look, vec3 up) { vec3 n; vec3 u; @@ -182,6 +178,11 @@ void mat4BuildLookAt(mat4 out, vec3 eye, vec3 look, vec3 up) { out[3] = 0; out[7] = 0; out[11] = 0; out[15] = 1; } +/** + * builds a projection matrix, overwriting out + * r, l, t, b are the right, left, top and bottom of the frustum + * n and f are the distance of the near and far planes from the camera + */ void mat4BuildProjection(mat4 out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat n, GLfloat f) { mat4Identity(out); @@ -196,6 +197,12 @@ void mat4BuildProjection(mat4 out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, G out[14] = - 2.0f * f / (f - n); } +/** + * builds a perspective projection matrix, overwriting out + * fovy is the field of view in the y direction + * aspect is the aspect ratio + * n and f are the distance of the near and far planes from the camera + */ void mat4BuildPerspective(mat4 out, GLfloat fovy, GLfloat aspect, GLfloat n, GLfloat f) { GLfloat t = n * tan(0.5f * fovy); GLfloat b = -t; @@ -204,4 +211,61 @@ void mat4BuildPerspective(mat4 out, GLfloat fovy, GLfloat aspect, GLfloat n, GLf GLfloat l = -r; mat4BuildProjection(out, r, l, t, b, n, f); -} \ No newline at end of file +} + + +/** + * subtracts b from a, storing the result in out + */ +void vec3Subtract(vec3 out, vec3 a, vec3 b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; +} + +/** + * calculates the cross product of a and b, storing the result in out + */ +void vec3CrossProduct(vec3 out, vec3 a, vec3 b) { + vec3 result; + result[0] = a[1] * b[2] - a[2] * b[1]; + result[1] = a[2] * b[0] - a[0] * b[2]; + result[2] = a[0] * b[1] - a[1] * b[0]; + + out[0] = result[0]; + out[1] = result[1]; + out[2] = result[2]; +} + +/** + * normalizes in storing the result in out + */ +void vec3Normalise(vec3 out, vec3 in) { + GLfloat length = vec3Length(in); + out[0] = in[0] / length; + out[1] = in[1] / length; + out[2] = in[2] / length; +} + +/** + * returns the length of the vector in + */ +GLfloat vec3Length(vec3 in) { + return sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]); +} + +/** + * returns the dot product of the vectors a and b + */ +GLfloat vec3DotProduct(vec3 a, vec3 b) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; +} + +/** + * sets the values of out to x, y and z + */ +void vec3Set(vec3 out, GLfloat x, GLfloat y, GLfloat z) { + out[0] = x; + out[1] = y; + out[2] = z; +} diff --git a/src/matrix-math.h b/src/matrix-math.h index 267cf9f..a5a0e14 100644 --- a/src/matrix-math.h +++ b/src/matrix-math.h @@ -31,5 +31,6 @@ extern void vec3CrossProduct(vec3 out, vec3 a, vec3 b); extern void vec3Normalise(vec3 out, vec3 in); extern GLfloat vec3Length(vec3 in); extern GLfloat vec3DotProduct(vec3 a, vec3 b); +extern void vec3Set(vec3 out, GLfloat x, GLfloat y, GLfloat z); #endif \ No newline at end of file