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
353342void 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
382374void 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