``````// Custom implementation of the LookAt function
glm::mat4 calculate_lookAt_matrix(glm::vec3 position, glm::vec3 target, glm::vec3 worldUp)
{
// 1. Position = known
glm::vec3 zaxis = glm::normalize(position - target);
// 3. Get positive right axis vector
glm::vec3 xaxis = glm::normalize(glm::cross(glm::normalize(worldUp), zaxis));
// 4. Calculate camera up vector
glm::vec3 yaxis = glm::cross(zaxis, xaxis);

// Create translation and rotation matrix
// In glm we access elements as mat[col][row] due to column-major layout
glm::mat4 translation; // Identity matrix by default
translation = -position.x; // Third column, first row
translation = -position.y;
translation = -position.z;
glm::mat4 rotation;
rotation = xaxis.x; // First column, first row
rotation = xaxis.y;
rotation = xaxis.z;
rotation = yaxis.x; // First column, second row
rotation = yaxis.y;
rotation = yaxis.z;
rotation = zaxis.x; // First column, third row
rotation = zaxis.y;
rotation = zaxis.z;

// Return lookAt matrix as combination of translation and rotation matrix
return rotation * translation; // Remember to read from right to left (first translation then rotation)
}
``````

This is a custom implementation of the lookAt function.
What i can’t understand is how exactly these axes coordinates makes the world rotate around us in that rotation matrix, inside the LookAt function? and how are we looking at the direction we specified by simply multiplying our vertices with that rotation matrix? In other words, i couldn’t understand how rotation matrix works in this function.