add camera

This commit is contained in:
lightling 2022-02-13 13:04:09 -05:00
parent 97ffe2efb5
commit fd52d57740
Signed by: lightling
GPG key ID: 016F11E0AA296B67
4 changed files with 121 additions and 0 deletions

78
Camera.cpp Normal file
View file

@ -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();
}

35
Camera.h Normal file
View file

@ -0,0 +1,35 @@
#pragma once
#include "Transform.h"
#include <memory>
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;
};

View file

@ -123,6 +123,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Camera.cpp" />
<ClCompile Include="DXCore.cpp" />
<ClCompile Include="Entity.cpp" />
<ClCompile Include="Game.cpp" />
@ -133,6 +134,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="BufferStructs.h" />
<ClInclude Include="Camera.h" />
<ClInclude Include="DXCore.h" />
<ClInclude Include="Entity.h" />
<ClInclude Include="Game.h" />

View file

@ -39,6 +39,9 @@
<ClCompile Include="Entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Vertex.h">
@ -65,6 +68,9 @@
<ClInclude Include="Entity.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Camera.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<FxCompile Include="PixelShader.hlsl">