From e34cfde985564ad0ac6a8b6f9f10e455265d2dbd Mon Sep 17 00:00:00 2001 From: Luc Date: Tue, 29 Dec 2020 00:52:24 +0100 Subject: [PATCH] add new mat function --- sweet_macro.h | 8 ++----- sweet_matrix.c | 48 ++++++++++++++++++++++++++++++++++++++++++ sweet_matrix.h | 13 ++++++++++++ sweet_overload_macro.h | 14 ++++++++++++ 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 sweet_overload_macro.h diff --git a/sweet_macro.h b/sweet_macro.h index 4bc842b..4c43026 100755 --- a/sweet_macro.h +++ b/sweet_macro.h @@ -19,18 +19,14 @@ #ifndef SWEET_MACRO #define SWEET_MACRO -#define _OVERLOAD_2_(_1, _2, NAME, ...) NAME -#define _OVERLOAD_3_(_1, _2, _3, NAME, ...) NAME -#define _OVERLOAD_4_(_1, _2, _3, _4, NAME, ...) NAME -#define _OVERLOAD_5_(_1, _2, _3, _4, _5, NAME, ...) NAME -#define _OVERLOAD_6_(_1, _2, _3, _4, _5, _6, NAME, ...) NAME +#include "sweet_overload_macro.h" #define vec2_new(...) _OVERLOAD_2_(__VA_ARGS__, vec2_new_2f, vec2_new_v3, NULL)(__VA_ARGS__) #define vec3_new(...) _OVERLOAD_3_(__VA_ARGS__, vec3_new_3f, vec3_new_v2_1f, vec3_new_v4, NULL)(__VA_ARGS__) #define vec4_new(...) _OVERLOAD_4_(__VA_ARGS__, vec4_new_4f, vec4_new_v2_2f, vec4_new_v3_1f, vec4_new_v4, NULL)(__VA_ARGS__) #define mat2_new(...) _OVERLOAD_4_(__VA_ARGS__, mat2_new_4f, mat2_new_3f, mat2_new_2v, mat2_new_m3, NULL)(__VA_ARGS__) -#define mat3_new(...) _OVERLOAD_3_(__VA_ARGS__, mat3_new_3v, mat3_new_2v, mat3_new_m4, NULL)(__VA_ARGS__) +#define mat3_new(...) _OVERLOAD_9_(__VA_ARGS__, mat3_new_9f, NULL, NULL, NULL, NULL, NULL, mat3_new_3v, mat3_new_2v, mat3_new_m4, NULL)(__VA_ARGS__) #define mat4_new(...) _OVERLOAD_4_(__VA_ARGS__, mat4_new_4v, mat4_new_3v, mat4_new_2v, mat4_new_m4, NULL)(__VA_ARGS__) #define mat2_insert_col(...) _OVERLOAD_4_(__VA_ARGS__, mat2_insert_col_2f, mat2_insert_col_1v, NULL, NULL)(__VA_ARGS__) diff --git a/sweet_matrix.c b/sweet_matrix.c index 3d9dce8..68f4150 100755 --- a/sweet_matrix.c +++ b/sweet_matrix.c @@ -110,6 +110,21 @@ mat3 mat3_new_3v (vec3 e1, vec3 e2, vec3 e3) return m; } +mat3 mat3_new_9f(float a0, float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8) +{ + mat3 m; + m.v[0] = a0; + m.v[1] = a1; + m.v[2] = a2; + m.v[3] = a3; + m.v[4] = a4; + m.v[5] = a5; + m.v[6] = a6; + m.v[7] = a7; + m.v[8] = a8; + return m; +} + mat4 mat4_new_m4 (mat4 m) { return m; @@ -492,6 +507,39 @@ matrix_perspective (double fovy, double aspect, double z_near, double z_far) return m; } +mat4 +matrix_perspective_infinite (double fovy, double aspect, double z_near) +{ + mat4 m; + float f; + float epsilon = 0.0001; + + fovy = degree_2_radian (fovy); + f = 1.0 / tan (fovy / 2.0); + + m.v[0] = f / aspect; + m.v[1] = 0.0; + m.v[2] = 0.0; + m.v[3] = 0.0; + + m.v[4] = 0.0; + m.v[5] = f; + m.v[6] = 0.0; + m.v[7] = 0.0; + + m.v[8] = 0.0; + m.v[9] = 0.0; + m.v[10] = (epsilon - 1.0); + m.v[11] = -1.0; + + m.v[12] = 0.0; + m.v[13] = 0.0; + m.v[14] = (epsilon - 2.0) * z_near; + m.v[15] = 0.0; + + return m; +} + mat4 matrix_frustum (float left, float right, float bottom, float top, diff --git a/sweet_matrix.h b/sweet_matrix.h index be25cdf..72e65af 100755 --- a/sweet_matrix.h +++ b/sweet_matrix.h @@ -31,6 +31,9 @@ mat2 mat2_new_4f (float f0, float f1, float f2, float f3); mat3 mat3_new_m4 (mat4 m); mat3 mat3_new_2v (vec2 e1, vec2 e2); mat3 mat3_new_3v (vec3 e1, vec3 e2, vec3 e3); +mat3 mat3_new_9f(float a0, float a1, float a2, + float a3, float a4, float a5, + float a6, float a7, float a8); mat4 mat4_new_m4 (mat4 m); mat4 mat4_new_2v (vec2 e1, vec2 e2); @@ -83,6 +86,16 @@ mat4 matrix_frustum (float left, float right, /** @return Perspective matrix 4x4 */ mat4 matrix_perspective (double fovy, double aspect, double z_near, double z_far); +/** Perspective infinite */ +/** @param fovy of the camera in degree as flaot */ +/** @param aspect of the view as float */ +/** @param zNear as float */ +/** @return Perspective matrix 4x4 but far plant is handled in a way this projection + ** can support really far values. +*/ +mat4 matrix_perspective_infinite (double fovy, double aspect, double z_near); + + /** Ortho */ /** @param Right as flaot */ /** @param Left as float */ diff --git a/sweet_overload_macro.h b/sweet_overload_macro.h new file mode 100644 index 0000000..8517d23 --- /dev/null +++ b/sweet_overload_macro.h @@ -0,0 +1,14 @@ +#ifndef SWEET_OVERLOAD_MACRO +#define SWEET_OVERLOAD_MACRO + +#define _OVERLOAD_1_(_1, NAME, ...) NAME +#define _OVERLOAD_2_(_1, _2, NAME, ...) NAME +#define _OVERLOAD_3_(_1, _2, _3, NAME, ...) NAME +#define _OVERLOAD_4_(_1, _2, _3, _4, NAME, ...) NAME +#define _OVERLOAD_5_(_1, _2, _3, _4, _5, NAME, ...) NAME +#define _OVERLOAD_6_(_1, _2, _3, _4, _5, _6, NAME, ...) NAME +#define _OVERLOAD_7_(_1, _2, _3, _4, _5, _6, _7, NAME, ...) NAME +#define _OVERLOAD_8_(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME +#define _OVERLOAD_9_(_1, _2, _3, _4, _5, _6, _7, _8, _9, NAME, ...) NAME + +#endif