C++ 理解 glm::lookAt()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21830340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:50:22  来源:igfitidea点击:

Understanding glm::lookAt()

c++openglglm-math

提问by Cashif Ilyas

I am following a tutorialto learn OpenGL in which they used glm::lookAt()function to build a view but I cannot understand the working of glm::lookAt()and apparently, there is no detailed documentation of GLM. Can anyone help me understand the parameters and working of glm::lookAt()please?

我正在学习 OpenGL的教程,其中他们使用glm::lookAt()函数来构建视图,但我无法理解其工作原理,glm::lookAt()而且显然没有 GLM 的详细文档。任何人都可以帮助我了解参数和工作glm::lookAt()吗?

GLM documentation says:

GLM 文档说:

detail::tmat4x4<T> glm::gtc::matrix_transform::lookAt   
(   
    detail::tvec3< T > const &  eye,
    detail::tvec3< T > const &  center,
    detail::tvec3< T > const &  up 
)

My current understanding is that camera is located at eyeand is faced towards center. (And I don't know what the upis)

我目前的理解是相机位于eye并面向center。(我不知道是什么up

回答by Preet Kukreti

The upvector is basically a vector defining your world's "upwards" direction. In almost all normal cases, this will be the vector (0, 1, 0)i.e. towards positive Y. eyeis the position of the camera's viewpoint, and centeris where you are looking at (a position). If you want to use a direction vector Dinstead of a center position, you can simply use eye + Das the center position, where Dcan be a unit vector for example.

up向量基本上是一个定义您的世界“向上”方向的向量。在几乎所有正常情况下,这将是向量,(0, 1, 0)即朝向正 Y。eye是相机视点的位置,并且center是您正在查看的位置(位置)。如果要使用方向向量D而不是中心位置,则可以简单地eye + D用作中心位置,例如,D可以是单位向量。

As for the inner workings, or more details, this is a common basic function for building a view matrix. Try reading the docs for gluLookAt()which is functionally equivalent.

至于内部工作,或者更多细节,这是构建视图矩阵的常见基本功能。尝试阅读功能等效的gluLookAt()文档。

回答by Sergey K.

Here, the Upvector defines the "upwards" direction in your 3D world (for this camera). For example, the value of vec3(0, 0, 1)means the Z-axis points upwards.

在这里,Up向量定义了 3D 世界中的“向上”方向(对于此相机)。例如,值vec3(0, 0, 1)表示 Z 轴指向上方。

Eyeis the point where you virtual 3D camera is located.

Eye是您的虚拟 3D 相机所在的点。

And Centeris the point which the camera looks at (center of the scene).

并且Center是相机所看的点(场景的中心)。

The best way to understand something is to make it yourself. Here is how a camera transformation can be constructed using 3 vectors: Eye, Center, and Up.

理解某事的最好方法是自己动手。下面是如何的相机转化可以使用3个向量来构造:EyeCenter,和Up

LMatrix4 LookAt( const LVector3& Eye, const LVector3& Center, const LVector3& Up )
{
    LMatrix4 Matrix;

    LVector3 X, Y, Z;

Create a new coordinate system:

创建一个新的坐标系:

    Z = Eye - Center;
    Z.Normalize();
    Y = Up;
    X = Y.Cross( Z );

Recompute Y = Z cross X:

重新计算Y = Z cross X

    Y = Z.Cross( X );

The length of the cross product is equal to the area of the parallelogram, which is < 1.0 for non-perpendicular unit-length vectors; so normalize X, Yhere:

叉积的长度等于平行四边形的面积,对于非垂直的单位长度向量小于 1.0;所以规范化XY这里:

    X.Normalize();
    Y.Normalize();

Put everything into the resulting 4x4 matrix:

将所有内容放入生成的 4x4 矩阵中:

    Matrix[0][0] = X.x;
    Matrix[1][0] = X.y;
    Matrix[2][0] = X.z;
    Matrix[3][0] = -X.Dot( Eye );
    Matrix[0][1] = Y.x;
    Matrix[1][1] = Y.y;
    Matrix[2][1] = Y.z;
    Matrix[3][1] = -Y.Dot( Eye );
    Matrix[0][2] = Z.x;
    Matrix[1][2] = Z.y;
    Matrix[2][2] = Z.z;
    Matrix[3][2] = -Z.Dot( Eye );
    Matrix[0][3] = 0;
    Matrix[1][3] = 0;
    Matrix[2][3] = 0;
    Matrix[3][3] = 1.0f;

    return Matrix;
}

回答by Denny

After the camera(or eye) and target(center), we can still rotate the camerato get different pictures, so here comes the upvector which makes the camerafixed and cannot be rotate.

camera(or eye) 和target( center) 之后,我们仍然可以旋转camera得到不同的图片,所以这里来了up使camera固定不能旋转的向量。

回答by peikun

detail::tmat4x4<T> glm::gtc::matrix_transform::lookAt   
(   
    detail::tvec3< T > const &  //eye position in worldspace
    detail::tvec3< T > const &  //the point where we look at
    detail::tvec3< T > const &  //the vector of upwords(your head is up)
)

It's not difficult, maybe you need to review the three coordinates: Object(or Model) coordinates, World coordinatesand Camera(or View) coordinates.

不难,也许你需要复习三个坐标: 对象(或模型)坐标世界坐标相机(或视图)坐标