Fichier source en cours : FW - Camera.cpp






Voici le code source cpp :




	#include "Framework.h"

using namespace fw;

Camera::Camera(const std::string& sName) :
	m_sName(sName),
	m_fNear(CAMERA_NEAR_DEFAULT_VALUE),
	m_fFar(CAMERA_FAR_DEFAULT_VALUE),
	m_bIgnoreUpdate(false),
	m_pCameraSceneNode(nullptr)
{
    D3DXMatrixIdentity(&m_view);
    D3DXMatrixIdentity(&m_proj);
 
	m_translation = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

    m_up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
    m_lookAt = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
	m_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);

    UpdateLens((float)D3DX_PI * 0.25f, (float)D3D10_RENDERER->GetViewportWidth() / (float)D3D10_RENDERER->GetViewportHeight(), m_fNear, m_fFar);

	BuildView();
}

Camera::~Camera()
{
	SAFE_DELETE(m_pCameraSceneNode);
}

std::string& Camera::GetName()
{
	return m_sName;
}

void Camera::LookAt(const D3DXVECTOR3& lookAt)
{
	m_lookAt = lookAt;

    BuildView();
}

void Camera::SetIgnoreUpdate(bool bIgnore)
{
	m_bIgnoreUpdate = bIgnore;
}

bool Camera::GetIgnoreUpdate()
{
	return m_bIgnoreUpdate;
}

void Camera::SetTarget(const D3DXVECTOR3& target)
{
	D3DXVECTOR3 L = target - m_translation;
	D3DXVec3Normalize(&L, &L);

	D3DXVECTOR3 R;
	D3DXVec3Cross(&R, &m_up, &L);
	D3DXVec3Normalize(&R, &R);

	D3DXVECTOR3 U;
	D3DXVec3Cross(&U, &L, &R);
	D3DXVec3Normalize(&U, &U);

	m_up = U;
	m_lookAt = L;
	m_right = R;

	BuildView();
}

void Camera::SetTarget(float x, float y, float z)
{
	SetTarget(D3DXVECTOR3(x, y, z));
}

D3DXVECTOR3 Camera::GetTarget()
{
	return m_translation - m_lookAt;
}

D3DXMATRIX* Camera::GetViewMatrix()
{
	return &m_view;
}

D3DXMATRIX* Camera::GetProjectionMatrix()
{
	return &m_proj;
}

void Camera::UpdateLens(float fFov, float fAspect, float fNearZ, float fFarZ)
{
    D3DXMatrixPerspectiveFovLH(&m_proj, fFov, fAspect, fNearZ, fFarZ);

	m_fNear = fNearZ;
	m_fFar = fFarZ;
}

void Camera::RebuildProj()
{
	UpdateLens((float)D3DX_PI * 0.25f, (float)SYSTEM->GetCurrentRenderer()->GetViewportWidth() / (float)SYSTEM->GetCurrentRenderer()->GetViewportHeight(), m_fNear, m_fFar);
}

D3DXVECTOR3 Camera::GetLookAt()
{
	return m_lookAt;
}

void Camera::SetUpVector(const D3DXVECTOR3& up)
{
	m_up = up;

	BuildView();
}

D3DXVECTOR3 Camera::GetUpVector()
{
	return m_up;
}

void Camera::Update(float fTimeSinceLastFrame)
{
	//SetTarget(m_constantTarget);

	BuildView();
}

void Camera::BuildView()
{
	D3DXMatrixLookAtLH(&m_view, &m_translation, &m_lookAt, &m_up);
}

float Camera::GetNearValue()
{
	return m_fNear;
}

float Camera::GetFarValue()
{
	return m_fFar;
}

void Camera::OnTransformationChange()
{
	if (m_pCameraSceneNode)
	{
		D3DXVECTOR3 pos = GetPosition();
		D3DXVECTOR3 rot = GetRotation();
		D3DXVECTOR3 scale = GetScale();

		m_pCameraSceneNode->SetPosition(pos);
		m_pCameraSceneNode->SetRotation(rot);
		m_pCameraSceneNode->SetScale(scale);
	}
}