在OpenGL中转换应用顺序令我感到困惑 [英] transform apply order in OpenGL confuse me
问题描述
我阅读了本教程, https://learnopengl.com/Getting-started/Transformations 最后,在最后一张图片的上方,它说:
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));
我不知道最终的转换值是什么,它是 translate *旋转或 rotate *翻译吗?
I do not know what is the final trans values is, it is translate * rotate or rotate * translate ?
在这里,我们首先绕原点(0,0,0)旋转容器,然后旋转一次 它旋转了,我们将其旋转版本翻译到右下角 屏幕的一角. 记住实际的转换顺序 应该反读:即使在代码中我们先翻译并 然后旋转,实际的变换首先应用旋转 然后是翻译.
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.
那么最后的 transform =翻译*旋转吗? 但是为什么它不写这样的代码:
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
顶点着色器中的代码如下:
gl_Position = transform * vec4(aPos, 1.0);
这似乎是 transform =翻译*旋转,因为我们需要先应用旋转"然后进行翻译.
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 ?
推荐答案
输出:
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;
}
这将计算 m * translationMat .因为翻译矩阵的顶部3x3角是单位,而第四列是(v,1.0)
.
This computes m*translationMat. Because the top 3x3 corner for the translation matrix is the identity and fourth column is (v,1.0)
.
这篇关于在OpenGL中转换应用顺序令我感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!