Added spot light type 🔦 & light direction now using global rotation

This commit is contained in:
Antoine Pilote
2024-07-28 11:50:14 -04:00
parent d4feaa15ec
commit 516a93a7f0
6 changed files with 873 additions and 757 deletions

View File

@@ -63,6 +63,7 @@ public:
ComponentTableReset(component.Type, Nuake::LightType::Point);
}
ImGui::TableNextColumn();
if (component.Type == Nuake::LightType::Directional)
{
{
@@ -100,6 +101,33 @@ public:
}
}
}
else if (component.Type == Nuake::LightType::Spot)
{
{
ImGui::Text("Cutoff");
ImGui::TableNextColumn();
ImGui::DragFloat("##cutoff", &component.Cutoff, 1.0f, 0.0f, 360.0f);
ImGui::TableNextColumn();
// Clamp inner angle so it doesnt exceed outer angle.
component.Cutoff = glm::min(component.Cutoff, component.OuterCutoff);
ComponentTableReset(component.Cutoff, 12.5f);
ImGui::TableNextColumn();
}
{
ImGui::Text("Outer Cutoff");
ImGui::TableNextColumn();
ImGui::DragFloat("##outercutoff", &component.OuterCutoff, 1.0f, component.Cutoff, 360.0f);
ImGui::TableNextColumn();
ComponentTableReset(component.OuterCutoff, 30.0f);
ImGui::TableNextColumn();
}
}
}
EndComponentTable();
}

View File

@@ -10,6 +10,7 @@
#include "src/Rendering/Shaders/ShaderManager.h"
#include "Engine.h"
#include "src/Core/Core.h"
#include "src/Core/Maths.h"
#include <glm/gtc/type_ptr.hpp>
#include "Buffers/VertexBufferLayout.h"
@@ -261,29 +262,30 @@ namespace Nuake
{
Shader* deferredShader = ShaderManager::GetShader("Resources/Shaders/deferred.shader");
deferredShader->Bind();
m_Lights.push_back({ transform , light });
size_t idx = m_Lights.size();
Vector3 direction = light.GetDirection();
Vector3 pos = transform.GetGlobalPosition();
Quat lightRotation = transform.GetGlobalRotation();
//light.m_Framebuffer->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(17);
int shadowmapAmount = 0;
if (light.Type == Directional)
{
int shadowmapAmount = 0;
deferredShader->SetUniform1i("u_DirectionalLight.Shadow", light.CastShadows);
if (light.CastShadows)
{
for (unsigned int i = 0; i < CSM_AMOUNT; i++)
{
light.m_Framebuffers[i]->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(17 + i);
// Bind shadowmap texture
const uint32_t shadowMapId = shadowmapAmount + i;
deferredShader->SetUniform1i("ShadowMaps[" + std::to_string(shadowMapId) + "]", 17 + i);
deferredShader->SetUniform1i("u_DirectionalLight.ShadowMapsIDs[" + std::to_string(i) + "]", shadowMapId);
deferredShader->SetUniform1f("u_DirectionalLight.CascadeDepth[" + std::to_string(i) + "]", light.mCascadeSplitDepth[i]);
deferredShader->SetUniformMat4f("u_DirectionalLight.LightTransforms[" + std::to_string(i) + "]", light.mViewProjections[i]);
Ref<Texture> shadowMapTexture = light.m_Framebuffers[i]->GetTexture(GL_DEPTH_ATTACHMENT);
deferredShader->SetUniformTex("ShadowMaps[" + std::to_string(shadowMapId) + "]", shadowMapTexture, 17 + i);
const std::string stringIndex = std::to_string(i);
deferredShader->SetUniform1i("u_DirectionalLight.ShadowMapsIDs[" + stringIndex + "]", shadowMapId);
deferredShader->SetUniform1f("u_DirectionalLight.CascadeDepth[" + stringIndex + "]", light.mCascadeSplitDepth[i]);
deferredShader->SetUniformMat4f("u_DirectionalLight.LightTransforms[" + stringIndex + "]", light.mViewProjections[i]);
}
}
@@ -293,17 +295,32 @@ namespace Nuake
shadowmapAmount += CSM_AMOUNT;
}
if (m_Lights.size() == MAX_LIGHT)
else
{
return;
if (m_Lights.size() == MAX_LIGHT)
{
return;
}
m_Lights.push_back({ transform , light });
size_t idx = m_Lights.size();
const std::string uniformAccessor = "Lights[" + std::to_string(idx - 1) + "].";
deferredShader->SetUniform3f(uniformAccessor + "Position", pos.x, pos.y, pos.z);
deferredShader->SetUniform3f(uniformAccessor + "Color", light.Color.r * light.Strength, light.Color.g * light.Strength, light.Color.b * light.Strength);
deferredShader->SetUniform1i(uniformAccessor + "Type", static_cast<int>(light.Type));
if (light.Type == Spot)
{
deferredShader->SetUniform3f(uniformAccessor + "Direction", direction.x, direction.y, direction.z);
deferredShader->SetUniform1f(uniformAccessor + "OuterAngle", glm::cos(Rad(light.OuterCutoff)));
deferredShader->SetUniform1f(uniformAccessor + "InnerAngle", glm::cos(Rad(light.Cutoff)));
}
deferredShader->SetUniform1i("LightCount", static_cast<int>(idx));
}
deferredShader->SetUniform1i("LightCount", (int)idx);
deferredShader->SetUniform3f("Lights[" + std::to_string(idx - 1) + "].Position", pos.x, pos.y, pos.z);
deferredShader->SetUniform3f("Lights[" + std::to_string(idx - 1) + "].Color", light.Color.r * light.Strength, light.Color.g * light.Strength, light.Color.b * light.Strength);
m_LightsUniformBuffer->Bind();
m_LightsUniformBuffer->Bind();
}
void Renderer::DrawLine(Vector3 start, Vector3 end, Color color, Matrix4 transform)

View File

@@ -26,8 +26,6 @@ namespace Nuake
mGBuffer->SetTexture(entityTexture, GL_COLOR_ATTACHMENT3); // Entity ID
mGBuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RED, GL_R16F, GL_FLOAT), GL_COLOR_ATTACHMENT4); // Emissive
mShadingBuffer = CreateScope<FrameBuffer>(true, defaultResolution);
mShadingBuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RGB, GL_RGB16F, GL_FLOAT));
@@ -49,7 +47,6 @@ namespace Nuake
void SceneRenderer::Cleanup()
{
}
void SceneRenderer::BeginRenderScene(const Matrix4& projection, const Matrix4& view, const Vector3& camPos)
@@ -683,6 +680,7 @@ namespace Nuake
RenderCommand::Clear();
RenderCommand::SetClearColor(Color(0, 0, 0, 1));
}
RenderCommand::Enable(RendererEnum::FACE_CULL);
Shader* shadingShader = ShaderManager::GetShader("Resources/Shaders/deferred.shader");
@@ -700,8 +698,14 @@ namespace Nuake
{
auto [transform, light, parent] = view.get<TransformComponent, LightComponent, ParentComponent>(l);
if (light.SyncDirectionWithSky)
if (light.Type == Directional && light.SyncDirectionWithSky)
{
light.Direction = env->ProceduralSkybox->GetSunDirection();
}
else
{
light.Direction = transform.GetGlobalRotation() * Vector3(0, 0, 1);
}
Renderer::RegisterDeferredLight(transform, light);
}

File diff suppressed because it is too large Load Diff

View File

@@ -22,6 +22,8 @@ namespace Nuake
public:
LightType Type = Point;
Vector3 Direction;
float Cutoff = 12.5f;
float OuterCutoff = 20.0f;
Vector3 Color;
bool IsVolumetric = false;

View File

@@ -41,12 +41,16 @@ uniform sampler2D m_Emissive;
uniform sampler2D m_UVOffset;
// Lights
const int MaxLight = 32;
const int MaxLight = 28;
uniform int LightCount = 0;
struct Light {
vec3 Color;
vec3 Position;
int Type; // 0 = Point, 1 = Directional, 2 = Spot
vec3 Direction;
float OuterAngle;
float InnerAngle;
};
struct DirectionalLight
@@ -306,12 +310,24 @@ void main()
for (int i = 0; i < LightCount; i++)
{
Light light = Lights[i];
vec3 L = normalize(Lights[i].Position - worldPos);
float distance = length(Lights[i].Position - worldPos);
float distance = length(light.Position - worldPos);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = Lights[i].Color * attenuation ;
vec3 radiance;
if(Lights[i].Type == 1) // Point
{
radiance = Lights[i].Color * attenuation;
}
else if(Lights[i].Type == 2) // Spot
{
float theta = dot(L, normalize(-Lights[i].Direction));
float epsilon = Lights[i].InnerAngle - Lights[i].OuterAngle;
float intensity = clamp((theta - Lights[i].OuterAngle) / epsilon, 0.0, 1.0);
radiance = Lights[i].Color * intensity;
}
// Cook-Torrance BRDF
vec3 H = normalize(V + L);