Add new font for the OnScreen GUI

Cleanup of GUI shader code
Make copy/paste work in Onscreen GUI
This commit is contained in:
Alexander Bock
2016-06-27 01:12:53 +02:00
parent 41ef7c1b55
commit a7f556a9c4
3 changed files with 180 additions and 154 deletions
+176 -150
View File
@@ -24,19 +24,14 @@
#include <modules/onscreengui/include/gui.h>
#include <ghoul/opengl/ghoul_gl.h>
#include <openspace/util/keys.h>
#include <openspace/engine/openspaceengine.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/filesystem/cachemanager.h>
#include <openspace/properties/property.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/rendering/screenspacerenderable.h>
#include <modules/base/rendering/screenspaceimage.h>
#include <openspace/util/keys.h>
#include <ghoul/filesystem/cachemanager.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/opengl/ghoul_gl.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/texture.h>
@@ -47,111 +42,126 @@
#include "gui_lua.inl"
namespace {
const std::string _loggerCat = "GUI";
const std::string configurationFile = "imgui.ini";
const ImVec2 size = ImVec2(350, 500);
//GLuint fontTex = 0;
GLint positionLocation = 0;
GLint uvLocation = 0;
GLint colorLocation = 0;
size_t vboMaxSize = 20000;
GLuint vao = 0;
GLuint vbo = 0;
std::unique_ptr<ghoul::opengl::ProgramObject> _program;
const std::string _loggerCat = "GUI";
const std::string configurationFile = "imgui.ini";
const ImVec2 size = ImVec2(350, 500);
std::unique_ptr<ghoul::opengl::Texture> _fontTexture;
//GLuint fontTex = 0;
GLint positionLocation = 0;
GLint uvLocation = 0;
GLint colorLocation = 0;
// A VBO max size of 0 will cause a lazy instantiation of the buffer
size_t vboMaxSize = 0;
GLuint vao = 0;
GLuint vbo = 0;
std::unique_ptr<ghoul::opengl::ProgramObject> _program;
std::unique_ptr<ghoul::opengl::Texture> _fontTexture;
char* iniFileBuffer = nullptr;
static void RenderDrawLists(ImDrawList** const commandLists, int nCommandLists) {
if (nCommandLists == 0)
return;
static void ImImpl_RenderDrawLists(ImDrawList** const commandLists, int nCommandLists) {
if (nCommandLists == 0)
return;
// Setup render state:
// alpha-blending enabled, no face culling, no depth testing, scissor enabled
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
ghoul::opengl::TextureUnit unit;
unit.activate();
_fontTexture->bind();
ghoul::opengl::TextureUnit unit;
unit.activate();
_fontTexture->bind();
//glBindTexture(GL_TEXTURE_2D, fontTex);
// Setup orthographic projection matrix
const float width = ImGui::GetIO().DisplaySize.x;
const float height = ImGui::GetIO().DisplaySize.y;
const glm::mat4 ortho(
2.f / width, 0.0f, 0.0f, 0.f,
0.0f, 2.0f / -height, 0.0f, 0.f,
0.0f, 0.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f
);
_program->activate();
// Setup orthographic projection matrix
const float width = ImGui::GetIO().DisplaySize.x;
const float height = ImGui::GetIO().DisplaySize.y;
const glm::mat4 ortho(
2.f / width, 0.0f, 0.0f, 0.f,
0.0f, 2.0f / -height, 0.0f, 0.f,
0.0f, 0.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f
);
_program->activate();
_program->setUniform("tex", unit);
_program->setUniform("ortho", ortho);
_program->setUniform("tex", unit);
_program->setUniform("ortho", ortho);
// Grow our buffer according to what we need
size_t total_vtx_count = 0;
for (int n = 0; n < nCommandLists; n++)
total_vtx_count += commandLists[n]->vtx_buffer.size();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// @BUGFIX @HACK The imgui crashes on complex scenes without the * 2 ---abock
size_t neededBufferSize = total_vtx_count * sizeof(ImDrawVert) * 2;
if (neededBufferSize > vboMaxSize) {
vboMaxSize = neededBufferSize + 5000; // Grow buffer
glBufferData(GL_ARRAY_BUFFER, neededBufferSize, NULL, GL_STREAM_DRAW);
}
// Grow our buffer according to what we need
size_t totalVertexCount = 0;
for (int i = 0; i < nCommandLists; ++i)
totalVertexCount += commandLists[i]->vtx_buffer.size();
// Copy and convert all vertices into a single contiguous buffer
unsigned char* buffer_data = (unsigned char*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (!buffer_data)
return;
for (int n = 0; n < nCommandLists; ++n) {
const ImDrawList* cmd_list = commandLists[n];
memcpy(buffer_data, &cmd_list->vtx_buffer[0], cmd_list->vtx_buffer.size() * sizeof(ImDrawVert));
buffer_data += cmd_list->vtx_buffer.size() * sizeof(ImDrawVert);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(vao);
int cmd_offset = 0;
for (int n = 0; n < nCommandLists; ++n) {
const ImDrawList* cmd_list = commandLists[n];
int vtx_offset = cmd_offset;
for (auto pcmd : cmd_list->commands) {
glScissor((int)pcmd.clip_rect.x, (int)(height - pcmd.clip_rect.w), (int)(pcmd.clip_rect.z - pcmd.clip_rect.x), (int)(pcmd.clip_rect.w - pcmd.clip_rect.y));
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd.vtx_count);
vtx_offset += pcmd.vtx_count;
}
cmd_offset = vtx_offset;
}
glBindVertexArray(0);
_program->deactivate();
glDisable(GL_SCISSOR_TEST);
glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
size_t neededBufferSize = totalVertexCount * sizeof(ImDrawVert);
if (neededBufferSize > vboMaxSize) {
// Grow buffer
vboMaxSize = neededBufferSize * 1.25f;
glBufferData(GL_ARRAY_BUFFER, vboMaxSize, NULL, GL_STREAM_DRAW);
}
}
namespace openspace {
// Copy and convert all vertices into a single contiguous buffer
unsigned char* bufferData = reinterpret_cast<unsigned char*>(
glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY)
);
void addScreenSpaceRenderable(std::string texturePath){
std::string filepath ="${OPENSPACE_DATA}/"+texturePath;
if( ! FileSys.fileExists(filepath)) {
LWARNING("Could not find image '" << filepath << "'");
if (!bufferData) {
LFATAL("Error mapping ImGui buffer");
return;
}
std::string luaTable = "{Type = 'ScreenSpaceImage', TexturePath = '+" + filepath + " ' }";
std::string script = "openspace.registerScreenSpaceRenderable(" + luaTable + ");";
OsEng.scriptEngine().queueScript(script);
// OsEng.renderEngine().registerScreenSpaceRenderable(std::make_shared<ScreenSpaceImage>(filepath));
for (int i = 0; i < nCommandLists; ++i) {
const ImDrawList* cmd_list = commandLists[i];
memcpy(
bufferData,
&cmd_list->vtx_buffer[0],
cmd_list->vtx_buffer.size() * sizeof(ImDrawVert)
);
bufferData += (cmd_list->vtx_buffer.size() * sizeof(ImDrawVert));
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(vao);
int cmdOffset = 0;
for (int i = 0; i < nCommandLists; ++i) {
const ImDrawList* cmd_list = commandLists[i];
int vtxOffset = cmdOffset;
for (const auto& pcmd : cmd_list->commands) {
glScissor(
static_cast<int>(pcmd.clip_rect.x),
static_cast<int>(height - pcmd.clip_rect.w),
static_cast<int>(pcmd.clip_rect.z - pcmd.clip_rect.x),
static_cast<int>(pcmd.clip_rect.w - pcmd.clip_rect.y)
);
glDrawArrays(GL_TRIANGLES, vtxOffset, pcmd.vtx_count);
vtxOffset += pcmd.vtx_count;
}
cmdOffset = vtxOffset;
}
glBindVertexArray(0);
_program->deactivate();
glDisable(GL_SCISSOR_TEST);
}
void addScreenSpaceRenderable(std::string texturePath) {
if (!FileSys.fileExists(texturePath)) {
LWARNING("Could not find image '" << texturePath << "'");
return;
}
std::string luaTable =
"{Type = 'ScreenSpaceImage', TexturePath = '+" + absPath(texturePath) + " ' }";
std::string script = "openspace.registerScreenSpaceRenderable(" + luaTable + ");";
OsEng.scriptEngine().queueScript(script);
}
} // namespace
namespace openspace {
namespace gui {
GUI::GUI()
@@ -168,17 +178,16 @@ void GUI::initialize() {
configurationFile, "", ghoul::filesystem::CacheManager::Persistent::Yes
);
char* buffer = new char[cachedFile.size() + 1];
iniFileBuffer = new char[cachedFile.size() + 1];
#ifdef WIN32
strcpy_s(buffer, cachedFile.size() + 1, cachedFile.c_str());
strcpy_s(iniFileBuffer, cachedFile.size() + 1, cachedFile.c_str());
#else
strcpy(buffer, cachedFile.c_str());
strcpy(iniFileBuffer, cachedFile.c_str());
#endif
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = buffer;
//io.IniSavingRate = 5.f;
io.IniFilename = iniFileBuffer;
io.DeltaTime = 1.f / 60.f;
io.KeyMap[ImGuiKey_Tab] = static_cast<int>(Key::Tab);
io.KeyMap[ImGuiKey_LeftArrow] = static_cast<int>(Key::Left);
@@ -198,9 +207,13 @@ void GUI::initialize() {
io.KeyMap[ImGuiKey_Y] = static_cast<int>(Key::Y);
io.KeyMap[ImGuiKey_Z] = static_cast<int>(Key::Z);
io.RenderDrawListsFn = ImImpl_RenderDrawLists;
//io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; // @TODO implement? ---abock
//io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; // @TODO implement? ---abock
io.RenderDrawListsFn = RenderDrawLists;
//io.SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; // @TODO implement? ---abock
//io.GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // @TODO implement? ---abock
io.Fonts->AddFontFromFileTTF(
absPath("${FONTS}/Roboto/Roboto-Regular.ttf").c_str(),
16.f
);
ImGuiStyle& style = ImGui::GetStyle();
style.WindowPadding = { 4.f, 4.f };
@@ -210,26 +223,26 @@ void GUI::initialize() {
style.ScrollbarWidth = 15.f;
style.ScrollbarRounding = 0.f;
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.21f, 0.24f, 1.00f);
style.Colors[ImGuiCol_Border] = ImVec4(0.10f, 0.39f, 0.42f, 0.59f);
style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.50f, 0.94f, 1.00f, 0.45f);
style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.50f, 0.94f, 1.00f, 0.45f);
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.12f, 0.71f, 0.80f, 0.43f);
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.75f, 0.80f, 0.65f);
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.40f, 0.75f, 0.80f, 0.65f);
style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.50f, 0.80f, 0.76f, 1.00f);
style.Colors[ImGuiCol_Button] = ImVec4(0.00f, 0.36f, 0.67f, 0.60f);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.00f, 0.51f, 0.94f, 1.00f);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.00f, 0.43f, 0.80f, 1.00f);
style.Colors[ImGuiCol_Header] = ImVec4(0.34f, 0.50f, 0.90f, 0.45f);
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.34f, 0.50f, 0.90f, 0.80f);
style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.53f, 0.63f, 0.87f, 0.80f);
style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
style.Colors[ImGuiCol_CloseButton] = ImVec4(0.56f, 0.17f, 0.17f, 1.00f);
style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(1.00f, 0.29f, 0.29f, 0.60f);
style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(1.00f, 0.29f, 0.29f, 1.00f);
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.0f, 0.21f, 0.24f, 1.0f);
style.Colors[ImGuiCol_Border] = ImVec4(0.1f, 0.39f, 0.42f, 0.59f);
style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.5f, 0.94f, 1.0f, 0.45f);
style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.5f, 0.94f, 1.0f, 0.45f);
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.12f, 0.71f, 0.8f, 0.43f);
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.4f, 0.75f, 0.8f, 0.65f);
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.4f, 0.75f, 0.8f, 0.65f);
style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.5f, 0.8f, 0.76f, 1.0f);
style.Colors[ImGuiCol_Button] = ImVec4(0.0f, 0.36f, 0.67f, 0.6f);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.0f, 0.51f, 0.94f, 1.0f);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.0f, 0.43f, 0.8f, 1.0f);
style.Colors[ImGuiCol_Header] = ImVec4(0.34f, 0.5f, 0.9f, 0.45f);
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.34f, 0.5f, 0.9f, 0.8f);
style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.53f, 0.63f, 0.87f, 0.8f);
style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
style.Colors[ImGuiCol_CloseButton] = ImVec4(0.75f, 0.75f, 0.75f, 1.0f);
style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.52f, 0.52f, 0.52f, 0.6f);
style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.52f, 0.52f, 0.52f, 1.0f);
_property.initialize();
_screenSpaceProperty.initialize();
@@ -237,10 +250,11 @@ void GUI::initialize() {
_performance.initialize();
_help.initialize();
_iswa.initialize();
}
void GUI::deinitialize() {
delete iniFileBuffer;
}
void GUI::initializeGL() {
@@ -255,18 +269,10 @@ void GUI::initializeGL() {
positionLocation = glGetAttribLocation(*_program, "in_position");
uvLocation = glGetAttribLocation(*_program, "in_uv");
colorLocation = glGetAttribLocation(*_program, "in_color");
//glGenTextures(1, &fontTex);
//glBindTexture(GL_TEXTURE_2D, fontTex);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
unsigned char* png_data;
int tex_x, tex_y;
ImGui::GetIO().Fonts->GetTexDataAsRGBA32(&png_data, &tex_x, &tex_y);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_x, tex_y, 0, GL_RGBA, GL_UNSIGNED_BYTE, png_data);
//stbi_image_free(tex_data);
_fontTexture = std::make_unique<ghoul::opengl::Texture>(
png_data,
@@ -276,11 +282,9 @@ void GUI::initializeGL() {
_fontTexture->setDataOwnership(ghoul::opengl::Texture::TakeOwnership::No);
_fontTexture->uploadTexture();
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vboMaxSize, NULL, GL_DYNAMIC_DRAW);
glGenVertexArrays(1, &vao);
@@ -290,12 +294,32 @@ void GUI::initializeGL() {
glEnableVertexAttribArray(uvLocation);
glEnableVertexAttribArray(colorLocation);
glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)offsetof(ImDrawVert, pos));
glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)offsetof(ImDrawVert, uv));
glVertexAttribPointer(colorLocation, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)offsetof(ImDrawVert, col));
glVertexAttribPointer(
positionLocation,
2,
GL_FLOAT,
GL_FALSE,
sizeof(ImDrawVert),
reinterpret_cast<GLvoid*>(offsetof(ImDrawVert, pos))
);
glVertexAttribPointer(
uvLocation,
2,
GL_FLOAT,
GL_FALSE,
sizeof(ImDrawVert),
reinterpret_cast<GLvoid*>(offsetof(ImDrawVert, uv))
);
glVertexAttribPointer(
colorLocation,
4,
GL_UNSIGNED_BYTE,
GL_TRUE,
sizeof(ImDrawVert),
reinterpret_cast<GLvoid*>(offsetof(ImDrawVert, col))
);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
_property.initializeGL();
_screenSpaceProperty.initializeGL();
@@ -324,18 +348,15 @@ void GUI::startFrame(float deltaTime, const glm::vec2& windowSize,
const glm::vec2& mousePos,
uint32_t mouseButtonsPressed)
{
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(windowSize.x, windowSize.y);
io.DeltaTime = deltaTime;
io.MousePos = ImVec2(mousePos.x * mousePosCorrectionFactor.x, mousePos.y * mousePosCorrectionFactor.y);
io.MousePos = ImVec2(
mousePos.x * mousePosCorrectionFactor.x,
mousePos.y * mousePosCorrectionFactor.y
);
//#ifdef __APPLE__
// io.MousePos = ImVec2(mousePos.x * 2, mousePos.y * 2);
//#else
// io.MousePos = ImVec2(mousePos.x, mousePos.y);
//#endif
io.MouseDown[0] = mouseButtonsPressed & (1 << 0);
io.MouseDown[1] = mouseButtonsPressed & (1 << 1);
@@ -362,7 +383,6 @@ void GUI::endFrame() {
}
bool GUI::mouseButtonCallback(MouseButton button, MouseAction action) {
//bool GUI::mouseButtonCallback(int key, int action) {
ImGuiIO& io = ImGui::GetIO();
bool consumeEvent = io.WantCaptureMouse;
return consumeEvent;
@@ -371,8 +391,9 @@ bool GUI::mouseButtonCallback(MouseButton button, MouseAction action) {
bool GUI::mouseWheelCallback(double position) {
ImGuiIO& io = ImGui::GetIO();
bool consumeEvent = io.WantCaptureMouse;
if (consumeEvent)
if (consumeEvent) {
io.MouseWheel = static_cast<float>(position);
}
return consumeEvent;
}
@@ -390,6 +411,10 @@ bool GUI::keyCallback(Key key, KeyModifier modifier, KeyAction action) {
if (action == KeyAction::Release)
io.KeysDown[keyIndex] = false;
}
io.KeyShift = hasKeyModifier(modifier, KeyModifier::Shift);
io.KeyCtrl = hasKeyModifier(modifier, KeyModifier::Control);
io.KeyAlt = hasKeyModifier(modifier, KeyModifier::Alt);
}
return consumeEvent;
}
@@ -454,9 +479,11 @@ void GUI::render() {
addScreenSpaceRenderable(std::string(addImageBuffer));
}
#if 0
ImGui::Begin("Style Editor");
ImGui::ShowStyleEditor();
ImGui::End();
#endif
ImGui::End();
}
@@ -487,6 +514,5 @@ scripting::ScriptEngine::LuaLibrary GUI::luaLibrary() {
};
}
} // namespace gui
} // namespace openspace