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

📄 ccameracontrol.cpp

📁 一个3D的保龄球的源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	m_pCurStage->iStartLookat[2] = vCoinPos.z+12;

	//set end posision
	CCameraStage* m_pStage=&m_pCameraCombinations[IDC_AIM][0];

	int ref[3];
	GetAbsCoords(CCRO_PLAYER,ref);

	m_pCurStage->iEndPos[0] = m_pStage->iStartPos[0]+ref[0];
	m_pCurStage->iEndPos[1] = m_pStage->iStartPos[1]+ref[1];
	m_pCurStage->iEndPos[2] = m_pStage->iStartPos[2]+ref[2];

	//set lookat
	m_pCurStage->iEndLookat[0] = m_pStage->iStartLookat[0];
	m_pCurStage->iEndLookat[1] = m_pStage->iStartLookat[1];
	m_pCurStage->iEndLookat[2] = m_pStage->iStartLookat[2];
}
//Qiu Li end

////////////////////////////////////////////////////////////////////////////////////
// up down influence hormat
/*
* =============================================================
* Name		: void CCameraControl::MoveCamera(int distance, int mode)
* Parameter	: 
		distance: means move step, if it is move action, or angle if
			it is rotate action.
		mode: move up, down, left, right, forward, backward; 
			rotate up, down, left, right.
* Implementation: 
*		Move camera according to the distance and move mode
*
* =============================================================
*/
void CCameraControl::MoveCamera(int distance, int mode)
{
	CMatrix44 mat;
	int dis = distance;
	int angle = distance;
	
	switch( mode )
	{
	case CAMERA_MOVE_UP:
		dis = -distance;
	case CAMERA_MOVE_DOWN:
		{
		mat.DefTranslate(0,dis,0);
		mat.PostMultiply(&m_origin);
		CMatrix44 m_tmpOrigin(&m_origin);
		m_origin=mat;

		CompMatrix();
		if(!IsInRange())
			m_origin=m_tmpOrigin;
		CompMatrix();
		}
		break;

	case CAMERA_MOVE_RIGHT:
		dis = -distance;
	case CAMERA_MOVE_LEFT:
		{
		mat.DefTranslate(dis,0,0);
		mat.PostMultiply(&m_HorMat); 
		CMatrix44 m_tmpHorMat(m_HorMat);
		m_HorMat=mat;

		CompMatrix();
		if(!IsInRange())
			m_HorMat=m_tmpHorMat;
		CompMatrix();
		}
		break;

	case CAMERA_MOVE_FORWARD:
		dis = -distance;
	case CAMERA_MOVE_BACKWARD:
		{
		mat.DefTranslate(0,0,dis);
		mat.PostMultiply(&m_HorMat); 
		CMatrix44 m_tmpHorMat(m_HorMat);
		m_HorMat=mat;

		CompMatrix();
		if(!IsInRange())
			m_HorMat=m_tmpHorMat;
		CompMatrix();
		}
		break;

	case CAMERA_ROTATE_DOWN:
		angle = -angle;
	case CAMERA_ROTATE_UP:
		{
		mat.DefRotateX(angle);
		mat.PostMultiply(&m_VerMat); 
		CMatrix44 m_tmpVerMat(m_VerMat);
		m_VerMat=mat;

		CompMatrix();
		if(!IsInViewAngle())
			m_VerMat=m_tmpVerMat;
		CompMatrix();
		}
		break;

	case CAMERA_ROTATE_LEFT:
		angle = -angle;
	case CAMERA_ROTATE_RIGHT:
		{
		mat.DefRotateY(angle);
		mat.PostMultiply(&m_HorMat); 
		CMatrix44 m_tmpHorMat(m_HorMat);
		m_HorMat=mat;

		CompMatrix();
		//Qiu Li, adjust position if beyond the mark when rotate left and right, 2004/12/29
		AdjustPosWhenRotLR();
		//Qiu Li end
		}
		break;
	}
}
/*
void CCameraControl::MoveUp(int distance)
{

	CMatrix44 mat;
	mat.DefTranslate(0,-distance,0);
	mat.PostMultiply(&m_origin);
	CMatrix44 m_tmpOrigin(&m_origin);
	m_origin=mat;

	CompMatrix();
	if(!IsInRange())
		m_origin=m_tmpOrigin;
	CompMatrix();

}


void CCameraControl::MoveDown(int distance)
{
	MoveUp(-distance);
}


// others influence vermat
void CCameraControl::MoveLeft(int distance)
{
	CMatrix44 mat;
	mat.DefTranslate(distance,0,0);
	mat.PostMultiply(&m_HorMat); 
	CMatrix44 m_tmpHorMat(m_HorMat);
	m_HorMat=mat;

	CompMatrix();
	if(!IsInRange())
		m_HorMat=m_tmpHorMat;
	CompMatrix();

}


void CCameraControl::MoveRight(int distance)
{
	MoveLeft(-distance);
}


void CCameraControl::MoveForward(int distance)
{
	CMatrix44 mat;
	mat.DefTranslate(0,0,-distance);
	mat.PostMultiply(&m_HorMat); 
	CMatrix44 m_tmpHorMat(m_HorMat);
	m_HorMat=mat;

	CompMatrix();
	if(!IsInRange())
		m_HorMat=m_tmpHorMat;
	CompMatrix();

}


void CCameraControl::MoveBackward(int distance)
{
	MoveForward(-distance);
}


void CCameraControl::RotateUp(int angle)
{
	CMatrix44 mat;
	mat.DefRotateX(angle);
	mat.PostMultiply(&m_VerMat); 
	CMatrix44 m_tmpVerMat(m_VerMat);
	m_VerMat=mat;

	CompMatrix();
	if(!IsInViewAngle())
		m_VerMat=m_tmpVerMat;
	CompMatrix();
}

void CCameraControl::RotateDown(int angle)
{
	RotateUp(-angle);
}


void CCameraControl::RotateLeft(int angle)
{
	CMatrix44 mat;
	mat.DefRotateY(-angle);
	mat.PostMultiply(&m_HorMat); 
	CMatrix44 m_tmpHorMat(m_HorMat);
	m_HorMat=mat;

	CompMatrix();
	//Qiu Li, adjust position if beyond the mark when rotate left and right, 2004/12/29
	AdjustPosWhenRotLR();
	//Qiu Li end
}

void CCameraControl::RotateRight(int angle)
{
	RotateLeft(-angle);
}
*/



void CCameraControl::SetViewMatrix()
{
	m_viewMat.LookAt(m_eye, m_lookat, m_up);
}


void CCameraControl::UpdateView()
{
	CGraphics3D* pG3D;
	pG3D=m_pScene->m_render3D.GetG3d();
	if(pG3D!=NULL)
	{

		CMatrix44 trm(m_viewMat);
		trm.Inverse();
		Vector4s eye;
		eye.x=trm.get(0,3);
		eye.y=trm.get(1,3);
		eye.z=trm.get(2,3);

		pG3D->SetEye(&eye);

		pG3D->SetMatrix(1, &m_viewMat);

//		DBGPRINTF("eye:%d %d %d,    trm:%d %d %d, m_eye:%d %d %d", eye.x, eye.y, eye.z,			
//			trm.get(0,3), trm.get(1,3), trm.get(2,3),
//			m_eye.x, m_eye.y, m_eye.z);
	}

}



void CCameraControl::CompMatrix()
{
	m_viewMat=m_VerMat;
	m_viewMat.PostMultiply(&m_HorMat);
	m_viewMat.PostMultiply(&m_origin);
/*
	DBGPRINTF("eye:%d %d %d, verMat:%d %d %d, horMat:%d %d %d, origin:%d %d %d", m_eye.x, m_eye.y, m_eye.z, 
		m_VerMat.get(0,3), m_VerMat.get(1,3), m_VerMat.get(2,3),
		m_HorMat.get(0,3), m_HorMat.get(1,3), m_HorMat.get(2,3),
		m_origin.get(0,3), m_origin.get(1,3), m_origin.get(2,3));
*/
	UpdateView();
}

/*
* =============================================================
* Name		: bool CCameraControl::IsInRange()
* Parameter	: none
* Return	: true: in range, otherwise false
* Implementation: 
*		In camera view mode, player can move and rotate camera,
*	and there is a limit, judge whether this movement or rotation
*	is in range or not.
*
* =============================================================
*/
bool CCameraControl::IsInRange()
{
	int x,y,z;
	CMatrix44 mat(&m_viewMat);
	mat.Inverse();
	x=mat.get(0,3);
	y=mat.get(1,3);
	z=mat.get(2,3);

	if(!(x>CMR_LEFT && x<CMR_RIGHT && 
		y>CMR_NEAR && y<CMR_FAR && 
		z>CMR_BOTTOM && z<CMR_TOP))
	{
		//DBGPRINTF("Failed===========");
		//DBGPRINTF("%d %d %d",x,y,z);
		return false;
	}

	Vector4s vet;
	vet=mat.Transform(Vector4s(0,0,1024));
//	vet=mat.Transform(Vector4s(0,1024,0));
	if(vet.z-z>CMR_LOOKUP)
		return false;
	if(vet.z-z<CMR_LOOKDOWN)
		return false;


	return true;
}

/*
* =============================================================
* Name		: bool CCameraControl::IsInViewAngle()
* Parameter	: none
* Return	: true: in view angle, otherwise false
* Implementation: 
*		In camera view mode, player can move and rotate camera,
*	and there is a limit, judge whether this movement or rotation
*	is in range or not.
*
* =============================================================
*/
bool CCameraControl::IsInViewAngle()
{
	int x,y,z;
	CMatrix44 mat(&m_viewMat);
	mat.Inverse();
	x=mat.get(0,3);
	y=mat.get(1,3);
	z=mat.get(2,3);


	Vector4s vet;
	vet=mat.Transform(Vector4s(0,0,1024));
//	vet=mat.Transform(Vector4s(0,1024,0));
	if(vet.z-z>CMR_LOOKUP)
		return false;
	if(vet.z-z<CMR_LOOKDOWN)
		return false;

	//Qiu Li, should check x,y,z for rotation will also change them, 2004/12/27
	if(!(x>CMR_LEFT && x<CMR_RIGHT && 
		y>CMR_NEAR && y<CMR_FAR && 
		z>CMR_BOTTOM && z<CMR_TOP))
		return false;
	//Qiu Li

	return true;
}

/*
* =============================================================
* Name		: void CCameraControl::AdjustPosWhenRotLR()
* Parameter	: none
* Implementation: 
*		In camera view mode, when player rotates camera, there
*	is an error to cause the eye get out of the range, modify the
*	eye back to previous position.
*
* =============================================================
*/
//Qiu Li, adjust position if beyond the mark when rotate left and right, 2004/12/29
void CCameraControl::AdjustPosWhenRotLR()
{
	int x,y,z;
	int deltaX=0;
	int deltaY=0;
	Vector4s vet;
	Vector4s lookat;

	//get camera position
	CMatrix44 mat(&m_viewMat);
	mat.Inverse();
	x=mat.get(0,3);
	y=mat.get(1,3);
	z=mat.get(2,3);

	vet=mat.Transform(Vector4s(0,0,1024));
	lookat = vet-Vector4s(x,y,z);

	//check range
	if( !(x>CMR_LEFT && x<CMR_RIGHT))
	{
		if(x<=CMR_LEFT)
			deltaX = (CMR_LEFT+1)-x;
		else
			deltaX = (CMR_RIGHT-1)-x;
	}
	if( !(y>CMR_NEAR && y<CMR_FAR))
	{
		if(y<=CMR_NEAR)
			deltaY = (CMR_NEAR+1)-y;
		else
			deltaY = (CMR_FAR-1)-y;
	}

	//move camera into range
	if(( deltaX == 0 )&&( deltaY == 0))
		return;

	//DBGPRINTF("======Begin===========");
	//DBGPRINTF("%d %d %d",x,y,z);
	//DBGPRINTF("%d %d %d",deltaX,deltaY,0);

	CMatrix44 mat2(&m_viewMat);
	int x1,y1;

	int len = 1024;//sqrt(lookat.x*lookat.x + lookat.y*lookat.y);
	x1 = y1 = 0;
	int absX = abs(lookat.x);
	int absY = abs(lookat.y);

	if( deltaX )
	{
		if( deltaX == 1 )
			deltaX++;
		else if( deltaX == -1 )
			deltaX--;

		if( lookat.x >= 0 && lookat.y >= 0 )
		{
			x1 -= ( deltaX <<4) * absY / len;
			y1 -= ( deltaX <<4) * absX / len;
		}
		else if( lookat.x > 0 && lookat.y < 0 )
		{
			x1 += (deltaX <<4) * absY / len;
			y1 -= (deltaX <<4) * absX / len;
		}
		else if( lookat.x < 0 && lookat.y > 0 )
		{
			x1 -= (deltaX <<4) * absY / len;
			y1 += (deltaX <<4) * absX / len;
		}
		else
		{
			x1 += (deltaX <<4) * absY / len;
			y1 += (deltaX <<4) * absX / len;
		}
	}
	if( deltaY )
	{
		if( deltaY == 1 )
			deltaY++;
		else if( deltaY == -1 )
			deltaY--;

		if( lookat.x >= 0 && lookat.y >= 0 )
		{
			x1 += (deltaY <<4) * absX / len;
			y1 -= (deltaY <<4) * absY / len;
		}
		else if( lookat.x > 0 && lookat.y < 0 )
		{
			x1 += (deltaY <<4) * absX / len;
			y1 += (deltaY <<4) * absY / len;
		}
		else if( lookat.x < 0 && lookat.y > 0 )
		{
			x1 -= (deltaY <<4) * absX / len;
			y1 -= (deltaY <<4) * absY / len;
		}
		else
		{
			x1 -= (deltaY <<4) * absX / len;
			y1 += (deltaY <<4) * absY / len;
		}
	}

	x1=(x1>>4);
	y1=(y1>>4);
	mat2.DefTranslate(x1, 0, y1);
	mat2.PostMultiply(&m_HorMat); 
	CMatrix44 m_tmpHorMat(m_HorMat);
	m_HorMat=mat2;


	CompMatrix();
	if(!IsInRange())
	{
		//continue use the adjust mat
		//m_HorMat=m_tmpHorMat;
		//CompMatrix();
		//DBGPRINTF("Fail===========");
	}

	//DBGPRINTF("%d %d",x1,y1);
}
//Qiu Li end

//Qiu Li, Is Camera CONSTANT, 2004/12/29
bool CCameraControl::IsCameraConstant()
{
	if( m_pCurStage->iControlType == CCSCT_CONSTANT )
		return true;
	else
		return false;
}
//Qiu Li end

void CCameraControl::Test()
{
	DBGPRINTF("iCmbsCount=%d",m_iCmbsCount);
	for(int i=0;i<m_iCmbsCount;i++)
	{
		DBGPRINTF("==CameraComb==%d",i);
		for(int j=0;j<m_pStagesCount[i];j++)
		{
			CCameraStage *pCam=&(m_pCameraCombinations[i][j]);
			DBGPRINTF("TotalTime:%d",pCam->iTotalTime);
			DBGPRINTF("ControlType:%d",pCam->iControlType);
			DBGPRINTF("PosRefer:%d",pCam->iPosRefer);
			DBGPRINTF("PosContStyle:%d",pCam->iPosContStyle);
			DBGPRINTF("LookatRefer:%d",pCam->iLookatRefer);
			DBGPRINTF("LookatContStyle:%d",pCam->iLookatContStyle);

			DBGPRINTF("StartRate:%d",pCam->iStartRate);
			DBGPRINTF("EndRate:%d",pCam->iEndRate);

			DBGPRINTF("StartPos:%d %d %d",pCam->iStartPos[0],pCam->iStartPos[1],pCam->iStartPos[2]);
			DBGPRINTF("StartLookat:%d %d %d",pCam->iStartLookat[0],pCam->iStartLookat[1],pCam->iStartLookat[2]);
			DBGPRINTF("StartUp:%d %d",pCam->iStartUp[0],pCam->iStartUp[1]);

			DBGPRINTF("EndPos:%d %d %d",pCam->iEndPos[0],pCam->iEndPos[1],pCam->iEndPos[2]);
			DBGPRINTF("EndLookat:%d %d %d",pCam->iEndLookat[0],pCam->iEndLookat[1],pCam->iEndLookat[2]);
			DBGPRINTF("EndUp:%d %d",pCam->iEndUp[0],pCam->iEndUp[1]);
		}
	}
}

⌨️ 快捷键说明

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