简体   繁体   English

了解 GLM 中的相机翻译 - openGL

[英]understanding camera translation in GLM- openGL

The GLM maths library for openGL gives this implementation for the construction of lookAt matrix . openGL 的 GLM 数学库为构造lookAt matrix提供了此实现。

template<typename T, qualifier Q>
    GLM_FUNC_QUALIFIER mat<4, 4, T, Q> lookAtLH(vec<3, T, Q> const& eye, vec<3, T, Q> const& center, vec<3, T, Q> const& up)
    {
        vec<3, T, Q> const f(normalize(center - eye));
        vec<3, T, Q> const s(normalize(cross(up, f)));
        vec<3, T, Q> const u(cross(f, s));

        mat<4, 4, T, Q> Result(1);
        Result[0][0] = s.x;
        Result[1][0] = s.y;
        Result[2][0] = s.z;
        Result[0][1] = u.x;
        Result[1][1] = u.y;
        Result[2][1] = u.z;
        Result[0][2] = f.x;
        Result[1][2] = f.y;
        Result[2][2] = f.z;
        Result[3][0] = -dot(s, eye);  //#this
        Result[3][1] = -dot(u, eye);  //#this
        Result[3][2] = -dot(f, eye);  //#this
        return Result;
    }

Everything is fine and okay except for the translation factors that is done in the last three lines I've marked with //#this .除了在我用//#this标记的最后三行中完成的翻译因素外,一切都很好。 The translation had to be done for the camera's world position x, y and z but instead it's done for the dot product of the camera's local coordinate and the direction vector, which can't possibly be equal.必须为相机的世界 position x, y and z完成转换,但它是为相机的局部坐标和方向向量的点积完成的,这不可能相等。

Consider one case where the vector 2*eye (eye being the camera's position) is passed as the vector center (center being the target position) then the camera's local z-axis would coincide with the direction vector giving us the translation factor for the camera as [0,0,1] , so we'd basically be moving along the world along only the z-axis (since we don't move the camera we'd be moving the world along just z axis in the negative direction) which is something we wouldn't want.考虑一种情况,其中vector 2*eye (眼睛是相机的位置)作为vector center (中心是目标位置)传递,然后相机的局部 z 轴将与方向矢量重合,从而为我们提供相机的平移因子作为[0,0,1] ,所以我们基本上只沿着z-axis沿着世界移动(因为我们不移动相机,我们将沿着负方向沿着 z 轴移动世界)这是我们不想要的。 Where am I missing the point or why is this being done for the translation?我在哪里遗漏了重点或者为什么要进行翻译?

glm::lookAt defines the view matrix. glm::lookAt定义视图矩阵。 The view matrix transforms vertex coordinates from world space to view space.视图矩阵将顶点坐标从世界空间转换到视图空间。
eye , center and up are positions respectively vectors in world space, which define the the position and orientation of the camera in world space. eyecenterup分别是世界空间中的位置向量,它们定义了相机在世界空间中的 position 和方向。 eye , center and up define the view space. eyecenterup定义视图空间。 If you would setup a matrix by this vectors, then the matrix would transform from view space to world space.如果你用这个向量设置一个矩阵,那么矩阵将从视图空间转换到世界空间。
Since the view matrix has to do the opposite (world space -> view space), the view matrix is the inverse matrix of that matrix which is defined by eye , center and up .由于视图矩阵必须执行相反的操作(世界空间 -> 视图空间),因此视图矩阵是由eyecenterup定义的那个矩阵的逆矩阵 glm::lookAt is a optimized algorithm for computing an inverse matrix in this spacial case. glm::lookAt是一种用于计算这种特殊情况下的逆矩阵的优化算法。
Note s , u , f are transposed when they are assigned to the matrix.注意suf在分配给矩阵时被转置。

The translation of the inverse matrix is not the negative translation of the matrix.逆矩阵的平移不是矩阵的负平移。 The translation of the inverse matrix has to consider the orientation (rotation).逆矩阵的平移必须考虑方向(旋转)。 Because of that the translation vector has to be rotated.因此必须旋转平移向量。 The rotation of a (3d) vector by a 3x3 Rotation matrix can be computed by (is the same as) the 3 Dot products of the axis vectors and the direction vector.通过 3x3旋转矩阵旋转 (3d) 矢量可以通过(相同于)轴矢量和方向矢量的 3点积来计算。 ( s , u , f ) define a 3x3 rotation matrix and eye is transformed by this matrix. ( s , u , f ) 定义一个 3x3 旋转矩阵, eye由该矩阵变换。

What the code actually dose is to concatenate a rotation by transposed ( s , u , f ) and a translation by -eye (very simplified pseudo code):代码实际上是通过转置( suf )连接旋转和-eye (非常简化的伪代码)的翻译:

viewmatrix = transpose(rotation(s, u, f)) * translation(-eye)

The dot product of one vector, a , with another normalized vector, n , can be thought of as the projection of a onto n .一个向量a与另一个归一化向量n的点积可以被认为是an的投影。 So, all that's happening here is that the eye vector is being projected onto f , s , and u , which are the basis vectors of the rotated coordinate frame.因此,这里发生的所有事情是eye矢量被投影到fsu上,它们是旋转坐标系的基本矢量。 With these projections, we can learn the x , y , and z coordinates of eye in the f-su coordinate frame.通过这些投影,我们可以在f-su坐标系中学习eyexyz坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM