From 78f3b42c3bcaf2f345605d83ce0d631e1df28bcc Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 16 Jun 2016 01:22:30 +0200 Subject: [PATCH] Started cleanup of ScreenSpaceImage --- modules/base/rendering/screenspaceimage.cpp | 179 +++++++++----------- modules/base/rendering/screenspaceimage.h | 31 ++-- src/rendering/renderengine.cpp | 2 +- 3 files changed, 92 insertions(+), 120 deletions(-) diff --git a/modules/base/rendering/screenspaceimage.cpp b/modules/base/rendering/screenspaceimage.cpp index 2b38c0029e..2c20a33f4f 100644 --- a/modules/base/rendering/screenspaceimage.cpp +++ b/modules/base/rendering/screenspaceimage.cpp @@ -1,80 +1,80 @@ /***************************************************************************************** -* * -* OpenSpace * -* * -* Copyright (c) 2014-2016 * -* * -* 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. * -****************************************************************************************/ + * * + * OpenSpace * + * * + * Copyright (c) 2014-2016 * + * * + * 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 -#include -#include #include - -#include #include #include +#include +#include + +#include + namespace { const std::string _loggerCat = "ScreenSpaceImage"; + + const std::string KeyName = "Name"; + const std::string KeyTexturePath = "TexturePath"; + const std::string KeyUrl = "URL"; } namespace openspace { + ScreenSpaceImage::ScreenSpaceImage(const ghoul::Dictionary& dictionary) - :ScreenSpaceRenderable(dictionary) - ,_texturePath("texturePath", "Texture path", "") - ,_downloadImage(false) + : ScreenSpaceRenderable(dictionary) + , _texturePath("texturePath", "Texture path", "") + , _downloadImage(false) { std::string name; - if(dictionary.getValue("Name", name)){ + if (dictionary.getValue(KeyName, name)) { setName(name); - }else{ - _id = id(); - setName("ScreenSpaceImage" + std::to_string(_id)); + } else { + static int id = 0; + setName("ScreenSpaceImage " + std::to_string(id)); + ++id; } addProperty(_texturePath); registerProperties(); std::string texturePath; - bool texturesucces = (dictionary.getValue("TexturePath", texturePath)); - if(texturesucces){ - _texturePath.set(texturePath); + if (dictionary.getValue(KeyTexturePath, texturePath)) { + _texturePath = texturePath; OsEng.gui()._screenSpaceProperty.registerProperty(&_texturePath); _texturePath.onChange([this](){ loadTexture(); }); } - bool urlsucces = dictionary.getValue("URL", _url); - if(urlsucces){ - _downloadImage =true; + if (dictionary.getValue(KeyUrl, _url)) { + _downloadImage = true; } - - //screenspaceCygnet does not have url or texturePath - // if(!texturesucces && !urlsucces){ - // LERROR("Must specify TexturePath or URL"); - // } - } -ScreenSpaceImage::~ScreenSpaceImage(){} +ScreenSpaceImage::~ScreenSpaceImage() {} -bool ScreenSpaceImage::initialize(){ +bool ScreenSpaceImage::initialize() { _originalViewportSize = OsEng.windowWrapper().currentWindowResolution(); createPlane(); @@ -84,7 +84,7 @@ bool ScreenSpaceImage::initialize(){ return isReady(); } -bool ScreenSpaceImage::deinitialize(){ +bool ScreenSpaceImage::deinitialize() { unregisterProperties(); glDeleteVertexArrays(1, &_quad); @@ -96,47 +96,32 @@ bool ScreenSpaceImage::deinitialize(){ _texturePath = ""; _texture = nullptr; - RenderEngine& renderEngine = OsEng.renderEngine(); if (_shader) { - renderEngine.removeRenderProgram(_shader); + OsEng.renderEngine().removeRenderProgram(_shader); _shader = nullptr; } return true; } -void ScreenSpaceImage::render(){ - if(!isReady()) return; - if(!_enabled) return; - - glm::mat4 rotation = rotationMatrix(); - glm::mat4 translation = translationMatrix(); - glm::mat4 scale = scaleMatrix(); - glm::mat4 modelTransform = rotation*translation*scale; - - draw(modelTransform); +void ScreenSpaceImage::render() { + draw(rotationMatrix() * translationMatrix() * scaleMatrix()); } - -void ScreenSpaceImage::update(){ - if(_downloadImage && _futureImage.valid() && DownloadManager::futureReady(_futureImage)){ +void ScreenSpaceImage::update() { + bool futureReady = DownloadManager::futureReady(_futureImage); + if (_downloadImage && _futureImage.valid() && futureReady) { loadTexture(); } } - -bool ScreenSpaceImage::isReady() const{ - bool ready = true; - if (!_shader) - ready &= false; - if(!_texture) - ready &= false; - return ready; +bool ScreenSpaceImage::isReady() const { + return _shader && _texture; } void ScreenSpaceImage::loadTexture() { std::unique_ptr texture = nullptr; - if(!_downloadImage) + if (!_downloadImage) texture = std::move(loadFromDisk()); else texture = std::move(loadFromMemory()); @@ -152,56 +137,54 @@ void ScreenSpaceImage::loadTexture() { } } -void ScreenSpaceImage::updateTexture(){ - if(!_downloadImage){ +void ScreenSpaceImage::updateTexture() { + if (!_downloadImage) { loadTexture(); } else { - if(_futureImage.valid()) + if (_futureImage.valid()) return; std::future future = downloadImageToMemory(_url); - if(future.valid()){ + if (future.valid()) { _futureImage = std::move(future); } } } - std::unique_ptr ScreenSpaceImage::loadFromDisk(){ + std::unique_ptr ScreenSpaceImage::loadFromDisk() { if (_texturePath.value() != "") return (ghoul::io::TextureReader::ref().loadTexture(absPath(_texturePath.value()))); return nullptr; } - std::unique_ptr ScreenSpaceImage::loadFromMemory(){ - - if(_futureImage.valid() && DownloadManager::futureReady(_futureImage) ){ + std::unique_ptr ScreenSpaceImage::loadFromMemory() { + if (_futureImage.valid() && DownloadManager::futureReady(_futureImage)) { DownloadManager::MemoryFile imageFile = _futureImage.get(); - if(imageFile.corrupted) + if (imageFile.corrupted) return nullptr; return (ghoul::io::TextureReader::ref().loadTexture( - (void*) imageFile.buffer, + reinterpret_cast(imageFile.buffer), imageFile.size, - imageFile.format)); + imageFile.format) + ); } } -std::future ScreenSpaceImage::downloadImageToMemory(std::string url){ - return std::move( DlManager.fetchFile( - url, - [url](const DownloadManager::MemoryFile& file){ - LDEBUG("Download to memory finished for screen space image"); - }, - [url](const std::string& err){ - LDEBUG("Download to memory failer for screen space image: " +err); - } - ) ); +std::future ScreenSpaceImage::downloadImageToMemory( + std::string url) +{ + return std::move(DlManager.fetchFile( + url, + [url](const DownloadManager::MemoryFile& file) { + LDEBUG("Download to memory finished for screen space image"); + }, + [url](const std::string& err) { + LDEBUG("Download to memory failer for screen space image: " +err); + } + )); } -int ScreenSpaceImage::id(){ - static int id = 0; - return id++; -} -} \ No newline at end of file +} // namespace openspace diff --git a/modules/base/rendering/screenspaceimage.h b/modules/base/rendering/screenspaceimage.h index e157f990a7..df66d02db7 100644 --- a/modules/base/rendering/screenspaceimage.h +++ b/modules/base/rendering/screenspaceimage.h @@ -21,57 +21,46 @@ * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ + #ifndef __SCREENSPACEIMAGE_H__ #define __SCREENSPACEIMAGE_H__ #include -#include -#include +#include #include +#include namespace openspace { -/** - * @brief Creates a textured plane rendered in screenspace - * @details The plane gets the same ratio as the texture. Implements - * the interface that ScreenSpaceImage speciefies. - * - * @param texturePath Path to the image that should be used as texture - */ -class ScreenSpaceImage : public ScreenSpaceRenderable { +class ScreenSpaceImage : public ScreenSpaceRenderable { public: - ScreenSpaceImage(std::string texturePath); ScreenSpaceImage(const ghoul::Dictionary& dictionary); ~ScreenSpaceImage(); bool initialize() override; bool deinitialize() override; void render() override; - virtual void update() override; + void update() override; bool isReady() const override; protected: + void loadTexture(); + void updateTexture(); + std::string _url; bool _downloadImage; std::future _futureImage; - void loadTexture(); - void updateTexture(); private: - - static int id(); std::future downloadImageToMemory(std::string url); std::unique_ptr loadFromDisk(); std::unique_ptr loadFromMemory(); properties::StringProperty _texturePath; - //std::string _memorybuffer; - - - int _id; }; } //namespace openspace -#endif //__SCREENSPACEIMAGE_H__ \ No newline at end of file + +#endif //__SCREENSPACEIMAGE_H__ diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index e2f2d788ef..839b60312f 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -407,7 +407,7 @@ void RenderEngine::render(const glm::mat4 &projectionMatrix, const glm::mat4 &vi } for (auto screenSpaceRenderable : _screenSpaceRenderables) { - if(screenSpaceRenderable->isEnabled()) + if (screenSpaceRenderable->isEnabled() && screenSpaceRenderable->isReady()) screenSpaceRenderable->render(); } }