From fd52d577402465036552ee0a589741df728e482d Mon Sep 17 00:00:00 2001 From: Lightling Date: Sun, 13 Feb 2022 13:04:09 -0500 Subject: [PATCH] add camera --- Camera.cpp | 78 +++++++++++++++++++++++++++++++++++++ Camera.h | 35 +++++++++++++++++ DX11Starter.vcxproj | 2 + DX11Starter.vcxproj.filters | 6 +++ 4 files changed, 121 insertions(+) create mode 100644 Camera.cpp create mode 100644 Camera.h diff --git a/Camera.cpp b/Camera.cpp new file mode 100644 index 0000000..ccc36be --- /dev/null +++ b/Camera.cpp @@ -0,0 +1,78 @@ +#include "Camera.h" + +using namespace DirectX; + +Camera::Camera(float _x, float _y, float _z, float _aspect, float _fov, float _near, float _far) +{ + transform.SetPosition(_x, _y, _z); + + aspect = _aspect; + fovYRadians = XMConvertToRadians(_fov); + clipNear = _near; + clipFar = _far; + + UpdateViewMatrix(); + UpdateProjectionMatrix(); +} + +Camera::~Camera() +{ +} + +void Camera::Update(float _dt) +{ + transform.TranslateRelative(0, 0, _dt); + UpdateViewMatrix(); +} + +void Camera::UpdateViewMatrix() +{ + XMFLOAT3 worldUp = XMFLOAT3(0, 1, 0); + XMMATRIX view = XMMatrixLookToLH(XMLoadFloat3(&transform.GetPosition()), XMLoadFloat3(&transform.GetForward()), XMLoadFloat3(&worldUp)); + XMStoreFloat4x4(&viewMatrix, view); +} + +void Camera::UpdateProjectionMatrix() +{ + XMMATRIX projection = XMMatrixPerspectiveFovLH(fovYRadians, aspect, clipNear, clipFar); + XMStoreFloat4x4(&projectionMatrix, projection); +} + +Transform* Camera::GetTransform() +{ + return &transform; +} + +DirectX::XMFLOAT4X4 Camera::GetViewMatrix() +{ + return viewMatrix; +} + +DirectX::XMFLOAT4X4 Camera::GetProjectionMatrix() +{ + return projectionMatrix; +} + +void Camera::SetAspect(float _aspect) +{ + aspect = _aspect; + UpdateProjectionMatrix(); +} + +void Camera::SetFOV(float _fov) +{ + fovYRadians = XMConvertToRadians(_fov); + UpdateProjectionMatrix(); +} + +void Camera::SetNearClip(float _near) +{ + clipNear = _near; + UpdateProjectionMatrix(); +} + +void Camera::SetFarClip(float _far) +{ + clipFar = _far; + UpdateProjectionMatrix(); +} diff --git a/Camera.h b/Camera.h new file mode 100644 index 0000000..5e49872 --- /dev/null +++ b/Camera.h @@ -0,0 +1,35 @@ +#pragma once + +#include "Transform.h" +#include + +class Camera +{ +public: + Camera(float _x, float _y, float _z, float _aspect, float _fov, float _near, float _far); + ~Camera(); + + void Update(float _dt); + void UpdateViewMatrix(); + void UpdateProjectionMatrix(); + + Transform* GetTransform(); + DirectX::XMFLOAT4X4 GetViewMatrix(); + DirectX::XMFLOAT4X4 GetProjectionMatrix(); + + void SetAspect(float _aspect); + void SetFOV(float _fov); + void SetNearClip(float _near); + void SetFarClip(float _far); + +private: + float aspect; + float fovYRadians; + float clipNear; + float clipFar; + + DirectX::XMFLOAT4X4 viewMatrix; + DirectX::XMFLOAT4X4 projectionMatrix; + + Transform transform; +}; diff --git a/DX11Starter.vcxproj b/DX11Starter.vcxproj index e3338d0..d2480fb 100644 --- a/DX11Starter.vcxproj +++ b/DX11Starter.vcxproj @@ -123,6 +123,7 @@ + @@ -133,6 +134,7 @@ + diff --git a/DX11Starter.vcxproj.filters b/DX11Starter.vcxproj.filters index 0ee7d77..1bc47d3 100644 --- a/DX11Starter.vcxproj.filters +++ b/DX11Starter.vcxproj.filters @@ -39,6 +39,9 @@ Source Files + + Source Files + @@ -65,6 +68,9 @@ Header Files + + Header Files +