Search code examples
cglm-math

How to create a cglm Mat4 from a float[16] array?


I know C++ has glm::make_mat4() with #include <glm/gtc/type_ptr.hpp>, but there does not seem to be a make_mat4() function available in CGLM.

Can someone please tell me how to create a CGLM Mat4 from a float[16] array?

I tried the following, but the Mat4 does not seem to print the float values I'm expecting.

#include "cglm/cglm.h" 
#include "cglm/call.h"

void main() {

   // Atempt #1
   float  aaa[16] = {
      1, 2, 3, 4,
      5, 6, 7, 8,
      9, 10, 11, 12,
      13, 14, 15, 16
   };
   mat4 bbb = {
   { aaa[0],aaa[1],aaa[2],aaa[3] },
   { aaa[4],aaa[5],aaa[6],aaa[7] },
   { aaa[8],aaa[9],aaa[10],aaa[11] },
   { aaa[12],aaa[13],aaa[14],aaa[15] }
   };
   for (int i = 0; i < 4; i++) {
       for (int k = 0; k < 4; k++) {
           printf("%d", bbb[i][k]);
           printf("\n");
       }
   }
   printf("\n");

   // Atempt #2
   //void *memcpy(void *dest, const void * src, size_t n)
   memcpy(bbb, aaa, sizeof(aaa));

   for (int i = 0; i < 4; i++) {
       for (int k = 0; k < 4; k++) {
           printf("%d", bbb[i][k]);
           printf("\n");
       }
   }
}

Solution

  • You need to make a void main() function as an entry point. Otherwise the compiler wont know where you program starts.

    
    int main(){
        float  aaa[16] = {
           1, 2, 3, 4,
           5, 6, 7, 8,
           9, 10, 11, 12,
           13, 14, 15, 16
        };
        float bbb[4][4] = {
        { aaa[0],aaa[1],aaa[2],aaa[3] },
        { aaa[4],aaa[5],aaa[6],aaa[7] },
        { aaa[8],aaa[9],aaa[10],aaa[11] },
        { aaa[12],aaa[13],aaa[14],aaa[15] }
        };
        for (int i = 0; i < 4; i++) {
            for (int k = 0; k < 4; k++) {
                printf("%f", bbb[i][k]);
                printf("\n");
            }
        }
        printf("\n");
    }
    

    This code works in for me.

    The diffrence to your code is:

    1. The main function
    2. I changed the data type from the bbb array to float and used the dimensions 4 by 4
    3. I used %f in the printf function for printing the values