From 1a0bf768f071d26a04ac20c5f0eb43056b14cc8f Mon Sep 17 00:00:00 2001 From: Bottinator22 <59987380+Bottinator22@users.noreply.github.com> Date: Sat, 28 Dec 2024 20:07:04 -0800 Subject: [PATCH] Update StarRenderer_opengl.cpp --- source/application/StarRenderer_opengl.cpp | 112 ++++++++++++++++++--- 1 file changed, 97 insertions(+), 15 deletions(-) diff --git a/source/application/StarRenderer_opengl.cpp b/source/application/StarRenderer_opengl.cpp index 9d0d717..ceb945d 100644 --- a/source/application/StarRenderer_opengl.cpp +++ b/source/application/StarRenderer_opengl.cpp @@ -308,21 +308,39 @@ void OpenGlRenderer::loadEffectConfig(String const& name, Json const& effectConf throw RendererException::format("Unrecognized effect parameter type '{}'", type); } - effect.parameters[p.first] = effectParameter; - - if (Json def = p.second.get("default", {})) { - if (type == "bool") { - setEffectParameter(p.first, def.toBool()); - } else if (type == "int") { - setEffectParameter(p.first, (int)def.toInt()); - } else if (type == "float") { - setEffectParameter(p.first, def.toFloat()); - } else if (type == "vec2") { - setEffectParameter(p.first, jsonToVec2F(def)); - } else if (type == "vec3") { - setEffectParameter(p.first, jsonToVec3F(def)); - } else if (type == "vec4") { - setEffectParameter(p.first, jsonToVec4F(def)); + if (p.second.getBool("scriptable",false)) { + if (Json def = p.second.get("default", {})) { + if (type == "bool") { + effectParameter.parameterValue = (RenderEffectParameter)def.toBool(); + } else if (type == "int") { + effectParameter.parameterValue = (RenderEffectParameter)(int)def.toInt(); + } else if (type == "float") { + effectParameter.parameterValue = (RenderEffectParameter)def.toFloat(); + } else if (type == "vec2") { + effectParameter.parameterValue = (RenderEffectParameter)jsonToVec2F(def); + } else if (type == "vec3") { + effectParameter.parameterValue = (RenderEffectParameter)jsonToVec3F(def); + } else if (type == "vec4") { + effectParameter.parameterValue = (RenderEffectParameter)jsonToVec4F(def); + } + } + effect.scriptables[p.first] = effectParameter; + } else { + effect.parameters[p.first] = effectParameter; + if (Json def = p.second.get("default", {})) { + if (type == "bool") { + setEffectParameter(p.first, def.toBool()); + } else if (type == "int") { + setEffectParameter(p.first, (int)def.toInt()); + } else if (type == "float") { + setEffectParameter(p.first, def.toFloat()); + } else if (type == "vec2") { + setEffectParameter(p.first, jsonToVec2F(def)); + } else if (type == "vec3") { + setEffectParameter(p.first, jsonToVec3F(def)); + } else if (type == "vec4") { + setEffectParameter(p.first, jsonToVec4F(def)); + } } } } @@ -384,6 +402,50 @@ void OpenGlRenderer::setEffectParameter(String const& parameterName, RenderEffec ptr->parameterValue = value; } +void OpenGlRenderer::setEffectScriptableParameter(String const& effectName, String const& parameterName, RenderEffectParameter const& value) { + auto find = m_effects.find(effectName); + if (find == m_effects.end()) + return; + + Effect& effect = find->second; + + auto ptr = effect.scriptables.ptr(parameterName); + if (!ptr || (ptr->parameterValue && *ptr->parameterValue == value)) + return; + + if (ptr->parameterType != value.typeIndex()) + throw RendererException::format("OpenGlRenderer::setEffectScriptableParameter '{}' parameter type mismatch", parameterName); + + ptr->parameterValue = value; +} + +Maybe OpenGlRenderer::getEffectScriptableParameter(String const& effectName, String const& parameterName) { + auto find = m_effects.find(effectName); + if (find == m_effects.end()) + return {}; + + Effect& effect = find->second; + + auto ptr = effect.scriptables.ptr(parameterName); + if (!ptr) + return {}; + + return ptr->parameterValue; +} +Maybe OpenGlRenderer::getEffectScriptableParameterType(String const& effectName, String const& parameterName) { + auto find = m_effects.find(effectName); + if (find == m_effects.end()) + return {}; + + Effect& effect = find->second; + + auto ptr = effect.scriptables.ptr(parameterName); + if (!ptr) + return {}; + + return ptr->parameterType; +} + void OpenGlRenderer::setEffectTexture(String const& textureName, ImageView const& image) { auto ptr = m_currentEffect->textures.ptr(textureName); if (!ptr) @@ -1063,6 +1125,26 @@ void OpenGlRenderer::setupGlUniforms(Effect& effect, Vec2U screenSize) { } glUniform2f(m_screenSizeUniform, screenSize[0], screenSize[1]); + + for (auto& param : effect.scriptables) { + auto ptr = ¶m.second; + auto mvalue = ptr->parameterValue; + if (mvalue) { + RenderEffectParameter value = mvalue.value(); + if (auto v = value.ptr()) + glUniform1i(ptr->parameterUniform, *v); + else if (auto v = value.ptr()) + glUniform1i(ptr->parameterUniform, *v); + else if (auto v = value.ptr()) + glUniform1f(ptr->parameterUniform, *v); + else if (auto v = value.ptr()) + glUniform2f(ptr->parameterUniform, (*v)[0], (*v)[1]); + else if (auto v = value.ptr()) + glUniform3f(ptr->parameterUniform, (*v)[0], (*v)[1], (*v)[2]); + else if (auto v = value.ptr()) + glUniform4f(ptr->parameterUniform, (*v)[0], (*v)[1], (*v)[2], (*v)[3]); + } + } } RefPtr OpenGlRenderer::getGlFrameBuffer(String const& id) {