From e06006b436aa83c5c3a7360164761f9c1f0159f0 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 16 Apr 2017 04:44:35 +0200 Subject: [PATCH] Turn more matrix_4x4 functions into macros --- libretro-common/gfx/math/matrix_4x4.c | 87 ------------------- libretro-common/include/gfx/math/matrix_4x4.h | 79 ++++++++++++++++- 2 files changed, 76 insertions(+), 90 deletions(-) diff --git a/libretro-common/gfx/math/matrix_4x4.c b/libretro-common/gfx/math/matrix_4x4.c index 3f7dc18824..6183aa9e8f 100644 --- a/libretro-common/gfx/math/matrix_4x4.c +++ b/libretro-common/gfx/math/matrix_4x4.c @@ -50,93 +50,6 @@ void matrix_4x4_transpose(math_matrix_4x4 *out, const math_matrix_4x4 *in) *out = mat; } -/* - * Builds an X-axis rotation matrix - */ -void matrix_4x4_rotate_x(math_matrix_4x4 *mat, float rad) -{ - float cosine = cosf(rad); - float sine = sinf(rad); - - MAT_ELEM_4X4(*mat, 0, 0) = 1.0f; - MAT_ELEM_4X4(*mat, 0, 1) = 0.0f; - MAT_ELEM_4X4(*mat, 0, 2) = 0.0f; - MAT_ELEM_4X4(*mat, 0, 3) = 0.0f; - MAT_ELEM_4X4(*mat, 1, 0) = 0.0f; - MAT_ELEM_4X4(*mat, 1, 1) = cosine; - MAT_ELEM_4X4(*mat, 1, 2) = -sine; - MAT_ELEM_4X4(*mat, 1, 3) = 0.0f; - MAT_ELEM_4X4(*mat, 2, 0) = 0.0f; - MAT_ELEM_4X4(*mat, 2, 1) = sine; - MAT_ELEM_4X4(*mat, 2, 2) = cosine; - MAT_ELEM_4X4(*mat, 2, 3) = 0.0f; - MAT_ELEM_4X4(*mat, 3, 0) = 0.0f; - MAT_ELEM_4X4(*mat, 3, 1) = 0.0f; - MAT_ELEM_4X4(*mat, 3, 2) = 0.0f; - MAT_ELEM_4X4(*mat, 3, 3) = 1.0f; -} - -/* - * Builds a rotation matrix using the - * rotation around the Y-axis. - */ -void matrix_4x4_rotate_y(math_matrix_4x4 *mat, float rad) -{ - float cosine = cosf(rad); - float sine = sinf(rad); - - MAT_ELEM_4X4(*mat, 0, 0) = cosine; - MAT_ELEM_4X4(*mat, 0, 1) = 0.0f; - MAT_ELEM_4X4(*mat, 0, 2) = -sine; - MAT_ELEM_4X4(*mat, 0, 3) = 0.0f; - - MAT_ELEM_4X4(*mat, 1, 0) = 0.0f; - MAT_ELEM_4X4(*mat, 1, 1) = 1.0f; - MAT_ELEM_4X4(*mat, 1, 2) = 0.0f; - MAT_ELEM_4X4(*mat, 1, 3) = 0.0f; - - MAT_ELEM_4X4(*mat, 2, 0) = sine; - MAT_ELEM_4X4(*mat, 2, 1) = 0.0f; - MAT_ELEM_4X4(*mat, 2, 2) = cosine; - MAT_ELEM_4X4(*mat, 2, 3) = 0.0f; - - MAT_ELEM_4X4(*mat, 3, 0) = 0.0f; - MAT_ELEM_4X4(*mat, 3, 1) = 0.0f; - MAT_ELEM_4X4(*mat, 3, 2) = 0.0f; - MAT_ELEM_4X4(*mat, 3, 3) = 1.0f; -} - - -/* - * Creates a perspective projection matrix. - */ -void matrix_4x4_projection(math_matrix_4x4 *out, - float y_fov, - float aspect, - float znear, - float zfar) -{ - float const a = 1.f / tan(y_fov / 2.f); - float delta_z = zfar - znear; - - MAT_ELEM_4X4(*out, 0, 0) = a / aspect; - MAT_ELEM_4X4(*out, 0, 1) = 0.0f; - MAT_ELEM_4X4(*out, 0, 2) = 0.0f; - MAT_ELEM_4X4(*out, 0, 3) = 0.0f; - MAT_ELEM_4X4(*out, 1, 0) = 0.0f; - MAT_ELEM_4X4(*out, 1, 1) = a; - MAT_ELEM_4X4(*out, 1, 2) = 0.0f; - MAT_ELEM_4X4(*out, 1, 3) = 0.0f; - MAT_ELEM_4X4(*out, 2, 0) = 0.0f; - MAT_ELEM_4X4(*out, 2, 1) = 0.0f; - MAT_ELEM_4X4(*out, 2, 2) = -((zfar + znear) / delta_z); - MAT_ELEM_4X4(*out, 2, 3) = -1.f; - MAT_ELEM_4X4(*out, 3, 0) = 0.0f; - MAT_ELEM_4X4(*out, 3, 1) = 0.0f; - MAT_ELEM_4X4(*out, 3, 2) = -((2.f * zfar * znear) / delta_z); - MAT_ELEM_4X4(*out, 3, 3) = 0.0f; -} - void matrix_4x4_lookat(math_matrix_4x4 *out, vec3_t eye, vec3_t center, diff --git a/libretro-common/include/gfx/math/matrix_4x4.h b/libretro-common/include/gfx/math/matrix_4x4.h index 34353dbeef..afd71bbabd 100644 --- a/libretro-common/include/gfx/math/matrix_4x4.h +++ b/libretro-common/include/gfx/math/matrix_4x4.h @@ -67,8 +67,57 @@ typedef struct math_matrix_4x4 void matrix_4x4_copy(math_matrix_4x4 *dst, const math_matrix_4x4 *src); void matrix_4x4_transpose(math_matrix_4x4 *out, const math_matrix_4x4 *in); -void matrix_4x4_rotate_x(math_matrix_4x4 *mat, float rad); -void matrix_4x4_rotate_y(math_matrix_4x4 *mat, float rad); +/* + * Builds an X-axis rotation matrix + */ +#define matrix_4x4_rotate_x(mat, radians) \ +{ \ + float cosine = cosf(radians); \ + float sine = sinf(radians); \ + MAT_ELEM_4X4(mat, 0, 0) = 1.0f; \ + MAT_ELEM_4X4(mat, 0, 1) = 0.0f; \ + MAT_ELEM_4X4(mat, 0, 2) = 0.0f; \ + MAT_ELEM_4X4(mat, 0, 3) = 0.0f; \ + MAT_ELEM_4X4(mat, 1, 0) = 0.0f; \ + MAT_ELEM_4X4(mat, 1, 1) = cosine; \ + MAT_ELEM_4X4(mat, 1, 2) = -sine; \ + MAT_ELEM_4X4(mat, 1, 3) = 0.0f; \ + MAT_ELEM_4X4(mat, 2, 0) = 0.0f; \ + MAT_ELEM_4X4(mat, 2, 1) = sine; \ + MAT_ELEM_4X4(mat, 2, 2) = cosine; \ + MAT_ELEM_4X4(mat, 2, 3) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 0) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 1) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 2) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 3) = 1.0f; \ +} + +/* + * Builds a rotation matrix using the + * rotation around the Y-axis. + */ + +#define matrix_4x4_rotate_y(mat, radians) \ +{ \ + float cosine = cosf(radians); \ + float sine = sinf(radians); \ + MAT_ELEM_4X4(mat, 0, 0) = cosine; \ + MAT_ELEM_4X4(mat, 0, 1) = 0.0f; \ + MAT_ELEM_4X4(mat, 0, 2) = -sine; \ + MAT_ELEM_4X4(mat, 0, 3) = 0.0f; \ + MAT_ELEM_4X4(mat, 1, 0) = 0.0f; \ + MAT_ELEM_4X4(mat, 1, 1) = 1.0f; \ + MAT_ELEM_4X4(mat, 1, 2) = 0.0f; \ + MAT_ELEM_4X4(mat, 1, 3) = 0.0f; \ + MAT_ELEM_4X4(mat, 2, 0) = sine; \ + MAT_ELEM_4X4(mat, 2, 1) = 0.0f; \ + MAT_ELEM_4X4(mat, 2, 2) = cosine; \ + MAT_ELEM_4X4(mat, 2, 3) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 0) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 1) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 2) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 3) = 1.0f; \ +} /* * Builds a rotation matrix using the @@ -171,7 +220,31 @@ void matrix_4x4_multiply(math_matrix_4x4 *out, const math_matrix_4x4 *a, const m MAT_ELEM_4X4(mat, 3, 2) = 0.0f; \ MAT_ELEM_4X4(mat, 3, 3) = 1.0f -void matrix_4x4_projection(math_matrix_4x4 *out, float y_fov, float aspect, float znear, float zfar); +/* + * Creates a perspective projection matrix. + */ + +#define matrix_4x4_projection(mat, y_fov, aspect, znear, zfar) \ +{ \ + float const a = 1.f / tan((y_fov) / 2.f); \ + float delta_z = (zfar) - (znear); \ + MAT_ELEM_4X4(mat, 0, 0) = a / (aspect); \ + MAT_ELEM_4X4(mat, 0, 1) = 0.0f; \ + MAT_ELEM_4X4(mat, 0, 2) = 0.0f; \ + MAT_ELEM_4X4(mat, 0, 3) = 0.0f; \ + MAT_ELEM_4X4(mat, 1, 0) = 0.0f; \ + MAT_ELEM_4X4(mat, 1, 1) = a; \ + MAT_ELEM_4X4(mat, 1, 2) = 0.0f; \ + MAT_ELEM_4X4(mat, 1, 3) = 0.0f; \ + MAT_ELEM_4X4(mat, 2, 0) = 0.0f; \ + MAT_ELEM_4X4(mat, 2, 1) = 0.0f; \ + MAT_ELEM_4X4(mat, 2, 2) = -(((zfar) + (znear)) / delta_z); \ + MAT_ELEM_4X4(mat, 2, 3) = -1.f; \ + MAT_ELEM_4X4(mat, 3, 0) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 1) = 0.0f; \ + MAT_ELEM_4X4(mat, 3, 2) = -((2.f * (zfar) * (znear)) / delta_z); \ + MAT_ELEM_4X4(mat, 3, 3) = 0.0f; \ +} RETRO_END_DECLS