how to understand the camera class. we should give a few comments about it.
let’s begin the picture.
we use the camera position subtract the target position. this gives the reverse vector that the camera forwards.
next, we will explain how to get view matrix.
view matrix is in view coordinate system. so we should define three perpendicular axes.
1/ forward axis
2/ up vector
3/ right vector.
we know the position of the camera, and the target we look at. this two position gives the forward vector.
up vector we can define it as the +y axis.
then we can use cross produce to get the right vector. picture like this:
last, we can use forward vector cross right vector to get up vector.
after these steps we can get three perpendicular vectors. then we can create the view matrix like this:
the following parts will explain how to tune the camera front vector. if we change the camera’s front vector. we will feel the first person viewpoint effect.
we will give a vector to represent the front vector of the camera.
vector r is forward vector of the camera.
we get
x = f.cosb.cosa
y = f.sinb
z = f.cosb.sina
then, we should normalized this vector.
last we should know how to change the value by receiving mouse input.
we can add the y2-y1 to pitch angle. we can add the x2 - x1 to yaw value.
afther this this key points you will understand the following code segment:
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if(firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.05;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if(pitch > 89.0f)
pitch = 89.0f;
if(pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
we also limit the pitch angle between -89 to 89.
next article will give a simple demo to feel the result by changing the camera view parameters.