#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 ;
}
Camera.cpp
最新推荐文章于 2022-08-01 15:59:20 发布