0

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");
       }
   }
}
CoolGuy
  • 107
  • 8

1 Answers1

1

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
Granate99
  • 26
  • 3
  • Please note that the lack of `main` in the posted snippet is likely a non-issue. The OP might just failed to produce a [mre]. About the code in this answer, I noticed you haven't used `mat4`, as explicitly asked in the question. Also, please don't suggest `void main()`: https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – Bob__ Dec 18 '22 at 21:26
  • @Granate99 Thanks! I did have a main() in my C program but I did not include that in my post. Also, the problem was that I was using "%d", when I should've used "%f" for printing floats in C. – CoolGuy Dec 18 '22 at 21:43