示例#1
0
void CCamera::SetPerspectiveProjection(float fovY, float aspectRatio, float near, float far, HANDEDNESS handedness)
{
	float yScale = 1 / tan(fovY / 2);
	float xScale = yScale / aspectRatio;

	CMatrix4 proj;
	proj.Clear();

	if(handedness == HANDEDNESS_LEFTHANDED)
	{
		proj(0, 0) = xScale;
		proj(1, 1) = yScale;
		proj(2, 2) = far / (far - near);
		proj(3, 3) = 0;

		proj(3, 2) = -near * far / (far - near);
		proj(2, 3) = 1;
	}
	else
	{
		proj(0, 0) = xScale;
		proj(1, 1) = yScale;
		proj(2, 2) = far / (near - far);
		proj(3, 3) = 0;

		proj(3, 2) = near * far / (near - far);
		proj(2, 3) = -1;
	}

	m_projMatrix = proj;
}
示例#2
0
void CCamera::SetOrthoProjection(float width, float height, float near, float far)
{
	CMatrix4 proj;
	proj.Clear();

	proj(0, 0) =  2.0f / width;
	proj(1, 1) =  2.0f / height;
	proj(2, 2) =  1.0f / (far - near);
	proj(3, 3) = 1;

	proj(3, 2) = near / (near - far);

	m_projMatrix = proj;
}
示例#3
0
void CCamera::LookAt(const CVector3& eye, const CVector3& target, const CVector3& up, HANDEDNESS handedness)
{
	CMatrix4 view;
	view.Clear();

	if(handedness == HANDEDNESS_LEFTHANDED)
	{
		//zaxis = normal(At - Eye)
		//xaxis = normal(cross(Up, zaxis))
		//yaxis = cross(zaxis, xaxis)

		CVector3 axisZ = (target - eye).Normalize();
		CVector3 axisX = (up.Cross(axisZ)).Normalize();
		CVector3 axisY = axisZ.Cross(axisX);

		view(0, 0) = axisX.x;
		view(1, 0) = axisX.y;
		view(2, 0) = axisX.z;
		view(3, 0) = -axisX.Dot(eye);

		view(0, 1) = axisY.x;
		view(1, 1) = axisY.y;
		view(2, 1) = axisY.z;
		view(3, 1) = -axisY.Dot(eye);

		view(0, 2) = axisZ.x;
		view(1, 2) = axisZ.y;
		view(2, 2) = axisZ.z;
		view(3, 2) = -axisZ.Dot(eye);

		view(3, 3) = 1;
	}
	else
	{
		//zaxis = normal(Eye - At)
		//xaxis = normal(cross(Up, zaxis))
		//yaxis = cross(zaxis, xaxis)

		CVector3 axisZ = (eye - target).Normalize();
		CVector3 axisX = (up.Cross(axisZ)).Normalize();
		CVector3 axisY = axisZ.Cross(axisX);

		view(0, 0) = axisX.x;
		view(1, 0) = axisX.y;
		view(2, 0) = axisX.z;
		view(3, 0) = -axisX.Dot(eye);

		view(0, 1) = axisY.x;
		view(1, 1) = axisY.y;
		view(2, 1) = axisY.z;
		view(3, 1) = -axisY.Dot(eye);

		view(0, 2) = axisZ.x;
		view(1, 2) = axisZ.y;
		view(2, 2) = axisZ.z;
		view(3, 2) = -axisZ.Dot(eye);

		view(3, 3) = 1;
	}

	m_viewMatrix = view;
}