Camera.cpp

#include "Camera.h"
#include <d3dx9math.h>

//		初始情况下将摄像机放置在同世界坐标轴重合
Camera ::Camera(float fov, float viewPortWidth, float viewPortHeight)
{
	position.x = 0 ;
	position.y = 0 ;
	position.z = 0 ;

	direction.x = 0 ;
	direction.y = 0 ;
	direction.z = 0 ;

	this ->fov = fov ;
	this ->viewPortWidth = viewPortWidth ;
	this ->viewPortHeight = viewPortHeight ;
	aspectRatio = viewPortWidth / viewPortHeight ;
}

Camera ::~Camera()
{
}

void Camera ::Update(float nearDistance, float farDistance, float yzFov, float screenWidth, float screenHeight)
{
	viewPortWidth = screenWidth ;
	viewPortHeight = screenHeight ;
	aspectRatio = viewPortWidth / viewPortHeight ;
	this ->yzFov = yzFov;

	this ->nearDistance = nearDistance ;
	this ->farDistance = farDistance ;
	nearTop = nearDistance * tan(yzFov / 2) ;
	nearBottom = -nearTop ;
	nearRight = nearTop * aspectRatio ;
	nearLeft = - nearRight ;
}

void Camera ::Strafe(float unit)
{
	Vector3 temp = {unit, 0, 0} ;
	VectorPlusVector(&position, &temp, &position) ;
}

void Camera ::Fly(float unit)
{
	Vector3 temp = {0, unit, 0} ;
	VectorPlusVector(&position, &temp, &position) ;
}

void Camera ::Walk(float unit)
{
	Vector3 temp = {0, 0, unit} ;
	VectorPlusVector(&position, &temp, &position) ;
}

void Camera ::BuildWroldToCameraMatrix()
{
	//		1:根据相机位置计算平移矩阵的逆矩阵
	Matrix4X4 translatedMatrix = {
		1.0f, 0, 0, 0,
		0, 1.0f, 0, 0,
		0, 0, 1.0f, 0,
		-position.x, -position.y, -position.z, 1
	} ;
	
	//		2:构建旋转逆矩阵

	//		构建绕X轴旋转的逆矩阵
	float sinTheta = sin(DEGREE_TO_RADIAN((direction.x))) ;
	float cosTheta = cos(DEGREE_TO_RADIAN((direction.x))) ;

	Matrix4X4 rotatedAroundX = {
		1, 0, 0, 0,
		0, -cosTheta, -sinTheta, 0,
		0, sinTheta, -cosTheta, 0,
		0, 0, 0, 1
	} ;

	//		构建绕Y轴旋转的逆矩阵
	sinTheta = sin(DEGREE_TO_RADIAN((direction.y))) ;
	cosTheta = cos(DEGREE_TO_RADIAN((direction.y))) ;

	Matrix4X4 rotatedAroundY = {
		-cosTheta, 0, sinTheta, 0,
		0, 1.0f, 0, 0,
		-sinTheta, 0, -cosTheta, 0,
		0, 0, 0, 1
	} ;

	//		构建绕Z轴旋转的逆矩阵
	sinTheta = sin(DEGREE_TO_RADIAN((direction.z))) ;
	cosTheta = cos(DEGREE_TO_RADIAN((direction.z))) ;

	Matrix4X4 rotatedAroundZ = {
		-cosTheta, -sinTheta, 0, 0,
		sinTheta, -cosTheta, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1
	} ;

	Matrix4X4 temp ;
	Matrix4X4 rotated ;

	//		ZYX顺序,虽然我现在不知道这个顺序意味着什么
	//		我试着改变了顺序,没看到什么不同昂,照理说矩阵不满足交换律的...先不管了
	MatrixMultMatrix(&rotatedAroundZ, &rotatedAroundY, &temp) ;
	MatrixMultMatrix(&temp, &rotatedAroundX, &rotated) ;

	//		合并移动和旋转矩阵 toOriginInverse
	MatrixMultMatrix(&translatedMatrix, &rotated, &toOriginInverse) ;
}

void Camera ::BuildCameraToClipMatrix()
{
	//		OpenGL风格的将相机空间中的点变换到裁剪空间中的点的矩阵,即各个轴的值范围为[-1,1]
	toClip.m11 = 2 * nearDistance / (nearRight - nearLeft) ;	
	toClip.m12 = 0 ;
	toClip.m13 = 0 ;
	toClip.m14 = 0 ;

	toClip.m21 = 0 ;
	toClip.m22 = 2 * nearDistance / (nearTop - nearBottom) ;	
	toClip.m23 = 0 ;
	toClip.m24 = 0 ;

	toClip.m31 = (nearRight + nearLeft) / (nearRight - nearLeft) ;
	toClip.m32 = (nearTop + nearBottom) / (nearTop - nearBottom) ;
	toClip.m33 = (farDistance + nearDistance) / (farDistance - nearDistance) ;
	toClip.m34 = 1 ;

	toClip.m41 = 0 ;
	toClip.m42 = (nearTop + nearBottom) / (nearTop - nearBottom) ;
	toClip.m43 = 2 * farDistance * nearDistance / (nearDistance - farDistance) ;	
	toClip.m44 = 0 ;

	//D3DXMATRIX temp ;

	//D3DXMatrixPerspectiveFovLH(&temp, yzFov, aspectRatio, nearDistance, farDistance) ;

	//toClip.m11 = temp._11 ; toClip.m12 = temp._12 ; toClip.m13 = temp._13 ; toClip.m14 = temp._14 ;
	//toClip.m21 = temp._21 ; toClip.m22 = temp._22 ; toClip.m23 = temp._23 ; toClip.m24 = temp._24 ;
	//toClip.m31 = temp._31 ; toClip.m32 = temp._32 ; toClip.m33 = temp._33 ; toClip.m34 = temp._34 ;
	//toClip.m41 = temp._41 ; toClip.m42 = temp._42 ; toClip.m43 = temp._43 ; toClip.m44 = temp._44 ;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值