55 ImGui::CreateContext();
58 ImGui::StyleColorsDark();
59 ImGuiIO& io = ImGui::GetIO();
60 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
85 ImGui::DestroyContext();
111 ImGuiIO& io = ImGui::GetIO();
112 io.IniSavingRate = 1.0f;
113 io.IniFilename = NULL;
114 io.KeyMap[ImGuiKey_Tab] =
K_tab;
115 io.KeyMap[ImGuiKey_LeftArrow] =
K_left;
116 io.KeyMap[ImGuiKey_RightArrow] =
K_right;
117 io.KeyMap[ImGuiKey_UpArrow] =
K_up;
118 io.KeyMap[ImGuiKey_DownArrow] =
K_down;
119 io.KeyMap[ImGuiKey_PageUp] =
K_pageUp;
120 io.KeyMap[ImGuiKey_PageDown] =
K_pageUp;
121 io.KeyMap[ImGuiKey_Home] =
K_home;
122 io.KeyMap[ImGuiKey_End] =
K_end;
123 io.KeyMap[ImGuiKey_Delete] =
K_delete;
125 io.KeyMap[ImGuiKey_Enter] =
K_enter;
126 io.KeyMap[ImGuiKey_Escape] =
K_esc;
127 io.KeyMap[ImGuiKey_A] =
'A';
128 io.KeyMap[ImGuiKey_C] =
'C';
129 io.KeyMap[ImGuiKey_V] =
'V';
130 io.KeyMap[ImGuiKey_X] =
'X';
131 io.KeyMap[ImGuiKey_Y] =
'Y';
132 io.KeyMap[ImGuiKey_Z] =
'Z';
135 io.DisplaySize = ImVec2(0, 0);
136 io.DisplayFramebufferScale = ImVec2(1, 1);
138 #if defined(SL_OS_ANDROID) || defined(SL_OS_MACIOS) || defined(SL_EMSCRIPTEN)
139 io.MouseDrawCursor =
false;
141 io.MouseDrawCursor =
true;
145 ImGuiStyle& style = ImGui::GetStyle();
146 style.FrameBorderSize = 1;
149 SLstring iniFile = configPath +
"imgui.ini";
154 ImGui::LoadIniSettingsFromMemory(iniContents.c_str(),
166 ImGuiIO& io = ImGui::GetIO();
174 io.Fonts->AddFontFromMemoryTTF(fontDataProp.
data,
175 static_cast<int>(fontDataProp.
size),
179 io.Fonts->AddFontFromMemoryTTF(fontDataFixed.
data,
180 static_cast<int>(fontDataFixed.
size),
181 fontFixedDotsToLoad);
191 GLint last_texture = -1, last_array_buffer = -1, last_vertex_array = -1;
192 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
193 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
194 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
199 SLstring vertex_shader =
"#version " + verGLSL;
200 if (state->
glIsES3()) vertex_shader +=
" es";
204 "precision highp float;\n"
207 "uniform mat4 ProjMtx;\n"
208 "in vec2 Position;\n"
211 "out vec2 Frag_UV;\n"
212 "out vec4 Frag_Color;\n"
216 " Frag_Color = Color;\n"
217 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
220 SLstring fragment_shader =
"#version " + verGLSL;
221 if (state->
glIsES3()) fragment_shader +=
" es";
225 "precision highp float;\n"
228 "uniform sampler2D Texture;\n"
230 "in vec4 Frag_Color;\n"
231 "out vec4 Out_Color;\n"
234 " Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n"
239 const char* srcVert = vertex_shader.c_str();
240 const char* srcFrag = fragment_shader.c_str();
278 (GLvoid*)offsetof(ImDrawVert, pos));
284 (GLvoid*)offsetof(ImDrawVert, uv));
290 (GLvoid*)offsetof(ImDrawVert, col));
295 ImGuiIO& io = ImGui::GetIO();
297 int width = -1, height = -1;
304 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
307 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
310 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
311 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
312 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
320 glBindTexture(GL_TEXTURE_2D, (
SLuint)last_texture);
323 glBindTexture(GL_TEXTURE_2D, (
SLuint)last_texture);
324 glBindBuffer(GL_ARRAY_BUFFER, (
SLuint)last_array_buffer);
325 glBindVertexArray((
SLuint)last_vertex_array);
373 ImGui::GetIO().Fonts->TexID =
nullptr;
383 SLint compileSuccess = 0;
384 glGetShaderiv((
SLuint)shaderHandle, GL_COMPILE_STATUS, &compileSuccess);
385 if (compileSuccess == GL_FALSE)
388 glGetShaderInfoLog((
SLuint)shaderHandle,
392 SL_LOG(
"*** COMPILER ERROR ***");
411 ImGuiIO& io = ImGui::GetIO();
416 if (io.DeltaTime < 0) io.DeltaTime = 1.0f / 60.0f;
426 if (ImGui::GetIO().WantSaveIniSettings)
429 size_t iniContentsSize;
430 const char* rawIniContents = ImGui::SaveIniSettingsToMemory(&iniContentsSize);
431 SLstring iniContents(rawIniContents, rawIniContents + iniContentsSize);
433 ImGui::GetIO().WantSaveIniSettings =
false;
447 ImGuiIO& io = ImGui::GetIO();
449 io.DisplayFramebufferScale = ImVec2(1, 1);
456 ImDrawData* draw_data = ImGui::GetDrawData();
458 ImGuiIO& io = ImGui::GetIO();
462 int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
463 int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
464 if (fb_width == 0 || fb_height == 0)
return;
465 draw_data->ScaleClipRects(io.DisplayFramebufferScale);
468 GLint last_active_texture = -1;
469 glGetIntegerv(GL_ACTIVE_TEXTURE, &last_active_texture);
470 glActiveTexture(GL_TEXTURE0);
472 GLint last_program = -1;
473 glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
474 GLint last_texture = -1;
475 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
476 GLint last_array_buffer = -1;
477 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
478 GLint last_element_array_buffer = -1;
479 glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
480 GLint last_vertex_array = -1;
481 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
482 GLint last_blend_src_rgb = -1;
483 glGetIntegerv(GL_BLEND_SRC_RGB, &last_blend_src_rgb);
484 GLint last_blend_dst_rgb = -1;
485 glGetIntegerv(GL_BLEND_DST_RGB, &last_blend_dst_rgb);
486 GLint last_blend_src_alpha = -1;
487 glGetIntegerv(GL_BLEND_SRC_ALPHA, &last_blend_src_alpha);
488 GLint last_blend_dst_alpha = -1;
489 glGetIntegerv(GL_BLEND_DST_ALPHA, &last_blend_dst_alpha);
490 GLint last_blend_equation_rgb = -1;
491 glGetIntegerv(GL_BLEND_EQUATION_RGB, &last_blend_equation_rgb);
492 GLint last_blend_equation_alpha = -1;
493 glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &last_blend_equation_alpha);
494 GLint last_viewport[4];
495 glGetIntegerv(GL_VIEWPORT, last_viewport);
496 GLint last_scissor_box[4];
497 glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
499 GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
500 GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
501 GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
502 GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
506 glBlendEquation(GL_FUNC_ADD);
507 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
508 glDisable(GL_CULL_FACE);
509 glDisable(GL_DEPTH_TEST);
510 glEnable(GL_SCISSOR_TEST);
519 glViewport((GLsizei)(viewportRect.
x * io.DisplayFramebufferScale.x),
520 (GLsizei)(viewportRect.
y * io.DisplayFramebufferScale.y),
521 (GLsizei)(viewportRect.
width * io.DisplayFramebufferScale.x),
522 (GLsizei)(viewportRect.
height * io.DisplayFramebufferScale.y));
526 const float ortho_projection[4][4] =
528 {2.0f / io.DisplaySize.x, 0.0f, 0.0f, 0.0f},
529 {0.0f, 2.0f / -io.DisplaySize.y, 0.0f, 0.0f},
530 {0.0f, 0.0f, -1.0f, 0.0f},
531 {-1.0f, 1.0f, 0.0f, 1.0f},
540 for (
int n = 0; n < draw_data->CmdListsCount; n++)
542 const ImDrawList* cmd_list = draw_data->CmdLists[n];
543 const ImDrawIdx* idx_buffer_offset =
nullptr;
546 glBufferData(GL_ARRAY_BUFFER,
547 (GLsizeiptr)cmd_list->VtxBuffer.Size * (GLsizeiptr)
sizeof(ImDrawVert),
548 (
const GLvoid*)cmd_list->VtxBuffer.Data,
552 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
553 (GLsizeiptr)cmd_list->IdxBuffer.Size * (GLsizeiptr)
sizeof(ImDrawIdx),
554 (
const GLvoid*)cmd_list->IdxBuffer.Data,
557 for (
int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
559 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
560 if (pcmd->UserCallback)
562 pcmd->UserCallback(cmd_list, pcmd);
566 glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
569 glScissor((
int)pcmd->ClipRect.x,
570 (
int)(fb_height - pcmd->ClipRect.w),
571 (
int)(pcmd->ClipRect.z - pcmd->ClipRect.x),
572 (
int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
574 glScissor((GLsizei)(viewportRect.
x * io.DisplayFramebufferScale.x),
575 (GLsizei)(viewportRect.
y * io.DisplayFramebufferScale.y),
576 (GLsizei)(viewportRect.
width * io.DisplayFramebufferScale.x),
577 (GLsizei)(viewportRect.
height * io.DisplayFramebufferScale.y));
579 glDrawElements(GL_TRIANGLES,
580 (GLsizei)pcmd->ElemCount,
581 sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
584 idx_buffer_offset += pcmd->ElemCount;
589 glUseProgram((
SLuint)last_program);
590 glBindTexture(GL_TEXTURE_2D, (
SLuint)last_texture);
591 glActiveTexture((
SLuint)last_active_texture);
592 glBindVertexArray((
SLuint)last_vertex_array);
593 glBindBuffer(GL_ARRAY_BUFFER, (
SLuint)last_array_buffer);
594 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (
SLuint)last_element_array_buffer);
595 glBlendEquationSeparate((
SLuint)last_blend_equation_rgb,
596 (
SLuint)last_blend_equation_alpha);
597 glBlendFuncSeparate((
SLuint)last_blend_src_rgb,
598 (
SLuint)last_blend_dst_rgb,
599 (
SLuint)last_blend_src_alpha,
600 (
SLuint)last_blend_dst_alpha);
601 if (last_enable_blend)
606 if (last_enable_cull_face)
607 glEnable(GL_CULL_FACE);
609 glDisable(GL_CULL_FACE);
611 if (last_enable_depth_test)
612 glEnable(GL_DEPTH_TEST);
614 glDisable(GL_DEPTH_TEST);
616 if (last_enable_scissor_test)
617 glEnable(GL_SCISSOR_TEST);
619 glDisable(GL_SCISSOR_TEST);
621 glViewport(last_viewport[0],
623 (GLsizei)last_viewport[2],
624 (GLsizei)last_viewport[3]);
626 glScissor(last_scissor_box[0],
628 (GLsizei)last_scissor_box[2],
629 (GLsizei)last_scissor_box[3]);
635 ImGuiIO& io = ImGui::GetIO();
637 if (button ==
MB_left) io.MouseDown[0] =
true;
638 if (button ==
MB_right) io.MouseDown[1] =
true;
639 if (button ==
MB_middle) io.MouseDown[2] =
true;
646 ImGuiIO& io = ImGui::GetIO();
648 if (button ==
MB_left) io.MouseDown[0] =
false;
649 if (button ==
MB_right) io.MouseDown[1] =
false;
650 if (button ==
MB_middle) io.MouseDown[2] =
false;
671 ImGuiIO& io = ImGui::GetIO();
672 io.KeysDown[key] =
true;
673 io.KeyCtrl =
mod &
K_ctrl ? true :
false;
675 io.KeyAlt =
mod &
K_alt ? true :
false;
681 ImGuiIO& io = ImGui::GetIO();
682 io.KeysDown[key] =
false;
683 io.KeyCtrl =
mod &
K_ctrl ? true :
false;
685 io.KeyAlt =
mod &
K_alt ? true :
false;
691 ImGuiIO& io = ImGui::GetIO();
692 if (c > 0 && c < 0x10000)
693 io.AddInputCharacter((
unsigned short)c);
static SLint mouseX
Last mouse position x in pixels.
static SLint mouseY
Last mouse position y in pixels.
static SLint dpi
Dot per inch resolution of screen.
SLMouseButton
Mouse button codes.
SLKey
Keyboard key codes enumeration.
void(SL_STDCALL * cbOnImGuiSaveConfig)()
Callback function typedef for ImGui save config function.
void(SL_STDCALL * cbOnImGuiLoadConfig)(int dpi)
Callback function typedef for ImGui load config function.
Singleton class for global render state.
void loadFonts(SLfloat fontPropDots, SLfloat fontFixedDots)
Loads the proportional and fixed size font depending on the passed DPI.
void printCompileErrors(SLint shaderHandle, const SLchar *src)
Prints the compile errors in case of a GLSL compile failure.
void onInitNewFrame(SLScene *s, SLSceneView *sv) override
Inits a new frame for the ImGui system.
void onMouseDown(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button down event.
static SLfloat fontFixedDots
Default font size of fixed size font.
void onPaint(const SLRecti &viewport) override
Callback for main rendering for the ImGui GUI system.
SLint _attribLocProjMtx
OpenGL attribute location for ???
static SLfloat fontPropDots
Default font size of proportional font.
void init(const string &configPath) override
Initializes OpenGL handles to zero and sets the ImGui key map.
SLint _attribLocUV
OpenGL attribute location for texture coords.
void onClose() override
Callback on closing the application.
SLuint _elementsHandle
OpenGL handle for vertex indexes.
void onCharInput(SLuint c) override
Callback on character input.
void renderExtraFrame(SLScene *s, SLSceneView *sv, SLint mouseX, SLint mouseY) override
Renders an extra frame with the current mouse position.
SLint _progHandle
OpenGL handle for shader program.
SLIOBuffer _fontDataProp
Raw data of proportional font file.
SLuint _vboHandle
OpenGL handle for vertex buffer object.
SLfloat _fontFixedDots
Active font size of fixed size font.
void onMouseWheel(SLfloat yoffset) override
Callback for the mouse scroll movement.
SLIOBuffer _fontDataFixed
Raw data of fixed size font file.
void onResize(const SLRecti &viewportRect) override
Callback if window got resized.
SLint _attribLocPosition
OpenGL attribute location for vertex pos.
SLint _fragHandle
OpenGL handle for fragment shader.
SLbool _mousePressed[3]
Mouse button press state.
SLstring _configPath
Path to config files.
void onMouseMove(SLint xPos, SLint yPos) override
Updates the mouse cursor position.
SLint _vertHandle
OpenGL handle for vertex shader.
void onKeyRelease(SLKey key, SLKey mod) override
Callback on key release event.
SLfloat _mouseWheel
Mouse wheel position.
SLfloat _fontPropDots
Active font size of proportional font.
void onMouseUp(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button up event.
SLint _attribLocColor
OpenGL attribute location for color.
cbOnImGuiSaveConfig _saveConfig
SLfloat _timeSec
Time in seconds.
void deleteOpenGLObjects()
Deletes all OpenGL objects for drawing the imGui.
void onKeyPress(SLKey key, SLKey mod) override
Callback on key press event.
void createOpenGLObjects()
Creates all OpenGL objects for drawing the imGui.
SLGLImGui(cbOnImGuiBuild buildCB, cbOnImGuiLoadConfig loadConfigCB, cbOnImGuiSaveConfig saveConfigCB, int dpi, SLIOBuffer fontDataProp, SLIOBuffer fontDataFixed)
SLint _attribLocTex
OpenGL attribute location for texture.
SLuint _vaoHandle
OpenGL vertex array object handle.
SLuint _fontTexture
OpenGL texture id for font.
Singleton class holding all OpenGL states.
static SLGLState * instance()
Public static instance getter for singleton pattern.
The SLScene class represents the top level instance holding the scene structure.
SceneView class represents a dynamic real time 3D view onto the scene.
SLRecti viewportRect() const
bool exists(std::string path, SLIOStreamKind kind)
Checks whether a given file exists.
std::string readIntoString(std::string path, SLIOStreamKind kind)
Reads an entire file into a string.
void writeString(std::string path, SLIOStreamKind kind, const std::string &string)
Writes a string to a file.
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Utility struct that holds a pointer and its length.
SLIOBuffer copy()
Creates a copy of the data in the buffer.