Hi I was trying out some c++ opengl codes and made a camera. I wanted to give the scene a mouse input to look around, so I added this code just like the tutorial here. https://learnopengl.com/Getting-started/Camera
However, there are some mathematical concepts I do not understand regarding the yaw and pitch values. Here is the callback function of the mouse movement.
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
if (firstMouse) //preventing large jumps
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.1f;
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 = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
//cameraFront is the direction vector of the camera. Where the camera is looking at
cameraFront = glm::normalize(front);
}
Here are the global variables with their initial values used in the mouse_callback function just in case.
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
//lookAt function requires position, target's position, up vector respectively.
There are 2 things I don't understand. As I understand, yaw and pitch are calculated from the y axis and the x axis respectively. And by using our right hand and putting the thumb towards the + direction of the axis, the direction the other fingers are curved is the positive direction of the angle.
Now let's say that I moved the mouse to the right without changing the yoffset. Then according to the mouse_callback function the yaw value increases since xoffset is positive. Since the positive direction of the y axis points to the top of the window we're watching, the increase in yaw means that the direction vector of the camera should rotate to the left direction right? But in the program, the camera turns and shows the right part of the scene. I don't understand what's happening.
If I know what's happening here, I think I can understand why the calculation order to get yoffset is different from getting the xoffset value as well.
Any help would be greatly appreciated. Tell me if I'm mistaking any mathematical concepts or something. Thanks in advance!