transform apply order in OpenGL confuse me

三世轮回 提交于 2020-01-05 09:14:12

问题


I read this tutorial, https://learnopengl.com/Getting-started/Transformations At the end, above the last picture, it says:

glm::mat4 trans;
trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));

I do not know what is the final trans values is, it is translate * rotate or rotate * translate ?

Here we first rotate the container around the origin (0,0,0) and once it's rotated, we translate its rotated version to the bottom-right corner of the screen. Remember that the actual transformation order should be read in reverse: even though in code we first translate and then later rotate, the actual transformations first apply a rotation and then a translation.

so the final transform = translate * rotate ? but why it does not write code like this:

glm::mat4 trans;
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f)); // revise order

the code in vertex shader is like: gl_Position = transform * vec4(aPos, 1.0); this seems like transform = translate * rotate because we need first apply rotate and then translate.

update: So, I know the multiply need read from right to left, because it is column major matrix, but I do not understand is when construct trans matrix, why do not use the revised version ?


回答1:


Output of the:

glm::mat4 trans;
trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));

Should be translate*rotate, I suspect that the authors wanted to mimic the left-to-right reading order not the order in which they are applied. You can confirm this by looking throught source code:

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

This computes m*translationMat. Because the top 3x3 corner for the translation matrix is the identity and fourth column is (v,1.0).



来源:https://stackoverflow.com/questions/52560301/transform-apply-order-in-opengl-confuse-me

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!