Search code examples
c++openglcameraglm-mathperspectivecamera

How can I set up a camera at bird's eye view using GLM?


I am trying to set up my camera at a bird's eye perspective. By that I mean pointing straight down. This is what I've initialized so far:

glm::vec3 camPosition = glm::vec3(0.0f, 10.0f, 0.0f);  // camera's position
glm::vec3 camFront = glm::vec3(0.0f, 0.0f, 0.0f);  // where the camera is pointing
glm::vec3 camUp = glm::vec3(0.0f, 0.0f, 1.0f);

I pass this into the glm::lookat function but this is not working at all. Perhaps I haven't understood it that well...


Solution

  • I am trying to set up my camera at a bird's eye perspective.

    I recommend to do the following. Define 2 vectors.

    1. Define the up vector of the world. This means the vector, which points form the ground to the sky, in the coordinate system of your world:

       glm::vec3 world_up( 0.0f, 0.0f, 1.0f );
      
    2. Define the direction to the north in the coordinate system of your world:

       glm::vec3 world_north( 0.0f, 1.0f, 0.0f );
      

    With this information the vectors of the view coordinates system can be set up.

    1. camPosition is the position of the "bird". A point hight up in the sky:

      float height = 10.0f;
      glm::vec3 camPosition = world_up * 10.0f;
      
    2. camTraget it the position where the "bird" is looking at. A point on the ground:

      glm::vec3 camTraget = glm::vec3(0.0f, 0.0f, 0.0f);
      
    3. camUp is perpendicular to the vector from camPosition to camTraget. Since the "bird" looks at the ground it is the flight direction of the bird (e.g. to the north):

      glm::vec3 camUp = world_north;
      

    With this vectrs the view matrix can be set up by glm::lookAt():

    glm::mat4 view = glm::lookAt( camPosition, camTraget, camUp );