mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-04-28 23:09:32 -05:00
Cleanup ScreenSpaceFramebuffer (fix orientation issue)
Add Dashboard for on-screen information (#closes 201)
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* 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/screenspacedashboard.h>
|
||||
|
||||
#include <openspace/documentation/documentation.h>
|
||||
#include <openspace/documentation/verifier.h>
|
||||
|
||||
#include <openspace/engine/openspaceengine.h>
|
||||
#include <openspace/engine/wrapper/windowwrapper.h>
|
||||
#include <openspace/interaction/navigationhandler.h>
|
||||
#include <openspace/rendering/renderengine.h>
|
||||
#include <openspace/scene/scenegraphnode.h>
|
||||
#include <openspace/util/distanceconversion.h>>
|
||||
#include <openspace/util/timeconversion.h>
|
||||
#include <openspace/util/timemanager.h>
|
||||
|
||||
#include <ghoul/font/fontmanager.h>
|
||||
#include <ghoul/font/fontrenderer.h>
|
||||
|
||||
namespace {
|
||||
const char* KeyName = "Name";
|
||||
const char* KeyFontMono = "Mono";
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace openspace {
|
||||
|
||||
documentation::Documentation ScreenSpaceDashboard::Documentation() {
|
||||
using namespace openspace::documentation;
|
||||
return {
|
||||
"ScreenSpace Dashboard",
|
||||
"base_screenspace_dashboard",
|
||||
{
|
||||
{
|
||||
KeyName,
|
||||
new StringVerifier,
|
||||
Optional::Yes,
|
||||
"Specifies the GUI name of the ScreenSpaceDashboard"
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
ScreenSpaceDashboard::ScreenSpaceDashboard(const ghoul::Dictionary& dictionary)
|
||||
: ScreenSpaceFramebuffer(dictionary)
|
||||
, _fontRenderer(nullptr)
|
||||
, _fontDate(nullptr)
|
||||
, _fontInfo(nullptr)
|
||||
{
|
||||
documentation::testSpecificationAndThrow(
|
||||
Documentation(),
|
||||
dictionary,
|
||||
"ScreenSpaceDashboard"
|
||||
);
|
||||
|
||||
if (dictionary.hasKey(KeyName)) {
|
||||
setName(dictionary.value<std::string>(KeyName));
|
||||
}
|
||||
else {
|
||||
static int id = 0;
|
||||
if (id == 0) {
|
||||
setName("ScreenSpaceDashboard");
|
||||
}
|
||||
else {
|
||||
setName("ScreenSpaceDashboard" + std::to_string(id));
|
||||
}
|
||||
++id;
|
||||
}
|
||||
|
||||
_scale = 1.f;
|
||||
_scale.setMaxValue(15.f);
|
||||
|
||||
_size.onChange([this]() {
|
||||
_fontRenderer->setFramebufferSize({ _size.value().z, _size.value().w });
|
||||
});
|
||||
}
|
||||
|
||||
ScreenSpaceDashboard::~ScreenSpaceDashboard() {}
|
||||
|
||||
bool ScreenSpaceDashboard::initializeGL() {
|
||||
ScreenSpaceFramebuffer::initializeGL();
|
||||
|
||||
_fontRenderer = ghoul::fontrendering::FontRenderer::createDefault();
|
||||
_fontRenderer->setFramebufferSize({ _size.value().z, _size.value().w });
|
||||
|
||||
|
||||
_fontDate = OsEng.fontManager().font(
|
||||
KeyFontMono,
|
||||
48
|
||||
);
|
||||
_fontInfo = OsEng.fontManager().font(
|
||||
KeyFontMono,
|
||||
32
|
||||
);
|
||||
|
||||
addRenderFunction([this]() {
|
||||
|
||||
glm::vec2 penPosition = glm::vec2(
|
||||
10.f,
|
||||
_size.value().w
|
||||
);
|
||||
|
||||
penPosition.y -= _fontDate->height();
|
||||
RenderFontCr(
|
||||
*_fontDate,
|
||||
penPosition,
|
||||
"Date: %s",
|
||||
OsEng.timeManager().time().UTC().c_str()
|
||||
);
|
||||
|
||||
std::pair<double, std::string> deltaTime = simplifyTime(
|
||||
OsEng.timeManager().time().deltaTime()
|
||||
);
|
||||
RenderFontCr(
|
||||
*_fontInfo,
|
||||
penPosition,
|
||||
"Simulation increment: %.1f %s / second",
|
||||
deltaTime.first,
|
||||
deltaTime.second.c_str()
|
||||
);
|
||||
|
||||
double distance = glm::length(
|
||||
OsEng.renderEngine().camera()->positionVec3() -
|
||||
OsEng.navigationHandler().focusNode()->worldPosition()
|
||||
);
|
||||
std::pair<double, std::string> dist = simplifyDistance(distance);
|
||||
RenderFontCr(
|
||||
*_fontInfo,
|
||||
penPosition,
|
||||
"Distance from focus: %f %s",
|
||||
dist.first,
|
||||
dist.second.c_str()
|
||||
);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenSpaceDashboard::deinitializeGL() {
|
||||
_fontRenderer = nullptr;
|
||||
return ScreenSpaceFramebuffer::deinitializeGL();
|
||||
}
|
||||
|
||||
bool ScreenSpaceDashboard::isReady() const {
|
||||
return (_fontRenderer != nullptr) && (_fontDate != nullptr) && (_fontInfo != nullptr) && ScreenSpaceFramebuffer::isReady();
|
||||
}
|
||||
|
||||
void ScreenSpaceDashboard::update() {
|
||||
if (OsEng.windowWrapper().windowHasResized()) {
|
||||
glm::ivec2 size = OsEng.windowWrapper().currentWindowResolution();
|
||||
_size = { 0.f, 0.f, size.x, size.y };
|
||||
_originalViewportSize = size;
|
||||
createFramebuffer();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
@@ -0,0 +1,61 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* 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___SCREENSPACEDASHBOARD___H__
|
||||
#define __OPENSPACE_MODULE_BASE___SCREENSPACEDASHBOARD___H__
|
||||
|
||||
#include <modules/base/rendering/screenspaceframebuffer.h>
|
||||
|
||||
namespace ghoul::fontrendering {
|
||||
class Font;
|
||||
class FontRenderer;
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
|
||||
namespace documentation { struct Documentation; }
|
||||
|
||||
class ScreenSpaceDashboard: public ScreenSpaceFramebuffer {
|
||||
public:
|
||||
ScreenSpaceDashboard(const ghoul::Dictionary& dictionary);
|
||||
~ScreenSpaceDashboard();
|
||||
|
||||
bool initializeGL() override;
|
||||
bool deinitializeGL() override;
|
||||
|
||||
bool isReady() const override;
|
||||
void update() override;
|
||||
|
||||
static documentation::Documentation Documentation();
|
||||
|
||||
private:
|
||||
std::unique_ptr<ghoul::fontrendering::FontRenderer> _fontRenderer;
|
||||
|
||||
std::shared_ptr<ghoul::fontrendering::Font> _fontDate;
|
||||
std::shared_ptr<ghoul::fontrendering::Font> _fontInfo;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
#endif // __OPENSPACE_MODULE_BASE___SCREENSPACEDASHBOARD___H__
|
||||
@@ -82,7 +82,7 @@ ScreenSpaceFramebuffer::~ScreenSpaceFramebuffer() {}
|
||||
|
||||
bool ScreenSpaceFramebuffer::initializeGL() {
|
||||
ScreenSpaceRenderable::initializeGL();
|
||||
createFragmentbuffer();
|
||||
createFramebuffer();
|
||||
|
||||
return isReady();
|
||||
}
|
||||
@@ -90,7 +90,9 @@ bool ScreenSpaceFramebuffer::initializeGL() {
|
||||
bool ScreenSpaceFramebuffer::deinitializeGL() {
|
||||
ScreenSpaceRenderable::deinitializeGL();
|
||||
|
||||
_framebuffer->activate();
|
||||
_framebuffer->detachAll();
|
||||
_framebuffer->deactivate();
|
||||
removeAllRenderFunctions();
|
||||
|
||||
return true;
|
||||
@@ -103,7 +105,7 @@ void ScreenSpaceFramebuffer::render() {
|
||||
float xratio = _originalViewportSize.x / (size.z-size.x);
|
||||
float yratio = _originalViewportSize.y / (size.w-size.y);;
|
||||
|
||||
if (!_renderFunctions.empty()) {
|
||||
if (!_renderFunctions.empty() || !_renderFunctionsShared.empty()) {
|
||||
glViewport(
|
||||
static_cast<GLint>(-size.x * xratio),
|
||||
static_cast<GLint>(-size.y * yratio),
|
||||
@@ -113,10 +115,12 @@ void ScreenSpaceFramebuffer::render() {
|
||||
GLint defaultFBO = _framebuffer->getActiveObject();
|
||||
_framebuffer->activate();
|
||||
|
||||
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClearColor(0.f, 0.f, 0.f, 0.f);
|
||||
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glEnable(GL_ALPHA_TEST);
|
||||
for (const auto& renderFunction : _renderFunctions) {
|
||||
renderFunction();
|
||||
}
|
||||
for (const auto& renderFunction : _renderFunctionsShared) {
|
||||
(*renderFunction)();
|
||||
}
|
||||
_framebuffer->deactivate();
|
||||
@@ -131,15 +135,15 @@ void ScreenSpaceFramebuffer::render() {
|
||||
|
||||
glm::mat4 rotation = rotationMatrix();
|
||||
glm::mat4 translation = translationMatrix();
|
||||
glm::mat4 scale = scaleMatrix();
|
||||
scale = glm::scale(scale, glm::vec3((1.0/xratio), -(1.0/yratio), 1.0f));
|
||||
glm::mat4 scale = glm::scale(
|
||||
scaleMatrix(),
|
||||
glm::vec3((1.f / xratio), (1.f / yratio), 1.f)
|
||||
);
|
||||
glm::mat4 modelTransform = rotation*translation*scale;
|
||||
draw(modelTransform);
|
||||
}
|
||||
}
|
||||
|
||||
void ScreenSpaceFramebuffer::update() {}
|
||||
|
||||
bool ScreenSpaceFramebuffer::isReady() const {
|
||||
bool ready = true;
|
||||
if (!_shader) {
|
||||
@@ -155,17 +159,22 @@ void ScreenSpaceFramebuffer::setSize(glm::vec4 size) {
|
||||
_size.set(size);
|
||||
}
|
||||
|
||||
void ScreenSpaceFramebuffer::addRenderFunction(std::function<void()> renderFunction) {
|
||||
_renderFunctions.push_back(std::move(renderFunction));
|
||||
}
|
||||
|
||||
void ScreenSpaceFramebuffer::addRenderFunction(
|
||||
std::shared_ptr<std::function<void()>> renderFunction)
|
||||
{
|
||||
_renderFunctions.push_back(renderFunction);
|
||||
_renderFunctionsShared.push_back(std::move(renderFunction));
|
||||
}
|
||||
|
||||
void ScreenSpaceFramebuffer::removeAllRenderFunctions() {
|
||||
_renderFunctions.clear();
|
||||
_renderFunctionsShared.clear();
|
||||
}
|
||||
|
||||
void ScreenSpaceFramebuffer::createFragmentbuffer() {
|
||||
void ScreenSpaceFramebuffer::createFramebuffer() {
|
||||
_framebuffer = std::make_unique<ghoul::opengl::FramebufferObject>();
|
||||
_framebuffer->activate();
|
||||
_texture = std::make_unique<ghoul::opengl::Texture>(glm::uvec3(
|
||||
|
||||
@@ -51,23 +51,25 @@ public:
|
||||
bool initializeGL() override;
|
||||
bool deinitializeGL() override;
|
||||
void render() override;
|
||||
void update() override;
|
||||
bool isReady() const override;
|
||||
|
||||
void setSize(glm::vec4);
|
||||
void addRenderFunction(std::shared_ptr<std::function<void()>> renderFunction);
|
||||
void addRenderFunction(std::function<void()> renderFunction);
|
||||
void removeAllRenderFunctions();
|
||||
|
||||
static documentation::Documentation Documentation();
|
||||
|
||||
private:
|
||||
void createFragmentbuffer();
|
||||
static int id();
|
||||
|
||||
protected:
|
||||
void createFramebuffer();
|
||||
properties::Vec4Property _size;
|
||||
|
||||
private:
|
||||
static int id();
|
||||
|
||||
std::unique_ptr<ghoul::opengl::FramebufferObject> _framebuffer;
|
||||
std::vector<std::shared_ptr<std::function<void()>>> _renderFunctions;
|
||||
std::vector<std::shared_ptr<std::function<void()>>> _renderFunctionsShared;
|
||||
std::vector<std::function<void()>> _renderFunctions;
|
||||
|
||||
int _id;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user