support a dynamic number of lights

This commit is contained in:
lightling 2022-03-29 10:34:00 -04:00
parent 4c911f8348
commit 51206816aa
3 changed files with 17 additions and 4 deletions

View file

@ -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,
};
}

View file

@ -34,6 +34,7 @@ void Material::Activate(Transform* _transform, std::shared_ptr<Camera> _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();

View file

@ -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)
{