⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccamera.cpp

📁 骨骼动画 此程序演示了如何在你的游戏中使用骨骼动画技术。
💻 CPP
字号:
#include "CCamera.h"
#include <zmouse.h>

CCamera::CCamera()
:m_bIsRot(false),
m_bIsTrans(false)
{
	_cameraType = AIRCRAFT;

	_pos   = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
	_up    = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	_look  = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
}

CCamera::CCamera(CameraType cameraType)
:m_bIsRot(false),
m_bIsTrans(false)
{
	_cameraType = cameraType;

	_pos   = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
	_up    = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	_look  = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
}

CCamera::~CCamera()
{

}

void CCamera::getPosition(D3DXVECTOR3* pos)
{
	*pos = _pos;
}

void CCamera::setPosition(D3DXVECTOR3* pos)
{
	_pos = *pos;
}

void CCamera::getRight(D3DXVECTOR3* right)
{
	*right = _right;
}

void CCamera::getUp(D3DXVECTOR3* up)
{
	*up = _up;
}

void CCamera::getLook(D3DXVECTOR3* look)
{
	*look = _look;
}

void CCamera::walk(float units)
{
	// move only on xz plane for land object
	if( _cameraType == LANDOBJECT )
		_pos += D3DXVECTOR3(_look.x, 0.0f, _look.z) * units;

	if( _cameraType == AIRCRAFT )
		_pos += _look * units;
}

void CCamera::strafe(float units)
{
	// move only on xz plane for land object
	if( _cameraType == LANDOBJECT )
		_pos += D3DXVECTOR3(_right.x, 0.0f, _right.z) * units;

	if( _cameraType == AIRCRAFT )
		_pos += _right * units;
}

void CCamera::fly(float units)
{
	if( _cameraType == AIRCRAFT )
		_pos += _up * units;
}

void CCamera::pitch(float angle)
{
	D3DXMATRIX T;
	D3DXMatrixRotationAxis(&T, &_right,	angle);

	// rotate _up and _look around _right vector
	D3DXVec3TransformCoord(&_up,&_up, &T);
	D3DXVec3TransformCoord(&_look,&_look, &T);
}

void CCamera::yaw(float angle)
{
	D3DXMATRIX T;

	// rotate around world y (0, 1, 0) always for land object
	if( _cameraType == LANDOBJECT )
		D3DXMatrixRotationY(&T, angle);

	// rotate around own up vector for aircraft
	if( _cameraType == AIRCRAFT )
		D3DXMatrixRotationAxis(&T, &_up, angle);

	// rotate _right and _look around _up or y-axis
	D3DXVec3TransformCoord(&_right,&_right, &T);
	D3DXVec3TransformCoord(&_look,&_look, &T);
}

void CCamera::roll(float angle)
{
	// only roll for aircraft type
	if( _cameraType == AIRCRAFT )
	{
		D3DXMATRIX T;
		D3DXMatrixRotationAxis(&T, &_look,	angle);

		// rotate _up and _right around _look vector
		D3DXVec3TransformCoord(&_right,&_right, &T);
		D3DXVec3TransformCoord(&_up,&_up, &T);
	}
}

void CCamera::getViewMatrix(D3DXMATRIX* V)
{
	// Keep camera's axes orthogonal to eachother
	D3DXVec3Normalize(&_look, &_look);

	D3DXVec3Cross(&_up, &_look, &_right);
	D3DXVec3Normalize(&_up, &_up);

	D3DXVec3Cross(&_right, &_up, &_look);
	D3DXVec3Normalize(&_right, &_right);

	// Build the view matrix:
	float x = -D3DXVec3Dot(&_right, &_pos);
	float y = -D3DXVec3Dot(&_up, &_pos);
	float z = -D3DXVec3Dot(&_look, &_pos);

	(*V)(0,0) = _right.x; (*V)(0, 1) = _up.x; (*V)(0, 2) = _look.x; (*V)(0, 3) = 0.0f;
	(*V)(1,0) = _right.y; (*V)(1, 1) = _up.y; (*V)(1, 2) = _look.y; (*V)(1, 3) = 0.0f;
	(*V)(2,0) = _right.z; (*V)(2, 1) = _up.z; (*V)(2, 2) = _look.z; (*V)(2, 3) = 0.0f;
	(*V)(3,0) = x;        (*V)(3, 1) = y;     (*V)(3, 2) = z;       (*V)(3, 3) = 1.0f;
}

LRESULT CCamera::HandleMessage(HWND hWnd, UINT msg, WPARAM wParam,LRESULT lParam)
{
	switch( msg )
	{	
	case WM_RBUTTONDOWN:
		{
			m_bIsRot = true;
			SetCapture(hWnd);
			GetCursorPos(&m_LastPoint);
		}
		break;
	
	case WM_MOUSEMOVE:
		{
			
		}
		break;

	case WM_RBUTTONUP:
		{
			m_bIsRot = false;
			ReleaseCapture();
		}
		break;
	case WM_MOUSEWHEEL:
		short distance = HIWORD(wParam);
		_pos += (distance*_look)/120;
	}

	return 0;
}
void CCamera::ProcesKey(LPDIRECT3DDEVICE9 Device,float fElapsedTime)
{
	if( Device )
	{
		// 更新摄像机
		// 平移
		if( ::GetAsyncKeyState('W') & 0x8000f ) //前移
		{
			OutputDebugString( "向前." );
			walk(4.0f * fElapsedTime);
		};

		if( ::GetAsyncKeyState('S') & 0x8000f ) //后移
		{
			OutputDebugString( "向后." );
			walk(-4.0f * fElapsedTime);
		};

		if( ::GetAsyncKeyState('A') & 0x8000f ) //左移
		{
			OutputDebugString( "向左." );
			strafe(-4.0f * fElapsedTime);
		};

		if( ::GetAsyncKeyState('D') & 0x8000f ) //右移
		{
			OutputDebugString( "向右." );
			strafe(4.0f * fElapsedTime);
		};

		// 飞行
		if( ::GetAsyncKeyState('R') & 0x8000f ) //上移
			fly(4.0f * fElapsedTime);
		if( ::GetAsyncKeyState('F') & 0x8000f ) //下移
			fly(-4.0f * fElapsedTime);

		// 倾斜
		if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f ) //抬头
			pitch(1.0f * fElapsedTime);
		if( ::GetAsyncKeyState(VK_UP) & 0x8000f ) //低头
			pitch(-1.0f * fElapsedTime);

		// 偏航
		if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f ) //左转头
			yaw(-1.0f * fElapsedTime);
		if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f ) //右转头
			yaw(1.0f * fElapsedTime);

		// 滚转
		if( ::GetAsyncKeyState('N') & 0x8000f )
			roll(1.0f * fElapsedTime);
		if( ::GetAsyncKeyState('M') & 0x8000f )
			roll(-1.0f * fElapsedTime);

		// 取得视矩阵,更新摄像机位置
		D3DXMATRIX V;
		D3DXMatrixIdentity(&V);
		getViewMatrix(&V);
		Device->SetTransform(D3DTS_VIEW, &V);
	}
}
void CCamera::Update( LPDIRECT3DDEVICE9 Device, float fElapsedTime )
{
	POINT ptCurrentPos = {0, 0};
	POINT ptDeltaPos = {0, 0};

	if(m_bIsRot)
	{
		//计算鼠标偏移
		GetCursorPos(&ptCurrentPos);
		ptDeltaPos.x = ptCurrentPos.x - m_LastPoint.x;
		ptDeltaPos.y = ptCurrentPos.y - m_LastPoint.y;
		m_LastPoint = ptCurrentPos;

		float fYaw = ptDeltaPos.x*0.01f;
		float fPitch = ptDeltaPos.y*0.01f;

		pitch( fPitch );
		yaw( fYaw );

		// 取得视矩阵,更新摄像机位置
		D3DXMATRIX V;
		D3DXMatrixIdentity(&V);
		getViewMatrix(&V);
		Device->SetTransform(D3DTS_VIEW, &V);
	}

}

void CCamera::setCameraType(CameraType cameraType)
{
	_cameraType = cameraType;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -