17 #include <imgui_internal.h>
24 SLint compileSuccess = 0;
25 glGetShaderiv((
SLuint)shaderHandle, GL_COMPILE_STATUS, &compileSuccess);
26 if (compileSuccess == GL_FALSE)
29 glGetShaderInfoLog((
SLuint)shaderHandle,
33 SL_LOG(
"*** COMPILER ERROR ***");
43 GLint last_texture, last_array_buffer, last_vertex_array;
44 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
45 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
46 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
51 SLstring vertex_shader =
"#version " + verGLSL;
52 if (state->
glIsES3()) vertex_shader +=
" es";
56 "precision mediump float;\n"
59 "uniform mat4 ProjMtx;\n"
64 "out vec4 Frag_Color;\n"
68 " Frag_Color = Color;\n"
69 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
72 SLstring fragment_shader =
"#version " + verGLSL;
73 if (state->
glIsES3()) fragment_shader +=
" es";
77 "precision mediump float;\n"
80 "uniform sampler2D Texture;\n"
82 "in vec4 Frag_Color;\n"
83 "out vec4 Out_Color;\n"
86 " Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n"
91 const char* srcVert = vertex_shader.c_str();
92 const char* srcFrag = fragment_shader.c_str();
127 #define OFFSETOF(TYPE, ELEMENT) ((size_t) & (((TYPE*)nullptr)->ELEMENT))
133 (GLvoid*)
OFFSETOF(ImDrawVert, pos));
145 (GLvoid*)
OFFSETOF(ImDrawVert, col));
160 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
163 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
168 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
176 glBindTexture(GL_TEXTURE_2D, (
SLuint)last_texture);
179 glBindTexture(GL_TEXTURE_2D, (
SLuint)last_texture);
180 glBindBuffer(GL_ARRAY_BUFFER, (
SLuint)last_array_buffer);
181 glBindVertexArray((
SLuint)last_vertex_array);
210 _context->IO.Fonts->TexID =
nullptr;
219 ImDrawData* draw_data = ImGui::GetDrawData();
225 int fbWidth = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
226 int fbHeight = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
227 if (fbWidth == 0 || fbHeight == 0)
229 draw_data->ScaleClipRects(io.DisplayFramebufferScale);
232 GLint last_active_texture;
233 glGetIntegerv(GL_ACTIVE_TEXTURE, &last_active_texture);
234 glActiveTexture(GL_TEXTURE0);
237 glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
239 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
240 GLint last_array_buffer;
241 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
242 GLint last_element_array_buffer;
243 glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
244 GLint last_vertex_array;
245 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
246 GLint last_blend_src_rgb;
247 glGetIntegerv(GL_BLEND_SRC_RGB, &last_blend_src_rgb);
248 GLint last_blend_dst_rgb;
249 glGetIntegerv(GL_BLEND_DST_RGB, &last_blend_dst_rgb);
250 GLint last_blend_src_alpha;
251 glGetIntegerv(GL_BLEND_SRC_ALPHA, &last_blend_src_alpha);
252 GLint last_blend_dst_alpha;
253 glGetIntegerv(GL_BLEND_DST_ALPHA, &last_blend_dst_alpha);
254 GLint last_blend_equation_rgb;
255 glGetIntegerv(GL_BLEND_EQUATION_RGB, &last_blend_equation_rgb);
256 GLint last_blend_equation_alpha;
257 glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &last_blend_equation_alpha);
258 GLint last_viewport[4];
259 glGetIntegerv(GL_VIEWPORT, last_viewport);
260 GLint last_scissor_box[4];
261 glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
263 GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
264 GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
265 GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
266 GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
270 glBlendEquation(GL_FUNC_ADD);
271 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
272 glDisable(GL_CULL_FACE);
273 glDisable(GL_DEPTH_TEST);
274 glEnable(GL_SCISSOR_TEST);
283 glViewport((GLsizei)(viewportRect.
x * io.DisplayFramebufferScale.x),
284 (GLsizei)(viewportRect.
y * io.DisplayFramebufferScale.y),
285 (GLsizei)(viewportRect.
width * io.DisplayFramebufferScale.x),
286 (GLsizei)(viewportRect.
height * io.DisplayFramebufferScale.y));
290 const float ortho_projection[4][4] =
292 {2.0f / io.DisplaySize.x, 0.0f, 0.0f, 0.0f},
293 {0.0f, 2.0f / -io.DisplaySize.y, 0.0f, 0.0f},
294 {0.0f, 0.0f, -1.0f, 0.0f},
295 {-1.0f, 1.0f, 0.0f, 1.0f},
304 for (
int n = 0; n < draw_data->CmdListsCount; n++)
306 const ImDrawList* cmd_list = draw_data->CmdLists[n];
307 const ImDrawIdx* idx_buffer_offset =
nullptr;
310 glBufferData(GL_ARRAY_BUFFER,
311 (GLsizeiptr)cmd_list->VtxBuffer.Size * (GLsizeiptr)
sizeof(ImDrawVert),
312 (
const GLvoid*)cmd_list->VtxBuffer.Data,
316 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
317 (GLsizeiptr)cmd_list->IdxBuffer.Size * (GLsizeiptr)
sizeof(ImDrawIdx),
318 (
const GLvoid*)cmd_list->IdxBuffer.Data,
321 for (
int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
323 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
324 if (pcmd->UserCallback)
326 pcmd->UserCallback(cmd_list, pcmd);
330 glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
333 glScissor((
int)pcmd->ClipRect.x,
334 (
int)(fbHeight - pcmd->ClipRect.w),
335 (
int)(pcmd->ClipRect.z - pcmd->ClipRect.x),
336 (
int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
338 glScissor((GLsizei)(viewportRect.
x * io.DisplayFramebufferScale.x),
339 (GLsizei)(viewportRect.
y * io.DisplayFramebufferScale.y),
340 (GLsizei)(viewportRect.
width * io.DisplayFramebufferScale.x),
341 (GLsizei)(viewportRect.
height * io.DisplayFramebufferScale.y));
343 glDrawElements(GL_TRIANGLES,
344 (GLsizei)pcmd->ElemCount,
345 sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
348 idx_buffer_offset += pcmd->ElemCount;
353 glUseProgram((
SLuint)last_program);
354 glBindTexture(GL_TEXTURE_2D, (
SLuint)last_texture);
355 glActiveTexture((
SLuint)last_active_texture);
356 glBindVertexArray((
SLuint)last_vertex_array);
357 glBindBuffer(GL_ARRAY_BUFFER, (
SLuint)last_array_buffer);
358 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (
SLuint)last_element_array_buffer);
359 glBlendEquationSeparate((
SLuint)last_blend_equation_rgb,
360 (
SLuint)last_blend_equation_alpha);
361 glBlendFuncSeparate((
SLuint)last_blend_src_rgb,
362 (
SLuint)last_blend_dst_rgb,
363 (
SLuint)last_blend_src_alpha,
364 (
SLuint)last_blend_dst_alpha);
365 if (last_enable_blend)
370 if (last_enable_cull_face)
371 glEnable(GL_CULL_FACE);
373 glDisable(GL_CULL_FACE);
375 if (last_enable_depth_test)
376 glEnable(GL_DEPTH_TEST);
378 glDisable(GL_DEPTH_TEST);
380 if (last_enable_scissor_test)
381 glEnable(GL_SCISSOR_TEST);
383 glDisable(GL_SCISSOR_TEST);
385 glViewport(last_viewport[0],
387 (GLsizei)last_viewport[2],
388 (GLsizei)last_viewport[3]);
390 glScissor(last_scissor_box[0],
392 (GLsizei)last_scissor_box[2],
393 (GLsizei)last_scissor_box[3]);
395 ImGui::SetCurrentContext(
nullptr);
404 io.KeyMap[ImGuiKey_Tab] =
K_tab;
405 io.KeyMap[ImGuiKey_LeftArrow] =
K_left;
406 io.KeyMap[ImGuiKey_RightArrow] =
K_right;
407 io.KeyMap[ImGuiKey_UpArrow] =
K_up;
408 io.KeyMap[ImGuiKey_DownArrow] =
K_down;
409 io.KeyMap[ImGuiKey_PageUp] =
K_pageUp;
410 io.KeyMap[ImGuiKey_PageDown] =
K_pageUp;
411 io.KeyMap[ImGuiKey_Home] =
K_home;
412 io.KeyMap[ImGuiKey_End] =
K_end;
413 io.KeyMap[ImGuiKey_Delete] =
K_delete;
415 io.KeyMap[ImGuiKey_Enter] =
K_enter;
416 io.KeyMap[ImGuiKey_Escape] =
K_esc;
417 io.KeyMap[ImGuiKey_A] =
'A';
418 io.KeyMap[ImGuiKey_C] =
'C';
419 io.KeyMap[ImGuiKey_V] =
'V';
420 io.KeyMap[ImGuiKey_X] =
'X';
421 io.KeyMap[ImGuiKey_Y] =
'Y';
422 io.KeyMap[ImGuiKey_Z] =
'Z';
425 io.DisplaySize = ImVec2(0, 0);
426 io.DisplayFramebufferScale = ImVec2(1, 1);
467 if (io.DeltaTime < 0) io.DeltaTime = 1.0f / 60.0f;
486 ImGui::SetCurrentContext(
nullptr);
496 io.DisplayFramebufferScale = ImVec2(1, 1);
513 io.MouseDown[0] =
true;
517 if (button ==
MB_right) io.MouseDown[1] =
true;
518 if (button ==
MB_middle) io.MouseDown[2] =
true;
527 if (button ==
MB_left) io.MouseDown[0] =
false;
528 if (button ==
MB_right) io.MouseDown[1] =
false;
529 if (button ==
MB_middle) io.MouseDown[2] =
false;
554 io.KeysDown[key] =
true;
555 io.KeyCtrl =
mod &
K_ctrl ? true :
false;
557 io.KeyAlt =
mod &
K_alt ? true :
false;
564 io.KeysDown[key] =
false;
565 io.KeyCtrl =
mod &
K_ctrl ? true :
false;
567 io.KeyAlt =
mod &
K_alt ? true :
false;
574 if (c > 0 && c < 0x10000)
575 io.AddInputCharacter((
unsigned short)c);
594 return _context->IO.WantCaptureKeyboard;
599 return _context->IO.WantCaptureMouse;
static SLint mouseX
Last mouse position x in pixels.
static SLint mouseY
Last mouse position y in pixels.
#define OFFSETOF(TYPE, ELEMENT)
SLMouseButton
Mouse button codes.
SLKey
Keyboard key codes enumeration.
void init(const std::string &configPath)
virtual void render(const SLRecti &viewportRect)
int _fragHandle
OpenGL handle for fragment shader.
int _attribLocPosition
OpenGL attribute location for vertex pos.
void printCompileErrors(SLint shaderHandle, const SLchar *src)
Prints the compile errors in case of a GLSL compile failure.
int _vertHandle
OpenGL handle for vertex shader.
int _attribLocColor
OpenGL attribute location for color.
int _attribLocUV
OpenGL attribute location for texture coords.
int _attribLocTex
OpenGL attribute location for texture.
unsigned int _elementsHandle
OpenGL handle for vertex indexes.
int _attribLocProjMtx
OpenGL attribute location for ???
unsigned int _fontTexture
OpenGL texture id for font.
void deleteOpenGLObjects()
Deletes all OpenGL objects for drawing the imGui.
int _progHandle
OpenGL handle for shader program.
unsigned int _vboHandle
OpenGL handle for vertex buffer object.
void render(const SLRecti &viewportRect) override
void createOpenGLObjects()
Creates all OpenGL objects for drawing the imGui.
unsigned int _vaoHandle
OpenGL vertex array object handle.
void onMouseDown(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button down event.
ImGuiWrapper(ImGuiContext *context, ImGuiRenderer *renderer)
virtual void build(SLScene *s, SLSceneView *sv)=0
void init(const std::string &configPath) override
Initializes OpenGL handles to zero and sets the ImGui key map.
void onMouseWheel(SLfloat yoffset) override
Callback for the mouse scroll movement.
void onMouseUp(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button up event.
void onKeyPress(SLKey key, SLKey mod) override
Callback on key press event.
void onResize(SLint scrW, SLint scrH) override
Callback if window got resized.
void onClose() override
Callback on closing the application.
void onMouseMove(SLint xPos, SLint yPos) override
Updates the mouse cursor position.
SLfloat _timeSec
Time in seconds.
bool doNotDispatchKeyboard() override
inform if user keyboard input was consumed by the ui
SLfloat _mouseWheel
Mouse wheel position.
void onPaint(const SLRecti &viewport) override
Callback for main rendering for the ImGui GUI system.
void onInitNewFrame(SLScene *s, SLSceneView *sv) override
Inits a new frame for the ImGui system.
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.
ImGuiRenderer * _renderer
void onKeyRelease(SLKey key, SLKey mod) override
Callback on key release event.
bool doNotDispatchMouse() override
inform if user mouse input was consumed by the ui
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
void log(const char *tag, const char *format,...)
logs a formatted string platform independently