www.pudn.com > HeightMap_D3D.zip > cCamera.cpp


//----------------------------------------------------------------------------- 
// File: cCamera.cpp 
// Desc: Camera class funcs are defined here. 
//----------------------------------------------------------------------------- 
#include "Main.h" 
 
 
 
 
//----------------------------------------------------------------------------- 
// Func: cCamera() 
// Desc: Constructor of our cCamera Class. 
//----------------------------------------------------------------------------- 
cCamera::cCamera() 
{ 
	vEyePt = D3DXVECTOR3( 0.0f, 0.0f, -50.0f ); 
	vLookAtPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f ); 
	vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f ); 
	fSpeed = 0.25f; 
} 
 
 
 
 
//----------------------------------------------------------------------------- 
// Func: Render() 
// Desc: Render function. calling for move() and rotate() before rendering. 
//----------------------------------------------------------------------------- 
HRESULT cCamera::Render() 
{ 
	if(FAILED(Rotate())) 
	{ 
		MessageBox( hWnd, "Rotate() Failed!", "cCamera::Render()", MB_OK ); 
		return E_FAIL; 
	} 
 
	if(FAILED(Move())) 
	{ 
		MessageBox( hWnd, "Move() Failed!", "cCamera::Render()", MB_OK ); 
		return E_FAIL; 
	} 
 
	D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookAtPt, &vUpVec ); 
	g_pApp->m_pD3DDevice->SetTransform( D3DTS_VIEW, &matView ); 
 
	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 1000.0f ); 
	g_pApp->m_pD3DDevice->SetTransform( D3DTS_PROJECTION, &matProj ); 
 
	char FontBuffer[255]; 
	sprintf(FontBuffer, "vEyePt(%f, %f, %f)", vEyePt.x, vEyePt.y, vEyePt.z ); 
	if(FAILED(g_pApp->RenderFont( FontBuffer, 12 ))) 
	{ 
		MessageBox(hWnd, "g_pApp->RenderFont( FontBuffer, 12 ) Failed!", "cCamera::Render()", MB_OK); 
		return E_FAIL; 
	} 
 
	sprintf(FontBuffer, "vLookAtPt(%f, %f, %f)", vLookAtPt.x, vLookAtPt.y, vLookAtPt.z ); 
	if(FAILED(g_pApp->RenderFont( FontBuffer, 24 ))) 
	{ 
		MessageBox(hWnd, "g_pApp->RenderFont( FontBuffer, 24 ) Failed!", "cCamera::Render()", MB_OK); 
		return E_FAIL; 
	} 
 
	sprintf(FontBuffer, "vUpVec(%f, %f, %f)", vUpVec.x, vUpVec.y, vUpVec.z ); 
	if(FAILED(g_pApp->RenderFont( FontBuffer, 36 ))) 
	{ 
		MessageBox(hWnd, "g_pApp->RenderFont( FontBuffer, 36 ) Failed!", "cCamera::Render()", MB_OK); 
		return E_FAIL; 
	} 
} 
 
 
 
 
//----------------------------------------------------------------------------- 
// Func: Move() 
// Desc: Moving the camera vEyePt and vLookAtPt by checking the keyboard input. 
//----------------------------------------------------------------------------- 
HRESULT cCamera::Move() 
{ 
	D3DXVECTOR3 vDirectPt; 
	D3DXVec3Normalize(&vDirectPt, &(vLookAtPt - vEyePt)); 
 
	if(SUCCEEDED(g_pApp->m_pInput->BKeyDown(DIK_W))) 
	{ 
		if(SUCCEEDED(g_pApp->m_pInput->BKeyDown(DIK_LSHIFT))) 
		{ 
			vEyePt += vDirectPt*(fSpeed * 4); 
			vLookAtPt += vDirectPt*(fSpeed * 4); 
		} 
		else 
		{ 
			vEyePt += vDirectPt*fSpeed; 
			vLookAtPt += vDirectPt*fSpeed; 
		} 
	} 
 
	if(SUCCEEDED(g_pApp->m_pInput->BKeyDown(DIK_S))) 
	{ 
		if(SUCCEEDED(g_pApp->m_pInput->BKeyDown(DIK_LSHIFT))) 
		{ 
			vEyePt -= vDirectPt*(fSpeed * 4); 
			vLookAtPt -= vDirectPt*(fSpeed * 4); 
		} 
		else 
		{ 
			vEyePt -= vDirectPt*fSpeed; 
			vLookAtPt -= vDirectPt*fSpeed; 
		} 
	} 
 
	if(SUCCEEDED(g_pApp->m_pInput->BKeyDown(DIK_A))) 
	{ 
		D3DXVec3Cross( &vDirectPt, &vDirectPt, &vUpVec ); 
		D3DXVec3Normalize( &vDirectPt, &vDirectPt ); 
 
		if(SUCCEEDED(g_pApp->m_pInput->BKeyDown(DIK_LSHIFT))) 
		{ 
			vEyePt += vDirectPt*(fSpeed * 4); 
			vLookAtPt += vDirectPt*(fSpeed * 4); 
		} 
		else 
		{ 
			vEyePt += vDirectPt*fSpeed; 
			vLookAtPt += vDirectPt*fSpeed; 
		} 
	} 
 
	if(SUCCEEDED(g_pApp->m_pInput->BKeyDown(DIK_D))) 
	{ 
		D3DXVec3Cross( &vDirectPt, &vDirectPt, &vUpVec ); 
		D3DXVec3Normalize( &vDirectPt, &vDirectPt ); 
 
		if(SUCCEEDED(g_pApp->m_pInput->BKeyDown(DIK_LSHIFT))) 
		{ 
			vEyePt -= vDirectPt*(fSpeed * 4); 
			vLookAtPt -= vDirectPt*(fSpeed * 4); 
		} 
		else 
		{ 
			vEyePt -= vDirectPt*fSpeed; 
			vLookAtPt -= vDirectPt*fSpeed; 
		} 
	} 
	return S_OK; 
} 
 
 
 
 
//----------------------------------------------------------------------------- 
// Func: Rotate() 
// Desc: Rotating the camera vLookAtPt and vUpVec around the axis by checking 
//		 the mouse input. 
//----------------------------------------------------------------------------- 
HRESULT cCamera::Rotate() 
{ 
	D3DXVECTOR3 vDirectPt, vRotAxisPt; 
	D3DXMATRIX matRotX, matRotY, matRotZ; 
 
	D3DXVec3Normalize( &vRotAxisPt, &( vLookAtPt - vEyePt ) ); 
	D3DXVec3Cross( &vRotAxisPt, &vRotAxisPt, &vUpVec ); 
	D3DXVec3Normalize( &vRotAxisPt, &vRotAxisPt ); 
 
 
	D3DXMatrixRotationAxis( &matRotX, &vRotAxisPt, (FLOAT)g_pApp->m_pInput->getM_BuffY()/( -360 ) ); 
	D3DXMatrixRotationZ( &matRotZ, (FLOAT)g_pApp->m_pInput->getM_BuffX()/( -360 ) ); 
 
	D3DXVec3TransformCoord( &vDirectPt, &( vLookAtPt - vEyePt ), &matRotX ); 
	D3DXVec3TransformCoord( &vUpVec, &vUpVec, &matRotX ); 
	vLookAtPt = vDirectPt + vEyePt; 
 
	D3DXVec3TransformCoord( &vDirectPt, &( vLookAtPt - vEyePt ), &matRotZ ); 
	D3DXVec3TransformCoord( &vUpVec, &vUpVec, &matRotZ ); 
	vLookAtPt = vDirectPt + vEyePt; 
	return S_OK; 
}