【问题标题】:C++ glm Opengl Transforming and rotating a glm::vec4 with glm::mat4C++ glm Opengl 使用 glm::mat4 转换和旋转 glm::vec4
【发布时间】:2016-01-21 16:52:00
【问题描述】:

所以我试图在 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?
}

这里是“Util::createTransform”函数:

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

那么,我在哪里搞砸了?

【问题讨论】:

    标签: c++ opengl translation glm-math


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      这个怎么样:

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

      【讨论】:

        猜你喜欢
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-28
        • 2016-05-23
        • 2013-03-13
        • 1970-01-01
        相关资源
        最近更新 更多