Last update: 2 September 2015

Tutorial 4: Quaternion camera movement

This tutorial implements a simple quaternion camera, where we can move and look in a 3D world. The Camera class is not optimized , So if you plan to use it in a real time application consider doing some optmization. I added an exponentiel fog to the pixel shader. For real time application its recommended to calculate the fog factor per vertex fog and write result to a varying variable that can be used by the pixel shader.  The tutorial feature multi texturing to add some details to the terrain.

in the function UpdateControls(const CControlPad& pad) in the Tutorial.cpp. To rotate the camera, we first get the current quaternion representation of the camera and we multiply it by the appropriate rotation quaternion to turn the camera based on the received control type ( rotate left/right or up/down). To move the camera, we take the current camera position and forward vector, and we change the camera position along the forward vector.


const float step = 0.6f;//mvt and look speed
   
    //Get camera quaternion
    const Quatf& q = m_pCamera->GetQuaternionRepresentation();
   
    if(pad.keyUp) // key up
    {
        Quatf rot = Quatf::fromAxisRot(vec3i(1, 0, 0), -step);
        Quatf qf = rot * q; //this will rotate the camera quaternion arround x axis (look up)
        m_pCamera->SetQuaternion(qf);// set the new camera quaternion
    }
   
    if(pad.keyDown)
    {
        Quatf rot = Quatf::fromAxisRot(vec3i(1, 0, 0), step);
        Quatf qf  = rot * q  ; //this will rotate the camera quaternion arround x axis (look down)
        m_pCamera->SetQuaternion(qf);
    }
   
    if(pad.keyLeft)
    {
        Quatf rotX = Quatf::fromAxisRot(vec3i(0, 0, 1), -step);
        Quatf qf  = q * rotX;// rotate the camera arround z vertical axis
        m_pCamera->SetQuaternion(qf);
    }
   
    if(pad.keyRight)
    {
        Quatf rotX = Quatf::fromAxisRot(vec3i(0, 0, 1), step);
        Quatf qf  = q * rotX;// rotate the camera arround z vertical axis
        m_pCamera->SetQuaternion(qf);
    }
   
    if(pad.keyMoveUp)
    {
        vec3f pos = m_pCamera->GetPosition();// camera position
        vec3f forward = m_pCamera->GetForwardVector();// camera forward vector
        pos += forward * step;// move camera arround forward vector
        m_pCamera->SetPosition(pos);//apply new positiion
       
    }
   
    else if(pad.keyMoveDown)
    {
        vec3f pos = m_pCamera->GetPosition();//camera position
        vec3f forward = m_pCamera->GetForwardVector();//camera forward vector
        pos -= forward * step;//move camera arrounv forward vector
        m_pCamera->SetPosition(pos);//apply new position
    }