maintenance and added vec3Set

This commit is contained in:
Luca Conte 2025-04-22 19:29:18 +02:00
parent b798a9a26e
commit c5f7407199
2 changed files with 99 additions and 34 deletions

View File

@ -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) { void mat4Multiply(mat4 result, mat4 A, mat4 B) {
// if result is one of the arguments, modify copy instead of result directly // 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; return;
} }
// loops over cells of the result matrix
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
int col = i / 4; int col = i / 4;
int row = i % 4; int row = i % 4;
// pointer to the current cell
GLfloat* curr = &(result[i]); GLfloat* curr = &(result[i]);
// initialise current cell with 0
*curr = 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++) { for (int j = 0; j < 4; j++) {
*curr += A[row + j * 4] * B[j + col * 4]; *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) { void mat4Translate(mat4 out, mat4 in, vec3 v) {
mat4 T; mat4 T;
mat4Identity(T); mat4Identity(T);
@ -77,6 +86,9 @@ void mat4Translate(mat4 out, mat4 in, vec3 v) {
mat4Multiply(out, T, in); mat4Multiply(out, T, in);
} }
/**
* scales by the vector v
*/
void mat4Scale(mat4 out, mat4 in, vec3 v) { void mat4Scale(mat4 out, mat4 in, vec3 v) {
mat4 T; mat4 T;
mat4Identity(T); mat4Identity(T);
@ -88,6 +100,10 @@ void mat4Scale(mat4 out, mat4 in, vec3 v) {
mat4Multiply(out, T, in); mat4Multiply(out, T, in);
} }
/**
* rotates around the X axis by the angle a (in radians)
*/
void mat4RotateX(mat4 out, mat4 in, GLfloat a) { void mat4RotateX(mat4 out, mat4 in, GLfloat a) {
mat4 T; mat4 T;
mat4Identity(T); mat4Identity(T);
@ -100,6 +116,9 @@ void mat4RotateX(mat4 out, mat4 in, GLfloat a) {
mat4Multiply(out, T, in); mat4Multiply(out, T, in);
} }
/**
* rotates around the Y axis by the angle a (in radians)
*/
void mat4RotateY(mat4 out, mat4 in, GLfloat a) { void mat4RotateY(mat4 out, mat4 in, GLfloat a) {
mat4 T; mat4 T;
mat4Identity(T); mat4Identity(T);
@ -112,6 +131,9 @@ void mat4RotateY(mat4 out, mat4 in, GLfloat a) {
mat4Multiply(out, T, in); mat4Multiply(out, T, in);
} }
/**
* rotates around the Z axis by the angle a (in radians)
*/
void mat4RotateZ(mat4 out, mat4 in, GLfloat a) { void mat4RotateZ(mat4 out, mat4 in, GLfloat a) {
mat4 T; mat4 T;
mat4Identity(T); mat4Identity(T);
@ -124,38 +146,12 @@ void mat4RotateZ(mat4 out, mat4 in, GLfloat a) {
mat4Multiply(out, T, in); mat4Multiply(out, T, in);
} }
void vec3Subtract(vec3 out, vec3 a, vec3 b) { /**
out[0] = a[0] - b[0]; * builds a look-at matrix, overwriting out
out[1] = a[1] - b[1]; * eye is the position of the camera
out[2] = a[2] - b[2]; * look is the position of the target to be looked at
} * up is the up vector
*/
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];
}
void mat4BuildLookAt(mat4 out, vec3 eye, vec3 look, vec3 up) { void mat4BuildLookAt(mat4 out, vec3 eye, vec3 look, vec3 up) {
vec3 n; vec3 n;
vec3 u; 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; 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) { void mat4BuildProjection(mat4 out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat n, GLfloat f) {
mat4Identity(out); 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); 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) { void mat4BuildPerspective(mat4 out, GLfloat fovy, GLfloat aspect, GLfloat n, GLfloat f) {
GLfloat t = n * tan(0.5f * fovy); GLfloat t = n * tan(0.5f * fovy);
GLfloat b = -t; GLfloat b = -t;
@ -205,3 +212,60 @@ void mat4BuildPerspective(mat4 out, GLfloat fovy, GLfloat aspect, GLfloat n, GLf
mat4BuildProjection(out, r, l, t, b, n, f); mat4BuildProjection(out, r, l, t, b, n, f);
} }
/**
* 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;
}

View File

@ -31,5 +31,6 @@ extern void vec3CrossProduct(vec3 out, vec3 a, vec3 b);
extern void vec3Normalise(vec3 out, vec3 in); extern void vec3Normalise(vec3 out, vec3 in);
extern GLfloat vec3Length(vec3 in); extern GLfloat vec3Length(vec3 in);
extern GLfloat vec3DotProduct(vec3 a, vec3 b); extern GLfloat vec3DotProduct(vec3 a, vec3 b);
extern void vec3Set(vec3 out, GLfloat x, GLfloat y, GLfloat z);
#endif #endif