Add helper methods to quickly render boxes (finalizing and #closes 696)

This commit is contained in:
Alexander Bock
2018-09-02 02:18:40 -04:00
parent 9f57d80541
commit 24ef67afa0
8 changed files with 450 additions and 20 deletions

View File

@@ -66,7 +66,7 @@ public:
void initialize();
void initializeGL();
void deinitialize();
void deinitializeGL() {}
void deinitializeGL();
void preSynchronization();
void postSynchronizationPreDraw();
void render(const glm::mat4& sceneMatrix, const glm::mat4& viewMatrix,

View File

@@ -0,0 +1,90 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2018 *
* *
* 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___HELPER___H__
#define __OPENSPACE_CORE___HELPER___H__
#include <ghoul/opengl/uniformcache.h>
namespace ghoul::opengl { class ProgramObject; }
namespace openspace::rendering::helper {
enum class Anchor {
Center,
NW,
NE,
SW,
SE
};
void initialize();
void deinitialize();
glm::mat4 ortho(const glm::vec2& position, const glm::vec2& size,
Anchor anchor = Anchor::NW);
void renderBox(ghoul::opengl::ProgramObject& program, GLint orthoLocation,
GLint colorLocation, const glm::vec2& position, const glm::vec2& size,
const glm::vec4& color, Anchor anchor = Anchor::NW);
void renderBox(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color,
Anchor anchor = Anchor::NW);
struct Shaders {
struct {
std::unique_ptr<ghoul::opengl::ProgramObject> program;
UniformCache(tex, hasTexture, shouldFlipTexture, ortho, color) cache;
} xyuvrgba;
struct {
std::unique_ptr<ghoul::opengl::ProgramObject> program;
UniformCache(tex, hasTexture, shouldFlipTexture, ortho, color) cache;
} screenfilling;
};
struct VertexObjects {
struct {
GLuint vao;
GLuint vbo;
} square;
struct {
GLuint vao;
} empty;
};
namespace detail {
Shaders& gShadersConstructor();
VertexObjects& gVertexObjectsConstructor();
} // namespace detail
static Shaders& shaders = detail::gShadersConstructor();
static VertexObjects& vertexObjects = detail::gVertexObjectsConstructor();
} // namespace openspace::rendering::helper
#endif // __OPENSPACE_CORE___HELPER___H__

View File

@@ -654,7 +654,7 @@ RenderableAtmosphere::RenderableAtmosphere(const ghoul::Dictionary& dictionary)
}
}
void RenderableAtmosphere::deinitialize() {
void RenderableAtmosphere::deinitializeGL() {
if (_deferredcaster) {
global::deferredcasterManager.detachDeferredcaster(*_deferredcaster);
_deferredcaster = nullptr;
@@ -705,9 +705,6 @@ void RenderableAtmosphere::initializeGL() {
return;
}
void RenderableAtmosphere::deinitializeGL() {
}
bool RenderableAtmosphere::isReady() const {
bool ready = true;
ready &= (_deferredcaster != nullptr);

View File

@@ -75,7 +75,6 @@ class RenderableAtmosphere : public Renderable {
public:
RenderableAtmosphere(const ghoul::Dictionary& dictionary);
void deinitialize() override;
void initializeGL() override;
void deinitializeGL() override;
bool isReady() const override;

View File

@@ -128,6 +128,7 @@ set(OPENSPACE_SOURCE
${OPENSPACE_BASE_DIR}/src/rendering/dashboarditem.cpp
${OPENSPACE_BASE_DIR}/src/rendering/framebufferrenderer.cpp
${OPENSPACE_BASE_DIR}/src/rendering/deferredcastermanager.cpp
${OPENSPACE_BASE_DIR}/src/rendering/helper.cpp
${OPENSPACE_BASE_DIR}/src/rendering/loadingscreen.cpp
${OPENSPACE_BASE_DIR}/src/rendering/luaconsole.cpp
${OPENSPACE_BASE_DIR}/src/rendering/raycastermanager.cpp
@@ -311,6 +312,7 @@ set(OPENSPACE_HEADER
${OPENSPACE_BASE_DIR}/include/openspace/rendering/deferredcastermanager.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/loadingscreen.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/luaconsole.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/helper.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/raycasterlistener.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/raycastermanager.h
${OPENSPACE_BASE_DIR}/include/openspace/rendering/renderable.h

View File

@@ -44,6 +44,7 @@
#include <openspace/performance/performancemanager.h>
#include <openspace/rendering/dashboard.h>
#include <openspace/rendering/dashboarditem.h>
#include <openspace/rendering/helper.h>
#include <openspace/rendering/loadingscreen.h>
#include <openspace/rendering/luaconsole.h>
#include <openspace/rendering/renderable.h>
@@ -337,6 +338,8 @@ void OpenSpaceEngine::initializeGL() {
//glbinding::Binding::useCurrentContext();
glbinding::Binding::initialize(glfwGetProcAddress);
rendering::helper::initialize();
// clear the screen so the user doesn't have to see old buffer contents left on the
// graphics card
LDEBUG("Clearing all Windows");
@@ -767,21 +770,10 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) {
void OpenSpaceEngine::deinitialize() {
LTRACE("OpenSpaceEngine::deinitialize(begin)");
// We want to render an image informing the user that we are shutting down
global::renderEngine.renderEndscreen();
global::windowDelegate.swapBuffer();
for (const std::function<void()>& func : global::callback::deinitializeGL) {
func();
}
for (const std::function<void()>& func : global::callback::deinitialize) {
func();
}
global::openSpaceEngine.assetManager().deinitialize();
global::openSpaceEngine._scene = nullptr;
global::navigationHandler.deinitialize();
LTRACE("deinitialize(begin)");
@@ -794,7 +786,6 @@ void OpenSpaceEngine::deinitialize() {
);
}
global::deinitializeGL();
global::deinitialize();
FactoryManager::deinitialize();
@@ -812,6 +803,27 @@ void OpenSpaceEngine::deinitialize() {
LTRACE("OpenSpaceEngine::deinitialize(end)");
}
void OpenSpaceEngine::deinitializeGL() {
LTRACE("OpenSpaceEngine::deinitializeGL(begin)");
// We want to render an image informing the user that we are shutting down
global::renderEngine.renderEndscreen();
global::windowDelegate.swapBuffer();
global::openSpaceEngine.assetManager().deinitialize();
global::openSpaceEngine._scene = nullptr;
for (const std::function<void()>& func : global::callback::deinitializeGL) {
func();
}
global::deinitializeGL();
rendering::helper::deinitialize();
LTRACE("OpenSpaceEngine::deinitializeGL(end)");
}
void OpenSpaceEngine::writeStaticDocumentation() {
// If a LuaDocumentationFile was specified, generate it now
if (!global::configuration.documentation.lua.empty()) {

324
src/rendering/helper.cpp Normal file
View File

@@ -0,0 +1,324 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2018 *
* *
* 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/helper.h>
#include <openspace/engine/globals.h>
#include <openspace/engine/windowdelegate.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/misc/assert.h>
#include <fstream>
#include <string>
namespace {
bool isInitialized = false;
std::string xyuvrgbaVertexFile;
std::string xyuvrgbaFragmentFile;
std::string screenFillingVertexFile;
std::string screenFillingFragmentFile;
constexpr const char* XyuvrgbaVertexCode = R"(
#version __CONTEXT__
layout(location = 0) in vec2 in_position;
layout(location = 1) in vec2 in_uv;
layout(location = 2) in vec4 in_color;
out vec2 out_position;
out vec2 out_uv;
out vec4 out_color;
uniform mat4 ortho;
void main() {
out_position = in_position;
out_uv = in_uv;
out_color = in_color;
gl_Position = ortho * vec4(in_position, 0.0, 1.0);
}
)";
constexpr const char* ScreenFillingQuadVertexCode = R"(
#version __CONTEXT__
vec2 positions[6] = vec2[](
vec2(-1.0, -1.0), vec2( 1.0, -1.0), vec2( 1.0, 1.0),
vec2(-1.0, -1.0), vec2( 1.0, 1.0), vec2(-1.0, 1.0)
);
out vec2 out_uv;
out vec4 out_color;
void main() {
gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);
out_uv = (positions[gl_VertexID] + 1.0) / 2.0;
out_color = vec4(1.0);
}
)";
constexpr const char* XyuvrgbaFragmentCode = R"(
#version __CONTEXT__
uniform bool hasTexture = false;
uniform bvec2 shouldFlipTexture = bvec2(false, false);
uniform sampler2D tex;
uniform vec4 color = vec4(1.0, 1.0, 1.0, 1.0);
in vec2 out_uv;
in vec4 out_color;
out vec4 FragColor;
void main() {
if (hasTexture) {
vec2 uv = out_uv;
if (shouldFlipTexture.x) {
uv.x = 1.0 - uv.x;
}
if (shouldFlipTexture.y) {
uv.y = 1.0 - uv.y;
}
FragColor = out_color * color * texture(tex, uv);
}
else {
FragColor = out_color * color;
}
}
)";
} // namespace
namespace openspace::rendering::helper {
namespace detail {
Shaders& gShadersConstructor() {
static Shaders g;
return g;
}
VertexObjects& gVertexObjectsConstructor() {
static VertexObjects g;
return g;
}
} // namespace detail
void initialize() {
ghoul_assert(!isInitialized, "Rendering Helper initialized twice");
//
// XYUVRGBA shader
//
xyuvrgbaVertexFile = absPath("${TEMPORARY}/xyuvrgba.vert");
{
std::fstream vertexFile;
vertexFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
vertexFile.open(xyuvrgbaVertexFile, std::fstream::out);
vertexFile << XyuvrgbaVertexCode;
vertexFile.close();
}
xyuvrgbaFragmentFile = absPath("${TEMPORARY}/xyuvrgba.frag");
{
std::fstream fragmentFile;
fragmentFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fragmentFile.open(xyuvrgbaFragmentFile, std::fstream::out);
fragmentFile << XyuvrgbaFragmentCode;
fragmentFile.close();
}
shaders.xyuvrgba.program = ghoul::opengl::ProgramObject::Build(
"xyuvrgba", xyuvrgbaVertexFile, xyuvrgbaFragmentFile);
ghoul::opengl::updateUniformLocations(*shaders.xyuvrgba.program,
shaders.xyuvrgba.cache,
{ "tex", "hasTexture", "shouldFlipTexture", "ortho", "color" });
//
// Screenfilling shader
//
screenFillingVertexFile = absPath("${TEMPORARY}/screenfilling.vert");
{
std::fstream vertexFile;
vertexFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
vertexFile.open(screenFillingVertexFile, std::fstream::out);
vertexFile << ScreenFillingQuadVertexCode;
vertexFile.close();
}
screenFillingFragmentFile = absPath("${TEMPORARY}/screenfilling.frag");
{
std::fstream fragmentFile;
fragmentFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fragmentFile.open(screenFillingFragmentFile, std::fstream::out);
fragmentFile << XyuvrgbaFragmentCode;
fragmentFile.close();
}
shaders.screenfilling.program = ghoul::opengl::ProgramObject::Build(
"screenfilling", xyuvrgbaVertexFile, xyuvrgbaFragmentFile);
ghoul::opengl::updateUniformLocations(*shaders.screenfilling.program,
shaders.screenfilling.cache,
{ "tex", "hasTexture", "shouldFlipTexture", "ortho", "color" });
//
// Square vertex objects
//
glGenVertexArrays(1, &vertexObjects.square.vao);
glGenBuffers(1, &vertexObjects.square.vbo);
glBindVertexArray(vertexObjects.square.vao);
glBindBuffer(GL_ARRAY_BUFFER, vertexObjects.square.vbo);
struct Vertex {
GLfloat xy[2];
GLfloat uv[2];
GLfloat rgba[4];
};
Vertex data[] = {
// X Y U V R G B A
-1.f, -1.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 0.f, 1.f, 1.f, 1.f, 1.f, 1.f,
1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f,
-1.f, -1.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f,
1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f,
1.f, -1.f, 1.f, 0.f, 1.f, 1.f, 1.f, 1.f
};
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(Vertex), data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(Vertex, uv)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(Vertex, rgba)));
glBindVertexArray(0);
//
// Empty vertex array objects
//
glGenVertexArrays(1, &vertexObjects.empty.vao);
isInitialized = true;
}
void deinitialize() {
ghoul_assert(isInitialized, "Rendering Helper not initialized");
if (!xyuvrgbaVertexFile.empty()) {
FileSys.deleteFile(xyuvrgbaVertexFile);
}
if (!xyuvrgbaFragmentFile.empty()) {
FileSys.deleteFile(xyuvrgbaFragmentFile);
}
shaders.xyuvrgba.program = nullptr;
if (!screenFillingVertexFile.empty()) {
FileSys.deleteFile(screenFillingVertexFile);
}
if (!screenFillingFragmentFile.empty()) {
FileSys.deleteFile(screenFillingVertexFile);
}
shaders.screenfilling.program = nullptr;
glDeleteVertexArrays(1, &vertexObjects.square.vao);
glDeleteBuffers(1, &vertexObjects.square.vbo);
glDeleteVertexArrays(1, &vertexObjects.empty.vao);
isInitialized = false;
}
glm::mat4 ortho(const glm::vec2& position, const glm::vec2& size, Anchor anchor) {
glm::ivec2 res = global::windowDelegate.currentDrawBufferResolution();
const float xSize = size.x;
const float ySize = size.y;
float xPos = (position.x - 0.5f) * 2.f;
float yPos = (1.f - position.y - 0.5f) * 2.f;
switch (anchor) {
case Anchor::Center:
break;
case Anchor::NW:
xPos += xSize;
yPos -= ySize;
break;
case Anchor::NE:
xPos -= xSize;
yPos -= ySize;
break;
case Anchor::SW:
xPos += xSize;
yPos += ySize;
break;
case Anchor::SE:
xPos -= xSize;
yPos += ySize;
break;
}
return glm::mat4(
xSize, 0.f, 0.f, 0.f,
0.f, ySize, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
xPos, yPos, 0.f, 1.f
);
}
void renderBox(ghoul::opengl::ProgramObject& program, GLint orthoLocation,
GLint colorLocation, const glm::vec2& position, const glm::vec2& size,
const glm::vec4& color, Anchor anchor)
{
program.setUniform(orthoLocation, ortho(position, size, anchor));
program.setUniform(colorLocation, color);
glBindVertexArray(vertexObjects.square.vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
void renderBox(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color,
Anchor anchor)
{
auto& shdr = shaders.xyuvrgba;
shdr.program->activate();
shdr.program->setUniform(shdr.cache.hasTexture, 0);
renderBox(*shdr.program, shdr.cache.ortho, shdr.cache.color, position, size, color,
anchor);
shdr.program->deactivate();
}
} // namespace openspace::rendering::helper

View File

@@ -37,6 +37,7 @@
#include <openspace/rendering/abufferrenderer.h>
#include <openspace/rendering/dashboard.h>
#include <openspace/rendering/deferredcastermanager.h>
#include <openspace/rendering/helper.h>
#include <openspace/rendering/framebufferrenderer.h>
#include <openspace/rendering/luaconsole.h>
#include <openspace/rendering/raycastermanager.h>
@@ -596,8 +597,13 @@ void RenderEngine::renderOverlays(const ShutdownInformation& shutdownInfo) {
}
void RenderEngine::renderEndscreen() {
glClearColor(0.f, 0.f, 0.f, 0.25f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
rendering::helper::renderBox(
glm::vec2(0.f, 0.f),
glm::vec2(1.f, 1.f),
glm::vec4(0.f, 0.f, 0.f, 0.5f)
);
using FR = ghoul::fontrendering::FontRenderer;
using BBox = FR::BoundingBoxInformation;