理解 glm::lookAt()

问题描述 投票:0回答:4

我正在按照 tutorial 学习 OpenGL,其中他们使用

glm::lookAt()
函数来构建视图,但我无法理解
glm::lookAt()
的工作原理,显然,没有 GLM 的详细文档。谁能帮我了解
glm::lookAt()
的参数和工作原理吗?

GLM 文档说:

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

我目前的理解是相机位于

eye
并且面向
center
。 (而且我不知道
up
是什么)

c++ opengl glm-math
4个回答
92
投票

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

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


59
投票

此处,

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

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

并且

Center
是相机所注视的点(场景的中心)。

理解某件事的最好方法就是自己动手去做。以下是如何使用 3 个向量构建相机变换:

Eye
Center
Up

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

    LVector3 X, Y, Z;

创建新的坐标系:

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

重新计算

Y = Z cross X
:

    Y = Z.Cross( X );

叉积的长度等于平行四边形的面积,即< 1.0 for non-perpendicular unit-length vectors; so normalize

X
Y
这里:

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

将所有内容放入生成的 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;
}

10
投票

设置

camera
(或
eye
)和
target
center
)后,使
camera
面向
target
,我们仍然可以旋转
camera
来获得不同的图片,所以这里是
up
向量,使
camera
固定且不能旋转。


-32
投票
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)
)

也许您需要检查三个坐标: 对象(或模型)坐标世界坐标相机(或视图)坐标

© www.soinside.com 2019 - 2024. All rights reserved.