Move browser specific functionality to screenspacebrowser

This commit is contained in:
Ylva Selling
2021-03-22 09:50:19 +01:00
parent 39b1887e14
commit 51f40172f7
5 changed files with 233 additions and 246 deletions
@@ -1,21 +1,24 @@
#include <modules/skybrowser/include/screenspaceskybrowser.h>
#include <ghoul/opengl/texture.h>
#include <modules/skybrowser/include/screenspaceskytarget.h>
#include <modules/webbrowser/include/webkeyboardhandler.h>
#include <modules/webbrowser/include/browserinstance.h>
#include <modules/webbrowser/include/screenspacebrowser.h>
#include <ghoul/logging/logmanager.h>
#include <openspace/engine/globals.h>
#include <openspace/engine/globalscallbacks.h>
#include <openspace/interaction/navigationhandler.h>
#include <openspace/engine/windowdelegate.h>
#include <glm/gtx/string_cast.hpp>
#include <openspace/interaction/navigationhandler.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/util/camera.h>
#include <openspace/scene/scene.h>
#include <ghoul/misc/dictionaryjsonformatter.h> // formatJson
#include <ghoul/logging/logmanager.h>
#include <ghoul/opengl/texture.h>
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtx/string_cast.hpp>
#include <glm/gtx/rotate_vector.hpp >
#include <glm/gtx/string_cast.hpp>
#include <thread>
#include <chrono> // Milliseconds
namespace {
constexpr const char* _loggerCat = "ScreenSpaceSkyBrowser";
@@ -26,11 +29,21 @@ namespace {
"Browser Dimensions Info",
"Set the dimensions of the SkyTarget according to the SkyBrowser ratio "
};
constexpr const openspace::properties::Property::PropertyInfo ZoomInfo =
{
"Zoom",
"Zoom Info",
"tjobidabidobidabidopp plupp"
};
struct [[codegen::Dictionary(ScreenSpaceSkyBrowser)]] Parameters {
// [[codegen::verbatim(BrowserDimensionInfo.description)]]
std::optional<glm::vec2> browserDimensions;
// [[codegen::verbatim(ZoomInfo.description)]]
std::optional<float> zoom;
};
#include "screenspaceskybrowser_codegen.cpp"
@@ -40,6 +53,8 @@ namespace openspace {
ScreenSpaceSkyBrowser::ScreenSpaceSkyBrowser(const ghoul::Dictionary& dictionary)
: ScreenSpaceBrowser(dictionary)
, _browserDimensions(BrowserDimensionInfo, _dimensions, glm::ivec2(0.f), glm::ivec2(300.f))
, _fieldOfView(ZoomInfo, 50.f, 0.1f, 70.f)
, _camIsSyncedWWT(true)
{
// Handle target dimension property
const Parameters p = codegen::bake<Parameters>(dictionary);
@@ -47,6 +62,17 @@ namespace openspace {
_browserDimensions.onChange([&]() { _browserDimIsDirty = true; });
addProperty(_browserDimensions);
_fieldOfView = p.zoom.value_or(_fieldOfView);
addProperty(_fieldOfView);
_fieldOfView.onChange([&]() {
if (_skyTarget) {
_skyTarget->updateFOV(_fieldOfView);
float scaleWhenFovIs10 = static_cast<float>(10.f / global::windowDelegate->getHorizFieldOfView());
_skyTarget->setScale(std::max(static_cast<float>(_fieldOfView / global::windowDelegate->getHorizFieldOfView()), scaleWhenFovIs10));
}
});
std::string identifier;
if (dictionary.hasValue<std::string>(KeyIdentifier)) {
identifier = dictionary.value<std::string>(KeyIdentifier);
@@ -61,6 +87,25 @@ namespace openspace {
_cartesianPosition.setValue(glm::vec3(_cartesianPosition.value().x, _cartesianPosition.value().y, -2.1f));
}
bool ScreenSpaceSkyBrowser::deinitializeGL() {
// Set flag to false so the thread can exit
_camIsSyncedWWT = false;
if (_threadWWTMessages.joinable()) {
_threadWWTMessages.join();
LINFO("Joined thread");
}
return ScreenSpaceBrowser::deinitializeGL();
}
float ScreenSpaceSkyBrowser::fieldOfView() const {
return _fieldOfView;
}
void ScreenSpaceSkyBrowser::scrollZoom(float scroll) {
float zoom = scroll > 0.0 ? -log(_fieldOfView + 1.1f) : log(_fieldOfView + 1.1f);
_fieldOfView = std::clamp(_fieldOfView + zoom, 0.001f, 70.0f);
}
void ScreenSpaceSkyBrowser::executeJavascript(std::string& script) const {
//LINFOC(_loggerCat, "Executing javascript " + script);
if (_browserInstance && _browserInstance->getBrowser() && _browserInstance->getBrowser()->GetMainFrame()) {
@@ -68,6 +113,42 @@ namespace openspace {
frame->ExecuteJavaScript(script, frame->GetURL(), 0);
}
}
bool ScreenSpaceSkyBrowser::sendMessageToWWT(const ghoul::Dictionary& msg) {
std::string script = "sendMessageToWWT(" + ghoul::formatJson(msg) + ");";
executeJavascript(script);
return true;
}
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForMovingWWTCamera(const glm::dvec2 celestCoords, const float fov, const bool moveInstantly) const {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "center_on_coordinates"s);
msg.setValue("ra", static_cast<double>(celestCoords[0]));
msg.setValue("dec", static_cast<double>(celestCoords[1]));
msg.setValue("fov", static_cast<double>(fov));
msg.setValue("instant", moveInstantly);
return msg;
}
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForLoadingWWTImgColl(const std::string& url) const {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "center_on_coordinates"s);
msg.setValue("url", url);
return msg;
}
ghoul::Dictionary ScreenSpaceSkyBrowser::createMessageForPausingWWTTime() const {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "pause_time"s);
return msg;
}
void ScreenSpaceSkyBrowser::sendMouseEvent(CefStructBase<CefMouseEventTraits> event, int x, int y) const {
//LINFOC(_loggerCat, "Executing javascript " + script);
@@ -80,6 +161,54 @@ namespace openspace {
}
}
void ScreenSpaceSkyBrowser::WWTfollowCamera() {
// Start a thread to enable user interaction while sending the calls to WWT
_threadWWTMessages = std::thread([&] {
while (_camIsSyncedWWT) {
// Get camera view direction and orthogonal coordinate system of camera view direction
glm::vec3 viewDirection = global::navigationHandler->camera()->viewDirectionWorldSpace();
glm::vec3 upDirection = global::navigationHandler->camera()->lookUpVectorWorldSpace();
glm::vec3 sideDirection = glm::cross(upDirection, viewDirection);
glm::vec2 angleOffset = _skyTarget ? _skyTarget->getAnglePosition() : glm::vec2(0);
// Change view if target is moved
glm::vec3 targetDirection = glm::rotate(viewDirection, angleOffset.x, upDirection);
targetDirection = glm::rotate(targetDirection, angleOffset.y, sideDirection);
// Convert to celestial coordinates
glm::dvec2 celestCoords = convertGalacticToCelestial(targetDirection);
ghoul::Dictionary message = createMessageForMovingWWTCamera(celestCoords, _fieldOfView);
// Sleep so we don't bombard WWT with too many messages
std::this_thread::sleep_for(std::chrono::milliseconds(50));
sendMessageToWWT(message);
}
});
}
glm::dvec2 ScreenSpaceSkyBrowser::convertGalacticToCelestial(glm::dvec3 rGal) const {
// Used the math from this website: https://gea.esac.esa.int/archive/documentation/GD -->
// R2/Data_processing/chap_cu3ast/sec_cu3ast_intro/ssec_cu3ast_intro_tansforms.html#SSS1
const glm::dmat3 conversionMatrix = glm::dmat3({
-0.0548755604162154, 0.4941094278755837, -0.8676661490190047, // col 0
-0.8734370902348850, -0.4448296299600112, -0.1980763734312015, // col 1
-0.4838350155487132, 0.7469822444972189, 0.4559837761750669 // col 2
});
glm::dvec3 rICRS = glm::transpose(conversionMatrix) * rGal;
float ra = atan2(rICRS[1], rICRS[0]);
float dec = atan2(rICRS[2], glm::sqrt((rICRS[0] * rICRS[0]) + (rICRS[1] * rICRS[1])));
ra = ra > 0 ? ra : ra + (2 * glm::pi<float>());
return glm::dvec2(glm::degrees(ra), glm::degrees(dec));
}
void ScreenSpaceSkyBrowser::translate(glm::vec2 translation) {
glm::vec3 position = _cartesianPosition;
_cartesianPosition = glm::translate(glm::mat4(1.f), glm::vec3(translation, 0.0f)) * glm::vec4(position, 1.0f);