recp / cglm

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug in glm_mat4_print?

deadwanderer opened this issue · comments

In glm_mat4_print() in io.h, it appears that row and column get switched when printing out the matrix.
(

cglm/include/cglm/io.h

Lines 87 to 97 in 1d51459

for (i = 0; i < m; i++) {
fprintf(ostream, " |");
for (j = 0; j < n; j++)
if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT)
fprintf(ostream, " % *.*f", cw[j], CGLM_PRINT_PRECISION, (double)matrix[j][i]);
else
fprintf(ostream, " % *g", cw[j], (double)matrix[j][i]);
fprintf(ostream, " |\n");
}
)

for (i = 0; i < m; i++) {
    fprintf(ostream, "  |");

    for (j = 0; j < n; j++)
      if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT)
        fprintf(ostream, " % *.*f", cw[j], CGLM_PRINT_PRECISION,
                (double)matrix[j][i]);
      else
        fprintf(ostream, " % *g", cw[j], (double)matrix[j][i]);

    fprintf(ostream, "  |\n");
  }

Shouldn't the matrix[j][i] accesses here be matrix[i][j]? Every other access in this function has been matrix[i][j], and it makes the matrix print out transposed.

This same behavior occurs in glm_mat3_print and glm_mat2_print. If it is a bug, I'm more than happy to provide a PR.

Hi @deadwanderer,

Thanks for the feedbacks,

cglm uses column-major layout as stated in documentation/README. An option could be provided to support row-major maybe in the future.

In this case to print matrix elements we need to access them through matrix[j][i] to make it readable by humans. Here:

m00   m10   m20   m30     <---  print this line first 
m01   m11   m21   m31     <---  then
m02   m12   m22   m32     <---  then
m03   m13   m23   m33     <---  then

hope it helps