如何使用数组初始化glm :: mat4?

j00*_*0hi 36 c++ opengl math glm-math

我正在使用OpenGL数学库(glm.g-truc.net),并希望glm::mat4使用float-array 初始化a .

float aaa[16];
glm::mat4 bbb(aaa);
Run Code Online (Sandbox Code Playgroud)

这不起作用.

我想这个解决方案很简单,但我不知道怎么做.我找不到关于glm的好文档.我会很感激一些有用的链接.

Mat*_*all 67

虽然没有构造函数,但GLM在glm/gtc/type_ptr.hpp中包含make_*函数:

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

  • 另外不要忘记确保源数组以**列**方式存储,否则你需要添加`glm :: mat4 bbbT = glm :: make_mat4(aaa); glm :: mat4 bbb = glm :: transpose(bbbT);` (12认同)

小智 6

您也可以直接复制内存:

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 ) );
Run Code Online (Sandbox Code Playgroud)


bdo*_*lan 5

您可以编写一个适配器函数:

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);
Run Code Online (Sandbox Code Playgroud)