如何重新定义 glm 矩阵变量或删除其变换?

How to redifine glm matrix variable or remove its transform?

我需要在不使用 for 循环的情况下一个接一个地制作多个模型。我用 glm::mat4 model(1) 定义第一个,对其进行一些平移和旋转,然后我想删除它的所有变换。但是让它等于 NULL 是行不通的。我可以调用 glm 中的某些函数吗?

另外还有一个问题,有人可以解释为什么我在声明 glm 矩阵变量时需要添加“(1)”。没有它是行不通的。声明 glm 矩阵数组 glm::mat4 models[] 时是否还需要添加一些内容?因为数组似乎不存储变量。

glm::mat4 的默认构造函数不初始化矩阵,它使矩阵的字段未初始化。

glm API documentation refers to The OpenGL Shading Language specification 4.20.

5.4.2 Vector and Matrix Constructors

If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.

这意味着,要通过 Identity matrix 初始化矩阵,必须使用具有单个标量的构造函数:

glm::mat4 model(1.0f);

当然 Identity matrix 可以分配给现有矩阵:

model = glm::mat4(1.0f); 

动态数组,例如std::vector 可以轻松生成 100 个单位矩阵:

std::vector<glm::mat4> models(100, glm::mat4(1.0f));