Fix bug with screenspaceskybrowser scale crashing OpenSpace

This commit is contained in:
Ylva Selling
2023-01-27 13:49:12 -05:00
parent 4c8f568861
commit f781dfcd9c
4 changed files with 44 additions and 55 deletions

View File

@@ -29,7 +29,7 @@
#include <openspace/documentation/documentation.h>
#include <openspace/properties/stringproperty.h>
#include <openspace/properties/triggerproperty.h>
#include <openspace/properties/vector/vec2property.h>
#include <openspace/properties/vector/ivec2property.h>
#ifdef _MSC_VER
#pragma warning (push)
@@ -74,17 +74,17 @@ public:
void updateBrowserSize();
void reload();
glm::vec2 browserPixelDimensions() const;
void setRatio(float ratio);
float browserRatio() const;
void setCallbackDimensions(const std::function<void(const glm::dvec2&)>& function);
protected:
properties::Vec2Property _browserDimensions;
properties::IVec2Property _browserDimensions;
properties::StringProperty _url;
properties::TriggerProperty _reload;
std::unique_ptr<ghoul::opengl::Texture> _texture;
void updateBrowserDimensions();
void executeJavascript(const std::string& script) const;
bool _isUrlDirty = false;

View File

@@ -52,7 +52,6 @@ public:
double setVerticalFovWithScroll(float scroll);
void setOpacity(float opacity);
void setRatio(float ratio);
void setIdInBrowser() const;
void setIsInitialized(bool isInitialized);
@@ -77,13 +76,11 @@ private:
// Flags
bool _isSyncedWithWwt = false;
bool _textureDimensionsIsDirty = false;
bool _ratioIsDirty = false;
bool _radiusIsDirty = false;
bool _isInitialized = false;
bool _radiusIsDirty = false;
int _borderRadiusTimer = -1;
float _ratio = 1.f;
float _lastTextureQuality = 1.f;
};
} // namespace openspace

View File

@@ -55,6 +55,9 @@ namespace {
};
struct [[codegen::Dictionary(Browser)]] Parameters {
// [[codegen::verbatim(DimensionsInfo.description)]]
std::optional<glm::ivec2> dimensions;
// [[codegen::verbatim(UrlInfo.description)]]
std::optional<std::string> url;
@@ -78,7 +81,7 @@ void Browser::RenderHandler::setTexture(GLuint t) {
Browser::Browser(const ghoul::Dictionary& dictionary)
: _browserDimensions(
DimensionsInfo,
global::windowDelegate->currentSubwindowSize(),
glm::vec2(1000.f),
glm::vec2(10.f),
glm::vec2(3000.f)
)
@@ -90,7 +93,9 @@ Browser::Browser(const ghoul::Dictionary& dictionary)
_url = p.url.value_or(_url);
_url.onChange([this]() { _isUrlDirty = true; });
_browserDimensions = p.dimensions.value_or(_browserDimensions);
_browserDimensions.onChange([this]() { _isDimensionsDirty = true; });
_reload.onChange([this]() { _shouldReload = true; });
// Create browser and render handler
@@ -119,6 +124,9 @@ void Browser::initializeGL() {
_browserInstance->initialize();
_browserInstance->loadUrl(_url);
// Update the dimensions upon initialization. Do this with flag as it affects
// derived classes as well
_isDimensionsDirty = true;
}
void Browser::deinitializeGL() {
@@ -153,11 +161,7 @@ void Browser::update() {
}
if (_isDimensionsDirty) {
glm::vec2 dim = _browserDimensions;
if (dim.x > 0 && dim.y > 0) {
_browserInstance->reshape(dim);
_isDimensionsDirty = false;
}
updateBrowserDimensions();
}
if (_shouldReload) {
@@ -170,10 +174,6 @@ bool Browser::isReady() const {
return _texture.get();
}
glm::vec2 Browser::browserPixelDimensions() const {
return _browserDimensions;
}
// Updates the browser size to match the size of the texture
void Browser::updateBrowserSize() {
_browserDimensions = _texture->dimensions();
@@ -183,15 +183,26 @@ void Browser::reload() {
_reload.set(true);
}
void Browser::setRatio(float ratio) {
float relativeRatio = ratio / browserRatio();
float newX = static_cast<float>(_browserDimensions.value().x) * relativeRatio;
glm::ivec2 newDims = { static_cast<int>(floor(newX)), _browserDimensions.value().y };
_browserDimensions = newDims;
_isDimensionsDirty = true;
}
float Browser::browserRatio() const {
return static_cast<float>(_texture->dimensions().x) /
static_cast<float>(_texture->dimensions().y);
}
void Browser::setCallbackDimensions(const std::function<void(const glm::dvec2&)>& func) {
_browserDimensions.onChange([&]() {
func(_browserDimensions.value());
});
void Browser::updateBrowserDimensions() {
glm::ivec2 dim = _browserDimensions;
if (dim.x > 0 && dim.y > 0) {
_texture->setDimensions(glm::uvec3(_browserDimensions.value(), 1));
_browserInstance->reshape(dim);
_isDimensionsDirty = false;
}
}
void Browser::executeJavascript(const std::string& script) const {

View File

@@ -122,17 +122,12 @@ ScreenSpaceSkyBrowser::ScreenSpaceSkyBrowser(const ghoul::Dictionary& dictionary
addProperty(_textureQuality);
addProperty(_verticalFov);
_textureQuality.onChange([this]() { _textureDimensionsIsDirty = true; });
_textureQuality.onChange([this]() { _isDimensionsDirty = true; });
if (global::windowDelegate->isMaster()) {
_borderColor = randomBorderColor();
}
_scale.onChange([this]() {
updateTextureResolution();
_borderRadiusTimer = 0;
});
_useRadiusAzimuthElevation.onChange(
[this]() {
std::for_each(
@@ -160,7 +155,6 @@ ScreenSpaceSkyBrowser::~ScreenSpaceSkyBrowser() {
bool ScreenSpaceSkyBrowser::initializeGL() {
WwtCommunicator::initializeGL();
ScreenSpaceRenderable::initializeGL();
updateTextureResolution();
return true;
}
@@ -192,19 +186,18 @@ void ScreenSpaceSkyBrowser::setIsInitialized(bool isInitialized) {
}
void ScreenSpaceSkyBrowser::updateTextureResolution() {
// Scale texture depending on the height of the window
// Set texture size to the actual pixel size it covers
glm::vec2 pixels = glm::vec2(global::windowDelegate->currentSubwindowSize());
// Check if texture quality has changed. If it has, adjust accordingly
if (abs(_textureQuality.value() - _lastTextureQuality) > glm::epsilon<float>()) {
float diffTextureQuality = _textureQuality / _lastTextureQuality;
glm::vec2 newRes = glm::vec2(_browserDimensions.value()) * diffTextureQuality;
_browserDimensions = glm::ivec2(newRes);
_lastTextureQuality = _textureQuality.value();
}
_objectSize = glm::ivec3(_browserDimensions.value(), 1);
// If the scale is 1, it covers half the window. Hence multiplication with 2
float newResY = pixels.y * 2.f * _scale;
float newResX = newResY * _ratio;
glm::vec2 newSize = glm::vec2(newResX , newResY) * _textureQuality.value();
_browserDimensions = glm::ivec2(newSize);
_texture->setDimensions(glm::ivec3(newSize, 1));
_objectSize = glm::ivec3(_texture->dimensions());
// The radius has to be updated when the texture resolution has changed
_radiusIsDirty = true;
_borderRadiusTimer = 0;
}
void ScreenSpaceSkyBrowser::addDisplayCopy(const glm::vec3& raePosition, int nCopies) {
@@ -313,16 +306,9 @@ void ScreenSpaceSkyBrowser::render() {
}
void ScreenSpaceSkyBrowser::update() {
// Texture of window is 1x1 when minimized
bool isWindow = global::windowDelegate->currentSubwindowSize() != glm::ivec2(1);
bool isWindowResized = global::windowDelegate->windowHasResized();
if ((isWindowResized && isWindow) || _textureDimensionsIsDirty) {
// Check for dirty flags
if (_isDimensionsDirty) {
updateTextureResolution();
_textureDimensionsIsDirty = false;
}
if (_ratioIsDirty) {
updateTextureResolution();
_ratioIsDirty = false;
}
if (_shouldReload) {
_isInitialized = false;
@@ -369,11 +355,6 @@ void ScreenSpaceSkyBrowser::setOpacity(float opacity) {
_opacity = opacity;
}
void ScreenSpaceSkyBrowser::setRatio(float ratio) {
_ratio = ratio;
_ratioIsDirty = true;
}
float ScreenSpaceSkyBrowser::opacity() const {
return _opacity;
}