From 51206816aa07c718fb1c380126b96f90d030521f Mon Sep 17 00:00:00 2001 From: Lightling Date: Tue, 29 Mar 2022 10:34:00 -0400 Subject: [PATCH] support a dynamic number of lights --- Game.cpp | 8 ++++++++ Material.cpp | 1 + SimplePixelShader.hlsl | 12 ++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Game.cpp b/Game.cpp index a01cf77..a22c255 100644 --- a/Game.cpp +++ b/Game.cpp @@ -152,12 +152,20 @@ void Game::LoadLighting() pointLight1.Intensity = 0.25f; pointLight1.Range = 10; + Light pointLight2 = {}; + pointLight2.Type = LIGHT_TYPE_POINT; + pointLight2.Position = XMFLOAT3(0, 2, 0); + pointLight2.Color = XMFLOAT3(1, 0, 0); + pointLight2.Intensity = 0.25f; + pointLight2.Range = 10; + lights = { directionalLight0, directionalLight1, directionalLight2, pointLight0, pointLight1, + pointLight2, }; } diff --git a/Material.cpp b/Material.cpp index 1d3b832..a1a3159 100644 --- a/Material.cpp +++ b/Material.cpp @@ -34,6 +34,7 @@ void Material::Activate(Transform* _transform, std::shared_ptr _camera, pixelShader->SetFloat3("ambient", _ambient); pixelShader->SetFloat("emitAmount", GetEmitAmount()); pixelShader->SetFloat3("tint", GetTint()); + pixelShader->SetFloat("lightCount", (int)_lights.size()); pixelShader->SetData("lights", &_lights[0], sizeof(Light) * (int)_lights.size()); pixelShader->CopyAllBufferData(); pixelShader->SetShader(); diff --git a/SimplePixelShader.hlsl b/SimplePixelShader.hlsl index 7fa6b0b..a7c90d2 100644 --- a/SimplePixelShader.hlsl +++ b/SimplePixelShader.hlsl @@ -2,19 +2,23 @@ #include "Helpers.hlsli" #include "Lights.hlsli" -// temporary -#define LIGHT_COUNT 5 +#define MAX_LIGHTS 128 cbuffer ExternalData : register(b0) { float3 cameraPosition; float roughness; + float2 offset; float2 scale; + float3 ambient; float emitAmount; + float3 tint; - Light lights[LIGHT_COUNT]; + float lightCount; + + Light lights[MAX_LIGHTS]; } Texture2D Albedo : register(t0); @@ -70,7 +74,7 @@ float4 main(VertexToPixel input) : SV_TARGET float3 light = ambient * surface; // loop through lights - for (int i = 0; i < LIGHT_COUNT; i++) + for (int i = 0; i < lightCount; i++) { switch (lights[i].Type) {