mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-04-21 10:28:44 -05:00
Add generic texture component for renderables
This commit is contained in:
+5
-3
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user