Allow unique input to the GUI

Move GUI from RenderEngine to OpenSpace engine
This commit is contained in:
Alexander Bock
2014-12-06 12:33:06 +01:00
parent a253ca3441
commit 9f7e1581de
7 changed files with 143 additions and 30 deletions
+6 -1
View File
@@ -38,7 +38,12 @@ public:
void initializeGL();
void deinitializeGL();
void render(float deltaTime, const glm::vec2& mousePos, bool mouseButtonsPressed[2]);
bool mouseButtonCallback(int key, int action);
bool keyCallback(int key, int action);
bool charCallback(unsigned int character);
void startFrame(float deltaTime, const glm::vec2& mousePos, bool mouseButtonsPressed[2]);
void endFrame();
};
} // namespace openspace
+2 -1
View File
@@ -34,7 +34,7 @@
namespace openspace {
// Forward declare to minimize dependencies
class GUI;
class SyncBuffer;
class LuaConsole;
@@ -95,6 +95,7 @@ private:
scripting::ScriptEngine _scriptEngine;
ghoul::cmdparser::CommandlineParser _commandlineParser;
LuaConsole _console;
GUI* _gui;
SyncBuffer* _syncBuffer;
@@ -36,7 +36,6 @@ class SceneGraph;
class ABuffer;
class ABufferVisualizer;
class ScreenLog;
class GUI;
class RenderEngine {
public:
@@ -79,7 +78,6 @@ private:
SceneGraph* _sceneGraph;
ABuffer* _abuffer;
ScreenLog* _log;
GUI* _gui;
bool _showInfo;
bool _showScreenLog;
+100 -1
View File
@@ -36,7 +36,12 @@ namespace {
GLuint fontTex = 0;
GLint shader_handle = 0;
GLint vert_handle = 0;
GLint frag_handle = 0;
GLint texture_location = 0;
GLint position_location = 0;
GLint uv_location = 0;
GLint colour_location = 0;
GLint ortho_location = 0;
GLuint vbo_handle = 0;
size_t vbo_max_size = 20000;
@@ -158,6 +163,49 @@ GUI::~GUI() {
}
void GUI::initializeGL() {
const GLchar *vertex_shader =
"#version 330\n"
"uniform mat4 ortho;\n"
"in vec2 Position;\n"
"in vec2 UV;\n"
"in vec4 Colour;\n"
"out vec2 Frag_UV;\n"
"out vec4 Frag_Colour;\n"
"void main()\n"
"{\n"
" Frag_UV = UV;\n"
" Frag_Colour = Colour;\n"
" gl_Position = ortho*vec4(Position.xy,0,1);\n"
"}\n";
const GLchar* fragment_shader =
"#version 330\n"
"uniform sampler2D Texture;\n"
"in vec2 Frag_UV;\n"
"in vec4 Frag_Colour;\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = Frag_Colour * texture( Texture, Frag_UV.st);\n"
"}\n";
shader_handle = glCreateProgram();
vert_handle = glCreateShader(GL_VERTEX_SHADER);
frag_handle = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vert_handle, 1, &vertex_shader, 0);
glShaderSource(frag_handle, 1, &fragment_shader, 0);
glCompileShader(vert_handle);
glCompileShader(frag_handle);
glAttachShader(shader_handle, vert_handle);
glAttachShader(shader_handle, frag_handle);
glLinkProgram(shader_handle);
texture_location = glGetUniformLocation(shader_handle, "Texture");
ortho_location = glGetUniformLocation(shader_handle, "ortho");
position_location = glGetAttribLocation(shader_handle, "Position");
uv_location = glGetAttribLocation(shader_handle, "UV");
colour_location = glGetAttribLocation(shader_handle, "Colour");
glGenTextures(1, &fontTex);
glBindTexture(GL_TEXTURE_2D, fontTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@@ -169,6 +217,23 @@ void GUI::initializeGL() {
void* tex_data = stbi_load_from_memory((const unsigned char*)png_data, (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_x, tex_y, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data);
stbi_image_free(tex_data);
glGenBuffers(1, &vbo_handle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_handle);
glBufferData(GL_ARRAY_BUFFER, vbo_max_size, NULL, GL_DYNAMIC_DRAW);
glGenVertexArrays(1, &vao_handle);
glBindVertexArray(vao_handle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_handle);
glEnableVertexAttribArray(position_location);
glEnableVertexAttribArray(uv_location);
glEnableVertexAttribArray(colour_location);
glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*) offsetof(ImDrawVert, pos));
glVertexAttribPointer(uv_location, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*) offsetof(ImDrawVert, uv));
glVertexAttribPointer(colour_location, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*) offsetof(ImDrawVert, col));
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void GUI::deinitializeGL() {
@@ -177,7 +242,7 @@ void GUI::deinitializeGL() {
glDeleteProgram(shader_handle);
}
void GUI::render(float deltaTime,
void GUI::startFrame(float deltaTime,
const glm::vec2& mousePos,
bool mouseButtonsPressed[2])
{
@@ -188,6 +253,11 @@ void GUI::render(float deltaTime,
io.MouseDown[1] = mouseButtonsPressed[1];
ImGui::NewFrame();
}
void GUI::endFrame()
{
static bool show = true;
//if (show) {
ImGui::ShowTestWindow(&show);
@@ -196,4 +266,33 @@ void GUI::render(float deltaTime,
ImGui::Render();
}
bool GUI::mouseButtonCallback(int key, int action) {
ImGuiIO& io = ImGui::GetIO();
bool consumeEvent = io.WantCaptureMouse;
return consumeEvent;
}
bool GUI::keyCallback(int key, int action) {
ImGuiIO& io = ImGui::GetIO();
bool consumeEvent = io.WantCaptureKeyboard;
if (consumeEvent) {
if (action == SGCT_PRESS)
io.KeysDown[key] = true;
if (action == SGCT_RELEASE)
io.KeysDown[key] = false;
}
return consumeEvent;
}
bool GUI::charCallback(unsigned int character) {
ImGuiIO& io = ImGui::GetIO();
bool consumeEvent = io.WantCaptureKeyboard;
if (consumeEvent) {
io.AddInputCharacter((unsigned short)character);
}
return consumeEvent;
}
} // namespace openspace
+34 -7
View File
@@ -24,7 +24,7 @@
#include <openspace/engine/openspaceengine.h>
// openspace
#include <openspace/engine/gui.h>
#include <openspace/engine/logfactory.h>
#include <openspace/interaction/interactionhandler.h>
#include <openspace/interaction/interactionhandler.h>
@@ -83,6 +83,7 @@ OpenSpaceEngine* OpenSpaceEngine::_engine = nullptr;
OpenSpaceEngine::OpenSpaceEngine(std::string programName)
: _commandlineParser(programName, true)
, _gui(nullptr)
, _syncBuffer(nullptr)
{
// initialize OpenSpace helpers
@@ -473,9 +474,14 @@ LuaConsole& OpenSpaceEngine::console() {
return _console;
}
bool OpenSpaceEngine::initializeGL()
{
return _renderEngine.initializeGL();
bool OpenSpaceEngine::initializeGL() {
bool success = _renderEngine.initializeGL();
int x,y;
sgct::Engine::instance()->getWindowPtr(0)->getFinalFBODimensions(x, y);
_gui = new GUI(glm::vec2(glm::ivec2(x,y)));
_gui->initializeGL();
return success;
}
void OpenSpaceEngine::preSynchronization() {
@@ -492,6 +498,16 @@ void OpenSpaceEngine::preSynchronization() {
void OpenSpaceEngine::postSynchronizationPreDraw() {
_renderEngine.postSynchronizationPreDraw();
double posX, posY;
sgct::Engine::instance()->getMousePos(0, &posX, &posY);
int button0 = sgct::Engine::instance()->getMouseButton(0, 0);
int button1 = sgct::Engine::instance()->getMouseButton(0, 1);
bool buttons[2] = { button0 != 0, button1 != 0 };
double dt = std::max(sgct::Engine::instance()->getDt(), 1.0/60.0);
_gui->startFrame(dt, glm::vec2(posX, posY), buttons);
}
void OpenSpaceEngine::render() {
@@ -502,9 +518,9 @@ void OpenSpaceEngine::render() {
if (sgct::Engine::instance()->isMaster() && !w->isUsingFisheyeRendering() && _console.isVisible()) {
_console.render();
}
_gui->endFrame();
}
void OpenSpaceEngine::postDraw() {
if (sgct::Engine::instance()->isMaster())
_interactionHandler.unlockControls();
@@ -514,6 +530,10 @@ void OpenSpaceEngine::postDraw() {
void OpenSpaceEngine::keyboardCallback(int key, int action) {
if (sgct::Engine::instance()->isMaster()) {
bool isConsumed = _gui->keyCallback(key, action);
if (isConsumed)
return;
if (key == _console.commandInputButton() && (action == SGCT_PRESS || action == SGCT_REPEAT))
_console.toggleVisibility();
@@ -527,14 +547,21 @@ void OpenSpaceEngine::keyboardCallback(int key, int action) {
}
void OpenSpaceEngine::charCallback(unsigned int codepoint) {
bool isConsumed = _gui->charCallback(codepoint);
if (isConsumed)
return;
if (_console.isVisible()) {
_console.charCallback(codepoint);
}
}
void OpenSpaceEngine::mouseButtonCallback(int key, int action) {
if (sgct::Engine::instance()->isMaster())
_interactionHandler.mouseButtonCallback(key, action);
bool isConsumed = _gui->mouseButtonCallback(key, action);
if (isConsumed)
return;
_interactionHandler.mouseButtonCallback(key, action);
}
void OpenSpaceEngine::mousePositionCallback(int x, int y) {
-17
View File
@@ -30,7 +30,6 @@
#include <openspace/abuffer/abufferSingleLinked.h>
#include <openspace/abuffer/abufferfixed.h>
#include <openspace/abuffer/abufferdynamic.h>
#include <openspace/engine/gui.h>
#include <openspace/engine/openspaceengine.h>
#include <openspace/scenegraph/scenegraph.h>
#include <openspace/util/camera.h>
@@ -112,7 +111,6 @@ RenderEngine::RenderEngine()
, _sceneGraph(nullptr)
, _abuffer(nullptr)
, _log(nullptr)
, _gui(nullptr)
, _showInfo(true)
, _showScreenLog(true)
, _takeScreenshot(false)
@@ -244,11 +242,6 @@ bool RenderEngine::initializeGL()
_visualizer = new ABufferVisualizer();
int x,y;
sgct::Engine::instance()->getActiveViewportSize(x, y);
_gui = new GUI(glm::vec2(glm::ivec2(x,y)));
_gui->initializeGL();
// successful init
return true;
}
@@ -338,16 +331,6 @@ void RenderEngine::render()
else {
_visualizer->render();
}
double posX, posY;
sgct::Engine::instance()->getMousePos(0, &posX, &posY);
int button0 = sgct::Engine::instance()->getMouseButton(0, 0);
int button1 = sgct::Engine::instance()->getMouseButton(0, 1);
bool buttons[2] = { button0 != 0, button1 != 0 };
double dt = std::max(sgct::Engine::instance()->getDt(), 1.0/60.0);
_gui->render(dt, glm::vec2(posX, posY), buttons);
#if 1