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

时间:2011-09-08 16:49:45

标签: c++ opengl math glm-math

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

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

这不起作用。

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

4 个答案:

答案 0 :(得分:64)

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

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

答案 1 :(得分:7)

您也可以直接复制内存:

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

答案 2 :(得分:3)

您可以编写适配器功能:

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

答案 3 :(得分:-3)

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