Factor ScreenSpaceImage into separate classes for local loading and online download (closes #296)

This commit is contained in:
Alexander Bock
2017-10-10 14:16:55 -04:00
parent 93534d13bc
commit aa969795cc
14 changed files with 425 additions and 230 deletions
@@ -76,29 +76,14 @@ ScreenSpaceFramebuffer::ScreenSpaceFramebuffer(const ghoul::Dictionary& dictiona
ScreenSpaceFramebuffer::~ScreenSpaceFramebuffer() {}
bool ScreenSpaceFramebuffer::initialize() {
_originalViewportSize = OsEng.windowWrapper().currentWindowResolution();
createPlane();
createShaders();
ScreenSpaceRenderable::initialize();
createFragmentbuffer();
return isReady();
}
bool ScreenSpaceFramebuffer::deinitialize() {
glDeleteVertexArrays(1, &_quad);
_quad = 0;
glDeleteBuffers(1, &_vertexPositionBuffer);
_vertexPositionBuffer = 0;
_texture = nullptr;
RenderEngine& renderEngine = OsEng.renderEngine();
if (_shader) {
renderEngine.removeRenderProgram(_shader);
_shader = nullptr;
}
ScreenSpaceRenderable::deinitialize();
_framebuffer->detachAll();
removeAllRenderFunctions();
@@ -31,8 +31,6 @@
#include <ghoul/opengl/framebufferobject.h>
#include <ghoul/opengl/textureunit.h>
#include <modules/base/rendering/screenspaceimage.h>
namespace openspace {
namespace documentation { struct Documentation; }
@@ -0,0 +1,123 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/base/rendering/screenspaceimagelocal.h>
#include <openspace/documentation/documentation.h>
#include <openspace/documentation/verifier.h>
#include <openspace/engine/openspaceengine.h>
#include <openspace/engine/wrapper/windowwrapper.h>
#include <openspace/rendering/renderengine.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/io/texture/texturereader.h>
#include <ghoul/filesystem/filesystem>
#include <ghoul/opengl/textureconversion.h>
namespace {
const char* KeyName = "Name";
const char* KeyUrl = "URL";
static const openspace::properties::Property::PropertyInfo TexturePathInfo = {
"TexturePath",
"Texture path",
"Sets the path of the texture that is displayed on this screen space plane. If "
"this value is changed, the image at the new path will automatically be loaded "
"and displayed. The size of the image will also automatically set the default "
"size of this plane."
};
} // namespace
namespace openspace {
documentation::Documentation ScreenSpaceImageLocal::Documentation() {
using namespace openspace::documentation;
return {
"ScreenSpace Local Image",
"base_screenspace_image_local",
{
{
KeyName,
new StringVerifier,
Optional::Yes,
"Specifies the GUI name of the ScreenspaceImage"
},
{
TexturePathInfo.identifier,
new StringVerifier,
Optional::Yes,
TexturePathInfo.description
}
}
};
}
ScreenSpaceImageLocal::ScreenSpaceImageLocal(const ghoul::Dictionary& dictionary)
: ScreenSpaceRenderable(dictionary)
, _texturePath(TexturePathInfo)
, _textureIsDirty(false)
{
documentation::testSpecificationAndThrow(
Documentation(),
dictionary,
"ScreenSpaceImageLocal"
);
if (dictionary.hasKey(KeyName)) {
setName(dictionary.value<std::string>(KeyName));
}
else {
static int id = 0;
setName("ScreenSpaceImageLocal " + std::to_string(id));
++id;
}
_texturePath.onChange([this]() { _textureIsDirty = true; });
addProperty(_texturePath);
if (dictionary.hasKey(TexturePathInfo.identifier)) {
_texturePath = dictionary.value<std::string>(TexturePathInfo.identifier);
}
}
void ScreenSpaceImageLocal::update() {
if (_textureIsDirty && !_texturePath.value().empty()) {
std::unique_ptr<ghoul::opengl::Texture> texture =
ghoul::io::TextureReader::ref().loadTexture(absPath(_texturePath));
if (texture) {
texture->uploadTexture();
// Textures of planets looks much smoother with AnisotropicMipMap rather than linear
texture->setFilter(ghoul::opengl::Texture::FilterMode::Linear);
_texture = std::move(texture);
_textureIsDirty = false;
}
}
}
} // namespace openspace
@@ -0,0 +1,53 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_BASE___SCREENSPACEIMAGELOCAL___H__
#define __OPENSPACE_MODULE_BASE___SCREENSPACEIMAGELOCAL___H__
#include <openspace/rendering/screenspacerenderable.h>
#include <openspace/properties/stringproperty.h>
#include <ghoul/opengl/texture.h>
namespace openspace {
namespace documentation { struct Documentation; }
class ScreenSpaceImageLocal : public ScreenSpaceRenderable {
public:
ScreenSpaceImageLocal(const ghoul::Dictionary& dictionary);
void update() override;
static documentation::Documentation Documentation();
private:
properties::StringProperty _texturePath;
bool _textureIsDirty;
};
} // namespace openspace
#endif // __OPENSPACE_MODULE_BASE___SCREENSPACEIMAGELOCAL___H__
@@ -22,7 +22,7 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/base/rendering/screenspaceimage.h>
#include <modules/base/rendering/screenspaceimageonline.h>
#include <openspace/documentation/documentation.h>
#include <openspace/documentation/verifier.h>
@@ -38,12 +38,11 @@
namespace {
const char* KeyName = "Name";
const char* KeyTexturePath = "TexturePath";
const char* KeyUrl = "URL";
static const openspace::properties::Property::PropertyInfo TexturePathInfo = {
"TexturePath",
"Texture path",
"Sets the path of the texture that is displayed on this screen space plane. If "
"Sets the URL of the texture that is displayed on this screen space plane. If "
"this value is changed, the image at the new path will automatically be loaded "
"and displayed. The size of the image will also automatically set the default "
"size of this plane."
@@ -52,11 +51,11 @@ namespace {
namespace openspace {
documentation::Documentation ScreenSpaceImage::Documentation() {
documentation::Documentation ScreenSpaceImageOnline::Documentation() {
using namespace openspace::documentation;
return {
"ScreenSpace Image",
"base_screenspace_image",
"ScreenSpace Online Image",
"base_screenspace_image_online",
{
{
KeyName,
@@ -68,16 +67,14 @@ documentation::Documentation ScreenSpaceImage::Documentation() {
KeyTexturePath,
new StringVerifier,
Optional::Yes,
"Specifies the image that is shown on the screenspace-aligned plane. If "
"this value is set and the URL is not, the disk image is used."
TexturePathInfo.description
}
}
};
}
ScreenSpaceImage::ScreenSpaceImage(const ghoul::Dictionary& dictionary)
ScreenSpaceImageOnline::ScreenSpaceImageOnline(const ghoul::Dictionary& dictionary)
: ScreenSpaceRenderable(dictionary)
, _downloadImage(false)
, _textureIsDirty(false)
, _texturePath(TexturePathInfo)
{
@@ -92,138 +89,75 @@ ScreenSpaceImage::ScreenSpaceImage(const ghoul::Dictionary& dictionary)
}
else {
static int id = 0;
setName("ScreenSpaceImage " + std::to_string(id));
setName("ScreenSpaceImageOnline " + std::to_string(id));
++id;
}
std::string texturePath;
if (dictionary.getValue(KeyTexturePath, texturePath)) {
_texturePath = dictionary.value<std::string>(KeyTexturePath);
}
if (dictionary.getValue(KeyUrl, _url)) {
_downloadImage = true;
}
_texturePath.onChange([this]() { _textureIsDirty = true; });
addProperty(_texturePath);
}
bool ScreenSpaceImage::initialize() {
_originalViewportSize = OsEng.windowWrapper().currentWindowResolution();
createPlane();
createShaders();
updateTexture();
return isReady();
}
bool ScreenSpaceImage::deinitialize() {
glDeleteVertexArrays(1, &_quad);
_quad = 0;
glDeleteBuffers(1, &_vertexPositionBuffer);
_vertexPositionBuffer = 0;
_texturePath = "";
_texture = nullptr;
if (_shader) {
OsEng.renderEngine().removeRenderProgram(_shader);
_shader = nullptr;
std::string texturePath;
if (dictionary.hasKey(TexturePathInfo.identifier)) {
_texturePath = dictionary.value<std::string>(TexturePathInfo.identifier);
}
return true;
}
void ScreenSpaceImage::render() {
draw(rotationMatrix() * translationMatrix() * scaleMatrix());
}
void ScreenSpaceImageOnline::update() {
if (_textureIsDirty) {
if (!_imageFuture.valid()) {
std::future<DownloadManager::MemoryFile> future = downloadImageToMemory(_texturePath);
if (future.valid()) {
_imageFuture = std::move(future);
}
}
void ScreenSpaceImage::update() {
bool download = _downloadImage ? (_futureImage.valid() && DownloadManager::futureReady(_futureImage)) : true;
if (download && _textureIsDirty) {
loadTexture();
}
}
if (_imageFuture.valid() && DownloadManager::futureReady(_imageFuture)) {
DownloadManager::MemoryFile imageFile = _imageFuture.get();
bool ScreenSpaceImage::isReady() const {
return _shader && _texture;
}
if (imageFile.corrupted) {
LERRORC(
"ScreenSpaceImageOnline",
"Error loading image from URL '" << _texturePath << "'"
);
return;
}
void ScreenSpaceImage::loadTexture() {
std::unique_ptr<ghoul::opengl::Texture> texture = nullptr;
if (!_downloadImage)
texture = loadFromDisk();
else
texture = loadFromMemory();
std::unique_ptr<ghoul::opengl::Texture> texture =
ghoul::io::TextureReader::ref().loadTexture(
reinterpret_cast<void*>(imageFile.buffer),
imageFile.size,
imageFile.format
);
if (texture) {
// LDEBUG("Loaded texture from '" << absPath(_texturePath) << "'");
texture->uploadTexture();
if (texture) {
// LDEBUG("Loaded texture from '" << absPath(_texturePath) << "'");
texture->uploadTexture();
// Textures of planets looks much smoother with AnisotropicMipMap rather than linear
texture->setFilter(ghoul::opengl::Texture::FilterMode::Linear);
// Textures of planets looks much smoother with AnisotropicMipMap rather than linear
texture->setFilter(ghoul::opengl::Texture::FilterMode::Linear);
_texture = std::move(texture);
_textureIsDirty = false;
}
}
void ScreenSpaceImage::updateTexture() {
if (!_downloadImage) {
loadTexture();
} else {
if (_futureImage.valid())
return;
std::future<DownloadManager::MemoryFile> future = downloadImageToMemory(_url);
if (future.valid()) {
_futureImage = std::move(future);
_texture = std::move(texture);
_textureIsDirty = false;
}
}
}
}
std::unique_ptr<ghoul::opengl::Texture> ScreenSpaceImage::loadFromDisk() {
if (_texturePath.value() != "")
return (ghoul::io::TextureReader::ref().loadTexture(absPath(_texturePath.value())));
return nullptr;
}
std::unique_ptr<ghoul::opengl::Texture> ScreenSpaceImage::loadFromMemory() {
if (_futureImage.valid() && DownloadManager::futureReady(_futureImage)) {
DownloadManager::MemoryFile imageFile = _futureImage.get();
if (imageFile.corrupted) {
return nullptr;
}
return (ghoul::io::TextureReader::ref().loadTexture(
reinterpret_cast<void*>(imageFile.buffer),
imageFile.size,
imageFile.format)
);
}
else {
return nullptr;
}
}
std::future<DownloadManager::MemoryFile> ScreenSpaceImage::downloadImageToMemory(
std::future<DownloadManager::MemoryFile> ScreenSpaceImageOnline::downloadImageToMemory(
std::string url)
{
return OsEng.downloadManager().fetchFile(
url,
[url](const DownloadManager::MemoryFile&) {
LDEBUGC(
"ScreenSpaceImage",
"ScreenSpaceImageOnline",
"Download to memory finished for screen space image"
);
},
[url](const std::string& err) {
LDEBUGC(
"ScreenSpaceImage",
"ScreenSpaceImageOnline",
"Download to memory failer for screen space image: " + err
);
}
@@ -22,8 +22,8 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_BASE___SCREENSPACEIMAGE___H__
#define __OPENSPACE_MODULE_BASE___SCREENSPACEIMAGE___H__
#ifndef __OPENSPACE_MODULE_BASE___SCREENSPACEIMAGEONLINE___H__
#define __OPENSPACE_MODULE_BASE___SCREENSPACEIMAGEONLINE___H__
#include <openspace/rendering/screenspacerenderable.h>
@@ -36,37 +36,24 @@ namespace openspace {
namespace documentation { struct Documentation; }
class ScreenSpaceImage : public ScreenSpaceRenderable {
class ScreenSpaceImageOnline : public ScreenSpaceRenderable {
public:
ScreenSpaceImage(const ghoul::Dictionary& dictionary);
ScreenSpaceImageOnline(const ghoul::Dictionary& dictionary);
bool initialize() override;
bool deinitialize() override;
void render() override;
void update() override;
bool isReady() const override;
static documentation::Documentation Documentation();
protected:
void loadTexture();
void updateTexture();
std::string _url;
bool _downloadImage;
bool _textureIsDirty;
std::future<DownloadManager::MemoryFile> _futureImage;
private:
std::future<DownloadManager::MemoryFile> downloadImageToMemory(std::string url);
std::unique_ptr<ghoul::opengl::Texture> loadFromDisk();
std::unique_ptr<ghoul::opengl::Texture> loadFromMemory();
std::future<DownloadManager::MemoryFile> _imageFuture;
properties::StringProperty _texturePath;
private:
std::future<DownloadManager::MemoryFile> downloadImageToMemory(std::string url);
};
} // namespace openspace
#endif // __OPENSPACE_MODULE_BASE___SCREENSPACEIMAGE___H__
#endif // __OPENSPACE_MODULE_BASE___SCREENSPACEIMAGEONLINE___H__