Add generic texture component for renderables

This commit is contained in:
Emma Broman
2021-01-12 15:56:14 +01:00
parent c075dbcdeb
commit 72ebeb7f6f
7 changed files with 209 additions and 107 deletions

View File

@@ -0,0 +1,70 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2021 *
* *
* 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_CORE___TEXTURECOMPONENT___H__
#define __OPENSPACE_CORE___TEXTURECOMPONENT___H__
#include <ghoul/opengl/ghoul_gl.h>
#include <ghoul/opengl/texture.h>
#include <optional>
namespace ghoul::filesystem { class File; }
namespace ghoul::opengl {class Texture; }
namespace openspace {
class TextureComponent {
using Texture = ghoul::opengl::Texture;
public:
TextureComponent() = default;
TextureComponent(const Texture::FilterMode filterMode, bool watchFile = true);
~TextureComponent() = default;
Texture* texture() const;
void bind();
void uploadToGpu();
// Loads a texture from a file on disk
void loadFromFile(const std::string& path);
// Function to call in a renderable's update function to make sure
// the texture is kept up to date
void update();
private:
std::unique_ptr<ghoul::filesystem::File> _textureFile = nullptr;
std::unique_ptr<Texture> _texture = nullptr;
Texture::FilterMode _filterMode = Texture::FilterMode::LinearMipMap;
bool _shouldWatchFile = true;
bool _fileIsDirty = false;
bool _textureIsDirty = false;
};
} // namespace openspace
#endif // __OPENSPACE_CORE___TEXTURECOMPONENT___H__

View File

@@ -31,14 +31,14 @@
#include <openspace/scene/scene.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/io/texture/texturereader.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/opengl/openglstatecache.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/texture.h>
#include <ghoul/opengl/textureunit.h>
namespace {
constexpr const char* _loggerCat = "RenderableDisc";
constexpr const std::array<const char*, 4> UniformNames = {
"modelViewProjectionTransform", "opacity", "width", "colorTexture"
};
@@ -106,8 +106,6 @@ RenderableDisc::RenderableDisc(const ghoul::Dictionary& dictionary)
, _size(SizeInfo, 1.f, 0.f, 1e13f)
, _width(WidthInfo, 0.5f, 0.f, 1.f)
{
using ghoul::filesystem::File;
documentation::testSpecificationAndThrow(
Documentation(),
dictionary,
@@ -120,25 +118,26 @@ RenderableDisc::RenderableDisc(const ghoul::Dictionary& dictionary)
addProperty(_size);
_texturePath = absPath(dictionary.value<std::string>(TextureInfo.identifier));
_textureFile = std::make_unique<File>(_texturePath);
_texturePath.onChange([&]() { _texture->loadFromFile(_texturePath); });
addProperty(_texturePath);
if (dictionary.hasKey(WidthInfo.identifier)) {
_width = dictionary.value<double>(WidthInfo.identifier);
}
addProperty(_width);
_texturePath.onChange([&]() { loadTexture(); });
addProperty(_texturePath);
_textureFile->setCallback([&](const File&) { _textureIsDirty = true; });
addProperty(_opacity);
_plane = std::make_unique<PlaneGeometry>(2*_size);
}
bool RenderableDisc::isReady() const {
return _shader && _texture;
return _shader && _texture && _plane;
}
void RenderableDisc::initialize() {
_texture = std::make_unique<TextureComponent>(
ghoul::opengl::Texture::FilterMode::AnisotropicMipMap
);
_plane = std::make_unique<PlaneGeometry>(2 * _size);
}
void RenderableDisc::initializeGL() {
@@ -150,15 +149,15 @@ void RenderableDisc::initializeGL() {
ghoul::opengl::updateUniformLocations(*_shader, _uniformCache, UniformNames);
_texture->loadFromFile(_texturePath);
_texture->uploadToGpu();
_plane->initialize();
loadTexture();
}
void RenderableDisc::deinitializeGL() {
_plane->deinitialize();
_plane = nullptr;
_textureFile = nullptr;
_texture = nullptr;
global::renderEngine->removeRenderProgram(_shader.get());
@@ -210,40 +209,11 @@ void RenderableDisc::update(const UpdateData&) {
}
if (_planeIsDirty) {
_plane->updateSize(2*_size);
_plane->updateSize(2 * _size);
_planeIsDirty = false;
}
if (_textureIsDirty) {
loadTexture();
_textureIsDirty = false;
}
}
void RenderableDisc::loadTexture() {
if (!_texturePath.value().empty()) {
using namespace ghoul::io;
using namespace ghoul::opengl;
std::unique_ptr<Texture> texture = TextureReader::ref().loadTexture(
absPath(_texturePath)
);
if (texture) {
LDEBUGC(
"RenderableDisc",
fmt::format("Loaded texture from '{}'", absPath(_texturePath))
);
_texture = std::move(texture);
_texture->uploadTexture();
_texture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap);
_textureFile = std::make_unique<ghoul::filesystem::File>(_texturePath);
_textureFile->setCallback(
[&](const ghoul::filesystem::File&) { _textureIsDirty = true; }
);
}
}
_texture->update();
}
} // namespace openspace

View File

@@ -25,19 +25,16 @@
#ifndef __OPENSPACE_MODULE_BASE___RENDERABLEDISC___H__
#define __OPENSPACE_MODULE_BASE___RENDERABLEDISC___H__
#include <openspace/rendering/renderable.h>
#include <openspace/properties/stringproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/rendering/renderable.h>
#include <openspace/rendering/texturecomponent.h>
#include <openspace/util/planegeometry.h>
#include <ghoul/opengl/uniformcache.h>
#include <ghoul/opengl/ghoul_gl.h>
namespace ghoul::filesystem { class File; }
namespace ghoul::opengl {
class ProgramObject;
class Texture;
} // namespace ghoul::opengl
namespace ghoul::opengl { class ProgramObject; }
namespace openspace {
@@ -47,6 +44,7 @@ class RenderableDisc : public Renderable {
public:
RenderableDisc(const ghoul::Dictionary& dictionary);
void initialize() override;
void initializeGL() override;
void deinitializeGL() override;
@@ -58,20 +56,16 @@ public:
static documentation::Documentation Documentation();
private:
void loadTexture();
properties::StringProperty _texturePath;
properties::FloatProperty _size;
properties::FloatProperty _width;
std::unique_ptr<ghoul::opengl::ProgramObject> _shader;
UniformCache(modelViewProjection, opacity, width, texture) _uniformCache;
std::unique_ptr<ghoul::opengl::Texture> _texture;
std::unique_ptr<ghoul::filesystem::File> _textureFile;
std::unique_ptr<PlaneGeometry> _plane;
std::unique_ptr<TextureComponent> _texture;
bool _textureIsDirty = false;
bool _planeIsDirty = false;
};

View File

@@ -33,10 +33,8 @@
#include <openspace/util/distanceconstants.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/io/texture/texturereader.h>
#include <ghoul/opengl/openglstatecache.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/texture.h>
#include <ghoul/opengl/textureunit.h>
namespace {
@@ -144,13 +142,9 @@ RenderableOrbitDisc::RenderableOrbitDisc(const ghoul::Dictionary& dictionary)
setBoundingSphere(_size + _offset.value().y * _size);
_texturePath = absPath(dictionary.value<std::string>(TextureInfo.identifier));
_textureFile = std::make_unique<File>(_texturePath);
_texturePath.onChange([&]() { _textureIsDirty = true; });
_texturePath.onChange([&]() { _texture->loadFromFile(_texturePath); });
addProperty(_texturePath);
_textureFile->setCallback([&](const File&) { _textureIsDirty = true; });
_eccentricity = static_cast<float>(
dictionary.value<double>(EccentricityInfo.identifier)
);
@@ -158,12 +152,17 @@ RenderableOrbitDisc::RenderableOrbitDisc(const ghoul::Dictionary& dictionary)
addProperty(_eccentricity);
addProperty(_opacity);
_plane = std::make_unique<PlaneGeometry>(planeSize());
}
bool RenderableOrbitDisc::isReady() const {
return _shader && _texture;
return _shader && _texture && _plane;
}
void RenderableOrbitDisc::initialize() {
_texture = std::make_unique<TextureComponent>(
ghoul::opengl::Texture::FilterMode::AnisotropicMipMap
);
_plane = std::make_unique<PlaneGeometry>(planeSize());
}
void RenderableOrbitDisc::initializeGL() {
@@ -175,15 +174,15 @@ void RenderableOrbitDisc::initializeGL() {
ghoul::opengl::updateUniformLocations(*_shader, _uniformCache, UniformNames);
_texture->loadFromFile(_texturePath);
_texture->uploadToGpu();
_plane->initialize();
loadTexture();
}
void RenderableOrbitDisc::deinitializeGL() {
_plane->deinitialize();
_plane = nullptr;
_textureFile = nullptr;
_texture = nullptr;
global::renderEngine->removeRenderProgram(_shader.get());
@@ -240,33 +239,7 @@ void RenderableOrbitDisc::update(const UpdateData&) {
_planeIsDirty = false;
}
if (_textureIsDirty) {
loadTexture();
_textureIsDirty = false;
}
}
void RenderableOrbitDisc::loadTexture() {
if (!_texturePath.value().empty()) {
std::unique_ptr<ghoul::opengl::Texture> texture =
ghoul::io::TextureReader::ref().loadTexture(absPath(_texturePath));
if (texture) {
LDEBUGC(
"RenderableOrbitDisc",
fmt::format("Loaded texture from '{}'", absPath(_texturePath))
);
_texture = std::move(texture);
_texture->uploadTexture();
_texture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap);
_textureFile = std::make_unique<ghoul::filesystem::File>(_texturePath);
_textureFile->setCallback(
[&](const ghoul::filesystem::File&) { _textureIsDirty = true; }
);
}
}
_texture->update();
}
float RenderableOrbitDisc::planeSize() const {

View File

@@ -29,9 +29,9 @@
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/vector/vec2property.h>
#include <openspace/rendering/renderable.h>
#include <openspace/rendering/texturecomponent.h>
#include <openspace/util/planegeometry.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/opengl/texture.h>
#include <ghoul/opengl/uniformcache.h>
namespace ghoul::filesystem { class File; }
@@ -47,6 +47,7 @@ class RenderableOrbitDisc : public Renderable {
public:
RenderableOrbitDisc(const ghoul::Dictionary& dictionary);
void initialize() override;
void initializeGL() override;
void deinitializeGL() override;
@@ -58,8 +59,6 @@ public:
static documentation::Documentation Documentation();
private:
void loadTexture();
// Computes the size of the plane quad using the relevant properties
float planeSize() const;
@@ -71,12 +70,10 @@ private:
std::unique_ptr<ghoul::opengl::ProgramObject> _shader = nullptr;
UniformCache(modelViewProjection, offset, opacity, texture,
eccentricity, semiMajorAxis) _uniformCache;
std::unique_ptr<ghoul::opengl::Texture> _texture = nullptr;
std::unique_ptr<ghoul::filesystem::File> _textureFile;
std::unique_ptr<PlaneGeometry> _plane;
std::unique_ptr<TextureComponent> _texture;
bool _textureIsDirty = false;
bool _planeIsDirty = false;
};

View File

@@ -143,6 +143,7 @@ set(OPENSPACE_SOURCE
${OPENSPACE_BASE_DIR}/src/rendering/renderengine.cpp
${OPENSPACE_BASE_DIR}/src/rendering/renderengine_lua.inl
${OPENSPACE_BASE_DIR}/src/rendering/screenspacerenderable.cpp
${OPENSPACE_BASE_DIR}/src/rendering/texturecomponent.cpp
${OPENSPACE_BASE_DIR}/src/rendering/transferfunction.cpp
${OPENSPACE_BASE_DIR}/src/rendering/volumeraycaster.cpp
${OPENSPACE_BASE_DIR}/src/scene/asset.cpp
@@ -323,6 +324,7 @@ set(OPENSPACE_HEADER
${OPENSPACE_BASE_DIR}/include/openspace/rendering/dashboard.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/dashboarditem.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/framebufferrenderer.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/deferredcaster.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/deferredcasterlistener.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/deferredcastermanager.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/loadingscreen.h
@@ -333,11 +335,11 @@ set(OPENSPACE_HEADER
${OPENSPACE_BASE_DIR}/include/openspace/rendering/renderable.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/renderer.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/renderengine.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/volume.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/screenspacerenderable.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/deferredcaster.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/volumeraycaster.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/texturecomponent.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/transferfunction.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/volume.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/volumeraycaster.h
${OPENSPACE_BASE_DIR}/include/openspace/scene/asset.h
${OPENSPACE_BASE_DIR}/include/openspace/scene/assetlistener.h
${OPENSPACE_BASE_DIR}/include/openspace/scene/assetloader.h

View File

@@ -0,0 +1,96 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2021 *
* *
* 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 <openspace/rendering/texturecomponent.h>
#include <ghoul/filesystem/file.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/io/texture/texturereader.h>
#include <ghoul/logging/logmanager.h>
namespace {
constexpr const char* _loggerCat = "TextureComponent";
} // namespace
namespace openspace {
TextureComponent::TextureComponent(const Texture::FilterMode filterMode, bool watchFile)
: _filterMode(filterMode), _shouldWatchFile(watchFile)
{}
ghoul::opengl::Texture* TextureComponent::texture() const {
return _texture.get();
}
void TextureComponent::bind() {
ghoul_assert(_texture, "Texture must be loaded before binding");
_texture->bind();
}
void TextureComponent::uploadToGpu() {
if (!_texture) {
LERROR("Could not upload texture to GPU. Texture not loaded");
return;
}
_texture->uploadTexture();
_texture->setFilter(_filterMode);
}
void TextureComponent::loadFromFile(const std::string& path) {
if (!path.empty()) {
using namespace ghoul::io;
using namespace ghoul::opengl;
std::unique_ptr<Texture> texture = TextureReader::ref().loadTexture(
absPath(path)
);
if (texture) {
LDEBUG(fmt::format("Loaded texture from '{}'", absPath(path)));
_texture = nullptr;
_texture = std::move(texture);
_textureFile = std::make_unique<ghoul::filesystem::File>(path);
if (_shouldWatchFile) {
_textureFile->setCallback(
[&](const ghoul::filesystem::File&) { _fileIsDirty = true; }
);
}
_fileIsDirty = false;
_textureIsDirty = true;
}
}
}
void TextureComponent::update() {
if (_fileIsDirty) {
loadFromFile(_textureFile->path());
}
if (_textureIsDirty) {
uploadToGpu();
_textureIsDirty = false;
}
}
} // namespace openspace