Skip to content

Commit 7f9dbac

Browse files
committed
Working on Camera
1 parent d6fdfe9 commit 7f9dbac

File tree

7 files changed

+242
-58
lines changed

7 files changed

+242
-58
lines changed

src/Cpp/1-getting-started/1-3-6-Camera/1-3-6-Camera.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ xcopy /Y $(ProjectDir)Assets\Textures\*.* $(OutDir)Assets\Textures\</Command>
118118
<ClCompile Include="..\..\..\..\lib\imgui\include\imgui\imgui_tables.cpp" />
119119
<ClCompile Include="..\..\..\..\lib\imgui\include\imgui\imgui_widgets.cpp" />
120120
<ClCompile Include="Application.cpp" />
121+
<ClCompile Include="Camera.cpp" />
121122
<ClCompile Include="DeviceContext.cpp" />
122123
<ClCompile Include="Main.cpp" />
123124
<ClCompile Include="CameraApplication.cpp" />
@@ -128,6 +129,7 @@ xcopy /Y $(ProjectDir)Assets\Textures\*.* $(OutDir)Assets\Textures\</Command>
128129
</ItemGroup>
129130
<ItemGroup>
130131
<ClInclude Include="Application.hpp" />
132+
<ClInclude Include="Camera.hpp" />
131133
<ClInclude Include="Definitions.hpp" />
132134
<ClInclude Include="DeviceContext.hpp" />
133135
<ClInclude Include="CameraApplication.hpp" />

src/Cpp/1-getting-started/1-3-6-Camera/1-3-6-Camera.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<ClCompile Include="..\..\..\..\lib\imgui\include\imgui\backend\imgui_impl_glfw.cpp">
6161
<Filter>Source Files</Filter>
6262
</ClCompile>
63+
<ClCompile Include="Camera.cpp">
64+
<Filter>Source Files</Filter>
65+
</ClCompile>
6366
</ItemGroup>
6467
<ItemGroup>
6568
<ClInclude Include="Application.hpp">
@@ -92,6 +95,9 @@
9295
<ClInclude Include="DeviceContext.hpp">
9396
<Filter>Header Files</Filter>
9497
</ClInclude>
98+
<ClInclude Include="Camera.hpp">
99+
<Filter>Header Files</Filter>
100+
</ClInclude>
95101
</ItemGroup>
96102
<ItemGroup>
97103
<Image Include="Assets\Textures\T_Good_Froge.dds" />

src/Cpp/1-getting-started/1-3-6-Camera/Assets/Shaders/Main.vs.hlsl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,16 @@ struct VSOutput
1212
float2 Uv: TEXCOORD0;
1313
};
1414

15-
cbuffer PerApplication
15+
cbuffer CameraBuffer
1616
{
1717
matrix ProjectionMatrix;
18-
}
19-
20-
cbuffer PerFrame
21-
{
2218
matrix ViewMatrix;
2319
}
2420

25-
cbuffer PerObject
21+
cbuffer Object
2622
{
2723
matrix WorldMatrix;
28-
}
24+
};
2925

3026
VSOutput Main(VSInput input)
3127
{
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include "Camera.hpp"
2+
3+
Camera::Camera(
4+
const float nearPlane,
5+
const float farPlane)
6+
{
7+
_nearPlane = nearPlane;
8+
_farPlane = farPlane;
9+
}
10+
11+
DirectX::XMFLOAT4X4 Camera::GetViewMatrix()
12+
{
13+
return _viewMatrix;
14+
}
15+
16+
DirectX::XMFLOAT4X4 Camera::GetProjectionMatrix()
17+
{
18+
return _projectionMatrix;
19+
}
20+
21+
CameraConstants& Camera::GetCameraConstants()
22+
{
23+
return _cameraConstants;
24+
}
25+
26+
void Camera::Update()
27+
{
28+
UpdateVectors();
29+
UpdateProjectionMatrix();
30+
UpdateViewMatrix();
31+
32+
_cameraConstants.ProjectionMatrix = _projectionMatrix;
33+
_cameraConstants.ViewMatrix = _viewMatrix;
34+
}
35+
36+
void Camera::UpdateViewMatrix()
37+
{
38+
DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookToLH(
39+
DirectX::XMLoadFloat3(&_position),
40+
DirectX::XMLoadFloat3(&_direction),
41+
DirectX::XMLoadFloat3(&_up));
42+
DirectX::XMStoreFloat4x4(&_viewMatrix, viewMatrix);
43+
}
44+
45+
void Camera::UpdateVectors()
46+
{
47+
float pitchSine;
48+
float pitchCosine;
49+
float yawSine;
50+
float yawCosine;
51+
52+
DirectX::XMScalarSinCos(&pitchSine, &pitchCosine, _pitch);
53+
DirectX::XMScalarSinCos(&yawSine, &yawCosine, _yaw);
54+
55+
float x = pitchCosine * yawCosine;
56+
float y = pitchSine;
57+
float z = pitchCosine * yawSine;
58+
59+
DirectX::XMFLOAT3 rotation = DirectX::XMFLOAT3(x, y, z);
60+
DirectX::XMFLOAT3 unitY = DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f);
61+
DirectX::XMVECTOR unitYVector = DirectX::XMLoadFloat3(&unitY);
62+
63+
DirectX::XMVECTOR direction = DirectX::XMVector3Normalize(DirectX::XMLoadFloat3(&rotation));
64+
DirectX::XMVECTOR right = DirectX::XMVector3Normalize(DirectX::XMVector3Cross(direction, unitYVector));
65+
DirectX::XMVECTOR up = DirectX::XMVector3Normalize(DirectX::XMVector3Cross(right, direction));
66+
67+
DirectX::XMStoreFloat3(&_direction, direction);
68+
DirectX::XMStoreFloat3(&_right, right);
69+
DirectX::XMStoreFloat3(&_up, up);
70+
}
71+
72+
void Camera::SetPosition(const DirectX::XMFLOAT3& position)
73+
{
74+
_position = position;
75+
}
76+
77+
void Camera::SetDirection(const DirectX::XMFLOAT3& direction)
78+
{
79+
_direction = _direction;
80+
}
81+
82+
void Camera::SetUp(const DirectX::XMFLOAT3& up)
83+
{
84+
_up = _up;
85+
}
86+
87+
PerspectiveCamera::PerspectiveCamera(
88+
const float fieldOfView,
89+
const int32_t width,
90+
const int32_t height,
91+
const float nearPlane,
92+
const float farPlane) : Camera(nearPlane, farPlane)
93+
{
94+
_aspectRatio = static_cast<float>(width) / static_cast<float>(height);
95+
_fieldOfView = DirectX::XMConvertToRadians(fieldOfView);
96+
}
97+
98+
void PerspectiveCamera::Resize(
99+
const int32_t width,
100+
const int32_t height)
101+
{
102+
_aspectRatio = static_cast<float>(width) / static_cast<float>(height);
103+
104+
UpdateProjectionMatrix();
105+
}
106+
107+
void PerspectiveCamera::UpdateProjectionMatrix()
108+
{
109+
DirectX::XMMATRIX projectionMatrix = DirectX::XMMatrixPerspectiveFovLH(
110+
_fieldOfView,
111+
_aspectRatio,
112+
_nearPlane,
113+
_farPlane);
114+
DirectX::XMStoreFloat4x4(&_projectionMatrix, projectionMatrix);
115+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#pragma once
2+
3+
#include <DirectXMath.h>
4+
5+
struct CameraConstants
6+
{
7+
DirectX::XMFLOAT4X4 ProjectionMatrix;
8+
DirectX::XMFLOAT4X4 ViewMatrix;
9+
};
10+
11+
class Camera
12+
{
13+
public:
14+
virtual void Resize(
15+
const int32_t width,
16+
const int32_t height) = 0;
17+
18+
void Update();
19+
20+
void SetPosition(const DirectX::XMFLOAT3& position);
21+
void SetDirection(const DirectX::XMFLOAT3& direction);
22+
void SetUp(const DirectX::XMFLOAT3& up);
23+
24+
[[nodiscard]] DirectX::XMFLOAT4X4 GetViewMatrix();
25+
[[nodiscard]] DirectX::XMFLOAT4X4 GetProjectionMatrix();
26+
[[nodiscard]] CameraConstants& GetCameraConstants();
27+
28+
protected:
29+
Camera(
30+
const float nearPlane,
31+
const float farPlane);
32+
33+
virtual void UpdateProjectionMatrix() = 0;
34+
35+
float _nearPlane = 0.0f;
36+
float _farPlane = 0.0f;
37+
38+
DirectX::XMFLOAT4X4 _projectionMatrix = DirectX::XMFLOAT4X4();
39+
DirectX::XMFLOAT4X4 _viewMatrix = DirectX::XMFLOAT4X4();
40+
CameraConstants _cameraConstants = {};
41+
42+
private:
43+
void UpdateVectors();
44+
void UpdateViewMatrix();
45+
46+
DirectX::XMFLOAT3 _position = {};
47+
DirectX::XMFLOAT3 _direction = {};
48+
DirectX::XMFLOAT3 _up = {};
49+
DirectX::XMFLOAT3 _right = {};
50+
float _pitch = 0.0f;
51+
float _yaw = 0.0f;
52+
};
53+
54+
class PerspectiveCamera final : public Camera
55+
{
56+
public:
57+
PerspectiveCamera(
58+
const float fieldOfView,
59+
const int32_t width,
60+
const int32_t height,
61+
const float nearPlane,
62+
const float farPlane);
63+
64+
void Resize(
65+
const int32_t width,
66+
const int32_t height) override;
67+
68+
protected:
69+
void UpdateProjectionMatrix() override;
70+
71+
private:
72+
float _fieldOfView = 0.0f;
73+
float _aspectRatio = 0.0f;
74+
75+
};

src/Cpp/1-getting-started/1-3-6-Camera/CameraApplication.cpp

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Pipeline.hpp"
55
#include "TextureFactory.hpp"
66
#include "ModelFactory.hpp"
7+
#include "Camera.hpp"
78

89
#include <GLFW/glfw3.h>
910
#define GLFW_EXPOSE_NATIVE_WIN32
@@ -56,9 +57,7 @@ CameraApplication::~CameraApplication()
5657
_solidFrameCullNoneRasterizerState.Reset();
5758

5859
_depthStencilView.Reset();
59-
_constantBuffers[ConstantBufferType::PerApplication].Reset();
60-
_constantBuffers[ConstantBufferType::PerFrame].Reset();
61-
_constantBuffers[ConstantBufferType::PerObject].Reset();
60+
_cameraConstantBuffer.Reset();
6261
_textureSrv.Reset();
6362
_pipeline.reset();
6463
_pipelineFactory.reset();
@@ -165,6 +164,7 @@ bool CameraApplication::Initialize()
165164
_pipelineFactory = std::make_unique<PipelineFactory>(_device);
166165
_textureFactory = std::make_unique<TextureFactory>(_device);
167166
_modelFactory = std::make_unique<ModelFactory>(_device);
167+
_camera = std::make_unique<PerspectiveCamera>(60.0f, GetWindowWidth(), GetWindowHeight(), 0.1f, 2048.0f);
168168

169169
return true;
170170
}
@@ -213,36 +213,30 @@ bool CameraApplication::Load()
213213
return false;
214214
}
215215

216-
const D3D11_BUFFER_DESC constantBufferDescriptor = CD3D11_BUFFER_DESC(
217-
sizeof(DirectX::XMMATRIX),
216+
const D3D11_BUFFER_DESC cameraConstantBufferDescriptor = CD3D11_BUFFER_DESC(
217+
sizeof(CameraConstants),
218218
D3D11_BIND_FLAG::D3D11_BIND_CONSTANT_BUFFER);
219219

220-
if (FAILED(_device->CreateBuffer(&constantBufferDescriptor, nullptr, &_constantBuffers[ConstantBufferType::PerApplication])))
221-
{
222-
std::cout << "D3D11: Unable to create constant buffer PerApplication\n";
223-
return false;
224-
}
225-
if (FAILED(_device->CreateBuffer(&constantBufferDescriptor, nullptr, &_constantBuffers[ConstantBufferType::PerFrame])))
220+
if (FAILED(_device->CreateBuffer(&cameraConstantBufferDescriptor, nullptr, &_cameraConstantBuffer)))
226221
{
227-
std::cout << "D3D11: Unable to create constant buffer PerFrame\n";
222+
std::cout << "D3D11: Unable to create CameraConstants buffer\n";
228223
return false;
229224
}
230-
if (FAILED(_device->CreateBuffer(&constantBufferDescriptor, nullptr, &_constantBuffers[ConstantBufferType::PerObject])))
225+
SetDebugName(_cameraConstantBuffer.Get(), "CB_Camera");
226+
227+
const D3D11_BUFFER_DESC objectConstantBufferDescriptor = CD3D11_BUFFER_DESC(
228+
sizeof(DirectX::XMMATRIX),
229+
D3D11_BIND_FLAG::D3D11_BIND_CONSTANT_BUFFER);
230+
231+
if (FAILED(_device->CreateBuffer(&objectConstantBufferDescriptor, nullptr, &_objectConstantBuffer)))
231232
{
232-
std::cout << "D3D11: Unable to create constant buffer PerObject\n";
233+
std::cout << "D3D11: Unable to create ObjectConstants buffer\n";
233234
return false;
234235
}
236+
SetDebugName(_objectConstantBuffer.Get(), "CB_Object");
235237

236-
_pipeline->BindVertexStageConstantBuffer(0, _constantBuffers[ConstantBufferType::PerApplication].Get());
237-
_pipeline->BindVertexStageConstantBuffer(1, _constantBuffers[ConstantBufferType::PerFrame].Get());
238-
_pipeline->BindVertexStageConstantBuffer(2, _constantBuffers[ConstantBufferType::PerObject].Get());
239-
240-
_projectionMatrix = DirectX::XMMatrixPerspectiveFovLH(
241-
DirectX::XMConvertToRadians(45.0f),
242-
GetWindowWidth() / static_cast<float>(GetWindowHeight()),
243-
0.1f,
244-
512.0f);
245-
_deviceContext->UpdateSubresource(_constantBuffers[PerApplication].Get(), &_projectionMatrix);
238+
_pipeline->BindVertexStageConstantBuffer(0, _cameraConstantBuffer.Get());
239+
_pipeline->BindVertexStageConstantBuffer(1, _objectConstantBuffer.Get());
246240

247241
if (!CreateDepthStencilStates())
248242
{
@@ -339,15 +333,10 @@ void CameraApplication::OnResize(
339333

340334
CreateSwapchainResources();
341335

342-
//ImGuiIO& io = ImGui::GetIO();
343-
//io.DisplaySize = ImVec2(GetWindowWidth(), GetWindowHeight());
336+
_camera->Resize(width, height);
344337

345-
_projectionMatrix = DirectX::XMMatrixPerspectiveFovLH(
346-
DirectX::XMConvertToRadians(45.0f),
347-
GetWindowWidth() / static_cast<float>(GetWindowHeight()),
348-
0.1f,
349-
512);
350-
_deviceContext->UpdateSubresource(_constantBuffers[PerApplication].Get(), &_projectionMatrix);
338+
ImGuiIO& io = ImGui::GetIO();
339+
io.DisplaySize = ImVec2(width, height);
351340
}
352341

353342
void CameraApplication::Update()
@@ -357,11 +346,9 @@ void CameraApplication::Update()
357346
Close();
358347
}
359348

360-
const auto eyePosition = DirectX::XMVectorSet(0, 50, 200, 1);
361-
const auto focusPoint = DirectX::XMVectorSet(0, 0, 0, 1);
362-
const auto upDirection = DirectX::XMVectorSet(0, 1, 0, 0);
363-
_viewMatrix = DirectX::XMMatrixLookAtLH(eyePosition, focusPoint, upDirection);
364-
_deviceContext->UpdateSubresource(_constantBuffers[PerFrame].Get(), &_viewMatrix);
349+
_camera->SetPosition(DirectX::XMFLOAT3{ 0.0f, 50.0f, -200.0f });
350+
_camera->SetDirection(DirectX::XMFLOAT3{ 0.0f, 0.0f, 1.0f });
351+
_camera->SetUp(DirectX::XMFLOAT3{ 0.0f, 1.0f, 0.0f });
365352

366353
static float angle = 0.0f;
367354
if (_toggledRotation)
@@ -375,12 +362,21 @@ void CameraApplication::Update()
375362

376363
const auto rotationAxis = DirectX::XMVectorSet(0, 1, 0, 0);
377364

378-
_worldMatrix = DirectX::XMMatrixRotationAxis(rotationAxis, DirectX::XMConvertToRadians(angle));
379-
_deviceContext->UpdateSubresource(_constantBuffers[PerObject].Get(), &_worldMatrix);
365+
_worldMatrix = DirectX::XMMatrixRotationAxis(
366+
rotationAxis,
367+
DirectX::XMConvertToRadians(angle));
368+
369+
DirectX::XMFLOAT4X4 worldMatrix;
370+
DirectX::XMStoreFloat4x4(&worldMatrix, _worldMatrix);
371+
_deviceContext->UpdateSubresource(_objectConstantBuffer.Get(), &worldMatrix);
380372
}
381373

382374
void CameraApplication::Render()
383375
{
376+
_camera->Update();
377+
CameraConstants& cameraConstants = _camera->GetCameraConstants();
378+
_deviceContext->UpdateSubresource(_cameraConstantBuffer.Get(), &cameraConstants);
379+
384380
constexpr float clearColor[] = { 0.1f, 0.1f, 0.1f, 1.0f };
385381

386382
_deviceContext->Clear(
@@ -394,7 +390,7 @@ void CameraApplication::Render()
394390

395391
_deviceContext->DrawIndexed();
396392

397-
//RenderUi();
393+
RenderUi();
398394
_swapChain->Present(1, 0);
399395
}
400396

0 commit comments

Comments
 (0)