Beispiel #1
0
Matrix4f computeModelViewMatrix( const Vector3f& eye, const Vector3f& center )
{
    const Vector3f zAxis = vmml::normalize( eye - center );

    // Avoid Gimbal lock effect when looking upwards/downwards
    Vector3f up( Vector3f::UP );
    const float angle = zAxis.dot( up );
    if( 1.f - std::abs( angle ) < 0.0001f )
    {
        Vector3f right( Vector3f::RIGHT );
        if( angle > 0 ) // Looking downwards
            right = Vector3f::LEFT;
        up = up.rotate( 0.01f, right );
        up.normalize();
    }

    const Vector3f xAxis = vmml::normalize( up.cross( zAxis ));
    const Vector3f yAxis = zAxis.cross( xAxis );

    Matrix3f rotationMatrix = Matrix4f::IDENTITY;
    rotationMatrix.set_column( 0, xAxis );
    rotationMatrix.set_column( 1, yAxis );
    rotationMatrix.set_column( 2, zAxis );

    return computeModelViewMatrix( rotationMatrix, eye );
}