From 2dc14f237aa9ccf940b06523b1f9a94a296cc7bb Mon Sep 17 00:00:00 2001 From: lucho1 Date: Wed, 22 Apr 2020 15:32:57 +0200 Subject: [PATCH 1/4] Implemented scene ambient color & corrected lighting parameters in editor --- Broken Engine/Game/Assets/Shaders/Standard.glsl | 7 +++++-- Broken Engine/Game/Settings/EditorConfig.json | 9 +++++++-- Broken Engine/Source/ComponentLight.cpp | 2 +- Broken Engine/Source/ModuleRenderer3D.cpp | 13 ++++++++++++- Broken Engine/Source/ModuleRenderer3D.h | 3 +++ Broken Engine/Source/PanelRendering.cpp | 7 +++++++ Broken Engine/Source/PanelRendering.h | 2 ++ Broken Engine/Source/ResourceShader.cpp | 1 + 8 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Broken Engine/Game/Assets/Shaders/Standard.glsl b/Broken Engine/Game/Assets/Shaders/Standard.glsl index 76d74aa84..9739e29d1 100644 --- a/Broken Engine/Game/Assets/Shaders/Standard.glsl +++ b/Broken Engine/Game/Assets/Shaders/Standard.glsl @@ -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; @@ -236,11 +237,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)); } diff --git a/Broken Engine/Game/Settings/EditorConfig.json b/Broken Engine/Game/Settings/EditorConfig.json index 768c3ff5b..b46282d0b 100644 --- a/Broken Engine/Game/Settings/EditorConfig.json +++ b/Broken Engine/Game/Settings/EditorConfig.json @@ -12,7 +12,7 @@ "Navigation": false, "Physics": true, "Project": true, - "Rendering": false, + "Rendering": true, "Resources": false, "Scene": true, "Settings": false, @@ -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, diff --git a/Broken Engine/Source/ComponentLight.cpp b/Broken Engine/Source/ComponentLight.cpp index 7e9876454..e8bdcbf9b 100644 --- a/Broken Engine/Source/ComponentLight.cpp +++ b/Broken Engine/Source/ComponentLight.cpp @@ -251,7 +251,7 @@ 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("#lightIntensity", &m_Intensity, 0.0f, 2.0f, "%.3f"/*, 0.5f*/); ImGui::NewLine(); if (m_LightType == LightType::SPOTLIGHT) diff --git a/Broken Engine/Source/ModuleRenderer3D.cpp b/Broken Engine/Source/ModuleRenderer3D.cpp index 617881681..3eda33437 100644 --- a/Broken Engine/Source/ModuleRenderer3D.cpp +++ b/Broken Engine/Source/ModuleRenderer3D.cpp @@ -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 ambR = file["Renderer3D"]["SceneAmbientColor"]["R"].is_null() ? 1.0f : file["Renderer3D"]["SceneAmbientColor"]["R"].get(); + float ambG = file["Renderer3D"]["SceneAmbientColor"]["G"].is_null() ? 1.0f : file["Renderer3D"]["SceneAmbientColor"]["G"].get(); + float ambB = file["Renderer3D"]["SceneAmbientColor"]["B"].is_null() ? 1.0f : file["Renderer3D"]["SceneAmbientColor"]["B"].get(); + 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; } @@ -809,8 +819,9 @@ void ModuleRenderer3D::DrawRenderMesh(std::vector 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); diff --git a/Broken Engine/Source/ModuleRenderer3D.h b/Broken Engine/Source/ModuleRenderer3D.h index 11e13c442..f43547320 100644 --- a/Broken Engine/Source/ModuleRenderer3D.h +++ b/Broken Engine/Source/ModuleRenderer3D.h @@ -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: @@ -209,6 +211,7 @@ class BROKEN_API ModuleRenderer3D : public Module //Lights vector std::vector m_LightsVec; float m_GammaCorrection = 2.0f; + float3 m_AmbientColor = float3::one; uint fbo = 0; uint cubemapTexID = 0; diff --git a/Broken Engine/Source/PanelRendering.cpp b/Broken Engine/Source/PanelRendering.cpp index f00d6050c..d2af8a938 100644 --- a/Broken Engine/Source/PanelRendering.cpp +++ b/Broken Engine/Source/PanelRendering.cpp @@ -20,6 +20,7 @@ PanelRendering::~PanelRendering() bool PanelRendering::Draw() { m_GammaCorretionValue = EngineApp->renderer3D->GetGammaCorrection(); + m_AmbientColorValue = EngineApp->renderer3D->GetSceneAmbientColor(); ImGui::SetCurrentContext(EngineApp->gui->getImgUICtx()); @@ -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; } diff --git a/Broken Engine/Source/PanelRendering.h b/Broken Engine/Source/PanelRendering.h index 6bc913996..421714910 100644 --- a/Broken Engine/Source/PanelRendering.h +++ b/Broken Engine/Source/PanelRendering.h @@ -2,6 +2,7 @@ #define __PANEL_RENDERING_H__ #include "Panel.h" +#include "Math.h" class PanelRendering : public Panel { @@ -15,6 +16,7 @@ class PanelRendering : public Panel private: float m_GammaCorretionValue = 1.0f; + float3 m_AmbientColorValue = float3::one; }; #endif \ No newline at end of file diff --git a/Broken Engine/Source/ResourceShader.cpp b/Broken Engine/Source/ResourceShader.cpp index a5b02e93c..e2e9442db 100644 --- a/Broken Engine/Source/ResourceShader.cpp +++ b/Broken Engine/Source/ResourceShader.cpp @@ -298,6 +298,7 @@ void ResourceShader::GetAllUniforms(std::vector& uniforms) || strcmp(name, "u_DrawNormalMapping_Lit_Adv") == 0 || strcmp(name, "u_LightsNumber") == 0 || strcmp(name, "u_GammaCorrection") == 0 + || strcmp(name, "u_AmbientColor") == 0 || std::string(name).find("u_BkLights") != std::string::npos || strcmp(name, "time") == 0) continue; From 57fd97ad593c3daf3077ddc15d28f87ee354ad6d Mon Sep 17 00:00:00 2001 From: lucho1 Date: Wed, 22 Apr 2020 17:04:45 +0200 Subject: [PATCH 2/4] Added Lighting Scripting functions & Light Distance Multiplier Also, uncapped some values :D --- .../Game/Assets/Shaders/Standard.glsl | 4 +- Broken Engine/Source/Broken Core.vcxproj | 2 + .../Source/Broken Core.vcxproj.filters | 6 + Broken Engine/Source/ComponentLight.cpp | 24 +- Broken Engine/Source/ComponentLight.h | 15 +- Broken Engine/Source/ModuleScripting.cpp | 26 ++ Broken Engine/Source/ResourceShader.cpp | 1 + Broken Engine/Source/ScriptingLighting.cpp | 245 ++++++++++++++++++ Broken Engine/Source/ScriptingLighting.h | 38 +++ 9 files changed, 350 insertions(+), 11 deletions(-) create mode 100644 Broken Engine/Source/ScriptingLighting.cpp create mode 100644 Broken Engine/Source/ScriptingLighting.h diff --git a/Broken Engine/Game/Assets/Shaders/Standard.glsl b/Broken Engine/Game/Assets/Shaders/Standard.glsl index 9739e29d1..d563dc4fb 100644 --- a/Broken Engine/Game/Assets/Shaders/Standard.glsl +++ b/Broken Engine/Game/Assets/Shaders/Standard.glsl @@ -91,6 +91,7 @@ struct BrokenLight vec3 color; float intensity; + float distanceMultiplier; vec3 attenuationKLQ; vec2 InOutCutoff; @@ -147,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 diff --git a/Broken Engine/Source/Broken Core.vcxproj b/Broken Engine/Source/Broken Core.vcxproj index f068e8cf9..378c9cc61 100644 --- a/Broken Engine/Source/Broken Core.vcxproj +++ b/Broken Engine/Source/Broken Core.vcxproj @@ -286,6 +286,7 @@ + @@ -458,6 +459,7 @@ + diff --git a/Broken Engine/Source/Broken Core.vcxproj.filters b/Broken Engine/Source/Broken Core.vcxproj.filters index 393f94c0a..6856322e7 100644 --- a/Broken Engine/Source/Broken Core.vcxproj.filters +++ b/Broken Engine/Source/Broken Core.vcxproj.filters @@ -723,6 +723,9 @@ Sources\BrokenEngine\Modules\Scripting Functions\SuperTranslator + + Sources\BrokenEngine\Modules\Scripting Functions + @@ -1235,6 +1238,9 @@ Sources\BrokenEngine\Tools + + Sources\BrokenEngine\Modules\Scripting Functions + diff --git a/Broken Engine/Source/ComponentLight.cpp b/Broken Engine/Source/ComponentLight.cpp index e8bdcbf9b..9881a350e 100644 --- a/Broken Engine/Source/ComponentLight.cpp +++ b/Broken Engine/Source/ComponentLight.cpp @@ -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) @@ -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 @@ -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; } @@ -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("#lightIntensity", &m_Intensity, 0.0f, 2.0f, "%.3f"/*, 0.5f*/); + 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 --- @@ -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); diff --git a/Broken Engine/Source/ComponentLight.h b/Broken Engine/Source/ComponentLight.h index 61c7b7af7..a3c71c088 100644 --- a/Broken Engine/Source/ComponentLight.h +++ b/Broken Engine/Source/ComponentLight.h @@ -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; } @@ -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; } @@ -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; diff --git a/Broken Engine/Source/ModuleScripting.cpp b/Broken Engine/Source/ModuleScripting.cpp index 7700201df..19e0a3284 100644 --- a/Broken Engine/Source/ModuleScripting.cpp +++ b/Broken Engine/Source/ModuleScripting.cpp @@ -24,6 +24,7 @@ #include "ScriptingInterface.h" #include "ScriptingScenes.h" #include "ScriptingNavigation.h" +#include "ScriptingLighting.h" #include "ScriptVar.h" #include @@ -138,6 +139,10 @@ bool ModuleScripting::JustCompile(std::string absolute_path) { .addConstructor() .endClass() + .beginClass ("Lighting") + .addConstructor() + .endClass() + .beginClass ("Audio") .addConstructor() .endClass() @@ -323,6 +328,27 @@ void ModuleScripting::CompileScriptTableClass(ScriptInstance* script) .addFunction("SetRandomParticlesScale", &ScriptingParticles::SetRandomParticleScale) .endClass() + // ---------------------------------------------------------------------------------- + // LIGHTING + // ---------------------------------------------------------------------------------- + .beginClass ("Lighting") + .addConstructor() + + .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 // ---------------------------------------------------------------------------------- diff --git a/Broken Engine/Source/ResourceShader.cpp b/Broken Engine/Source/ResourceShader.cpp index e2e9442db..7fffc84f3 100644 --- a/Broken Engine/Source/ResourceShader.cpp +++ b/Broken Engine/Source/ResourceShader.cpp @@ -299,6 +299,7 @@ void ResourceShader::GetAllUniforms(std::vector& uniforms) || 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; diff --git a/Broken Engine/Source/ScriptingLighting.cpp b/Broken Engine/Source/ScriptingLighting.cpp new file mode 100644 index 000000000..1c28d7e79 --- /dev/null +++ b/Broken Engine/Source/ScriptingLighting.cpp @@ -0,0 +1,245 @@ +#include "ScriptingLighting.h" + +// -- Modules -- +#include "Application.h" +#include "ModuleScripting.h" +#include "ModuleSceneManager.h" + +// -- Components - +#include "GameObject.h" +#include "Components.h" +#include "ComponentLight.h" + +#include "ScriptData.h" +#include "ResourceScene.h" + +// -- Utilities -- +//#include "TranslatorUtilities.h" + +using namespace Broken; + +// --- Setters --- +void ScriptingLighting::SetIntensity(float intensity, uint gameobject_UUID) +{ + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + light->SetLightIntensity(intensity); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); +} + +void ScriptingLighting::SetDistanceMultiplier(float distMult, uint gameobject_UUID) +{ + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + light->SetLightDistanceMultiplier(distMult); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); +} + +void ScriptingLighting::SetType(int type, uint gameobject_UUID) +{ + if (type <= -1 || type >= 3) + { + ENGINE_CONSOLE_LOG("Error! Light Type Parameter Invalid!"); + return; + } + + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + light->SetLightType((LightType)type); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); +} + +void ScriptingLighting::SetColor(float r, float g, float b, uint gameobject_UUID) +{ + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + light->SetLightColor(float3(r, g, b)); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); +} + +void ScriptingLighting::SetAttenuation(float K, float L, float Q, uint gameobject_UUID) +{ + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + light->SetLightAttenuationFactors(K, L, Q); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); +} + +void ScriptingLighting::SetCutoff(float innerCutoff, float outerCutoff, uint gameobject_UUID) +{ + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + return light->SetLightInOutCutoff(innerCutoff, outerCutoff); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); +} + +// --- Getters --- +float ScriptingLighting::GetIntensity(uint gameobject_UUID) const +{ + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + return light->GetLightIntensity(); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + + return 0.0; +} + +float ScriptingLighting::GetDistanceMultiplier(uint gameobject_UUID) const +{ + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + return light->GetLightDistanceMultiplier(); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + + return 0.0; +} + +int ScriptingLighting::GetType(uint gameobject_UUID) const +{ + int ret = -1; + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + { + ret = (int)light->GetLightType(); + if(ret <= -1 || ret >= 3) + ENGINE_CONSOLE_LOG("WARNING! Light Type Invalid"); + } + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + + return ret; +} + +luabridge::LuaRef ScriptingLighting::GetColor(uint gameobject_UUID, lua_State* L) const +{ + float3 color = float3(0.0f); + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + color = light->GetLightColor(); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + + luabridge::LuaRef table = luabridge::newTable(L); + table.append(color.x); + table.append(color.y); + table.append(color.z); + + return table; +} + +luabridge::LuaRef ScriptingLighting::GetAttenuation(uint gameobject_UUID, lua_State* L) const +{ + float3 att = float3(0.0f); + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + att = light->GetLightAttenuationKLQ(); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + + luabridge::LuaRef table = luabridge::newTable(L); + table.append(att.x); + table.append(att.y); + table.append(att.z); + + return table; +} + +luabridge::LuaRef ScriptingLighting::GetCutoff(uint gameobject_UUID, lua_State* L) const +{ + float2 cutoff = float2(0.0f); + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); + + if (go) + { + ComponentLight* light = go->GetComponent(); + if (light) + cutoff = light->GetLightInOutCutoff(); + else + ENGINE_CONSOLE_LOG("Light Component is null"); + } + else + ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + + luabridge::LuaRef table = luabridge::newTable(L); + table.append(cutoff.x); + table.append(cutoff.y); + + return table; +} diff --git a/Broken Engine/Source/ScriptingLighting.h b/Broken Engine/Source/ScriptingLighting.h new file mode 100644 index 000000000..4ac6c0d7c --- /dev/null +++ b/Broken Engine/Source/ScriptingLighting.h @@ -0,0 +1,38 @@ +#ifndef __SCRIPTINGLIGHTING_H__ +#define __SCRIPTINGLIGHTING_H__ + +#include "BrokenCore.h" + +class lua_State; + +namespace luabridge +{ + class LuaRef; +} + +BE_BEGIN_NAMESPACE +class BROKEN_API ScriptingLighting +{ +public: + + ScriptingLighting() {} + ~ScriptingLighting() {} + + // --- Setters --- + void SetIntensity(float intensity, uint gameobject_UUID); + void SetDistanceMultiplier(float distMult, uint gameobject_UUID); + void SetType(int type, uint gameobject_UUID); + void SetColor(float r, float g, float b, uint gameobject_UUID); + void SetAttenuation(float K, float L, float Q, uint gameobject_UUID); + void SetCutoff(float innerCutoff, float outerCutoff, uint gameobject_UUID); + + // --- Getters --- + float GetIntensity(uint gameobject_UUID) const; + float GetDistanceMultiplier(uint gameobject_UUID) const; + int GetType(uint gameobject_UUID) const; //NONE = -1, DIR = 0, POINT = 1, SPOT = 2, MAX = 3 + luabridge::LuaRef GetColor(uint gameobject_UUID, lua_State* L) const; + luabridge::LuaRef GetAttenuation(uint gameobject_UUID, lua_State* L) const; + luabridge::LuaRef GetCutoff(uint gameobject_UUID, lua_State* L) const; +}; +BE_END_NAMESPACE +#endif //__SCRIPTINGLIGHTING_H__ \ No newline at end of file From 001c53a2fec6b13fda6cb033b3ce61084d920952 Mon Sep 17 00:00:00 2001 From: lucho1 Date: Wed, 22 Apr 2020 17:24:01 +0200 Subject: [PATCH 3/4] Distance Multiplier light value Save/Load --- Broken Engine/Source/ComponentLight.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Broken Engine/Source/ComponentLight.cpp b/Broken Engine/Source/ComponentLight.cpp index 9881a350e..60f4b5b37 100644 --- a/Broken Engine/Source/ComponentLight.cpp +++ b/Broken Engine/Source/ComponentLight.cpp @@ -325,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; } @@ -352,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 @@ -363,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); } \ No newline at end of file From ce759f6f7d78a5cad598781a8c3600d1db76fa8c Mon Sep 17 00:00:00 2001 From: Sergi PR Date: Wed, 22 Apr 2020 19:38:03 +0200 Subject: [PATCH 4/4] Structural changes to script functions --- Broken Engine/Source/ScriptingLighting.cpp | 64 ++++++++++++---------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/Broken Engine/Source/ScriptingLighting.cpp b/Broken Engine/Source/ScriptingLighting.cpp index 1c28d7e79..d712ac3c0 100644 --- a/Broken Engine/Source/ScriptingLighting.cpp +++ b/Broken Engine/Source/ScriptingLighting.cpp @@ -28,10 +28,10 @@ void ScriptingLighting::SetIntensity(float intensity, uint gameobject_UUID) if (light) light->SetLightIntensity(intensity); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (SetIntensity) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (SetIntensity) Could not find GameObject with UUID %d", gameobject_UUID); } void ScriptingLighting::SetDistanceMultiplier(float distMult, uint gameobject_UUID) @@ -43,17 +43,17 @@ void ScriptingLighting::SetDistanceMultiplier(float distMult, uint gameobject_UU if (light) light->SetLightDistanceMultiplier(distMult); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (SetDistanceMultiplier) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (SetDistanceMultiplier) Could not find GameObject with UUID %d", gameobject_UUID); } void ScriptingLighting::SetType(int type, uint gameobject_UUID) { if (type <= -1 || type >= 3) { - ENGINE_CONSOLE_LOG("Error! Light Type Parameter Invalid!"); + ENGINE_CONSOLE_LOG("![Script]: (SetType) Error! Light Type Parameter Invalid!"); return; } @@ -64,10 +64,10 @@ void ScriptingLighting::SetType(int type, uint gameobject_UUID) if (light) light->SetLightType((LightType)type); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (SetType) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (SetType) Could not find GameObject with UUID %d", gameobject_UUID); } void ScriptingLighting::SetColor(float r, float g, float b, uint gameobject_UUID) @@ -79,10 +79,10 @@ void ScriptingLighting::SetColor(float r, float g, float b, uint gameobject_UUID if (light) light->SetLightColor(float3(r, g, b)); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (SetColor) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (SetColor) Could not find GameObject with UUID %d", gameobject_UUID); } void ScriptingLighting::SetAttenuation(float K, float L, float Q, uint gameobject_UUID) @@ -94,10 +94,10 @@ void ScriptingLighting::SetAttenuation(float K, float L, float Q, uint gameobjec if (light) light->SetLightAttenuationFactors(K, L, Q); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (SetAttenuation) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (SetAttenuation) Could not find GameObject with UUID %d", gameobject_UUID); } void ScriptingLighting::SetCutoff(float innerCutoff, float outerCutoff, uint gameobject_UUID) @@ -109,45 +109,49 @@ void ScriptingLighting::SetCutoff(float innerCutoff, float outerCutoff, uint gam if (light) return light->SetLightInOutCutoff(innerCutoff, outerCutoff); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (SetCutoff) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (SetCutoff) Could not find GameObject with UUID %d", gameobject_UUID); } // --- Getters --- float ScriptingLighting::GetIntensity(uint gameobject_UUID) const { + float ret = 0.0f; + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); if (go) { ComponentLight* light = go->GetComponent(); if (light) - return light->GetLightIntensity(); + ret = light->GetLightIntensity(); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (GetIntensity) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (GetIntensity) Could not find GameObject with UUID %d", gameobject_UUID); - return 0.0; + return ret; } float ScriptingLighting::GetDistanceMultiplier(uint gameobject_UUID) const { + float ret = 0.0f; + GameObject* go = App->scene_manager->currentScene->GetGOWithUID(gameobject_UUID); if (go) { ComponentLight* light = go->GetComponent(); if (light) - return light->GetLightDistanceMultiplier(); + ret = light->GetLightDistanceMultiplier(); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (GetDistanceMultiplier) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (GetDistanceMultiplier) Could not find GameObject with UUID %d", gameobject_UUID); - return 0.0; + return ret; } int ScriptingLighting::GetType(uint gameobject_UUID) const @@ -162,13 +166,13 @@ int ScriptingLighting::GetType(uint gameobject_UUID) const { ret = (int)light->GetLightType(); if(ret <= -1 || ret >= 3) - ENGINE_CONSOLE_LOG("WARNING! Light Type Invalid"); + ENGINE_CONSOLE_LOG("![Script]: (GetType) WARNING! Light Type Invalid"); } else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (GetType) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (GetType) Could not find GameObject with UUID %d", gameobject_UUID); return ret; } @@ -184,10 +188,10 @@ luabridge::LuaRef ScriptingLighting::GetColor(uint gameobject_UUID, lua_State* L if (light) color = light->GetLightColor(); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (GetColor) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (GetColor) Could not find GameObject with UUID %d", gameobject_UUID); luabridge::LuaRef table = luabridge::newTable(L); table.append(color.x); @@ -208,10 +212,10 @@ luabridge::LuaRef ScriptingLighting::GetAttenuation(uint gameobject_UUID, lua_St if (light) att = light->GetLightAttenuationKLQ(); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (GetAttenuation) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (GetAttenuation) Could not find GameObject with UUID %d", gameobject_UUID); luabridge::LuaRef table = luabridge::newTable(L); table.append(att.x); @@ -232,10 +236,10 @@ luabridge::LuaRef ScriptingLighting::GetCutoff(uint gameobject_UUID, lua_State* if (light) cutoff = light->GetLightInOutCutoff(); else - ENGINE_CONSOLE_LOG("Light Component is null"); + ENGINE_CONSOLE_LOG("![Script]: (GetCutoff) Light Component is null"); } else - ENGINE_CONSOLE_LOG("(SCRIPTING) Alert! Could not find GameObject with UUID %d", gameobject_UUID); + ENGINE_CONSOLE_LOG("![Script]: (GetCutoff) Could not find GameObject with UUID %d", gameobject_UUID); luabridge::LuaRef table = luabridge::newTable(L); table.append(cutoff.x);