Temp
This commit is contained in:
45
project/src/Buffers/DepthBuffer.cpp
Normal file
45
project/src/Buffers/DepthBuffer.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "DepthBuffer.h"
|
||||
|
||||
DepthBuffer::DepthBuffer(ID3D11Device* device, UINT width, UINT height)
|
||||
: m_device(device), m_width(width), m_height(height), m_depthBuffer(nullptr), m_depthStencilView(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
DepthBuffer::~DepthBuffer()
|
||||
{
|
||||
if (m_depthStencilView) m_depthStencilView->Release();
|
||||
if (m_depthBuffer) m_depthBuffer->Release();
|
||||
}
|
||||
|
||||
bool DepthBuffer::Initialize()
|
||||
{
|
||||
// Create a depth buffer
|
||||
D3D11_TEXTURE2D_DESC desc = {};
|
||||
desc.Width = m_width;
|
||||
desc.Height = m_height;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_D32_FLOAT;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Usage = D3D11_USAGE_DEFAULT;
|
||||
desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
||||
desc.CPUAccessFlags = 0;
|
||||
|
||||
HRESULT hr = m_device->CreateTexture2D(&desc, nullptr, &m_depthBuffer);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create DepthStencilView
|
||||
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc = {};
|
||||
dsvDesc.Format = DXGI_FORMAT_D32_FLOAT;
|
||||
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
|
||||
hr = m_device->CreateDepthStencilView(m_depthBuffer, &dsvDesc, &m_depthStencilView);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
26
project/src/Buffers/DepthBuffer.h
Normal file
26
project/src/Buffers/DepthBuffer.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef GP1_DIRECTX_DEPTHBUFFER_H
|
||||
#define GP1_DIRECTX_DEPTHBUFFER_H
|
||||
|
||||
#include <d3d11.h>
|
||||
|
||||
class DepthBuffer
|
||||
{
|
||||
public:
|
||||
DepthBuffer(ID3D11Device* device, UINT width, UINT height);
|
||||
~DepthBuffer();
|
||||
|
||||
bool Initialize();
|
||||
ID3D11Texture2D* GetDepthBuffer() const { return m_depthBuffer; }
|
||||
ID3D11DepthStencilView* GetDepthStencilView() const { return m_depthStencilView; }
|
||||
|
||||
private:
|
||||
ID3D11Device* m_device;
|
||||
UINT m_width;
|
||||
UINT m_height;
|
||||
|
||||
ID3D11Texture2D* m_depthBuffer;
|
||||
ID3D11DepthStencilView* m_depthStencilView;
|
||||
};
|
||||
|
||||
|
||||
#endif //GP1_DIRECTX_DEPTHBUFFER_H
|
||||
27
project/src/Buffers/SamplerState.cpp
Normal file
27
project/src/Buffers/SamplerState.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "SamplerState.h"
|
||||
|
||||
SamplerState::SamplerState(ID3D11Device *device)
|
||||
: m_device(device), m_samplerState(nullptr) {
|
||||
}
|
||||
|
||||
SamplerState::~SamplerState() {
|
||||
if (m_samplerState) m_samplerState->Release();
|
||||
}
|
||||
|
||||
bool SamplerState::Initialize() {
|
||||
D3D11_SAMPLER_DESC samplerDesc = {};
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_LESS_EQUAL;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
HRESULT hr = m_device->CreateSamplerState(&samplerDesc, &m_samplerState);
|
||||
if (FAILED(hr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
20
project/src/Buffers/SamplerState.h
Normal file
20
project/src/Buffers/SamplerState.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef GP1_DIRECTX_SAMPLERSTATE_H
|
||||
#define GP1_DIRECTX_SAMPLERSTATE_H
|
||||
|
||||
#include <d3d11.h>
|
||||
|
||||
class SamplerState
|
||||
{
|
||||
public:
|
||||
SamplerState(ID3D11Device* device);
|
||||
~SamplerState();
|
||||
|
||||
bool Initialize();
|
||||
ID3D11SamplerState* GetSamplerState() const { return m_samplerState; }
|
||||
|
||||
private:
|
||||
ID3D11Device* m_device;
|
||||
ID3D11SamplerState* m_samplerState;
|
||||
};
|
||||
|
||||
#endif //GP1_DIRECTX_SAMPLERSTATE_H
|
||||
44
project/src/Buffers/ShadowMapBuffer.cpp
Normal file
44
project/src/Buffers/ShadowMapBuffer.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "ShadowMapBuffer.h"
|
||||
|
||||
ShadowMapBuffer::ShadowMapBuffer(ID3D11Device *device, UINT width, UINT height)
|
||||
: m_device(device), m_width(width), m_height(height), m_shaderResourceView(nullptr),
|
||||
m_depthBuffer(device, width, height), m_samplerState(device) {
|
||||
}
|
||||
|
||||
ShadowMapBuffer::~ShadowMapBuffer() {
|
||||
if (m_shaderResourceView) m_shaderResourceView->Release();
|
||||
}
|
||||
|
||||
bool ShadowMapBuffer::Initialize() {
|
||||
if (!m_depthBuffer.Initialize()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_samplerState.Initialize()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create ShaderResourceView for the depth buffer
|
||||
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
|
||||
srvDesc.Format = DXGI_FORMAT_R32_FLOAT;
|
||||
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
||||
HRESULT hr = m_device->CreateShaderResourceView(m_depthBuffer.GetDepthBuffer(), &srvDesc, &m_shaderResourceView);
|
||||
if (FAILED(hr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ShadowMapBuffer::SetRenderTarget(ID3D11DeviceContext *deviceContext) {
|
||||
deviceContext->OMSetRenderTargets(0, nullptr, m_depthBuffer.GetDepthStencilView());
|
||||
}
|
||||
|
||||
void ShadowMapBuffer::ResetRenderTarget(ID3D11DeviceContext *deviceContext) {
|
||||
ID3D11RenderTargetView *nullRTV = nullptr;
|
||||
deviceContext->OMSetRenderTargets(1, &nullRTV, nullptr);
|
||||
}
|
||||
|
||||
ID3D11DepthStencilView *ShadowMapBuffer::GetDepthStencilView() {
|
||||
return m_depthBuffer.GetDepthStencilView();
|
||||
}
|
||||
44
project/src/Buffers/ShadowMapBuffer.h
Normal file
44
project/src/Buffers/ShadowMapBuffer.h
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by Bram on 13/01/2025.
|
||||
//
|
||||
|
||||
#ifndef GP1_DIRECTX_SHADOWMAPBUFFER_H
|
||||
#define GP1_DIRECTX_SHADOWMAPBUFFER_H
|
||||
|
||||
#include <d3d11.h>
|
||||
#include <DirectXMath.h>
|
||||
#include "DepthBuffer.h"
|
||||
#include "SamplerState.h"
|
||||
|
||||
class ShadowMapBuffer {
|
||||
public:
|
||||
ShadowMapBuffer(ID3D11Device *device, UINT width, UINT height);
|
||||
|
||||
~ShadowMapBuffer();
|
||||
|
||||
bool Initialize();
|
||||
|
||||
void SetRenderTarget(ID3D11DeviceContext *deviceContext);
|
||||
|
||||
void ResetRenderTarget(ID3D11DeviceContext *deviceContext);
|
||||
|
||||
// Get the depth stencil view and the sampler state
|
||||
ID3D11ShaderResourceView *GetShaderResourceView() const { return m_shaderResourceView; }
|
||||
|
||||
ID3D11SamplerState *GetSamplerState() const { return m_samplerState.GetSamplerState(); }
|
||||
|
||||
|
||||
ID3D11DepthStencilView *GetDepthStencilView();
|
||||
|
||||
private:
|
||||
ID3D11Device *m_device;
|
||||
UINT m_width;
|
||||
UINT m_height;
|
||||
|
||||
DepthBuffer m_depthBuffer;
|
||||
SamplerState m_samplerState;
|
||||
|
||||
ID3D11ShaderResourceView *m_shaderResourceView;
|
||||
};
|
||||
|
||||
#endif //GP1_DIRECTX_SHADOWMAPBUFFER_H
|
||||
Reference in New Issue
Block a user