GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


OpenGL Camera by Ozwald
1
 
class glCamera
{
    public:
    GLfloat    m_MaxPitchRate, m_MaxHeadingRate,
            m_HeadingDegrees, m_PitchDegrees,
            m_MaxForwardVelocity, m_ForwardVelocity;
            
    glQuaternion m_qHeading, m_qPitch;
    glPoint m_Position;
    fvector3 m_DirectionVector;
    fvector3 target;
    
    void ChangeVelocity( GLfloat vel );
    void ChangeHeading( GLfloat degrees );
    void ChangePitch( GLfloat degrees );
    void Shuffle( GLfloat speed );
    glCamera();
    virtual ~glCamera();
};

// ---------

glCamera::glCamera()
{
    m_MaxPitchRate            = 1.0f;
    m_MaxHeadingRate        = 1.0f;
    m_HeadingDegrees        = 0.0f;
    m_PitchDegrees            = 0.0f;
    m_MaxForwardVelocity    = 0.1f;
    m_ForwardVelocity        = 0.0f;
    
    m_Position.y = 1.0f;
}

glCamera::~glCamera()
{    }

void glCamera::SetPerspective()
{
    GLfloat Matrix[16];
    glQuaternion q;
    
    m_qPitch.CreateFromAxisAngle    ( 1.0f, 0.0f, 0.0f, m_PitchDegrees );
    //m_qPitch.CreateFromAxisAngle    ( 1.0f, 0.0f, 0.0f, 0.0f );
    m_qHeading.CreateFromAxisAngle    ( 0.0f, 1.0f, 0.0f, m_HeadingDegrees );
    
    q = m_qPitch * m_qHeading;
    q.CreateMatrix( Matrix );
    
    glMultMatrixf( Matrix );
    
    m_qPitch.CreateMatrix( Matrix );
    m_DirectionVector.y = Matrix[9];
    
    q = m_qHeading * m_qPitch;
    q.CreateMatrix( Matrix );
    m_DirectionVector.x = Matrix[8];
    m_DirectionVector.z = Matrix[10];
    
    m_DirectionVector = m_DirectionVector * m_ForwardVelocity;
    
    m_Position.x += m_DirectionVector.x;
    //m_Position.y += m_DirectionVector.y;    // this is what makes us fly around up&down
    m_Position.z += m_DirectionVector.z;
    
    gluLookat( -m_Position.x, -m_Position.y, -m_Position.z, target.x, target.y, target.z, m_PitchDegrees, m_HeadingDegrees, 0.0f );
}

void glCamera::ChangePitch( GLfloat degrees )
{
    if( fabs(degrees) < fabs(m_MaxPitchRate) ) {
        m_PitchDegrees += degrees;
    } else {
        if( degrees < 0 ) {
            m_PitchDegrees -= m_MaxPitchRate;
        } else {
            m_PitchDegrees += m_MaxPitchRate;
        }
    }
    
    if( m_PitchDegrees > 360.0f ) {
        m_PitchDegrees -= 360.0f;
    } else if(m_PitchDegrees < -360.0f ) {
        m_PitchDegrees += 360.0f;
    }
}

void glCamera::ChangeHeading( GLfloat degrees )
{
    if( fabs(degrees) < fabs(m_MaxHeadingRate) ) {
        if( m_PitchDegrees > 90 && m_PitchDegrees < 270 || (m_PitchDegrees < -90 && m_PitchDegrees > -270) ) {
            m_HeadingDegrees -= degrees;
        } else {
            m_HeadingDegrees += degrees;
        }
    } else {
        if( degrees < 0 ) {
            if( m_PitchDegrees > 90 && m_PitchDegrees < 270 || (m_PitchDegrees < -90 && m_PitchDegrees > -270) ) {
                m_HeadingDegrees += m_MaxHeadingRate;
            } else {
                m_HeadingDegrees -= m_MaxHeadingRate;
            }
        } else {
            if( m_PitchDegrees > 90 && m_PitchDegrees < 270 || (m_PitchDegrees < -90 && m_PitchDegrees > -270) ) {
                m_HeadingDegrees -= m_MaxHeadingRate;
            } else {
                m_HeadingDegrees += m_MaxHeadingRate;
            }
        }
    }
    
    if( m_HeadingDegrees > 360.0f ) {
        m_HeadingDegrees -= 360.0f;
    } else if( m_HeadingDegrees < -360.0f ) {
        m_HeadingDegrees += 360.0f;
    }
}

void glCamera::ChangeVelocity( GLfloat vel )
{
    if( fabs(vel) < fabs(m_MaxForwardVelocity) ) {
        m_ForwardVelocity += vel;
    } else {
        if( vel < 0 ) {
            m_ForwardVelocity -= -m_MaxForwardVelocity;
        } else {
            m_ForwardVelocity += m_MaxForwardVelocity;
        }
    }
}

void glCamera::Shuffle( GLfloat speed )
{
    GLfloat Matrix[16];
    glQuaternion q;
    
    m_qPitch.CreateFromAxisAngle    ( 1.0f, 0.0f, 0.0f, m_PitchDegrees );
    //m_qPitch.CreateFromAxisAngle    ( 1.0f, 0.0f, 0.0f, 0.0f );
    m_qHeading.CreateFromAxisAngle    ( 0.0f, 1.0f, 0.0f, m_HeadingDegrees + 90.0f );
    
    q = m_qPitch * m_qHeading;
    q.CreateMatrix( Matrix );
    
    glMultMatrixf( Matrix );
    
    m_qPitch.CreateMatrix( Matrix );
    m_DirectionVector.y = Matrix[9];
    
    q = m_qHeading * m_qPitch;
    q.CreateMatrix( Matrix );
    m_DirectionVector.x = Matrix[8];
    m_DirectionVector.z = Matrix[10];
    
    m_DirectionVector = m_DirectionVector * speed;
    
    m_Position.x += m_DirectionVector.x;
    //m_Position.y += m_DirectionVector.y;    // this is what makes us fly around up&down
    m_Position.z += m_DirectionVector.z;
}