Skip to content

Commit

Permalink
Added mat4_get_position, and TODO mat4_get_rotation and scale
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi688 committed Oct 29, 2023
1 parent f6eef3a commit 1cdf646
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
33 changes: 26 additions & 7 deletions include/hpml/mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


#include <hpml/defines.h>
#include <hpml/vec4.h> // for vec4_t

#include <math.h> // for sin and cos
#include <stdarg.h> // for variable arugments
Expand Down Expand Up @@ -66,7 +67,7 @@ static HPML_FORCE_INLINE mat4_t mat4(float v00, float v01, float v02, float v03,
float v20, float v21, float v22, float v23,
float v30, float v31, float v32, float v33)
{
return MAT4
return MAT4
{
v00, v01, v02, v03,
v10, v11, v12, v13,
Expand All @@ -90,13 +91,13 @@ HPML_API float* const* const __mat4_data(mat4_t* m);
* NOTE: this function doesn't support rvalue src matrix; use mat4_move instead
*/
#define mat4_copy(dest, src) __mat4_copy(&(dest), &(src))
static HPML_FORCE_INLINE void __mat4_copy(mat4_t* const dest, const mat4_t* const src)
{
static HPML_FORCE_INLINE void __mat4_copy(mat4_t* const dest, const mat4_t* const src)
{
/*
TODO: This should be like this:
TODO: This should be like this:
memcpy(dest, src, COMPILE_TIME( 16 * sizeof(float) ));
*/
memcpy(dest, src, 16 * sizeof(float));
memcpy(dest, src, 16 * sizeof(float));
}

/* mat4_move: Performs a move operation of 4x4 matrices
Expand All @@ -110,6 +111,24 @@ static HPML_FORCE_INLINE void __mat4_move(mat4_t* const dest, const mat4_t src)
__mat4_copy(dest, &src);
}

/* mat4_get_position: Retrieves the position componenets from the 4x4 matrix
* m: Input 4x4 matrix
* returns: vec4 position vector
*/
HPML_API vec4_t mat4_get_position(mat4_t m);

/* mat4_get_rotation: Retrieves the euler angle rotation from the 4x4 matrix
* m: Input 4x4 matrix
* returns: vec4 euler angles rotation vector (prefer not to use the 4th component, as it might not have any meaning to a typical context)
*/
HPML_API vec4_t mat4_get_rotation(mat4_t m);

/* mat4_get_scale: Retrieves the scale componenets from the 4x4 matrix
* m: Input 4x4 matrix
* returns: vec4 scale vector
*/
HPML_API vec4_t mat4_get_scale(mat4_t m);

/* mat4_determinant: Calculates the determinant value of a 4x4 matrix
* m: Input 4x4 matrix
* returns: determinant value
Expand Down Expand Up @@ -168,7 +187,7 @@ static HPML_FORCE_INLINE mat4_t mat4_diagonal(float x, float y, float z, float w
};
}

/* mat4_trace: Calculates trace of 2x2 matrix
/* mat4_trace: Calculates trace of 2x2 matrix
* mat4_t m: Matrix of which the trace to be calculated
* returns: float trace value
*/
Expand Down Expand Up @@ -247,7 +266,7 @@ HPML_API mat4_t mat4_transpose(mat4_t m);
*/
HPML_API mat4_t mat4_lerp(mat4_t m1, mat4_t m2, float lerp_value);

/* mat4_inverse: Inverts a 4x4 matrix
/* mat4_inverse: Inverts a 4x4 matrix
* mat4_t m: Matrix to be inverted
* returns: mat4_t inverted 4x4 matrix
*/
Expand Down
25 changes: 23 additions & 2 deletions include/hpml/vec4.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#endif

#include <hpml/defines.h>
#include <hpml/vec4.h>
#include <hpml/vec2.h>
#include <hpml/vec3.h>

/*DATA*/
/*vec4_t struct*/
Expand All @@ -21,6 +24,24 @@ typedef union
float r, g, b, a;
};

struct
{
vec3_t xyz;
float _pad0;
};

struct
{
vec2_t xy;
vec2_t zw;
};

struct
{
float _pad1;
vec3_t yzw;
};

float v[4];
} vec4_t;

Expand Down Expand Up @@ -229,8 +250,8 @@ HPML_API vec4_t vec4_rotate(vec4_t v, float x, float y, float z);

/*vec4_axis_rotate*/
/*
let axis = a;
let vector = v;
let axis = a;
let vector = v;
let angle = r;
k = v - (v.a)a
Expand Down
19 changes: 18 additions & 1 deletion source/mat4.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ HPML_API float* const* const __mat4_data(mat4_t* m)
return (float* const* const)m->data;
}

HPML_API vec4_t mat4_get_position(mat4_t m)
{
return vec4(m.m03, m.m13, m.m23, m.m33);
}

HPML_API vec4_t mat4_get_rotation(mat4_t m)
{
/* TODO */
return vec4_zero();
}

HPML_API vec4_t mat4_get_scale(mat4_t m)
{
/* TODO */
return vec4_one();
}

HPML_API float mat4_det(mat4_t m)
{
return m.m00 * (m.m11 * (m.m22 * m.m33 - m.m23 * m.m32) - m.m12 * (m.m21 * m.m33 - m.m23 * m.m31) + m.m13 * (m.m21 * m.m32 - m.m22 * m.m31)) -
Expand Down Expand Up @@ -96,7 +113,7 @@ HPML_API mat4_t mat4_add(mat4_t m1, mat4_t m2)

HPML_API mat4_t mat4_sub(mat4_t m1, mat4_t m2)
{
return MAT4
return MAT4
{
m1.m00 - m2.m00,
m1.m01 - m2.m01,
Expand Down

0 comments on commit 1cdf646

Please sign in to comment.