我知道 C++ 有 glm::make_mat4() 和 #include
有人可以告诉我如何从 float[16] 数组创建 CGLM Mat4 吗?
我尝试了以下操作,但 Mat4 似乎没有打印我期望的浮点值。
#include "cglm/cglm.h"
#include "cglm/call.h"
void main() {
// Atempt #1
float aaa[16] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
mat4 bbb = {
{ aaa[0],aaa[1],aaa[2],aaa[3] },
{ aaa[4],aaa[5],aaa[6],aaa[7] },
{ aaa[8],aaa[9],aaa[10],aaa[11] },
{ aaa[12],aaa[13],aaa[14],aaa[15] }
};
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
printf("%d", bbb[i][k]);
printf("\n");
}
}
printf("\n");
// Atempt #2
//void *memcpy(void *dest, const void * src, size_t n)
memcpy(bbb, aaa, sizeof(aaa));
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
printf("%d", bbb[i][k]);
printf("\n");
}
}
}
您需要创建一个
void main()
函数作为入口点。否则编译器不会知道你的程序从哪里开始。
int main(){
float aaa[16] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
float bbb[4][4] = {
{ aaa[0],aaa[1],aaa[2],aaa[3] },
{ aaa[4],aaa[5],aaa[6],aaa[7] },
{ aaa[8],aaa[9],aaa[10],aaa[11] },
{ aaa[12],aaa[13],aaa[14],aaa[15] }
};
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
printf("%f", bbb[i][k]);
printf("\n");
}
}
printf("\n");
}
这段代码对我有用。
与您的代码的区别是: