From ea2017e11c3550159e8a037cf9a9d08372809f19 Mon Sep 17 00:00:00 2001 From: Joakim Kilby Date: Fri, 20 Feb 2015 16:54:30 +0100 Subject: [PATCH] Introduced fadeIn and fadeOut functionality via scripting. --- include/openspace/rendering/renderengine.h | 6 ++ src/main.cpp | 8 ++- src/rendering/renderengine.cpp | 75 +++++++++++++++++++++- src/scripting/scriptengine.cpp | 11 +++- 4 files changed, 94 insertions(+), 6 deletions(-) diff --git a/include/openspace/rendering/renderengine.h b/include/openspace/rendering/renderengine.h index 2eeec180b5..2e2924e121 100644 --- a/include/openspace/rendering/renderengine.h +++ b/include/openspace/rendering/renderengine.h @@ -90,6 +90,9 @@ public: // This is a temporary method to change the origin of the coordinate system ---abock void changeViewPoint(std::string origin); + //temporaray fade functionality + void startFading(int direction, float fadeDuration); + private: void storePerformanceMeasurements(); @@ -108,6 +111,9 @@ private: void generateGlslConfig(); float _globalOpactity; + float _fadeDuration; + float _currentFadeTime; + int _fadeDirection; bool _visualizeABuffer; ABufferVisualizer* _visualizer; diff --git a/src/main.cpp b/src/main.cpp index eafb155f64..5906ce5dd0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ - /***************************************************************************************** * * * OpenSpace * @@ -240,7 +239,12 @@ void mainLogCallback(const char* msg){ void postFXPass(){ glUniform1i(_postFXTexLoc, 0); - glUniform1f(_postFXOpacityLoc, OsEng.ref().renderEngine()->globalOpacity()); + if (OsEng.isMaster()){ + glUniform1f(_postFXOpacityLoc, 1.f); + } + else{ + glUniform1f(_postFXOpacityLoc, OsEng.ref().renderEngine()->globalOpacity()); + } } void setupPostFX(){ diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index 60a71f9ed5..a561e775fc 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -160,6 +160,37 @@ namespace openspace { return 0; } + /** + * \ingroup LuaScripts + * fadeIn(float): + * start a global fadein over (float) seconds + */ + int fadeIn(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + double t = luaL_checknumber(L, -1); + + OsEng.renderEngine()->startFading(1, t); + return 0; + } + /** + * \ingroup LuaScripts + * fadeIn(float): + * start a global fadeout over (float) seconds + */ + int fadeOut(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + double t = luaL_checknumber(L, -1); + + OsEng.renderEngine()->startFading(-1, t); + return 0; + } + } // namespace luascriptfunctions @@ -176,6 +207,10 @@ namespace openspace { , _visualizeABuffer(false) , _visualizer(nullptr) , _globalOpactity(1.f) + , _fadeDuration(2.f) + , _currentFadeTime(0.f) + , _fadeDirection(0) + { } @@ -328,6 +363,24 @@ namespace openspace { void RenderEngine::postSynchronizationPreDraw() { + //temporary fade funtionality + if (_fadeDirection != 0){ + if (_currentFadeTime > _fadeDuration){ + _fadeDirection = 0; + _globalOpactity = fminf(1.f, fmaxf(0.f, _globalOpactity)); + } + else{ + + if (_fadeDirection < 0){ + _globalOpactity = glm::smoothstep(1.f, 0.f, _currentFadeTime / _fadeDuration); + } + else{ + _globalOpactity = glm::smoothstep(0.f, 1.f, _currentFadeTime / _fadeDuration); + } + _currentFadeTime += static_cast(sgct::Engine::instance()->getAvgDt()); + } + } + if (_mainCamera){ _mainCamera->postSynchronizationPreDraw(); } @@ -616,6 +669,12 @@ namespace openspace { _globalOpactity = opacity; } + void RenderEngine::startFading(int direction, float fadeDuration){ + _fadeDirection = direction; + _fadeDuration = fadeDuration; + _currentFadeTime = 0.f; + } + void RenderEngine::generateGlslConfig() { LDEBUG("Generating GLSLS config, expect shader recompilation"); int xSize = sgct::Engine::instance()->getActiveWindowPtr()->getXFramebufferResolution();; @@ -690,7 +749,21 @@ namespace openspace { &luascriptfunctions::changeToSunViewPoint, "", "" - } + }, + //also temporary @JK + { + "fadeIn", + &luascriptfunctions::fadeIn, + "number", + "" + }, + //also temporary @JK + { + "fadeOut", + &luascriptfunctions::fadeOut, + "number", + "" + }, }, }; } diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index 1d2e18833d..5dab478e48 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -260,15 +260,19 @@ void ScriptEngine::addLibrary(LuaLibrary library) { } bool ScriptEngine::runScript(const std::string& script) { - if (script.empty()) - return false; + if (script.empty()){ + LWARNING("Script was empty"); + return false; + } + int status = luaL_loadstring(_state, script.c_str()); if (status != LUA_OK) { LERROR("Error loading script: '" << lua_tostring(_state, -1) << "'"); return false; } - //LDEBUG("Executing script"); + LDEBUG("Executing script"); + LINFO(script); if (lua_pcall(_state, 0, LUA_MULTRET, 0)) { LERROR("Error executing script: " << lua_tostring(_state, -1)); return false; @@ -608,6 +612,7 @@ bool ScriptEngine::writeDocumentation(const std::string& filename, const std::st void ScriptEngine::serialize(SyncBuffer* syncBuffer){ syncBuffer->encode(_currentSyncedScript); + _currentSyncedScript.clear(); } void ScriptEngine::deserialize(SyncBuffer* syncBuffer){