Skip to content

Commit 9bb6a0d

Browse files
committed
Phong lighting model (TEMPORAL)
1 parent 2055b3b commit 9bb6a0d

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

src/Shader/GBufferPass.hlsl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ cbuffer WVP : register(b0)
88
struct VertexOutput
99
{
1010
float4 position : SV_Position;
11-
float4 normal : NORMAL;
12-
float2 uv : TEXCOORD;
11+
float4 normal : NORMAL0;
12+
float2 uv : TEXCOORD0;
13+
float4 vertexPos : POSITION0;
1314
};
1415

1516
SamplerState texSampler : register(s0);
1617
Texture2D tex : register(t0);
1718

18-
VertexOutput VertexMain(float4 position : POSITION0, float4 normal : NORMAL0, float2 uv : TEXCOORD0)
19+
VertexOutput VertexMain(float4 position : POSITION, float4 normal : NORMAL, float2 uv : TEXCOORD)
1920
{
2021
VertexOutput output;
2122

2223
output.position = mul(position, World);
2324
output.position = mul(output.position, View);
2425
output.position = mul(output.position, Projection);
25-
output.normal = mul(normal, World);
26+
27+
output.normal = float4(normalize(mul((float3) normal, (float3x3) World)), 0.0f);
2628
output.uv = uv;
29+
output.vertexPos = mul(position, World);
2730

2831
return output;
2932
}
@@ -40,6 +43,6 @@ PixelOutput PixelMain(VertexOutput input)
4043
PixelOutput output;
4144
output.albedo = tex.Sample(texSampler, float2(input.uv.x, 1 - input.uv.y));
4245
output.normal = input.normal * 0.5f + 0.5f;
43-
output.position = input.position;
46+
output.position = input.vertexPos;
4447
return output;
4548
}

src/Shader/LightPass.hlsl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ struct PixelOutput
2222
PixelOutput PixelMain(VertexOutput input, uint index : SV_SampleIndex)
2323
{
2424
PixelOutput output;
25-
output.screen = albedo.Load(input.position.xy, index);
25+
float4 albedoColor = albedo.Load(input.position.xy, index);
26+
float4 nml = normalize(normal.Load(input.position.xy, index) * 2 - 1);
27+
float4 vertexPos = position.Load(input.position.xy, index);
28+
29+
float4 ambientColor = float4(0.1f, 0.1f, 0.1f, 1.f);
30+
float4 lightPos = float4(0.f, 200.f, 200.f, 1.f);
31+
float4 lightColor = float4(1.f, 1.f, 1.f, 1.f);
32+
33+
float3 lightDir = normalize(lightPos.xyz - vertexPos.xyz);
34+
float LdotN = saturate(dot(lightDir, nml.xyz));
35+
36+
float4 outputColor = saturate(saturate(LdotN + ambientColor) * lightColor) * albedoColor;
37+
38+
output.screen = outputColor;
2639
return output;
2740
}

src/private/Core/GameObject/Camera/EditorCamera.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "Core/GameObject/Camera/EditorCamera.h"
22

33
EditorCamera::EditorCamera(std::string name) : Camera(name) {
4-
this->m_fSensX = 15.f;
4+
this->m_fSensX = 10.f;
55
this->m_fSensY = 10.f;
66

7-
this->m_fSpeed = 5.f;
7+
this->m_fSpeed = 50.f;
88
}
99

1010
void EditorCamera::Init() {

src/private/Core/GameObject/Component/Mesh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void Mesh::InitConstantBuffer() {
122122
void Mesh::UpdateConstantBuffer() {
123123
UINT nWVPSize = (sizeof(this->m_wvp) + 255) & ~255;
124124
this->m_wvp.World = XMMatrixTranspose(XMMatrixIdentity());
125-
this->m_wvp.World *= XMMatrixTranspose(XMMatrixScaling(.01f, .01f, .01f));
125+
this->m_wvp.World *= XMMatrixTranspose(XMMatrixScaling(0.1f, 0.1f, 0.1f));
126126
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(this->m_transform.rotation.x)));
127127
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(this->m_transform.rotation.y)));
128128
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(this->m_transform.rotation.z)));

src/private/Core/Renderer/D3D12.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,16 @@ void D3D12::Init(HWND hwnd) {
137137
this->m_screenQuad->Init();
138138
this->InitGBufferShader();
139139

140-
RECT rect;
141-
GetClientRect(this->m_hwnd, &rect);
142-
143140
ZeroMemory(&this->m_viewport, sizeof(D3D12_VIEWPORT));
144-
this->m_viewport.Width = rect.right;
145-
this->m_viewport.Height = rect.bottom;
141+
this->m_viewport.Width = this->m_nWidth;
142+
this->m_viewport.Height = this->m_nHeight;
146143
this->m_viewport.MaxDepth = 1.f;
147144

148145
ZeroMemory(&this->m_scissor, sizeof(D3D12_RECT));
149146
this->m_scissor.left = 0;
150147
this->m_scissor.top = 0;
151-
this->m_scissor.right = rect.right;
152-
this->m_scissor.bottom = rect.bottom;
148+
this->m_scissor.right = this->m_nWidth;
149+
this->m_scissor.bottom = this->m_nHeight;
153150

154151

155152
ThrowIfFailed(this->m_list->Close());
@@ -286,8 +283,17 @@ void D3D12::Update() {
286283
this->m_list->ResolveSubresource(this->m_backBuffers[this->m_nActualBackBuffer].Get(), 0, sqRes.Get(), 0, DXGI_FORMAT_B8G8R8A8_UNORM);
287284

288285
this->ResourceBarrier(sqRes, D3D12_RESOURCE_STATE_RESOLVE_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
289-
this->ResourceBarrier(this->m_backBuffers[this->m_nActualBackBuffer], D3D12_RESOURCE_STATE_RESOLVE_DEST, D3D12_RESOURCE_STATE_PRESENT);
286+
this->ResourceBarrier(this->m_backBuffers[this->m_nActualBackBuffer], D3D12_RESOURCE_STATE_RESOLVE_DEST, D3D12_RESOURCE_STATE_RENDER_TARGET);
287+
this->m_list->OMSetRenderTargets(1, &rtv.cpuHandle, FALSE, nullptr);
288+
289+
ImGui_ImplWin32_NewFrame();
290+
ImGui_ImplDX12_NewFrame();
291+
ImGui::NewFrame();
292+
293+
ImGui::Render();
294+
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), this->m_list.Get());
290295

296+
this->ResourceBarrier(this->m_backBuffers[this->m_nActualBackBuffer], D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
291297
ThrowIfFailed(this->m_list->Close());
292298

293299
ID3D12CommandList* commandLists[] = {

0 commit comments

Comments
 (0)