add new mat function

This commit is contained in:
Luc 2020-12-29 00:52:24 +01:00
parent 87915532ee
commit e34cfde985
4 changed files with 77 additions and 6 deletions

View File

@ -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__)

View File

@ -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,

View File

@ -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 */

14
sweet_overload_macro.h Normal file
View File

@ -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