library c++ opengl math glm-math

c++ - library - ¿Cómo inicializar un glm:: mat4 con una matriz?



glm github (4)

Estoy usando OpenGL Mathematics Library ( glm.g-truc.net ) y quiero inicializar un glm::mat4 con un float-array.

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

Esto no funciona

Supongo que la solución es trivial, pero no sé cómo hacerlo. No pude encontrar una buena documentación sobre glm. Agradecería algunos enlaces útiles.


Aunque no hay un constructor, GLM incluye funciones make_ * en glm/gtc/type_ptr.hpp :

#include <glm/gtc/type_ptr.hpp> float aaa[16]; glm::mat4 bbb = glm::make_mat4(aaa);


Puede escribir una función de adaptador:

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


También puede copiar directamente la memoria:

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


glm::mat4x4 view{ 2 / (right - left), 0, 0, 0, 0, 2 / (top - bottom), 0, 0, 0, 0, -2 / (farplane - nearplane), 0, -((right + left) / (right - left)), -((top + bottom) / (top - bottom)), -((farplane + nearplane) / (farplane - nearplane)), 1 }; glm::mat4 view{ 2 / (right - left), 0, 0, 0, 0, 2 / (top - bottom), 0, 0, 0, 0, -2 / (farplane - nearplane), 0, -((right + left) / (right - left)), -((top + bottom) / (top - bottom)), -((farplane + nearplane) / (farplane - nearplane)), 1 };