Camera.cpp

來源:互聯網
上載者:User
#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 ;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.