1
| | #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;
pos.z *= -1.0f;
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;
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;
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 )
{
m_target.x = 0.0f;
m_target.y = 0.0f;
m_target.z =-1.0f;
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] )
{
|