47

I'm using the OpenGL Mathematics Library (glm.g-truc.net) and want to initialize a glm::mat4 with a float-array.

float aaa[16];
glm::mat4 bbb(aaa);

This doesn't work.

I guess the solution is trivial, but I don't know how to do it. I couldn't find a good documentation about glm. I would appreciate some helpful links.

genpfault
  • 51,148
  • 11
  • 85
  • 139
j00hi
  • 5,420
  • 3
  • 45
  • 82

3 Answers3

93

Although there isn't a constructor, GLM includes make_* functions in glm/gtc/type_ptr.hpp:

#include <glm/gtc/type_ptr.hpp>
float aaa[16];
glm::mat4 bbb = glm::make_mat4(aaa);
Someone
  • 560
  • 9
  • 21
Matthew Marshall
  • 5,793
  • 2
  • 21
  • 14
  • 22
    Also don't forget to ensure that the source array is stored **column-wise**, otherwise you will need to add `glm::mat4 bbbT = glm::make_mat4(aaa); glm::mat4 bbb = glm::transpose(bbbT);` – Rian Rizvi Sep 03 '14 at 16:19
11

You can also directly copy the memory:

float aaa[16] = {
   1, 2, 3, 4,
   5, 6, 7, 8,
   9, 10, 11, 12,
   13, 14, 15, 16
};
glm::mat4 bbb;

memcpy( glm::value_ptr( bbb ), aaa, sizeof( aaa ) );
Anderson Silva
  • 131
  • 1
  • 4
5

You could write an adapter function:

template<typename T>
tvec4<T> tvec4_from_t(const T *arr) {
    return tvec4<T>(arr[0], arr[1], arr[2], arr[3]);
}

template<typename T>
tmat4<T> tmat4_from_t(const T *arr) {
    return tmat4<T>(tvec4_from_t(arr), tvec4_from_t(arr + 4), tvec4_from_t(arr + 8), tvec4_from_t(arr + 12));
}


// later
float aaa[16];
glm::mat4 bbb = tmac4_from_t(aaa);
bdonlan
  • 224,562
  • 31
  • 268
  • 324