fix the lights upload error in forward render
This commit is contained in:
@ -120,10 +120,10 @@ uniform DirectionalLight u_DirectionalLights;
|
|||||||
uniform vec3 u_CameraPosition;
|
uniform vec3 u_CameraPosition;
|
||||||
|
|
||||||
uniform int u_PointLightCount;
|
uniform int u_PointLightCount;
|
||||||
uniform PointLight u_PointLights;
|
uniform PointLight u_PointLights[8];
|
||||||
|
|
||||||
uniform int u_SpotLightCount;
|
uniform int u_SpotLightCount;
|
||||||
uniform SpotLight u_SpotLights;
|
uniform SpotLight u_SpotLights[8];
|
||||||
|
|
||||||
|
|
||||||
// PBR
|
// PBR
|
||||||
@ -455,11 +455,19 @@ void main()
|
|||||||
vec3 lightContribution = u_DirectionalLights.Intensity > 0.0 ? Lighting(F0) * shadowFactor : vec3(0.0);
|
vec3 lightContribution = u_DirectionalLights.Intensity > 0.0 ? Lighting(F0) * shadowFactor : vec3(0.0);
|
||||||
|
|
||||||
if(u_PointLightCount > 0)
|
if(u_PointLightCount > 0)
|
||||||
lightContribution += ComputePointLight(u_PointLights, F0, m_Params, vs_Input.WorldPosition);
|
{
|
||||||
|
for( int i = 0; i < u_PointLightCount; i ++) {
|
||||||
|
lightContribution += ComputePointLight(u_PointLights[i], F0, m_Params, vs_Input.WorldPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(u_SpotLightCount > 0)
|
if(u_SpotLightCount > 0)
|
||||||
lightContribution += ComputeSpotLight(u_SpotLights, F0, m_Params, vs_Input.WorldPosition);
|
{
|
||||||
|
for(int i = 0; i < u_SpotLightCount; i ++)
|
||||||
|
{
|
||||||
|
lightContribution += ComputeSpotLight(u_SpotLights[i], F0, m_Params, vs_Input.WorldPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
vec3 iblContribution = IBL(F0, Lr) * u_IBLContribution;
|
vec3 iblContribution = IBL(F0, Lr) * u_IBLContribution;
|
||||||
|
|
||||||
|
|||||||
@ -106,10 +106,10 @@ layout(location = 1) out vec4 o_BloomColor;
|
|||||||
uniform DirectionalLight u_DirectionalLights;
|
uniform DirectionalLight u_DirectionalLights;
|
||||||
|
|
||||||
uniform int u_PointLightCount;
|
uniform int u_PointLightCount;
|
||||||
uniform PointLight u_PointLights;
|
uniform PointLight u_PointLights[8];
|
||||||
|
|
||||||
uniform int u_SpotLightCount;
|
uniform int u_SpotLightCount;
|
||||||
uniform SpotLight u_SpotLights;
|
uniform SpotLight u_SpotLights[8];
|
||||||
|
|
||||||
|
|
||||||
uniform vec3 u_CameraPosition;
|
uniform vec3 u_CameraPosition;
|
||||||
@ -441,11 +441,21 @@ void main()
|
|||||||
// directional light with with shadow
|
// directional light with with shadow
|
||||||
vec3 lightContribution = u_DirectionalLights.Intensity > 0.0 ? Lighting(F0) * shadowFactor : vec3(0.0);
|
vec3 lightContribution = u_DirectionalLights.Intensity > 0.0 ? Lighting(F0) * shadowFactor : vec3(0.0);
|
||||||
|
|
||||||
|
|
||||||
if(u_PointLightCount > 0)
|
if(u_PointLightCount > 0)
|
||||||
lightContribution += ComputePointLight(u_PointLights, F0, m_Params, vs_Input.WorldPosition);
|
{
|
||||||
|
for( int i = 0; i < u_PointLightCount; i ++) {
|
||||||
|
lightContribution += ComputePointLight(u_PointLights[i], F0, m_Params, vs_Input.WorldPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(u_SpotLightCount > 0)
|
if(u_SpotLightCount > 0)
|
||||||
lightContribution += ComputeSpotLight(u_SpotLights, F0, m_Params, vs_Input.WorldPosition);
|
{
|
||||||
|
for(int i = 0; i < u_SpotLightCount; i ++)
|
||||||
|
{
|
||||||
|
lightContribution += ComputeSpotLight(u_SpotLights[i], F0, m_Params, vs_Input.WorldPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
vec3 iblContribution = IBL(F0, Lr) * u_IBLContribution;
|
vec3 iblContribution = IBL(F0, Lr) * u_IBLContribution;
|
||||||
|
|
||||||
|
|||||||
@ -252,6 +252,7 @@ namespace Prism
|
|||||||
if (!depthTest)
|
if (!depthTest)
|
||||||
{
|
{
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
glDisable(GL_BLEND);
|
||||||
}
|
}
|
||||||
|
|
||||||
GLenum glPrimitiveType = 0;
|
GLenum glPrimitiveType = 0;
|
||||||
@ -277,6 +278,7 @@ namespace Prism
|
|||||||
if (!depthTest)
|
if (!depthTest)
|
||||||
{
|
{
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glEnable(GL_BLEND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -583,7 +583,7 @@ namespace Prism
|
|||||||
{
|
{
|
||||||
const auto& directionalLights = s_Data.SceneData.SceneLightEnvironment.DirectionalLights;
|
const auto& directionalLights = s_Data.SceneData.SceneLightEnvironment.DirectionalLights;
|
||||||
auto cmd = Renderer::GetCommandBuffer();
|
auto cmd = Renderer::GetCommandBuffer();
|
||||||
if (!s_Data.ShadowEnabled || directionalLights[0].Intensity == 0.0f || !directionalLights[0].CastShadows)
|
if (!s_Data.ShadowEnabled || directionalLights.Intensity == 0.0f || !directionalLights.CastShadows)
|
||||||
{
|
{
|
||||||
// Clear shadow maps
|
// Clear shadow maps
|
||||||
Renderer::BeginRenderPass(s_Data.ShadowMapRenderPass);
|
Renderer::BeginRenderPass(s_Data.ShadowMapRenderPass);
|
||||||
@ -592,7 +592,7 @@ namespace Prism
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this will not be hardcode
|
// TODO: this will not be hardcode
|
||||||
const glm::vec3 lightDir = glm::normalize(directionalLights[0].Direction); // 光线方向(从光源指向场景)
|
const glm::vec3 lightDir = glm::normalize(directionalLights.Direction); // 光线方向(从光源指向场景)
|
||||||
const glm::vec3 lightPos = lightDir * 100.0f;
|
const glm::vec3 lightPos = lightDir * 100.0f;
|
||||||
const glm::mat4 lightView = glm::lookAt(lightPos, glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
const glm::mat4 lightView = glm::lookAt(lightPos, glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
|
||||||
@ -685,12 +685,39 @@ namespace Prism
|
|||||||
|
|
||||||
// Set lights (TODO: move to light environment and don't do per mesh)
|
// Set lights (TODO: move to light environment and don't do per mesh)
|
||||||
|
|
||||||
baseMaterial->Set("u_DirectionalLights", s_Data.SceneData.SceneLightEnvironment.DirectionalLights[0]);
|
const auto& lights = s_Data.SceneData.SceneLightEnvironment;
|
||||||
baseMaterial->Set("u_PointLightCount", s_Data.SceneData.SceneLightEnvironment.PointLightCount);
|
|
||||||
baseMaterial->Set("u_PointLights", s_Data.SceneData.SceneLightEnvironment.PointLights[0]);
|
baseMaterial->Set("u_DirectionalLights", lights.DirectionalLights);
|
||||||
|
baseMaterial->Set("u_PointLightCount", lights.PointLightCount);
|
||||||
|
|
||||||
|
auto shader = baseMaterial->GetShader();
|
||||||
|
|
||||||
|
for (int i = 0; i < lights.PointLightCount; i++)
|
||||||
|
{
|
||||||
|
std::string base = "u_PointLights[" + std::to_string(i) + "]";
|
||||||
|
shader->SetFloat3(base + ".Position", lights.PointLights[i].Position);
|
||||||
|
shader->SetFloat3(base + ".Radiance", lights.PointLights[i].Radiance);
|
||||||
|
shader->SetFloat(base + ".Intensity", lights.PointLights[i].Intensity);
|
||||||
|
shader->SetFloat(base + ".Range", lights.PointLights[i].Radius);
|
||||||
|
shader->SetBool(base + ".CastShadows", lights.PointLights[i].CastShadows);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
baseMaterial->Set("u_SpotLightCount", lights.SpotLightCount);
|
||||||
|
for (int i = 0; i < lights.SpotLightCount; i++)
|
||||||
|
{
|
||||||
|
std::string base = "u_SpotLights[" + std::to_string(i) + "]";
|
||||||
|
shader->SetFloat3(base + ".Position", lights.SpotLights[i].Position);
|
||||||
|
shader->SetFloat3(base + ".Direction", lights.SpotLights[i].Direction);
|
||||||
|
shader->SetFloat3(base + ".Radiance", lights.SpotLights[i].Radiance);
|
||||||
|
shader->SetFloat(base + ".Intensity", lights.SpotLights[i].Intensity);
|
||||||
|
shader->SetFloat(base + ".Range", lights.SpotLights[i].Range);
|
||||||
|
shader->SetFloat(base + ".InnerConeCos", lights.SpotLights[i].InnerConeCos);
|
||||||
|
shader->SetFloat(base + ".OuterConeCos", lights.SpotLights[i].OuterConeCos);
|
||||||
|
shader->SetBool(base + ".CastShadows", lights.SpotLights[i].CastShadows);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
baseMaterial->Set("u_SpotLightCount", s_Data.SceneData.SceneLightEnvironment.SpotLightCount);
|
|
||||||
baseMaterial->Set("u_SpotLights", s_Data.SceneData.SceneLightEnvironment.SpotLights[0]);
|
|
||||||
// baseMaterial->Set("u_DirectionalLights", s_Data.SceneData.ActiveLight);
|
// baseMaterial->Set("u_DirectionalLights", s_Data.SceneData.ActiveLight);
|
||||||
|
|
||||||
// shadow
|
// shadow
|
||||||
@ -741,11 +768,37 @@ namespace Prism
|
|||||||
baseMaterial->Set("u_BRDFLUTTexture", s_Data.BRDFLUT);
|
baseMaterial->Set("u_BRDFLUTTexture", s_Data.BRDFLUT);
|
||||||
|
|
||||||
// Set lights (TODO: move to light environment and don't do per mesh)
|
// Set lights (TODO: move to light environment and don't do per mesh)
|
||||||
baseMaterial->Set("u_DirectionalLights", s_Data.SceneData.SceneLightEnvironment.DirectionalLights[0]);
|
|
||||||
baseMaterial->Set("u_PointLightCount", s_Data.SceneData.SceneLightEnvironment.PointLightCount);
|
|
||||||
baseMaterial->Set("u_PointLights", s_Data.SceneData.SceneLightEnvironment.PointLights[0]);
|
const auto& lights = s_Data.SceneData.SceneLightEnvironment;
|
||||||
baseMaterial->Set("u_SpotLightCount", s_Data.SceneData.SceneLightEnvironment.SpotLightCount);
|
baseMaterial->Set("u_DirectionalLights", lights.DirectionalLights);
|
||||||
baseMaterial->Set("u_SpotLights", s_Data.SceneData.SceneLightEnvironment.SpotLights[0]);
|
baseMaterial->Set("u_PointLightCount", lights.PointLightCount);
|
||||||
|
|
||||||
|
|
||||||
|
auto shader = baseMaterial->GetShader();
|
||||||
|
for (int i = 0; i < lights.PointLightCount; i++)
|
||||||
|
{
|
||||||
|
std::string base = "u_PointLights[" + std::to_string(i) + "]";
|
||||||
|
shader->SetFloat3(base + ".Position", lights.PointLights[i].Position);
|
||||||
|
shader->SetFloat3(base + ".Radiance", lights.PointLights[i].Radiance);
|
||||||
|
shader->SetFloat(base + ".Intensity", lights.PointLights[i].Intensity);
|
||||||
|
shader->SetFloat(base + ".Range", lights.PointLights[i].Radius);
|
||||||
|
shader->SetBool(base + ".CastShadows", lights.PointLights[i].CastShadows);
|
||||||
|
}
|
||||||
|
|
||||||
|
baseMaterial->Set("u_SpotLightCount", lights.SpotLightCount);
|
||||||
|
for (int i = 0; i < lights.SpotLightCount; i++)
|
||||||
|
{
|
||||||
|
std::string base = "u_SpotLights[" + std::to_string(i) + "]";
|
||||||
|
shader->SetFloat3(base + ".Position", lights.SpotLights[i].Position);
|
||||||
|
shader->SetFloat3(base + ".Direction", lights.SpotLights[i].Direction);
|
||||||
|
shader->SetFloat3(base + ".Radiance", lights.SpotLights[i].Radiance);
|
||||||
|
shader->SetFloat(base + ".Intensity", lights.SpotLights[i].Intensity);
|
||||||
|
shader->SetFloat(base + ".Range", lights.SpotLights[i].Range);
|
||||||
|
shader->SetFloat(base + ".InnerConeCos", lights.SpotLights[i].InnerConeCos);
|
||||||
|
shader->SetFloat(base + ".OuterConeCos", lights.SpotLights[i].OuterConeCos);
|
||||||
|
shader->SetBool(base + ".CastShadows", lights.SpotLights[i].CastShadows);
|
||||||
|
}
|
||||||
// baseMaterial->Set("u_DirectionalLights", s_Data.SceneData.ActiveLight);
|
// baseMaterial->Set("u_DirectionalLights", s_Data.SceneData.ActiveLight);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -667,12 +667,11 @@ namespace Prism
|
|||||||
// direction Light
|
// direction Light
|
||||||
{
|
{
|
||||||
const auto lights = m_Registry.group<DirectionalLightComponent>(entt::get<TransformComponent>);
|
const auto lights = m_Registry.group<DirectionalLightComponent>(entt::get<TransformComponent>);
|
||||||
uint32_t directionalLightIndex = 0;
|
|
||||||
for (const auto entity : lights)
|
for (const auto entity : lights)
|
||||||
{
|
{
|
||||||
auto [transformComponent, lightComponent] = lights.get<TransformComponent, DirectionalLightComponent>(entity);
|
auto [transformComponent, lightComponent] = lights.get<TransformComponent, DirectionalLightComponent>(entity);
|
||||||
const glm::vec3 direction = glm::normalize(glm::mat3(transformComponent.GetTransform()) * glm::vec3(0.0f, 0.0f,1.0f));
|
const glm::vec3 direction = glm::normalize(glm::mat3(transformComponent.GetTransform()) * glm::vec3(0.0f, 0.0f,1.0f));
|
||||||
m_LightEnvironment.DirectionalLights[directionalLightIndex++] =
|
m_LightEnvironment.DirectionalLights =
|
||||||
{
|
{
|
||||||
direction,
|
direction,
|
||||||
lightComponent.Radiance,
|
lightComponent.Radiance,
|
||||||
@ -685,12 +684,12 @@ namespace Prism
|
|||||||
// Point Light
|
// Point Light
|
||||||
{
|
{
|
||||||
const auto pointLights = m_Registry.group<PointLightComponent>(entt::get<TransformComponent>);
|
const auto pointLights = m_Registry.group<PointLightComponent>(entt::get<TransformComponent>);
|
||||||
uint32_t pointLightIndex = 0;
|
m_LightEnvironment.PointLightCount = 0;
|
||||||
for (const auto entity : pointLights)
|
for (const auto entity : pointLights)
|
||||||
{
|
{
|
||||||
if (pointLightIndex >= 8) break;
|
if (m_LightEnvironment.PointLightCount >= 8) break;
|
||||||
auto [transform, light] = pointLights.get<TransformComponent, PointLightComponent>(entity);
|
auto [transform, light] = pointLights.get<TransformComponent, PointLightComponent>(entity);
|
||||||
m_LightEnvironment.PointLights[pointLightIndex++] = {
|
m_LightEnvironment.PointLights[m_LightEnvironment.PointLightCount++] = {
|
||||||
transform.Translation,
|
transform.Translation,
|
||||||
light.Radiance,
|
light.Radiance,
|
||||||
light.Radius,
|
light.Radius,
|
||||||
@ -698,22 +697,21 @@ namespace Prism
|
|||||||
light.CastShadows,
|
light.CastShadows,
|
||||||
};
|
};
|
||||||
|
|
||||||
m_LightEnvironment.PointLightCount ++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spot Light
|
// Spot Light
|
||||||
{
|
{
|
||||||
const auto spotLights = m_Registry.group<SpotLightComponent>(entt::get<TransformComponent>);
|
const auto spotLights = m_Registry.group<SpotLightComponent>(entt::get<TransformComponent>);
|
||||||
uint32_t pointLightIndex = 0;
|
m_LightEnvironment.SpotLightCount = 0;
|
||||||
for (const auto entity : spotLights)
|
for (const auto entity : spotLights)
|
||||||
{
|
{
|
||||||
if (pointLightIndex >= 8) break;
|
if (m_LightEnvironment.SpotLightCount >= 8) break;
|
||||||
auto [transform, light] = spotLights.get<TransformComponent, SpotLightComponent>(entity);
|
auto [transform, light] = spotLights.get<TransformComponent, SpotLightComponent>(entity);
|
||||||
const glm::vec3 direction = glm::normalize(glm::mat3(transform.GetTransform()) * glm::vec3(0.0f, 0.0f, 1.0f));
|
const glm::vec3 direction = glm::normalize(glm::mat3(transform.GetTransform()) * glm::vec3(0.0f, 0.0f, 1.0f));
|
||||||
const float innerCos = glm::cos(glm::radians(light.InnerConeAngle));
|
const float innerCos = glm::cos(glm::radians(light.InnerConeAngle));
|
||||||
const float outerCos = glm::cos(glm::radians(light.OuterConeAngle));
|
const float outerCos = glm::cos(glm::radians(light.OuterConeAngle));
|
||||||
m_LightEnvironment.SpotLights[pointLightIndex++] = {
|
m_LightEnvironment.SpotLights[m_LightEnvironment.SpotLightCount++] = {
|
||||||
transform.Translation,
|
transform.Translation,
|
||||||
direction,
|
direction,
|
||||||
light.Radiance,
|
light.Radiance,
|
||||||
@ -723,7 +721,6 @@ namespace Prism
|
|||||||
outerCos,
|
outerCos,
|
||||||
light.CastShadows,
|
light.CastShadows,
|
||||||
};
|
};
|
||||||
m_LightEnvironment.SpotLightCount ++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -52,7 +52,7 @@ namespace Prism
|
|||||||
|
|
||||||
struct LightEnvironment
|
struct LightEnvironment
|
||||||
{
|
{
|
||||||
GPUDirectionalLight DirectionalLights[4];
|
GPUDirectionalLight DirectionalLights;
|
||||||
|
|
||||||
GPUPointLight PointLights[8]{};
|
GPUPointLight PointLights[8]{};
|
||||||
int PointLightCount = 0;
|
int PointLightCount = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user