From 75c719a66d37a6e2d70cd75dd972be9a4cf465af Mon Sep 17 00:00:00 2001 From: Bram Verhulst Date: Fri, 17 Jan 2025 19:50:50 +0100 Subject: [PATCH] Fix shadow again --- project/resources/SuperSimpleDiffuse.fx | 35 ++++++++++++++++++++----- project/src/Effects/BaseEffect.cpp | 2 +- project/src/Renderer.cpp | 1 - project/src/Scenes/ShadowTestScene.cpp | 31 +++++++++++++++++++--- project/src/Scenes/ShadowTestScene.h | 2 ++ project/src/main.cpp | 9 +++++-- 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/project/resources/SuperSimpleDiffuse.fx b/project/resources/SuperSimpleDiffuse.fx index 569ea6e..df47b41 100644 --- a/project/resources/SuperSimpleDiffuse.fx +++ b/project/resources/SuperSimpleDiffuse.fx @@ -101,8 +101,6 @@ float ShadowCalculation(float4 shadowCoord, float3 normal) { shadow /= 9.0f; -// float shadowDepth = gShadowMap.Sample(gShadowSampler2, UVCoords.xy).r; - if(projCoord.z > 1.0) shadow = 0.0; @@ -110,15 +108,38 @@ float ShadowCalculation(float4 shadowCoord, float3 normal) { return shadow; } // Pixel shader +// float4 PS(VS_OUTPUT input) : SV_TARGET { +// float shadowFactor = ShadowCalculation(input.ShadowCoord, input.Normal); +// +// float3 diffuseColor = gDiffuseMap.Sample(gSampleState, input.TexCoord).rgb; +// +// float3 finalColor = diffuseColor * (1 - shadowFactor); +// finalColor += gAmbient; +// +// return float4(finalColor, 1.0f); +// } + float4 PS(VS_OUTPUT input) : SV_TARGET { - float shadowFactor = ShadowCalculation(input.ShadowCoord, input.Normal); + float3 color = gDiffuseMap.Sample(gSampleState, input.TexCoord).rgb; + float3 normal = normalize(input.Normal); - float3 diffuseColor = gDiffuseMap.Sample(gSampleState, input.TexCoord).rgb; + float3 lightDir = normalize(-gLightDirection); + float3 viewDir = normalize(gCameraPosition - input.WorldPosition.xyz); - float3 finalColor = diffuseColor * (1 - shadowFactor); - finalColor += gAmbient; + float3 ambient = gAmbient; - return float4(finalColor, 1.0f); + float diff = max(dot(normal, lightDir), 0.0); + + float3 diffuse = gLightColor * diff * color; + + float3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32); + float3 specular = gLightColor * spec; + + float shadow = ShadowCalculation(input.ShadowCoord, normal); + float3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color; + + return float4(lighting, 1.0f); } // DepthStencilState diff --git a/project/src/Effects/BaseEffect.cpp b/project/src/Effects/BaseEffect.cpp index 182f819..d83dcdf 100644 --- a/project/src/Effects/BaseEffect.cpp +++ b/project/src/Effects/BaseEffect.cpp @@ -54,6 +54,7 @@ ID3DX11Effect *BaseEffect::LoadEffect(ID3D11Device *devicePtr, const std::wstrin ss << errorsPtr[i]; OutputDebugStringW(ss.str().c_str()); + errorBlobPtr->Release(); errorBlobPtr = nullptr; return nullptr; @@ -72,7 +73,6 @@ void BaseEffect::SetWorldViewProjMatrix(const dae::Matrix &matrix) { } void BaseEffect::NextSamplingState() { - } void BaseEffect::SetCameraPos(const dae::Vector3 &vector3) const { diff --git a/project/src/Renderer.cpp b/project/src/Renderer.cpp index ce2ff4d..3b3e1cb 100644 --- a/project/src/Renderer.cpp +++ b/project/src/Renderer.cpp @@ -4,7 +4,6 @@ #include "Utils.h" #include "Texture.h" #include "Effects/Effect.h" -#include "Effects/FireEffect.h" #include "HitTest.h" #include "Scenes/MainScene.h" #include "Scenes/DioramaScene.h" diff --git a/project/src/Scenes/ShadowTestScene.cpp b/project/src/Scenes/ShadowTestScene.cpp index 7173d7a..413d0b5 100644 --- a/project/src/Scenes/ShadowTestScene.cpp +++ b/project/src/Scenes/ShadowTestScene.cpp @@ -34,9 +34,26 @@ void ShadowTestScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *D auto otherEffect = new ShadowDiffuse(DevicePtr, L"resources/SuperSimpleDiffuse.fx"); auto shadowMesh = new ShadowMesh(DevicePtr, vertices, indices, material, shadowEffect, otherEffect); + + + if(!Utils::ParseOBJNew("resources/tuktuk.obj", vertices, indices, true)){ + assert(true && "Model failed to load"); + } + + material = std::make_shared(); + material->diffuseTexturePtr = Texture::LoadFromFile("resources/tuktuk.png", DevicePtr); + + shadowEffect = new ShadowEffect(DevicePtr, L"resources/shadowEffect.fx"); + otherEffect = new ShadowDiffuse(DevicePtr, L"resources/SuperSimpleDiffuse.fx"); + + auto shadowMesh2 = new ShadowMesh(DevicePtr, vertices, indices, material, shadowEffect, otherEffect); + + shadowMesh2->SetWorldMatrix(Matrix::CreateTranslation(Vector3(0, -5, 40))); + m_tuktukMesh = shadowMesh2; + m_shadowMeshes.push_back(shadowMesh2); // -// std::vector> materialMeshes; +// std::vector> materialMeshes; // Utils::LoadObjWithMaterials("resources/scene.obj", materialMeshes, true, DevicePtr); // for (const auto &mesh: materialMeshes) { // if (mesh->vertices.size() > 0) { @@ -70,14 +87,20 @@ void ShadowTestScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *D } void ShadowTestScene::Update() { - // m_Light.SetTarget(lightTarget); Vector3 pos = m_Light.GetPosition(); pos.y = sin(SDL_GetTicks() / 1000.f) * 10.f + 10.f; m_Light.SetPosition(pos); - - m_Light.Update(); + + + //rotate tutkuk + + Matrix newOne; + newOne *= Matrix::CreateRotationY(0.001f * SDL_GetTicks()); + newOne *= Matrix::CreateTranslation(Vector3(20, -5, 40)); + m_tuktukMesh->SetWorldMatrix(newOne); + } void ShadowTestScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, diff --git a/project/src/Scenes/ShadowTestScene.h b/project/src/Scenes/ShadowTestScene.h index 0b2865e..25df68c 100644 --- a/project/src/Scenes/ShadowTestScene.h +++ b/project/src/Scenes/ShadowTestScene.h @@ -34,6 +34,8 @@ private: float angle = 0.0f; + ShadowMesh* m_tuktukMesh; + void RenderShadowMapToScreen(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView *renderTargetViewPtr); }; diff --git a/project/src/main.cpp b/project/src/main.cpp index c31639f..c2dcc94 100644 --- a/project/src/main.cpp +++ b/project/src/main.cpp @@ -120,6 +120,7 @@ void CheckController(Renderer* pRenderer, bool& printFPS){ } } + int main(int argc, char *args[]) { //Unreferenced parameters (void) argc; @@ -129,9 +130,13 @@ int main(int argc, char *args[]) { //Create window + surfaces SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); +// +// const uint32_t width = 640; +// const uint32_t height = 480; - const uint32_t width = 640; - const uint32_t height = 480; + + const uint32_t width = 900; + const uint32_t height = 600; RenderSettings::GetInstance().setHeight(height); RenderSettings::GetInstance().setWidth(width);