From 29b3605cbaab729c9181e93118aa566b7360d8c4 Mon Sep 17 00:00:00 2001 From: Lightling Date: Sun, 27 Mar 2022 16:07:07 -0400 Subject: [PATCH] make corrections to light calculations --- Game.cpp | 18 +++++++++--------- Helpers.hlsli | 2 +- SimplePixelShader.hlsl | 11 ++++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Game.cpp b/Game.cpp index 46aa80c..a991acf 100644 --- a/Game.cpp +++ b/Game.cpp @@ -123,36 +123,36 @@ void Game::LoadTextures() void Game::LoadLighting() { - ambient = XMFLOAT3(0.5f, 0.5f, 0.5f); + ambient = XMFLOAT3(0.1f, 0.1f, 0.25f); Light directionalLight0 = {}; directionalLight0.Type = LIGHT_TYPE_DIRECTIONAL; - directionalLight0.Direction = XMFLOAT3(1, 0, 0); + directionalLight0.Direction = XMFLOAT3(1, 0.5f, 0.5f); directionalLight0.Color = XMFLOAT3(1, 1, 1); - directionalLight0.Intensity = 0.5f; + directionalLight0.Intensity = 1.0f; Light directionalLight1 = {}; directionalLight1.Type = LIGHT_TYPE_DIRECTIONAL; - directionalLight1.Direction = XMFLOAT3(0, -1, 0); + directionalLight1.Direction = XMFLOAT3(-0.25f, -1, 0.75f); directionalLight1.Color = XMFLOAT3(1, 1, 1); - directionalLight1.Intensity = 0.5f; + directionalLight1.Intensity = 1.0f; Light directionalLight2 = {}; directionalLight2.Type = LIGHT_TYPE_DIRECTIONAL; directionalLight2.Direction = XMFLOAT3(-1, 1, -0.5f); directionalLight2.Color = XMFLOAT3(1, 1, 1); - directionalLight2.Intensity = 0.5f; + directionalLight2.Intensity = 1.0f; Light pointLight0 = {}; pointLight0.Type = LIGHT_TYPE_POINT; - pointLight0.Position = XMFLOAT3(-2, -2, 0); + pointLight0.Position = XMFLOAT3(-1.5f, 0, 0); pointLight0.Color = XMFLOAT3(1, 1, 1); - pointLight0.Intensity = 0.5f; + pointLight0.Intensity = 1.0f; pointLight0.Range = 10; Light pointLight1 = {}; pointLight1.Type = LIGHT_TYPE_POINT; - pointLight1.Position = XMFLOAT3(2, 2, 0); + pointLight1.Position = XMFLOAT3(1.5f, 0, 0); pointLight1.Color = XMFLOAT3(1, 1, 1); pointLight1.Intensity = 0.5f; pointLight1.Range = 10; diff --git a/Helpers.hlsli b/Helpers.hlsli index 80f6e47..f674ce4 100644 --- a/Helpers.hlsli +++ b/Helpers.hlsli @@ -10,7 +10,7 @@ float3 getView(float3 cameraPosition, float3 pixelWorldPosition) // gets reflection vector, needed per light float3 getReflection(float3 direction, float3 normal) { - return reflect(direction, normal); + return reflect(-direction, normal); } // gets specular exponent: (1-roughness) * max diff --git a/SimplePixelShader.hlsl b/SimplePixelShader.hlsl index 949df9d..01c58cc 100644 --- a/SimplePixelShader.hlsl +++ b/SimplePixelShader.hlsl @@ -40,9 +40,9 @@ float3 calculateDirectionalLight(Light light, float3 normal, float3 view, float // Gets the RGB value of a pixel with a point light float3 calculatePointLight(Light light, float3 normal, float3 view, float3 worldPosition, float roughness, float3 surfaceColor) { - float3 lightDirection = normalize(worldPosition - light.Position); + float3 lightDirection = normalize(light.Position - worldPosition); float attenuation = getAttenuation(light.Position, worldPosition, light.Range); - float diffuse = getDiffuse(normal, -lightDirection); + float diffuse = getDiffuse(normal, lightDirection); float specular = calculateSpecular(normal, lightDirection, view, roughness); return (diffuse * surfaceColor + specular) * attenuation * light.Intensity * light.Color; @@ -58,7 +58,8 @@ float4 main(VertexToPixel input) : SV_TARGET float3 view = getView(cameraPosition, input.worldPosition); float4 albedo = Albedo.Sample(BasicSampler, input.uv).rgba; - float3 light = ambient * albedo.rgb * tint; + float3 surface = albedo.rgb * tint; + float3 light = ambient * surface; // loop through lights for (int i = 0; i < LIGHT_COUNT; i++) @@ -66,10 +67,10 @@ float4 main(VertexToPixel input) : SV_TARGET switch (lights[i].Type) { case LIGHT_TYPE_DIRECTIONAL: - light += calculateDirectionalLight(lights[i], input.normal, view, roughness, tint); + light += calculateDirectionalLight(lights[i], input.normal, view, roughness, surface); break; case LIGHT_TYPE_POINT: - light += calculatePointLight(lights[i], input.normal, view, input.worldPosition, roughness, tint); + light += calculatePointLight(lights[i], input.normal, view, input.worldPosition, roughness, surface); break; } }