I am following a tutorial to 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?
GLM documentation says:
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 eye
and is faced towards center
. (And I don't know what the up
is)
It's not difficult, maybe you need to review the three coordinates: Object(or Model) coordinates, World coordinates and Camera(or View) coordinates.
Here, the
Up
vector defines the "upwards" direction in your 3D world (for this camera). For example, the value ofvec3(0, 0, 1)
means the Z-axis points upwards.Eye
is the point where you virtual 3D camera is located.And
Center
is the point which the camera looks at (center of the scene).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
, andUp
.Create a new coordinate system:
Recompute
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
,Y
here:Put everything into the resulting 4x4 matrix:
The
up
vector 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.eye
is the position of the camera's viewpoint, andcenter
is where you are looking at (a position). If you want to use a direction vectorD
instead of a center position, you can simply useeye + D
as the center position, whereD
can be a unit vector for example.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.