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:: Add Line Numbers


picking ray camera.cpp by Pompei2
#include "camera.h"

SPos3D operator- ( SPos3D v1, SPos3D v2 )
{
    SPos3D res;
    res.x = v1.x - v2.x;
    res.y = v1.y - v2.y;
    res.z = v1.z - v2.z;
    return res;
}

int v_normalize( PPos3D v )
{
    float mag = sqrt( v->x * v->x + v->y * v->y + v->z * v->z );

    if( mag == 0.0f )
        return 1;

    v->x /= mag;
    v->y /= mag;
    v->z /= mag;

    return ERR_OK;
}

SPos3D rotateX( float deg, SPos3D pos )
{
    SPos3D res = pos;
    /* Here we turn around X. Your math teacher should know how it works :p just look
    * at it from the X axis (so you just see Y and Z, thus 2D).
    */
    pos.z *= -1.0f;    /* Because for opengl, -z is into the scene, but it's inverted in 2D. */

    /* Rotate the lookAt vector. */
    /* We do minus for Z to re-adapt it to openGL 3D (remember, z is inverted). */
    res.z = -(cosf(RAD(deg)) * pos.z - sinf(RAD(deg)) * pos.y);
    res.y =  (sinf(RAD(deg)) * pos.z + cosf(RAD(deg)) * pos.y);

    return res;
}

SPos3D rotateY( float deg, SPos3D pos )
{
    SPos3D res = pos;
    /* Here we turn around Y. Your math teacher should know how it works :p just look
    * at it from the Y axis (so you just see X and Z, thus 2D).
    */
    res.z = (cosf(RAD(deg)) * pos.z - sinf(RAD(deg)) * pos.x);
    res.x = (sinf(RAD(deg)) * pos.z + cosf(RAD(deg)) * pos.x);

    return res;
}

SPos3D rotateZ( float deg, SPos3D pos )
{
    SPos3D res = pos;
    /* Here we turn around Z. Your math teacher should know how it works :p just look
    * at it from the Z axis (so you just see X and Y, thus 2D).
    */
    res.x = cosf(RAD(deg)) * pos.x - sinf(RAD(deg)) * pos.y;
    res.y = sinf(RAD(deg)) * pos.x + cosf(RAD(deg)) * pos.y;
    v_normalize( &res );

    return res;
}




CCamera::CCamera( void )
{
    m_pos.x = 0.0f;
    m_pos.y = 0.0f;
    m_pos.z = 10.0f;
    m_target.x = 0.0f;
    m_target.y = 0.0f;
    m_target.z = -1.0f;
    m_rX = m_rY = m_rZ = 0.0f;

    m_pFixate = NULL;

    recalcTarget( );
    glPushMatrix( );
    render( );
}

CCamera::~CCamera( void )
{
    glPopMatrix( );
}

void CCamera::render( void )
{
    SPos3D lookDir = m_target - m_pos;

    gluLookAt( m_pos.x, m_pos.y, m_pos.z,
               m_target.x, m_target.y, m_target.z,
               0.0f, 1.0f, 0.0f );
}

void CCamera::setPos( float x, float y, float z )
{
    m_pos.x = x;
    m_pos.y = y;
    m_pos.z = z;

    recalcTarget( );
}

void CCamera::target( float x, float y, float z )
{
    m_target.x = x;
    m_target.y = y;
    m_target.z = z;
}

void CCamera::recalcTarget( void )
{
    /* First, we reset it to the default, normalized lookat vector. */
    m_target.x = 0.0f;
    m_target.y = 0.0f;
    m_target.z =-1.0f;

    /* Now, we transform this vector trough all the transformations the cam
     * is transformed. */
    m_target = ::rotateX( -m_rX, m_target );
    m_target = ::rotateY( -m_rY, m_target );
    m_target = ::rotateZ( -m_rZ, m_target );
    m_target = m_target - m_pos;
}

void CCamera::getModelviewMatrix( double mv[16] )
{
//    glPopMatrix( );
        glGetDoublev( GL_MODELVIEW_MATRIX, mv );
//    glPushMatrix( );
}

void CCamera::getProjectionMatrix( double mp[16] )
{
//    glPopMatrix( );
        glGetDoublev( GL_PROJECTION_MATRIX, mp );
//    glPushMatrix( );
}