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

📄 camera.cpp

📁 c++程序
💻 CPP
字号:
// Camera.cpp: Implementierung der Klasse CCamera.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Camera.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

// Conversion factor for converting between degrees and radians
#define PI_OVER_180 0.0174532925f

//////////////////////////////////////////////////////////////////////
// Construcktion/Destrucktion
//////////////////////////////////////////////////////////////////////

CCamera::CCamera()
{
	// Init all member variables to default values
	m_XPos          = 0.0f;
	m_YPos          = 1.0f;
	m_ZPos          = 0.0f;
	m_YRotate       = 0.0f;
	m_UpDown        = 0.0f;
	m_WalkBiasAngle = 0.0f;
	m_WalkBias      = 0.0f;
	m_SizeOfViewer  = 0.075f;
	m_Speed         = 0.45f;
	m_Landscape     = 0;
}

CCamera::~CCamera()
{

}

void CCamera::RotateHorizontal(float fDegrees)
{
	// Rotate horizontal
	m_YRotate += fDegrees;
}

void CCamera::RotateVertical(float fDegrees)
{
	// Rotate vertikal
	m_UpDown += fDegrees;

	// Check range
	if (m_UpDown > 90.0f)
		m_UpDown = 90.0f;
	if (m_UpDown < -90.0f)
		m_UpDown = -90.0f;
}

void CCamera::CenterVertical()
{
	// Center the vertical axis of the camera
	m_UpDown = 0.0f;
}

void CCamera::MoveDefaultSpeed(bool bForward, bool bBackward, bool bStrafeLeft, bool bStrafeRight,
							   bool bMoveUp, bool bMoveDown)
{
	// Move the camera with the speed defined at m_Speed.
	
	// Covert parameters for MoveCustomSpeed() and pass them
	MoveCustomSpeed(bForward * m_Speed, bBackward * m_Speed,
		bStrafeLeft * m_Speed, bStrafeRight * m_Speed,
		bMoveUp * m_Speed, bMoveDown * m_Speed);
}

void CCamera::MoveCustomSpeed(float fForward, float fBackward, float fStrafeLeft, float fStrafeRight,
							  float fMoveUp, float fMoveDown)
{
	// Move the camera with the passed speed values.

	// Temp variables to store target position
	float fNewXPos = m_XPos;
	float fNewYPos = m_YPos;
	float fNewZPos = m_ZPos;
	
	// Should the camera move forward ?
	if (fForward)
	{
		// Calculate new position
		fNewXPos -= (float) sin(m_YRotate * PI_OVER_180) * fForward;
		fNewZPos -= (float) cos(m_YRotate * PI_OVER_180) * fForward;
	}

	// Should the camera move backward ?
	if (fBackward)
	{
		// Calculate new position
		fNewXPos += (float) sin(m_YRotate * PI_OVER_180) * fBackward;
		fNewZPos += (float) cos(m_YRotate * PI_OVER_180) * fBackward;
	}

	// Should the camera strafe left ?
	if (fStrafeLeft)
	{
		// Calculate new position
		fNewXPos += (float) sin((m_YRotate - 90) * PI_OVER_180) * fStrafeLeft;
		fNewZPos += (float) cos((m_YRotate - 90) * PI_OVER_180) * fStrafeLeft;
	}

	// Should the camera strafe right ?
	if (fStrafeRight)
	{
		// Calculate new position
		fNewXPos -= (float) sin((m_YRotate - 90) * PI_OVER_180) * fStrafeRight;
		fNewZPos -= (float) cos((m_YRotate - 90) * PI_OVER_180) * fStrafeRight;
	}

	// Should the camera move up ?
	if (fMoveUp)
	{
		// Calculate new position
		fNewYPos += fMoveUp;
	}

	// Should the camera move down ?
	if (fMoveUp)
	{
		// Calculate new position
		fNewYPos -= fMoveDown;
	}

	// Walkbias
	IncrementWalkBias();

	// Collision detection
	// (If a landscape has been associated with the camera)
	if (m_Landscape)
	{
		// Restrict movement to the landscape, abort if camera would
		// leave it.
		if (m_Landscape->GetSurfaceHeight(fNewZPos, fNewXPos) == -1.0f)
			// Movement failed
			return;
	}
	
	// Set target position
	m_XPos = fNewXPos;
	m_YPos = fNewYPos;
	m_ZPos = fNewZPos;
}

float CCamera::GetSurfaceHeight()
{
	// Return the height of the eventually associated landscape
	// at the current camera position. If no landscape has been
	// associated return the current camera height.

	if (m_Landscape)
		// Height of landscape
		return m_Landscape->GetSurfaceHeight(m_ZPos, m_XPos);
	else
		// Current camera height
		return m_YPos;
}

void CCamera::IncrementWalkBias()
{
	// Calculate the new walkbias

	if (m_WalkBiasAngle <= 30.0f)
		m_WalkBiasAngle = 359.0f;
	else m_WalkBiasAngle+= 15;
	m_WalkBias = (float) sin(m_WalkBiasAngle * PI_OVER_180) / 20.0f;
}


void CCamera::TransformWorld()
{
	// Transform the world

	// Transformation variables
	GLfloat fXTrans, fZTrans, fYTrans;
	GLfloat fSceneRotY;
	
	// Calculate translations & rotations
	fXTrans = -m_XPos;
	fZTrans = -m_ZPos;
	fSceneRotY = 360.0f - m_YRotate;
	// Walkbias
	fYTrans = -m_YPos + (-m_WalkBias) / 13.0f;
	// Size of viewer
	fYTrans -= m_SizeOfViewer;

	// Do translations & rotations
	glRotatef(m_UpDown, 1.0f, 0.0f , 0.0f);
	glRotatef(fSceneRotY, 0.0f, 1.0f, 0.0f);
	glTranslatef(fXTrans, fYTrans, fZTrans);
}

void CCamera::SetLandscape(CLandscape *pLandscape)
{
	m_Landscape = pLandscape;
}

void CCamera::SetSizeOfViewer(float fSizeOfViewer)
{
	if (fSizeOfViewer >= 0.0f)
		m_SizeOfViewer = fSizeOfViewer;
}

void CCamera::SetSpeed(float fSpeed)
{
	if (fSpeed > 0.0f)
		m_Speed = fSpeed;
}

void CCamera::SetXPos(float fXPos)
{
	m_XPos = fXPos;
}

void CCamera::SetYPos(float fYPos)
{
	m_YPos = fYPos;
}

void CCamera::SetZPos(float fZPos)
{
	m_ZPos = fZPos;
}

float CCamera::GetSizeOfViewer()
{
	return m_SizeOfViewer;
}

float CCamera::GetSpeed()
{
	return m_Speed;
}

float CCamera::GetUpDownRotation()
{
	return m_UpDown;
}

float CCamera::GetXPos()
{
	return m_XPos;
}

float CCamera::GetYPos()
{
	return m_YPos;
}

float CCamera::GetZPos()
{
	return m_ZPos;
}

float CCamera::GetYRotation()
{
	return m_YRotate;
}

CLandscape* CCamera::GetLandscape()
{
	return m_Landscape;
}

⌨️ 快捷键说明

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