From db81b3a3b97a778dfb449331ff30aea6dceb2418 Mon Sep 17 00:00:00 2001
From: Lightling <contactlightling@gmail.com>
Date: Sat, 19 Mar 2022 17:23:25 -0400
Subject: [PATCH] create helper for directional light

---
 Game.cpp               |  2 +-
 SimplePixelShader.hlsl | 19 +++++++++++++------
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/Game.cpp b/Game.cpp
index 00d5c90..ff5aed4 100644
--- a/Game.cpp
+++ b/Game.cpp
@@ -179,7 +179,7 @@ void Game::Update(float deltaTime, float totalTime)
 	for (int i = 0; i < entities.size(); ++i)
 	{
 		entities[i]->GetTransform()->SetRotation(1.0f * (i + 1) * sin(totalTime), 1.0f * (i + 1) * sin(totalTime), 1.0f * (i + 1) * sin(totalTime));
-		entities[i]->GetMaterial()->SetRoughness(sin(totalTime) * 0.5f + 0.5f);
+		entities[i]->GetMaterial()->SetRoughness(sin(totalTime * 4) * 0.5f + 0.49f);
 	}
 }
 
diff --git a/SimplePixelShader.hlsl b/SimplePixelShader.hlsl
index 2a89796..f95df1a 100644
--- a/SimplePixelShader.hlsl
+++ b/SimplePixelShader.hlsl
@@ -11,7 +11,7 @@ cbuffer ExternalData : register(b0)
 	Light directionalLight1;
 }
 
-float calculateSpecular(float3 normal, float3 worldPosition, float3 cameraPosition, float3 direction, float roughness)
+float calculateSpecular(float3 normal, float3 direction, float3 worldPosition, float3 cameraPosition, float roughness)
 {
 	return getSpecular(
 		getView(cameraPosition, worldPosition),
@@ -20,15 +20,22 @@ float calculateSpecular(float3 normal, float3 worldPosition, float3 cameraPositi
 	);
 }
 
+float3 calculateDirectionalLight(Light light, float3 normal, float3 worldPosition, float3 cameraPosition, float roughness, float3 surfaceColor)
+{
+	float3 lightDirection = normalize(light.Direction);
+	float diffuse = getDiffuse(normal, -lightDirection);
+	float specular = calculateSpecular(normal, lightDirection, worldPosition, cameraPosition, roughness);
+
+	return (diffuse * surfaceColor + specular) * light.Intensity * light.Color;
+}
+
 float4 main(VertexToPixel input) : SV_TARGET
 {
 	input.normal = normalize(input.normal);
-	float3 directionalLight1Dir = normalize(-directionalLight1.Direction);
 
-	float diffuse = getDiffuse(input.normal, directionalLight1Dir);
-	float specular = calculateSpecular(input.normal, input.worldPosition, cameraPosition, directionalLight1Dir, roughness);
-
-	float3 final = (diffuse * directionalLight1.Color * tint) + (ambient * tint) + specular;
+	float3 light = calculateDirectionalLight(directionalLight1, input.normal, input.worldPosition, cameraPosition, roughness, tint);
+	float3 ambientTint = ambient * tint;
+	float3 final = light + ambientTint;
 
 	return float4(final, 1);
 }