Skip to content

Commit 103870c

Browse files
committed
Light: Started with shadow mapping
1 parent 1bf20a5 commit 103870c

File tree

3 files changed

+71
-25
lines changed

3 files changed

+71
-25
lines changed

src/Shader/ShadowPass.hlsl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cbuffer WVP : register(b0) {
2+
matrix World;
3+
matrix View;
4+
matrix Projection;
5+
}
6+
7+
struct VertexOutput
8+
{
9+
float4 position : SV_Position;
10+
};
11+
12+
VertexOutput VertexMain(float4 position : POSITION)
13+
{
14+
VertexOutput output;
15+
output.position = mul(position, World);
16+
output.position = mul(output.position, View);
17+
output.position = mul(output.position, Projection);
18+
19+
return output;
20+
}
21+
22+
void PixelMain(VertexOutput input)
23+
{
24+
return;
25+
}

src/private/Core/GameObject/Light/Light.cpp

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,47 @@ void Light::Init() {
1515
GameObject::Init();
1616

1717
if (D3D12* renderer = reinterpret_cast<D3D12*>(this->m_renderer)) {
18-
Transform transform = this->transform;
19-
XMVECTOR eye = XMVectorSet(
20-
transform.location.x,
21-
transform.location.y,
22-
transform.location.z,
23-
1.0f
24-
);
25-
26-
float pitch = XMConvertToRadians(transform.rotation.x);
27-
float yaw = XMConvertToRadians(transform.rotation.y);
28-
29-
XMVECTOR forward = XMVectorSet(
30-
cosf(pitch) * sinf(yaw),
31-
-sinf(pitch),
32-
-cosf(pitch) * cosf(yaw),
33-
0.0f
34-
);
18+
this->InitConstantBuffers(renderer);
19+
renderer->m_dsvHeap->Allocate(1);
20+
this->m_nDepthIndex = renderer->m_dsvHeap->GetLastDescriptorIndex();
3521

36-
XMVECTOR at = XMVectorAdd(eye, forward);
22+
this->m_shader = new Shader("ShadowPass.hlsl", "VertexMain", "PixelMain");
3723

38-
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
39-
this->m_lightBuffer.View = XMMatrixTranspose(XMMatrixLookAtLH(eye, at, up));
40-
this->m_lightBuffer.Projection = XMMatrixTranspose(XMMatrixPerspectiveFovLH(
41-
XMConvertToRadians(70.f),
42-
static_cast<float>(renderer->m_nWidth) / static_cast<float>(renderer->m_nHeight),
43-
0.001f,
44-
3000.f));
4524
}
4625

4726
}
4827

28+
void Light::InitConstantBuffers(D3D12* renderer) {
29+
Transform transform = this->transform;
30+
XMVECTOR eye = XMVectorSet(
31+
transform.location.x,
32+
transform.location.y,
33+
transform.location.z,
34+
1.0f
35+
);
36+
37+
float pitch = XMConvertToRadians(transform.rotation.x);
38+
float yaw = XMConvertToRadians(transform.rotation.y);
39+
40+
XMVECTOR forward = XMVectorSet(
41+
cosf(pitch) * sinf(yaw),
42+
-sinf(pitch),
43+
-cosf(pitch) * cosf(yaw),
44+
0.0f
45+
);
46+
47+
XMVECTOR at = XMVectorAdd(eye, forward);
48+
49+
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
50+
this->m_lightBuffer.View = XMMatrixTranspose(XMMatrixLookAtLH(eye, at, up));
51+
this->m_lightBuffer.Projection = XMMatrixTranspose(XMMatrixPerspectiveFovLH(
52+
XMConvertToRadians(70.f),
53+
static_cast<float>(renderer->m_nWidth) / static_cast<float>(renderer->m_nHeight),
54+
0.001f,
55+
3000.f));
56+
}
57+
58+
4959
void Light::Update() {
5060
GameObject::Update();
5161
}

src/public/Core/GameObject/Light/Light.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
#include "Core/GameObject/GameObject.h"
44
#include "Util.h"
55
#include <DXMath/DirectXMath.h>
6+
#include <d3d12.h>
7+
#include <wrl.h>
68

9+
using namespace Microsoft::WRL;
710
using namespace DirectX;
811

912
struct LightBuffer {
@@ -18,10 +21,18 @@ class Light : public GameObject {
1821
private:
1922
Core* m_core;
2023
Renderer* m_renderer;
24+
25+
ComPtr<ID3D12Resource> m_depth;
26+
UINT m_nDepthIndex;
27+
28+
void InitConstantBuffers(D3D12* renderer);
29+
30+
Shader* m_shader;
2131
public:
2232
RGBA m_lightColor;
2333

2434
LightBuffer m_lightBuffer;
35+
WVP m_wvp;
2536

2637
Light(std::string name);
2738

0 commit comments

Comments
 (0)