www.pudn.com > DEM.zip > DemAction.cpp, change:2007-01-22,size:4471b


// DemAction.cpp: implementation of the CDemAction class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "DEM.h" 
#include "DemAction.h" 
#include "Vector.h" 
#include "math.h" 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CDemAction::CDemAction() 
{ 
	CVector vZero = CVector(0.0, 0.0, 0.0);		 
	CVector vView = CVector(0.0, 1.0, 0.5);	 
	CVector vUp   = CVector(0.0, 0.0, 1.0);	 
 
	m_vPosition	= vZero; 
	m_vView		= vView; 
	m_vUpVector	= vUp;	 
 
	m_range = 2; 
//	m_Role = false; 
	Role_Flag = false; 
	m_fzAngle = 1.0f; 
    m_fxAngle = 1.0f; 
} 
 
CDemAction::~CDemAction() 
{ 
 
} 
 
void CDemAction::PositionCamera(float positionX, float positionY, float positionZ, 
				  		     float viewX,     float viewY,     float viewZ, 
							 float upVectorX, float upVectorY, float upVectorZ) 
{ 
	CVector vPosition	= CVector(positionX, positionY, positionZ); 
	CVector vView		= CVector(viewX, viewY, viewZ); 
	CVector vUpVector	= CVector(upVectorX, upVectorY, upVectorZ); 
 
	m_vPosition = vPosition; 
	m_vView     = vView; 
	m_vUpVector = vUpVector; 
} 
 
void CDemAction::RotateView(float angle, float x, float y, float z) 
{ 
	CVector vNewView; 
 
	CVector vView = m_vView - m_vPosition;		 
 
	float cosTheta = (float)cos(angle); 
	float sinTheta = (float)sin(angle); 
 
	vNewView.X  = (cosTheta + (1 - cosTheta) * x * x)		* vView.X; 
	vNewView.X += ((1 - cosTheta) * x * y - z * sinTheta)	* vView.Y; 
	vNewView.X += ((1 - cosTheta) * x * z + y * sinTheta)	* vView.Z; 
 
	vNewView.Y  = ((1 - cosTheta) * x * y + z * sinTheta)	* vView.X; 
	vNewView.Y += (cosTheta + (1 - cosTheta) * y * y)		* vView.Y; 
	vNewView.Y += ((1 - cosTheta) * y * z - x * sinTheta)	* vView.Z; 
 
	vNewView.Z  = ((1 - cosTheta) * x * z - y * sinTheta)	* vView.X; 
	vNewView.Z += ((1 - cosTheta) * y * z + x * sinTheta)	* vView.Y; 
	vNewView.Z += (cosTheta + (1 - cosTheta) * z * z)		* vView.Z; 
	m_vView = m_vPosition + vNewView; 
 
} 
 
void CDemAction::Look() 
{ 
	gluLookAt(m_vPosition.X, m_vPosition.Y, m_vPosition.Z,	 
			  m_vView.X,	 m_vView.Y,     m_vView.Z,	 
			  m_vUpVector.X , m_vUpVector.Y , m_vUpVector.Z); 
} 
 
void CDemAction::CalculateHorizonVec() 
{ 
	CVector frontderection; 
	CVector horizonderection; 
	CVector ViewVector=m_vPosition-m_vView; 
	frontderection=CVector(ViewVector.X,ViewVector.Y,0); 
	m_vStrafe=Cross(ViewVector,frontderection); 
	Normalize(m_vStrafe); 
} 
 
void CDemAction::CalculateVerticalVec() 
{ 
	CVector ViewVector=m_vPosition-m_vView; 
	VerticalVector=Cross(ViewVector,m_vStrafe); 
	Normalize(VerticalVector); 
} 
 
void CDemAction::Update(int w,int h,float speed) 
{ 
 
	CalculateHorizonVec(); 
	CalculateVerticalVec(); 
	ViewDerection = m_vView - m_vPosition; 
	ViewDerection = Normalize(ViewDerection); 
	CVector vCross = Cross(m_vView - m_vPosition, m_vUpVector); 
 
	m_vStrafe = Normalize(vCross); 
	CheckForMovement(speed); 
} 
 
void CDemAction::StrafeCamera(float speed) 
{ 
	m_vPosition.X += m_vStrafe.X * speed; 
	m_vPosition.Z += m_vStrafe.Z * speed; 
 
	// Add the strafe vector to our view 
	m_vView.X += m_vStrafe.X * speed; 
	m_vView.Z += m_vStrafe.Z * speed; 
 
} 
 
void CDemAction::CheckForMovement(float speed) 
{ 
 
	if(GetKeyState(VK_UP) & 0x80 ) {				 
 
	 
		MoveCamera(speed);				 
	} 
 
 
	if(GetKeyState(VK_DOWN) & 0x80 ) {			 
 
		MoveCamera(-speed);				 
	} 
	 
	if(GetKeyState(VK_LEFT) & 0x80 ) {			 
		StrafeCamera(speed); 
	} 
 
	if(GetKeyState(VK_RIGHT) & 0x80) {			 
		 
		StrafeCamera(-speed); 
	} 
	 
	if(m_range==0){ 
		StrafeCamera(speed); 
	} 
 
	if(m_range==1){ 
		StrafeCamera(-speed); 
	} 
	 
	if(GetKeyState('W') & 0x80 ) 
	{ 
 
	} 
	 
	if(GetKeyState('S') & 0x80 ) 
	{ 
	} 
 
	if(GetKeyState('M') & 0x80) 
	{	 
		 
		Role_Flag = true; 
		ShowCursor(false); 
	} 
 
	if(GetKeyState('N') & 0x80) 
	{		 
		Role_Flag = false; 
		ShowCursor(true); 
	} 
} 
 
void  CDemAction::MoveCamera(float speed) 
{ 
	CVector vVector = m_vView - m_vPosition; 
	vVector = Normalize(vVector); 
 
	m_vPosition.X += vVector.X * speed;	 
	m_vPosition.Y += vVector.Y * speed;	 
	m_vPosition.Z += vVector.Z * speed;		 
	m_vView.X += vVector.X * speed;			 
	m_vView.Y += vVector.Y * speed;			 
	m_vView.Z += vVector.Z * speed;	 
}