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

时间:2016-01-21 16:52:01

标签: c++ opengl translation glm-math

所以我试图在我的批处理渲染系统上转换CPU的顶点。我试图复制glsl,但它根本不起作用。 (模型没有出现)

glm::vec4 off = glm::vec4(0, 0, 0, 1);

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] *= glm::vec3(off.x, off.y, off.z); //I think its here I might have messed up?
}

这是&#34; Util :: createTransform&#34;功能:

glm::mat4 Util::createTransform(glm::vec3 pos, glm::vec3 rot) {
    glm::mat4 trans = glm::mat4(1.0);
    trans = glm::rotate(trans, glm::radians(rot.x), glm::vec3(1, 0, 0));
    trans = glm::rotate(trans, glm::radians(rot.y), glm::vec3(0, 1, 0));
    trans = glm::rotate(trans, glm::radians(rot.z), glm::vec3(0, 0, 1));
    trans = glm::translate(trans, pos);
    return trans;
}

那么,我在哪里搞砸了?

2 个答案:

答案 0 :(得分:0)

这个怎么样:

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

答案 1 :(得分:0)

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

您正在尝试创建一个表示旋转和平移组成的变换矩阵。此操作无法由单个vec4表示。您可以单独为转换执行此操作,然后只需将相同的矢量添加到所有顶点以转换偏移量。但是,通过旋转 - 或除翻译之外的其他转换 - 您将需要完整的矩阵。

由于glm使用相同的约定,旧的&#34;固定功能&#34;使用GL时,必须使用Matrix * Vector乘法顺序将变换矩阵应用于顶点。所以你的代码应该是这样的:

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