Search code examples
c++openglcamera

Does the camera face the x axis when the yaw is 0?


so I’ve been reading about the camera in learnopengl and noticed that in the yaw image, it seems as the though camera is facing the x axis when the yaw is 0. Shouldn’t the camera be facing the negative z axis? I attached the image to this message. In the image, the yaw is already a certain amount of degrees but if the yaw is 0, would that mean that the camera is facing the x axis?

Image of yaw from learnopengl


Solution

  • First, let's bring a little bit more context into your question so that we know what your are actually talking about.

    We can assume that when you say

    so I’ve been reading about the camera in learnopengl

    that by this you are specifically referring to the chapter called "Camera" in the https://learnopengl.com/Getting-started/Camera tutorial.

    Under the sub-section "Euler angles", there is the image which you are also including in your question.

    If you read a little bit further, you'd see the following definition of the direction vector, which that tutorial later uses to build a lookat matrix:

    direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
    direction.y = sin(glm::radians(pitch));
    direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
    

    So, just by looking at that and doing the math, we see that direction will be (1, 0, 0) when yaw and pitch are 0.

    But, if we read further, we see this paragraph:

    We've set up the scene world so everything's positioned in the direction of the negative z-axis. However, if we look at the x and z yaw triangle we see that a θ of 0 results in the camera's direction vector to point towards the positive x-axis. To make sure the camera points towards the negative z-axis by default we can give the yaw a default value of a 90 degree clockwise rotation. Positive degrees rotate counter-clockwise so we set the default yaw value to:

    yaw = -90.0f;
    

    So that is the answer to your question: In the context of that tutorial, by simply its own definition of the direction vector, it will point to (1, 0, 0) when both angles are 0. And to counteract this, that tutorial will assume that the initial value of yaw is -90.

    Sure, they could've just used a different formula for the direction vector components to yield (0, 0, -1) as the result when the angles are 0, but they didn't.