mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-02-26 14:58:51 -06:00
Started cleanup of ScreenSpaceImage
This commit is contained in:
@@ -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 <modules/base/rendering/screenspaceimage.h>
|
||||
#include <ghoul/io/texture/texturereader.h>
|
||||
#include <ghoul/filesystem/filesystem>
|
||||
|
||||
#include <openspace/engine/openspaceengine.h>
|
||||
|
||||
#include <modules/onscreengui/include/gui.h>
|
||||
#include <openspace/engine/wrapper/windowwrapper.h>
|
||||
#include <openspace/rendering/renderengine.h>
|
||||
|
||||
#include <ghoul/io/texture/texturereader.h>
|
||||
#include <ghoul/filesystem/filesystem>
|
||||
|
||||
#include <modules/onscreengui/include/gui.h>
|
||||
|
||||
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<ghoul::opengl::Texture> 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<DownloadManager::MemoryFile> future = downloadImageToMemory(_url);
|
||||
if(future.valid()){
|
||||
if (future.valid()) {
|
||||
_futureImage = std::move(future);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<ghoul::opengl::Texture> ScreenSpaceImage::loadFromDisk(){
|
||||
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) ){
|
||||
std::unique_ptr<ghoul::opengl::Texture> 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<void*>(imageFile.buffer),
|
||||
imageFile.size,
|
||||
imageFile.format));
|
||||
imageFile.format)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
std::future<DownloadManager::MemoryFile> 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<DownloadManager::MemoryFile> 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++;
|
||||
}
|
||||
}
|
||||
} // namespace openspace
|
||||
|
||||
@@ -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 <openspace/rendering/screenspacerenderable.h>
|
||||
#include <ghoul/opengl/texture.h>
|
||||
#include <openspace/engine/downloadmanager.h>
|
||||
|
||||
#include <openspace/engine/downloadmanager.h>
|
||||
#include <openspace/properties/stringproperty.h>
|
||||
|
||||
#include <ghoul/opengl/texture.h>
|
||||
|
||||
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<DownloadManager::MemoryFile> _futureImage;
|
||||
|
||||
void loadTexture();
|
||||
void updateTexture();
|
||||
private:
|
||||
|
||||
static int id();
|
||||
std::future<DownloadManager::MemoryFile> downloadImageToMemory(std::string url);
|
||||
std::unique_ptr<ghoul::opengl::Texture> loadFromDisk();
|
||||
std::unique_ptr<ghoul::opengl::Texture> loadFromMemory();
|
||||
|
||||
properties::StringProperty _texturePath;
|
||||
//std::string _memorybuffer;
|
||||
|
||||
|
||||
int _id;
|
||||
};
|
||||
|
||||
} //namespace openspace
|
||||
#endif //__SCREENSPACEIMAGE_H__
|
||||
|
||||
#endif //__SCREENSPACEIMAGE_H__
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user