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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7351659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:49:02  来源:igfitidea点击:

How to initialize a glm::mat4 with an array?

c++openglmathglm-math

提问by j00hi

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

我正在使用 OpenGL 数学库 ( glm.g-truc.net) 并想glm::mat4用浮点数组初始化 a 。

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.

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

回答by Matthew Marshall

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

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

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

回答by Anderson Silva

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

回答by bdonlan

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