support 3 directional lights

This commit is contained in:
lightling 2022-03-19 17:39:52 -04:00
parent db81b3a3b9
commit 8ec8b16a88
Signed by: lightling
GPG key ID: 016F11E0AA296B67
4 changed files with 50 additions and 12 deletions

View file

@ -80,6 +80,9 @@
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
</Link> </Link>
<FxCompile>
<ShaderModel>5.0</ShaderModel>
</FxCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile> <ClCompile>
@ -91,6 +94,9 @@
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
</Link> </Link>
<FxCompile>
<ShaderModel>5.0</ShaderModel>
</FxCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile> <ClCompile>
@ -106,6 +112,9 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
</Link> </Link>
<FxCompile>
<ShaderModel>5.0</ShaderModel>
</FxCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile> <ClCompile>
@ -121,6 +130,9 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
</Link> </Link>
<FxCompile>
<ShaderModel>5.0</ShaderModel>
</FxCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Camera.cpp" /> <ClCompile Include="Camera.cpp" />

View file

@ -98,11 +98,27 @@ void Game::LoadShaders()
void Game::LoadLighting() void Game::LoadLighting()
{ {
directionalLight1 = {}; ambient = XMFLOAT3(0.05f, 0.05f, 0.20f);
Light directionalLight0 = {};
directionalLight0.Type = LIGHT_TYPE_DIRECTIONAL;
directionalLight0.Direction = XMFLOAT3(1, 0, 0);
directionalLight0.Color = XMFLOAT3(1, 0, 0);
directionalLight0.Intensity = 1;
Light directionalLight1 = {};
directionalLight1.Type = LIGHT_TYPE_DIRECTIONAL; directionalLight1.Type = LIGHT_TYPE_DIRECTIONAL;
directionalLight1.Direction = XMFLOAT3(1, 0, 0); directionalLight1.Direction = XMFLOAT3(0, -1, 0);
directionalLight1.Color = XMFLOAT3(1.0f, 0, 0); directionalLight1.Color = XMFLOAT3(0, 1, 0);
directionalLight1.Intensity = 1.0f; directionalLight1.Intensity = 1;
Light directionalLight2 = {};
directionalLight2.Type = LIGHT_TYPE_DIRECTIONAL;
directionalLight2.Direction = XMFLOAT3(-1, 1, -0.5f);
directionalLight2.Color = XMFLOAT3(0, 0, 1);
directionalLight2.Intensity = 1;
lights = {
directionalLight0,
directionalLight1,
directionalLight2,
};
} }
// -------------------------------------------------------- // --------------------------------------------------------
@ -190,7 +206,6 @@ void Game::Draw(float deltaTime, float totalTime)
{ {
// Background color (Cornflower Blue in this case) for clearing // Background color (Cornflower Blue in this case) for clearing
static const float color[4] = { 0.4f, 0.6f, 0.75f, 0.0f }; static const float color[4] = { 0.4f, 0.6f, 0.75f, 0.0f };
static const DirectX::XMFLOAT3 ambient = XMFLOAT3(0.1f, 0.1f, 0.25f);
// Clear the render target and depth buffer (erases what's on the screen) // Clear the render target and depth buffer (erases what's on the screen)
// - Do this ONCE PER FRAME // - Do this ONCE PER FRAME
@ -216,7 +231,7 @@ void Game::Draw(float deltaTime, float totalTime)
ps->SetFloat("roughness", entity->GetMaterial()->GetRoughness()); ps->SetFloat("roughness", entity->GetMaterial()->GetRoughness());
ps->SetFloat3("tint", entity->GetMaterial()->GetTint()); ps->SetFloat3("tint", entity->GetMaterial()->GetTint());
ps->SetFloat3("ambient", ambient); ps->SetFloat3("ambient", ambient);
ps->SetData("directionalLight1", &directionalLight1, sizeof(Light)); ps->SetData("lights", &lights[0], sizeof(Light) * (int)lights.size());
ps->CopyAllBufferData(); ps->CopyAllBufferData();
entity->GetMaterial()->GetVertexShader()->SetShader(); entity->GetMaterial()->GetVertexShader()->SetShader();

3
Game.h
View file

@ -55,7 +55,8 @@ private:
// A6 Materials // A6 Materials
std::vector<std::shared_ptr<Material>> materials; std::vector<std::shared_ptr<Material>> materials;
// A7 Lights // A7 Lights
Light directionalLight1; std::vector<Light> lights;
DirectX::XMFLOAT3 ambient;
Microsoft::WRL::ComPtr<ID3D11Buffer> constantBufferVS; Microsoft::WRL::ComPtr<ID3D11Buffer> constantBufferVS;
}; };

View file

@ -2,13 +2,16 @@
#include "Helpers.hlsli" #include "Helpers.hlsli"
#include "Lights.hlsli" #include "Lights.hlsli"
// temporary
#define LIGHT_COUNT 3
cbuffer ExternalData : register(b0) cbuffer ExternalData : register(b0)
{ {
float3 cameraPosition; float3 cameraPosition;
float roughness; float roughness;
float3 ambient; float3 ambient;
float3 tint; float3 tint;
Light directionalLight1; Light lights[LIGHT_COUNT];
} }
float calculateSpecular(float3 normal, float3 direction, float3 worldPosition, float3 cameraPosition, float roughness) float calculateSpecular(float3 normal, float3 direction, float3 worldPosition, float3 cameraPosition, float roughness)
@ -33,9 +36,16 @@ float4 main(VertexToPixel input) : SV_TARGET
{ {
input.normal = normalize(input.normal); input.normal = normalize(input.normal);
float3 light = calculateDirectionalLight(directionalLight1, input.normal, input.worldPosition, cameraPosition, roughness, tint); float3 light = ambient * tint;
float3 ambientTint = ambient * tint; for (int i = 0; i < LIGHT_COUNT; i++)
float3 final = light + ambientTint; {
switch (lights[i].Type)
{
case LIGHT_TYPE_DIRECTIONAL:
light += calculateDirectionalLight(lights[i], input.normal, input.worldPosition, cameraPosition, roughness, tint);
break;
}
}
return float4(final, 1); return float4(light, 1);
} }