recp / cglm

📽 Highly Optimized 2D / 3D Graphics Math (glm) for C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

proposal: glm_make_mat#x#

EasyIP2023 opened this issue · comments

Similar to glm::make_mat4x4(float *)

https://glm.g-truc.net/0.9.4/api/a00158.html#gaa287485a3978d319e60a1cadd8a1c139

Make 4x4 matrix given a float array of size 16.

Would probably be good to add the whole suite of glm::make_mat#x#(float *) functions.

https://glm.g-truc.net/0.9.4/api/a00158.html#func-members

Feedback on whether this would be accepted as PR. I can implement.

Hi @EasyIP2023,

Thanks for the proposal,

Feedback on whether this would be accepted as PR.

sure, a PR would be awesome :)

it could be:

CGLM_INLINE
void
glm_mat4_make(float * __restrict src, mat4 dest) {
  dest[0][0] = src[0];   dest[1][0] = src[4];
  dest[0][1] = src[1];   dest[1][1] = src[5];
  dest[0][2] = src[2];   dest[1][2] = src[6];
  dest[0][3] = src[3];   dest[1][3] = src[7];

  dest[2][0] = src[8];   dest[3][0] = src[12];
  dest[2][1] = src[9];   dest[3][1] = src[13];
  dest[2][2] = src[10];  dest[3][2] = src[14];
  dest[2][3] = src[11];  dest[3][3] = src[15];
}

or maybe:

CGLM_INLINE
void
glm_mat4_from_cols(float * __restrict src, mat4 dest) {
  dest[0][0] = src[0];   dest[1][0] = src[4];
  dest[0][1] = src[1];   dest[1][1] = src[5];
  dest[0][2] = src[2];   dest[1][2] = src[6];
  dest[0][3] = src[3];   dest[1][3] = src[7];

  dest[2][0] = src[8];   dest[3][0] = src[12];
  dest[2][1] = src[9];   dest[3][1] = src[13];
  dest[2][2] = src[10];  dest[3][2] = src[14];
  dest[2][3] = src[11];  dest[3][3] = src[15];
}

CGLM_INLINE
void
glm_mat4_from_rows(float * __restrict src, mat4 dest) {
  dest[0][0] = src[0];   dest[0][1] = src[4];
  dest[1][0] = src[1];   dest[1][1] = src[5];
  dest[2][0] = src[2];   dest[2][1] = src[6];
  dest[3][0] = src[3];   dest[3][1] = src[7];

  dest[0][2] = src[8];   dest[0][3] = src[12];
  dest[1][2] = src[9];   dest[1][3] = src[13];
  dest[2][2] = src[10];  dest[2][3] = src[14];
  dest[3][2] = src[11];  dest[3][3] = src[15];
}

the second one allows to copy from ro-major layout to cglm, the first one is also cool. These are just suggestions maybe we get better name alternatives and impl

Hey, @recp Thanks! Yeah personally I prefer the first function call name.

Transpose can be called after glm_mat4_make to simulate glm_mat4_from_rows 👍 thanks for the PR, let's continue in there 🚀

PR to follow: #299

@EasyIP2023 I'm going to close this, thanks for your contributions.