Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #267 from Broken-Gem-Studio/LightingCorrections&Sc…
Browse files Browse the repository at this point in the history
…ripting

Lighting: Corrections, Improvements & Scripting
  • Loading branch information
t3m1X authored Apr 22, 2020
2 parents cb9e010 + ce759f6 commit 16f152f
Show file tree
Hide file tree
Showing 14 changed files with 394 additions and 17 deletions.
11 changes: 8 additions & 3 deletions Broken Engine/Game/Assets/Shaders/Standard.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ in mat3 v_TBN;

//Uniforms
uniform float u_GammaCorrection = 1.0;
uniform vec4 u_AmbientColor = vec4(1.0);

uniform float u_Shininess = 1.5;
uniform int u_UseTextures = 0;
Expand All @@ -90,6 +91,7 @@ struct BrokenLight
vec3 color;

float intensity;
float distanceMultiplier;

vec3 attenuationKLQ;
vec2 InOutCutoff;
Expand Down Expand Up @@ -146,7 +148,8 @@ vec3 CalculatePointlight(BrokenLight light, vec3 normal, vec3 viewDir)
// direction = v_TBN * normalize(direction);

//Attenuation Calculation
float d = length(light.pos - v_FragPos);
float dMult = 1/light.distanceMultiplier;
float d = length(light.pos - v_FragPos) * dMult;
float lightAttenuation = 1.0/(light.attenuationKLQ.x + light.attenuationKLQ.y * d + light.attenuationKLQ.z *(d * d));

//Result
Expand Down Expand Up @@ -236,11 +239,13 @@ void main()

if(u_DrawNormalMapping_Lit == 0 && u_DrawNormalMapping_Lit_Adv == 0)
{
vec3 finalColor = u_AmbientColor.rgb * v_Color.rgb;

//Resulting Color
if(u_UseTextures == 0 || (HasTransparencies == 0 && u_UseTextures == 1 && texture(u_AlbedoTexture, v_TexCoord).a < 0.1))
out_color = vec4(colorResult + v_Color.rgb, alpha);
out_color = vec4(colorResult + finalColor, alpha);
else if(u_UseTextures == 1)
out_color = vec4(colorResult + v_Color.rgb * texture(u_AlbedoTexture, v_TexCoord).rgb, alpha);
out_color = vec4(colorResult + finalColor * texture(u_AlbedoTexture, v_TexCoord).rgb, alpha);

out_color = pow(out_color, vec4(vec3(1.0/u_GammaCorrection), 1.0));
}
Expand Down
9 changes: 7 additions & 2 deletions Broken Engine/Game/Settings/EditorConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"Navigation": false,
"Physics": true,
"Project": true,
"Rendering": false,
"Rendering": true,
"Resources": false,
"Scene": true,
"Settings": false,
Expand Down Expand Up @@ -197,7 +197,12 @@
"staticFriction": 1.0
},
"Renderer3D": {
"GammaCorrection": 1.8650000095367432
"GammaCorrection": 1.8650000095367432,
"SceneAmbientColor": {
"R": 1.0,
"G": 1.0,
"B": 1.0
}
},
"Window": {
"borderless": false,
Expand Down
2 changes: 2 additions & 0 deletions Broken Engine/Source/Broken Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
<ClInclude Include="ScriptingCamera.h" />
<ClInclude Include="ScriptingGameobject.h" />
<ClInclude Include="ScriptingInterface.h" />
<ClInclude Include="ScriptingLighting.h" />
<ClInclude Include="ScriptingNavigation.h" />
<ClInclude Include="ScriptingParticles.h" />
<ClInclude Include="ScriptingPhysics.h" />
Expand Down Expand Up @@ -458,6 +459,7 @@
<ClCompile Include="ScriptingCamera.cpp" />
<ClCompile Include="ScriptingGameobject.cpp" />
<ClCompile Include="ScriptingInterface.cpp" />
<ClCompile Include="ScriptingLighting.cpp" />
<ClCompile Include="ScriptingNavigation.cpp" />
<ClCompile Include="ScriptingParticles.cpp" />
<ClCompile Include="ScriptingPhysics.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions Broken Engine/Source/Broken Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@
<ClInclude Include="TranslatorUtilities.h">
<Filter>Sources\BrokenEngine\Modules\Scripting Functions\SuperTranslator</Filter>
</ClInclude>
<ClInclude Include="ScriptingLighting.h">
<Filter>Sources\BrokenEngine\Modules\Scripting Functions</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MathGeoLib\include\SystemInfo.cpp">
Expand Down Expand Up @@ -1235,6 +1238,9 @@
<ClCompile Include="TranslatorUtilities.cpp">
<Filter>Sources\BrokenEngine\Tools</Filter>
</ClCompile>
<ClCompile Include="ScriptingLighting.cpp">
<Filter>Sources\BrokenEngine\Modules\Scripting Functions</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="MathGeoLib\include\Geometry\KDTree.inl">
Expand Down
28 changes: 23 additions & 5 deletions Broken Engine/Source/ComponentLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ void ComponentLight::SendUniforms(uint shaderID, uint lightIndex)
int attLoc = glGetUniformLocation(shaderID, (light_index_str + ".attenuationKLQ").c_str());
int cutoffLoc = glGetUniformLocation(shaderID, (light_index_str + ".InOutCutoff").c_str());
int LtypeLoc = glGetUniformLocation(shaderID, (light_index_str + ".LightType").c_str());
int distMultiLoc = glGetUniformLocation(shaderID, (light_index_str + ".distanceMultiplier").c_str());

//u_DistanceMultiplier distMultiLoc


if ((!active || m_LightType == LightType::NONE || m_LightType == LightType::MAX_LIGHT_TYPES) && !m_SetToZero)
Expand All @@ -114,6 +117,9 @@ void ComponentLight::SendUniforms(uint shaderID, uint lightIndex)
// --- Passing Light Attenuation
glUniform3f(attLoc, 0.0f, 0.0f, 0.0f);

// --- Passing Light Distance Multiplicator ---
glUniform1f(distMultiLoc, 1.0f);

m_SetToZero = true;
}
else
Expand Down Expand Up @@ -143,6 +149,9 @@ void ComponentLight::SendUniforms(uint shaderID, uint lightIndex)
// --- Passing Light Attenuation
glUniform3f(attLoc, m_AttenuationKLQFactors.x, m_AttenuationKLQFactors.y, m_AttenuationKLQFactors.z);

// --- Passing Light Distance Multiplicator ---
glUniform1f(distMultiLoc, m_DistanceMultiplier);

if(m_SetToZero)
m_SetToZero = false;
}
Expand Down Expand Up @@ -251,9 +260,16 @@ void ComponentLight::CreateInspectorNode()
ImGui::Text("Intensity");
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x + 10.0f);
ImGui::SetNextItemWidth(300.0f);
ImGui::SliderFloat("", &m_Intensity, 0.0f, INFINITY);
ImGui::SliderFloat("", &m_Intensity, 0.0f, 100.0f, "%.3f", 2.0f);
ImGui::NewLine();

// --- Distance Multiplier ---
ImGui::Text("Distance Multiplier");
ImGui::SameLine(); ImGui::SetNextItemWidth(65.0f);
ImGui::DragFloat("##DistMulti", &m_DistanceMultiplier, 0.1f, 0.1f, INFINITY, "%.4f");
ImGui::NewLine();

// --- Type-According Values ---
if (m_LightType == LightType::SPOTLIGHT)
{
// --- Cutoff ---
Expand All @@ -270,11 +286,11 @@ void ComponentLight::CreateInspectorNode()
// --- Attenuation ---
ImGui::Separator(); ImGui::NewLine();
ImGui::Text("Constant Attenuation Value (K):"); ImGui::SameLine(); ImGui::SetNextItemWidth(65.0f);
ImGui::DragFloat("##AttK", &m_AttenuationKLQFactors.x, 0.001f, 0.000f, 2.0f);
ImGui::DragFloat("##AttK", &m_AttenuationKLQFactors.x, 0.001f, 0.000f);
ImGui::Text("Linear Attenuation Value (L):"); ImGui::SameLine(); ImGui::SetNextItemWidth(65.0f);
ImGui::DragFloat("##AttL", &m_AttenuationKLQFactors.y, 0.001f, 0.000f, 2.0f);
ImGui::DragFloat("##AttL", &m_AttenuationKLQFactors.y, 0.001f, 0.000f);
ImGui::Text("Quadratic Attenuation Value (Q):"); ImGui::SameLine(); ImGui::SetNextItemWidth(65.0f);
ImGui::DragFloat("##AttQ", &m_AttenuationKLQFactors.z, 0.00001f, 0.000000f, 2.0f, "%.5f");
ImGui::DragFloat("##AttQ", &m_AttenuationKLQFactors.z, 0.00001f, 0.000000f, 10.0f, "%.5f");

if (ImGui::Button("Default", { 57, 18 }))
m_AttenuationKLQFactors = float3(1.0f, 0.09f, 0.032f);
Expand Down Expand Up @@ -309,7 +325,7 @@ json ComponentLight::Save() const

node["Intensity"] = std::to_string(m_Intensity);
node["LightType"] = std::to_string((int)m_LightType);

node["DistanceMultiplier"] = std::to_string(m_DistanceMultiplier);

return node;
}
Expand All @@ -336,6 +352,7 @@ void ComponentLight::Load(json& node)
std::string str_outCut = node["OuterCutoff"].is_null() ? "45" : node["OuterCutoff"];

std::string str_intensity = node["Intensity"].is_null() ? "0.5" : node["Intensity"];
std::string str_distMult = node["DistanceMultiplier"].is_null() ? "1.0" : node["DistanceMultiplier"];
std::string str_LType = node["LightType"].is_null() ? "1" : node["LightType"];

// --- Pass Strings to the needed Data Type
Expand All @@ -347,4 +364,5 @@ void ComponentLight::Load(json& node)

m_Intensity = std::stof(str_intensity);
m_LightType = (LightType)(std::stoi(str_LType));
m_DistanceMultiplier = std::stof(str_distMult);
}
15 changes: 9 additions & 6 deletions Broken Engine/Source/ComponentLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ class BROKEN_API ComponentLight : public Component
public:

// --- Getters --
inline const float3 GetLightDirection() const { return m_Direction; }
inline const float3 GetLightColor() const { return m_Color; }
inline const float3 GetLightAttenuationKLQ() const { return m_AttenuationKLQFactors; }
inline const float2 GetLightInOutCutoff() const { return m_InOutCutoffDegrees; }
inline const float GetLightIntensity() const { return m_Intensity; }
inline const float3 GetLightDirection() const { return m_Direction; }
inline const float3 GetLightColor() const { return m_Color; }
inline const float3 GetLightAttenuationKLQ() const { return m_AttenuationKLQFactors; }
inline const float2 GetLightInOutCutoff() const { return m_InOutCutoffDegrees; }
inline const float GetLightIntensity() const { return m_Intensity; }
inline const float GetLightDistanceMultiplier() const { return m_DistanceMultiplier; }

inline const LightType GetLightType() const { return m_LightType; }
inline const LightType GetLightType() const { return m_LightType; }

// -- Setters ---
void SetLightDirection(float3 dir) { m_Direction = dir; }
Expand All @@ -55,6 +56,7 @@ class BROKEN_API ComponentLight : public Component
void SetLightInOutCutoff(float innerCutoff, float outerCutoff) { m_InOutCutoffDegrees = float2(innerCutoff, outerCutoff); }

void SetLightIntensity(float intensity) { m_Intensity = intensity; }
void SetLightDistanceMultiplier(float distMulti) { m_DistanceMultiplier = distMulti; }

void SetLightType(LightType type) { if (type != LightType::NONE && (uint)type < (uint)LightType::MAX_LIGHT_TYPES) m_LightType = type; }

Expand All @@ -72,6 +74,7 @@ class BROKEN_API ComponentLight : public Component
float2 m_InOutCutoffDegrees = float2(12.5f, 45.0f);

float m_Intensity = 0.5f;
float m_DistanceMultiplier = 1.0f;

// --- Others ---
LightType m_LightType = LightType::NONE;
Expand Down
13 changes: 12 additions & 1 deletion Broken Engine/Source/ModuleRenderer3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,22 @@ bool ModuleRenderer3D::CleanUp()
void ModuleRenderer3D::LoadStatus(const json& file)
{
m_GammaCorrection = file["Renderer3D"]["GammaCorrection"].is_null() ? 1.0f : file["Renderer3D"]["GammaCorrection"].get<float>();

float ambR = file["Renderer3D"]["SceneAmbientColor"]["R"].is_null() ? 1.0f : file["Renderer3D"]["SceneAmbientColor"]["R"].get<float>();
float ambG = file["Renderer3D"]["SceneAmbientColor"]["G"].is_null() ? 1.0f : file["Renderer3D"]["SceneAmbientColor"]["G"].get<float>();
float ambB = file["Renderer3D"]["SceneAmbientColor"]["B"].is_null() ? 1.0f : file["Renderer3D"]["SceneAmbientColor"]["B"].get<float>();
m_AmbientColor = float3(ambR, ambG, ambB);
}

const json& ModuleRenderer3D::SaveStatus() const
{
static json m_config;

m_config["GammaCorrection"] = m_GammaCorrection;
m_config["SceneAmbientColor"]["R"] = m_AmbientColor.x;
m_config["SceneAmbientColor"]["G"] = m_AmbientColor.y;
m_config["SceneAmbientColor"]["B"] = m_AmbientColor.z;

return m_config;
}

Expand Down Expand Up @@ -809,8 +819,9 @@ void ModuleRenderer3D::DrawRenderMesh(std::vector<RenderMesh> meshInstances)
glUniform1i(glGetUniformLocation(shader, "u_DrawNormalMapping_Lit"), (int)m_Draw_normalMapping_Lit);
glUniform1i(glGetUniformLocation(shader, "u_DrawNormalMapping_Lit_Adv"), (int)m_Draw_normalMapping_Lit_Adv);

// --- Gamma Correction Value ---
// --- Gamma Correction & Ambient Color Values ---
glUniform1f(glGetUniformLocation(shader, "u_GammaCorrection"), m_GammaCorrection);
glUniform4f(glGetUniformLocation(shader, "u_AmbientColor"), m_AmbientColor.x, m_AmbientColor.y, m_AmbientColor.z, 1.0f);

// --- Set Textures usage to 0 ---
//glUniform1i(glGetUniformLocation(shader, "u_HasDiffuseTexture"), 0);
Expand Down
3 changes: 3 additions & 0 deletions Broken Engine/Source/ModuleRenderer3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ class BROKEN_API ModuleRenderer3D : public Module
void SetActiveCamera(ComponentCamera* camera);
void SetCullingCamera(ComponentCamera* camera);
void SetGammaCorrection(float gammaCorr) { m_GammaCorrection = gammaCorr; }
void SetSceneAmbientColor(float3 color) { m_AmbientColor = color; }

// --- Getters ---
bool GetVSync() const { return vsync; }
const float GetGammaCorrection() const { return m_GammaCorrection; }
const float3 GetSceneAmbientColor() const { return m_AmbientColor; }

private:

Expand Down Expand Up @@ -209,6 +211,7 @@ class BROKEN_API ModuleRenderer3D : public Module
//Lights vector
std::vector<ComponentLight*> m_LightsVec;
float m_GammaCorrection = 2.0f;
float3 m_AmbientColor = float3::one;

uint fbo = 0;
uint cubemapTexID = 0;
Expand Down
26 changes: 26 additions & 0 deletions Broken Engine/Source/ModuleScripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "ScriptingInterface.h"
#include "ScriptingScenes.h"
#include "ScriptingNavigation.h"
#include "ScriptingLighting.h"
#include "ScriptVar.h"
#include <iterator>

Expand Down Expand Up @@ -138,6 +139,10 @@ bool ModuleScripting::JustCompile(std::string absolute_path) {
.addConstructor<void(*) (void)>()
.endClass()

.beginClass <ScriptingLighting>("Lighting")
.addConstructor<void(*) (void)>()
.endClass()

.beginClass <ScriptingAudio>("Audio")
.addConstructor<void(*) (void)>()
.endClass()
Expand Down Expand Up @@ -323,6 +328,27 @@ void ModuleScripting::CompileScriptTableClass(ScriptInstance* script)
.addFunction("SetRandomParticlesScale", &ScriptingParticles::SetRandomParticleScale)
.endClass()

// ----------------------------------------------------------------------------------
// LIGHTING
// ----------------------------------------------------------------------------------
.beginClass <ScriptingLighting>("Lighting")
.addConstructor<void(*) (void)>()

.addFunction("GetLightColor", &ScriptingLighting::GetColor)
.addFunction("GetLightAttenuation", &ScriptingLighting::GetAttenuation)
.addFunction("GetLightCutoff", &ScriptingLighting::GetCutoff)
.addFunction("GetLightType", &ScriptingLighting::GetType)
.addFunction("GetLightIntensity", &ScriptingLighting::GetIntensity)
.addFunction("GetDistMultiplier", &ScriptingLighting::GetDistanceMultiplier)

.addFunction("SetLightIntensity", &ScriptingLighting::SetIntensity)
.addFunction("SetLightType", &ScriptingLighting::SetType)
.addFunction("SetLightColor", &ScriptingLighting::SetColor)
.addFunction("SetLightAttenuation", &ScriptingLighting::SetAttenuation)
.addFunction("SetLightCutoff", &ScriptingLighting::SetCutoff)
.addFunction("SetDistMultiplier", &ScriptingLighting::SetDistanceMultiplier)
.endClass()

// ----------------------------------------------------------------------------------
// AUDIO
// ----------------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions Broken Engine/Source/PanelRendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ PanelRendering::~PanelRendering()
bool PanelRendering::Draw()
{
m_GammaCorretionValue = EngineApp->renderer3D->GetGammaCorrection();
m_AmbientColorValue = EngineApp->renderer3D->GetSceneAmbientColor();

ImGui::SetCurrentContext(EngineApp->gui->getImgUICtx());

Expand All @@ -33,9 +34,15 @@ bool PanelRendering::Draw()
ImGui::SetNextItemWidth(200.0f);
ImGui::SliderFloat("##GammaCorrection", &m_GammaCorretionValue, 0.1f, 5.0f);
ImGui::NewLine();

ImGui::Text("Ambient Color");
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x + 10.0f);
ImGui::ColorEdit4("##AmbientColor", (float*)&m_AmbientColorValue, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_AlphaBar);
ImGui::NewLine();
}
ImGui::End();

EngineApp->renderer3D->SetGammaCorrection(m_GammaCorretionValue);
EngineApp->renderer3D->SetSceneAmbientColor(m_AmbientColorValue);
return false;
}
2 changes: 2 additions & 0 deletions Broken Engine/Source/PanelRendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __PANEL_RENDERING_H__

#include "Panel.h"
#include "Math.h"

class PanelRendering : public Panel
{
Expand All @@ -15,6 +16,7 @@ class PanelRendering : public Panel
private:

float m_GammaCorretionValue = 1.0f;
float3 m_AmbientColorValue = float3::one;
};

#endif
2 changes: 2 additions & 0 deletions Broken Engine/Source/ResourceShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ void ResourceShader::GetAllUniforms(std::vector<Uniform*>& uniforms)
|| strcmp(name, "u_DrawNormalMapping_Lit_Adv") == 0
|| strcmp(name, "u_LightsNumber") == 0
|| strcmp(name, "u_GammaCorrection") == 0
|| strcmp(name, "u_AmbientColor") == 0
|| strcmp(name, "HasTransparencies") == 0
|| std::string(name).find("u_BkLights") != std::string::npos
|| strcmp(name, "time") == 0)
continue;
Expand Down
Loading

0 comments on commit 16f152f

Please sign in to comment.