【问题标题】:GLM translation issueGLM 翻译问题
【发布时间】:2018-10-30 16:22:38
【问题描述】:

我有一个模型并且想要沿着它的轴移动它,如果我有一个变换矩阵glm::mat4 并且向上向量是glm::vec4 up(matrix[1]); 那么如果我想沿着向上向量移动模型值up*=d; 那么matrix=glm::translate(matrix,up);,结果矩阵不会向右移动模型,例如,如果向上向量 id (0,0.707106769,0.707106769)“模型绕 X 轴旋转 45d”并且我想将它移动 5 个单位,所以平移向量是 (0,3.535533845,3.535533845) 那么在平移之后,位置分量仅在 Y 轴上发生变化,因此它仅沿 Y 轴移动。

【问题讨论】:

    标签: c++ matrix 3d transformation glm-math


    【解决方案1】:

    translate的GLM源代码:

    template<typename T, qualifier Q>
    GLM_FUNC_QUALIFIER mat<4, 4, T, Q> translate(mat<4, 4, T, Q> const& m, vec<3, T, Q> const& v)
    {
        mat<4, 4, T, Q> Result(m);
        Result[3] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3];
        return Result;
    }
    

    应用平移的效果由现有矩阵的旋转分量(左上角的 3x3 子矩阵,或者如果底行是0 0 0 1,则为前 3 列)修改,即:

    glm::translateprepends,而不是appends,翻译。

    也就是说,上面的代码等价于:

    // create an identity matrix and apply the translation
    glm::mat4 translation = glm::translate(glm::mat4(1.f), up);
    
    // post-multiply (i.e. the applied translation comes FIRST)
    matrix = matrix * translation;
    

    你想要的效果可以通过:

    1)
    
    // pre-multiply (i.e. the applied translation comes AFTER)
    matrix = translation * matrix;
    

    或者等价地,在模型的local基础上构建翻译:

    2)
    
    // local up vector (Y-axis)
    glm::vec3 local_up(0.f, 1.f, 0.f);
    local_up *= d;
    
    // apply using translate as before
    matrix = glm::translate(matrix, local_up);
    

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 2013-05-03
      • 2012-10-27
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多