diff --git a/modules/skybrowser/skybrowsermodule.cpp b/modules/skybrowser/skybrowsermodule.cpp index cf8419e880..7bfe126c20 100644 --- a/modules/skybrowser/skybrowsermodule.cpp +++ b/modules/skybrowser/skybrowsermodule.cpp @@ -36,8 +36,7 @@ #include #include #include -#include - +#include // For atan2 #include "skybrowsermodule_lua.inl" @@ -88,16 +87,24 @@ scripting::LuaLibrary SkybrowserModule::luaLibrary() const { res.name = "skybrowser"; res.functions = { { - "test", - &skybrowser::luascriptfunctions::testFunction, + "create", + &skybrowser::luascriptfunctions::createBrowser, {}, "string or list of strings", "Add one or multiple exoplanet systems to the scene, as specified by the " "input. An input string should be the name of the system host star" }, { - "update", - &skybrowser::luascriptfunctions::updateFunction, + "move", + &skybrowser::luascriptfunctions::moveBrowser, + {}, + "string or list of strings", + "Add one or multiple exoplanet systems to the scene, as specified by the " + "input. An input string should be the name of the system host star" + }, + { + "follow", + &skybrowser::luascriptfunctions::followCamera, {}, "string or list of strings", "Add one or multiple exoplanet systems to the scene, as specified by the " @@ -123,6 +130,21 @@ void SkybrowserModule::internalInitialize(const ghoul::Dictionary& dict) { */ } +void SkybrowserModule::WWTfollowCamera() const { + // Get camera view direction + const glm::dvec3 viewDirection = global::navigationHandler->camera()->viewDirectionWorldSpace(); + + // Convert to celestial coordinates + const SkybrowserModule* module = global::moduleEngine->module(); + glm::dvec2 celestCoords = module->convertGalacticToCelestial(viewDirection); + + // Execute javascript on browser + ScreenSpaceBrowser* browser = dynamic_cast(global::renderEngine->screenSpaceRenderable("ScreenSpaceBowser")); + std::string script = "vm.onMessage({event: 'center_on_coordinates', ra : Number(" + std::to_string(celestCoords[0]) + "), dec : Number(" + std::to_string(celestCoords[1]) + "), fov : Number(" + std::to_string(_zoomFactor) + "), instant : false})"; + browser->executeJavascript(script); + +} + glm::dvec2 SkybrowserModule::convertGalacticToCelestial(glm::dvec3 rGal) const { // Used the math from this website: https://gea.esac.esa.int/archive/documentation/GD --> diff --git a/modules/skybrowser/skybrowsermodule.h b/modules/skybrowser/skybrowsermodule.h index 33a5afeab4..8c02482383 100644 --- a/modules/skybrowser/skybrowsermodule.h +++ b/modules/skybrowser/skybrowsermodule.h @@ -47,6 +47,7 @@ public: float zoomFactor() const; glm::dvec2 convertGalacticToCelestial(glm::dvec3 coords) const; + void WWTfollowCamera() const; scripting::LuaLibrary luaLibrary() const override; //std::vector documentations() const override; diff --git a/modules/skybrowser/skybrowsermodule_lua.inl b/modules/skybrowser/skybrowsermodule_lua.inl index b482dd8033..b522dd155d 100644 --- a/modules/skybrowser/skybrowsermodule_lua.inl +++ b/modules/skybrowser/skybrowsermodule_lua.inl @@ -27,43 +27,31 @@ namespace { namespace openspace::skybrowser::luascriptfunctions { - int updateFunction(lua_State* L) { - // Get FOV from argument - ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::updateFunction"); - float fov = ghoul::lua::value(L, 1); + int followCamera(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::followCamera"); - // Get camera position - //const glm::dvec3 cameraPosition = global::navigationHandler->camera()->positionVec3(); - const glm::dvec3 cameraPosition = global::navigationHandler->camera()->viewDirectionWorldSpace(); - - // Convert to celestial coordinates const SkybrowserModule* module = global::moduleEngine->module(); - glm::dvec2 celestCoords = module->convertGalacticToCelestial(glm::dvec3(cameraPosition[0], cameraPosition[1], cameraPosition[2])); - - // Execute javascript on browser - ScreenSpaceBrowser* browser = dynamic_cast(global::renderEngine->screenSpaceRenderable("ScreenSpaceBowser")); - std::string script = "vm.onMessage({event: 'center_on_coordinates', ra : Number(" + std::to_string(celestCoords[0]) + "), dec : Number(" + std::to_string(celestCoords[1]) + "), fov : Number(" + std::to_string(fov) + "), instant : false})"; - browser->executeJavascript(script); + module->WWTfollowCamera(); return 1; } - int testFunction(lua_State* L) { - ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::testFunction"); + int moveBrowser(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::moveBrowser"); + ScreenSpaceBrowser* browser = dynamic_cast(global::renderEngine->screenSpaceRenderable("ScreenSpaceBowser")); + browser->translate(glm::vec3(-0.8, -0.4, 0.0)); + return 1; + } + + int createBrowser(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::createBrowser"); + ghoul::lua::value(L, 1); const SkybrowserModule* module = global::moduleEngine->module(); - glm::dvec3 testvec = glm::dvec3(0.0, 0.0, 0.0); - module->convertGalacticToCelestial(testvec); - - LINFOC(_loggerCat, "hoho"); - LINFOC(_loggerCat, std::to_string(module->zoomFactor())); //std::string _url = "https://wallpaperaccess.com/full/3010132.jpg"; // 'https://cdn.wallpapersafari.com/6/92/0nbCPw.jpg' - /* - // get url from user - const std::string _url = ghoul::lua::value(L, 1); - */ + using namespace std::string_literals; ghoul::Dictionary node; @@ -77,8 +65,6 @@ namespace openspace::skybrowser::luascriptfunctions { "openspace.addScreenSpaceRenderable(" + ghoul::formatLua(node) + ")", scripting::ScriptEngine::RemoteScripting::Yes ); - - return 1; } diff --git a/modules/webbrowser/include/screenspacebrowser.h b/modules/webbrowser/include/screenspacebrowser.h index 0f9f9adcd6..514d989043 100644 --- a/modules/webbrowser/include/screenspacebrowser.h +++ b/modules/webbrowser/include/screenspacebrowser.h @@ -73,6 +73,7 @@ public: bool isReady() const override; void executeJavascript(std::string &script) const; + void translate(glm::vec3 translation); private: class ScreenSpaceRenderHandler : public WebRenderHandler { diff --git a/modules/webbrowser/src/screenspacebrowser.cpp b/modules/webbrowser/src/screenspacebrowser.cpp index 4d861fdcdb..dcdee6a601 100644 --- a/modules/webbrowser/src/screenspacebrowser.cpp +++ b/modules/webbrowser/src/screenspacebrowser.cpp @@ -62,7 +62,13 @@ void ScreenSpaceBrowser::executeJavascript(std::string &script) const { LINFOC(_loggerCat, "Executing javascript " + script); CefRefPtr frame = _browserInstance->getBrowser()->GetMainFrame(); - frame->ExecuteJavaScript(script, frame->GetURL(), 0); + frame->ExecuteJavaScript(script, frame->GetURL(), 0); +} + +void ScreenSpaceBrowser::translate(glm::vec3 translation) { + glm::vec4 homogenousCoords(glm::vec4(translation, 1.0)); + glm::vec3 position = _cartesianPosition; + _cartesianPosition = glm::translate(glm::mat4(1.f), translation) * glm::vec4(position, 1.0f); } void ScreenSpaceBrowser::ScreenSpaceRenderHandler::draw() {}