سؤال

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.

هل كانت مفيدة؟

المحلول

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);

نصائح أخرى

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 ) );

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top