Search code examples
c++openglglm-math

Creating a view matrix with glm


I am trying to create a view matrix with glm. I know of glm::lookAt and understand how it works. I want to know if there are similar functions that will acheive the same outcome that take different parameters. For example, is there a function that takes an up-vector, a directional bearing on the plane perpendicular(?) to the vector, and an angle? (i.e. The sky is that way, I turn N degrees/radians to my left and tilt my head M degrees/radians upward).


Solution

  • You can just build the matrix by composing a set of operations:

    // define your up vector
    glm::vec3 upVector = glm::vec3(0, 1, 0);
    // rotate around to a given bearing: yaw
    glm::mat4 camera = glm::rotate(glm::mat4(), bearing, upVector);
    // Define the 'look up' axis, should be orthogonal to the up axis
    glm::vec3 pitchVector = glm::vec3(1, 0, 0);
    // rotate around to the required head tilt: pitch
    camera = glm::rotate(camera, tilt, pitchVector);
    
    // now get the view matrix by taking the camera inverse
    glm::mat4 view = glm::inverse(camera);