C++ C++;glm Opengl使用glm::mat4转换和旋转glm::vec4

C++ C++;glm Opengl使用glm::mat4转换和旋转glm::vec4,c++,opengl,translation,glm-math,C++,Opengl,Translation,Glm Math,因此,我尝试在我的批处理渲染系统的CPU上转换顶点。我曾尝试复制glsl,但它根本不起作用。(模型没有显示) 那么,我在哪里搞砸了?这个怎么样: // I think its here I might have messed up? Tvertex[i] *= glm::vec3(off.x, off.y, off.z); // I think that might be what you wanted: Tvertex[i] += glm::vec3(off.x, off.y, off.z

因此,我尝试在我的批处理渲染系统的CPU上转换顶点。我曾尝试复制glsl,但它根本不起作用。(模型没有显示)

那么,我在哪里搞砸了?

这个怎么样:

// I think its here I might have messed up?
Tvertex[i] *= glm::vec3(off.x, off.y, off.z); 

// I think that might be what you wanted:
Tvertex[i] += glm::vec3(off.x, off.y, off.z);

Util::createTransform()
返回一个
glm::mat4
,而您只需获取该矩阵最右边的列并将其存储在
glm::vec4

您正在尝试创建表示旋转和平移组合的变换矩阵。此操作不能由单个
vec4
表示。您可以单独对平移执行此操作,然后只需将相同的向量添加到所有顶点以平移周围的偏移。但是,对于旋转-或除平移之外的其他变换-您将需要完整的矩阵

由于
glm
使用了与GL使用的旧“固定函数”相同的约定,因此必须使用矩阵*向量乘法顺序将变换矩阵应用于顶点。因此,您的代码应该如下所示:

glm::mat4 off = Util::createTransform(offset, glm::vec3(0, 45, 0)) * off; //translated the vertex by the offset(supplied by the function) and rotates by 45 degrees on the Y axis

for (int i = 0; i < Tvertex.size(); i++) {
    Tvertex[i] = off * Tvertex[i];
}
glm::mat4 off=Util::createTransform(偏移量,glm::vec3(0,45,0))*off//将顶点平移偏移量(由函数提供),并在Y轴上旋转45度
对于(int i=0;i
// I think its here I might have messed up?
Tvertex[i] *= glm::vec3(off.x, off.y, off.z); 

// I think that might be what you wanted:
Tvertex[i] += glm::vec3(off.x, off.y, off.z);
glm::mat4 off = Util::createTransform(offset, glm::vec3(0, 45, 0)) * off; //translated the vertex by the offset(supplied by the function) and rotates by 45 degrees on the Y axis

for (int i = 0; i < Tvertex.size(); i++) {
    Tvertex[i] = off * Tvertex[i];
}