#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) ;//合并移動和旋轉矩陣 toOriginInverseMatrixMultMatrix(&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 ;}