了解glm :: lookAt()

我正在学习一个教程来学习OpenGL,其中他们使用glm::lookAt()函数来构build一个视图,但我无法理解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 。 (我不知道这是什么)

你想了解内部工作还是只是参数?

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

至于内部工作或更多细节,这是构build视图matrix的常见基本function。 尝试阅读function相同的gluLookAt()的文档。

在这里, Upvector定义了3D世界中的“向上”方向(用于本相机)。 例如, vec3(0, 0, 1)意味着Z轴向上指向。

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

Center则是相机所看到的点(场景的中心)。

理解某件事的最好方法就是自己去做。 以下是如何使用3个vector构build相机转换: EyeCenterUp

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

创build一个新的坐标系:

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

重新计算Y = Z cross X

  Y = Z.Cross( X ); 

叉积的长度等于平行四边形的面积,对于非垂直的单位长度vector,其长度小于1.0。 所以在这里规范XY

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

把所有的东西放到最终的4x4matrix中:

  Matrix[0][0] = Xx; Matrix[1][0] = Xy; Matrix[2][0] = Xz; Matrix[3][0] = -X.Dot( Eye ); Matrix[0][1] = Yx; Matrix[1][1] = Yy; Matrix[2][1] = Yz; Matrix[3][1] = -Y.Dot( Eye ); Matrix[0][2] = Zx; Matrix[1][2] = Zy; Matrix[2][2] = Zz; 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; } 
 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) ) 

这并不难,也许您需要查看三个坐标: 对象(或模型)坐标世界坐标相机(或视图)坐标

    Interesting Posts