Skip to content

Commit

Permalink
Fix : Directional light bug
Browse files Browse the repository at this point in the history
  • Loading branch information
InsaneZeroGame committed Jul 29, 2024
1 parent 2c5d315 commit 228bcc9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion engine/stb_texture_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace AssetLoader
const auto& filePath = std::filesystem::path(fileName);
const auto& fileExtension = str_tolower(filePath.extension().string());

if (fileExtension == ".png")
if (fileExtension == ".png" || fileExtension == ".jpg" || fileExtension == ".jpeg")
{
return stbLoadTexture(filePath);
}
Expand Down
8 changes: 8 additions & 0 deletions renderer/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ void Renderer::Gui::EndGui(ID3D12GraphicsCommandList* InCmd) {

void Renderer::Gui::Render() {
ImGui::TextColored({ 0.0f, 1.0f, 0.0f, 1.0f }, "Hello Re3D");
ImGui::SliderFloat3("SunLight Dir", mRenderer.lock()->mSunLightDir.data(),-360,360);
ImGui::SliderFloat("SunLight Intensity", &mRenderer.lock()->mSunLightIntensity, 0.0f, 10.0f);


if (mRenderer.lock())
{
//ImGui::Checkbox("Tone Mapping", &mRenderer.lock()->mUseToneMapping);
Expand All @@ -59,6 +63,10 @@ void Renderer::Gui::Render() {
ImGui::SliderFloat("Bloom: Kernel Size", &mRenderer.lock()->mBloomBlurKernelSize, 0.0f, 10.0f);
ImGui::SliderFloat("Bloom: Brightness", &mRenderer.lock()->mBloomBrightness, 1.0, 10.0f);

ImGui::SliderFloat("Bloom: bloom intensity", &mRenderer.lock()->mbloomIntensity, 0.0, 10.0f);
ImGui::SliderFloat("Bloom: base intensity", &mRenderer.lock()->mbaseIntensity, 0.0, 10.0f);
ImGui::SliderFloat("Bloom: bloom saturation", &mRenderer.lock()->mbloomSaturation, 0.0, 10.0f);
ImGui::SliderFloat("Bloom: base saturation", &mRenderer.lock()->mbloomBaseSaturation, 0.0, 10.0f);
}
if (mCurrentScene)
{
Expand Down
34 changes: 21 additions & 13 deletions renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ void Renderer::BaseRenderer::SetTargetWindowAndCreateSwapChain(HWND InWindow, in
mDeviceManager->SetTargetWindowAndCreateSwapChain(InWindow, InWidth, InHeight);
//Use Reverse Z
mDefaultCamera = std::make_unique<Gameplay::PerspectCamera>((float)InWidth, (float)InHeight, 0.1f,true);
mDefaultCamera->LookAt({ 0.0,3.0,2.0 }, { 0.0f,3.0f,0.0f }, { 0.0f,1.0f,0.0f });
mDefaultCamera->LookAt({ 3.0,3.0,2.0 }, { 0.0f,3.0f,0.0f }, { 0.0f,1.0f,0.0f });
mShadowCamera = std::make_unique<Gameplay::PerspectCamera>((float)InWidth, (float)InHeight, 0.1f,false);
mShadowCamera->LookAt({ -0.1,50,5.0 }, { 0.0f,0.0f,0.0f }, { 0.0f,1.0f,0.0f });
mShadowCamera->LookAt({ -5,25,0.0 }, { 0.0f,0.0f,0.0f }, { 0.0f,1.0f,0.0f });
mViewPort = { 0,0,(float)mWidth,(float)mHeight,0.0,1.0 };
mRect = { 0,0,mWidth,mHeight };
mContext->CreateWindowDependentResource(InWidth, InHeight);
CreateGui();
}


void Renderer::BaseRenderer::Update(float delta)
{
UpdataFrameData();
Expand Down Expand Up @@ -340,9 +341,9 @@ void Renderer::BaseRenderer::CreateRenderTask()

ppBloomCombine->SetSourceTexture(sceneTex->GetSRVGPU());
ppBloomCombine->SetSourceTexture2(blurHalf->GetSRVGPU());
ppBloomCombine->SetBloomCombineParameters(1.25f, 1.f, 1.f, 1.f);
ppBloomCombine->SetBloomCombineParameters(mbloomIntensity, mbaseIntensity, mbloomSaturation, mbloomBaseSaturation);
mGraphicsCmd->OMSetRenderTargets(1, &bloomRes->GetRTV(), FALSE, nullptr);
mGraphicsCmd->RSSetViewports(1, &mViewPort);
mGraphicsCmd->RSSetViewports(1, &mViewPort);
ppBloomCombine->Process(mGraphicsCmd);

{
Expand Down Expand Up @@ -436,8 +437,10 @@ void Renderer::BaseRenderer::CreateBuffers()
mFrameDataGPU[i] = std::make_unique<Resource::VertexBuffer>();
mFrameDataGPU[i]->Create(L"FrameData", 1, sizeof(FrameData),D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);

mFrameData[i].DirectionalLightColor = SimpleMath::Vector4(1.0f, 1.0f, 1.0f, 1.0f);
mFrameData[i].DirectionalLightDir = SimpleMath::Vector4(1.0, 1.0, 2.0, 1.0f);
mFrameData[i].DirectionalLightColor = SimpleMath::Vector4(1.0f, 1.0f, 1.0f, 1.0);
mFrameData[i].DirectionalLightDir = SimpleMath::Vector3(1.0, 10.0, 12.0);
mFrameData[i].SunLightIntensity = 1.0;

}

mLightUploadBuffer = std::make_shared<Resource::UploadBuffer>();
Expand Down Expand Up @@ -594,12 +597,13 @@ void Renderer::BaseRenderer::CreateSkybox()
LoadStaticMeshToGpu(*mSkybox->GetStaticMeshComponent());

mSkyboxTexture = std::make_shared<Resource::Texture>();
auto skybox_bottom = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/ny.png");
auto skybox_top = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/py.png");
auto skybox_front = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/pz.png");
auto skybox_back = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/nz.png");
auto skybox_left = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/nx.png");
auto skybox_right = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/px.png");
auto skybox_bottom = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/bottom.jpg");
auto skybox_top = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/top.jpg");
auto skybox_front = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/front.jpg");
auto skybox_back = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/back.jpg");
auto skybox_left = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/left.jpg");
auto skybox_right = AssetLoader::gStbTextureLoader->LoadTextureFromFile("skybox/right.jpg");

Ensures(skybox_bottom.has_value());
Ensures(skybox_top.has_value());
Ensures(skybox_front.has_value());
Expand Down Expand Up @@ -655,7 +659,7 @@ void Renderer::BaseRenderer::InitPostProcess()
DXGI_FORMAT_UNKNOWN);
ppToneMap = std::make_unique<ToneMapPostProcess>(g_Device, toneMapRTState,
ToneMapPostProcess::ACESFilmic,
ToneMapPostProcess::SRGB);
ToneMapPostProcess::Linear);

//RenderTargetState imageBlit(g_DisplayFormat,
// DXGI_FORMAT_D32_FLOAT);
Expand Down Expand Up @@ -961,6 +965,10 @@ void Renderer::BaseRenderer::UpdataFrameData()
mFrameData[frameDataCpuIndex].ShadowViewMatrix = mShadowCamera->GetView();
mFrameData[frameDataCpuIndex].ShadowViewPrjMatrix = mShadowCamera->GetPrjView();
mFrameData[frameDataCpuIndex].NormalMatrix = mDefaultCamera->GetNormalMatrix();
mFrameData[frameDataCpuIndex].DirectionalLightDir.x = mSunLightDir[0];
mFrameData[frameDataCpuIndex].DirectionalLightDir.y = mSunLightDir[1];
mFrameData[frameDataCpuIndex].DirectionalLightDir.z = mSunLightDir[2];
mFrameData[frameDataCpuIndex].SunLightIntensity = mSunLightIntensity;
mFrameData[frameDataCpuIndex].DirectionalLightDir.Normalize();
mFrameData[frameDataCpuIndex].LightGridZParams.x = gridParas.x;
mFrameData[frameDataCpuIndex].LightGridZParams.y = gridParas.y;
Expand Down
10 changes: 8 additions & 2 deletions renderer/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace Renderer
DirectX::SimpleMath::Matrix NormalMatrix;
DirectX::SimpleMath::Matrix ShadowViewMatrix;
DirectX::SimpleMath::Matrix ShadowViewPrjMatrix;
DirectX::SimpleMath::Vector4 DirectionalLightDir;
DirectX::SimpleMath::Vector3 DirectionalLightDir;
float SunLightIntensity;
DirectX::SimpleMath::Vector4 DirectionalLightColor;
DirectX::SimpleMath::Vector4 ViewSizeAndInvSize;
DirectX::SimpleMath::Matrix ClipToView;
Expand Down Expand Up @@ -81,7 +82,12 @@ namespace Renderer
float mBloomThreshold = 0.25;
float mBloomBlurKernelSize = 4.0f;
float mBloomBrightness = 1.0f;

float mbloomIntensity = 0.0f;
float mbaseIntensity = 1.0f;
float mbloomSaturation = 1.0f;
float mbloomBaseSaturation = 1.0f;
std::array<float, 3> mSunLightDir = {1.0,1.0,1.0};
float mSunLightIntensity = 1.0;
protected:
void CreateGui();
void CreateRenderTask();
Expand Down
13 changes: 6 additions & 7 deletions renderer/shaders/ForwardPS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ float3 ApplyLightCommon(
float3 halfVec = normalize(lightDir - viewDir);
float nDotH = saturate(dot(halfVec, normal));

FSchlick(specularColor,diffuseColor , lightDir, halfVec);
//FSchlick(specularColor,diffuseColor , lightDir, halfVec);

float specularFactor = nDotH;
//float specularFactor = specularMask * pow(nDotH, gloss) * (gloss + 2) / 8;


//specularFactor = 0.0f;
float nDotL = saturate(dot(normal, lightDir));

//return nDotL * lightColor * (diffuseColor + specularColor);
Expand Down Expand Up @@ -137,15 +137,14 @@ float4 main(PSInput input) : SV_TARGET
return (diffuse + ambient) * colorAfterCorrection;
#else
float4 diffuseColor = defaultTexture.Sample(defaultSampler, input.UVCoord);
//float4 diffuseColor = input.color;
//float4 diffuseColor = float4(input.color);
float4 DirLightViewSpace = mul(float4(input.DirectionalLightDir.xyz, 0.0), frameData.ViewMatrix);
float DirLightIntense = 0.25f;
float3 directionalLight = ApplyLightCommon(
diffuseColor.xyz,
diffuseColor.xyz,
0, 0, input.normalViewSpace.xyz, input.viewsSpacePos,
DirLightViewSpace, input.DirectionalLightColor) * DirLightIntense;

DirLightViewSpace, input.DirectionalLightColor) * frameData.DirectionalLightDir.w;
//return float4(directionalLight, 1.0f);
float3 pointLight = float3(0.0,0.0,0.0);
uint GirdIndex = ComputeLightGridCellIndex(uint2(input.position.xy), input.position.w);
uint3 GridCoord = ComputeLightGridCellCoordinate(uint2(input.position.xy), input.position.w, 0);
Expand Down Expand Up @@ -183,7 +182,7 @@ float4 main(PSInput input) : SV_TARGET
float shadow = shadowMap.SampleCmpLevelZero(shadowSampler, shadowCoord, input.shadowCoord.z/input.shadowCoord.w);
//shadow = 1.0f;
//return diffuseColor;
return color * (0.15 + 0.85 * shadow);
return color * (0.25 + 0.75 * shadow);
//if (input.position.x / screen_size < 0.5)
//{
// return diffuse;
Expand Down

0 comments on commit 228bcc9

Please sign in to comment.