502 int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
503 int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
504 if (fb_width <= 0 || fb_height <= 0)
510 GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture);
511 glActiveTexture(GL_TEXTURE0);
512 GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program);
513 GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture);
514#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
515 GLuint last_sampler;
if (bd->
GlVersion >= 330 || bd->
GlProfileIsES3) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); }
else { last_sampler = 0; }
517 GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer);
518#ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
520 GLint last_element_array_buffer; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
521 ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_pos; last_vtx_attrib_state_pos.GetState(bd->
AttribLocationVtxPos);
522 ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_uv; last_vtx_attrib_state_uv.GetState(bd->
AttribLocationVtxUV);
523 ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_color; last_vtx_attrib_state_color.GetState(bd->
AttribLocationVtxColor);
525#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
526 GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object);
528#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
529 GLint last_polygon_mode[2];
if (bd->
HasPolygonMode) { glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); }
531 GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
532 GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
533 GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb);
534 GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb);
535 GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha);
536 GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha);
537 GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb);
538 GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha);
539 GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
540 GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
541 GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
542 GLboolean last_enable_stencil_test = glIsEnabled(GL_STENCIL_TEST);
543 GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
544#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
545 GLboolean last_enable_primitive_restart = (bd->
GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE;
551 GLuint vertex_array_object = 0;
552#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
553 GL_CALL(glGenVertexArrays(1, &vertex_array_object));
555 ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
558 ImVec2 clip_off = draw_data->DisplayPos;
559 ImVec2 clip_scale = draw_data->FramebufferScale;
562 for (
int n = 0; n < draw_data->CmdListsCount; n++)
564 const ImDrawList* draw_list = draw_data->CmdLists[n];
574 const GLsizeiptr vtx_buffer_size = (GLsizeiptr)draw_list->VtxBuffer.Size * (
int)
sizeof(ImDrawVert);
575 const GLsizeiptr idx_buffer_size = (GLsizeiptr)draw_list->IdxBuffer.Size * (
int)
sizeof(ImDrawIdx);
588 GL_CALL(glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (
const GLvoid*)draw_list->VtxBuffer.Data));
589 GL_CALL(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (
const GLvoid*)draw_list->IdxBuffer.Data));
593 GL_CALL(glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (
const GLvoid*)draw_list->VtxBuffer.Data, GL_STREAM_DRAW));
594 GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (
const GLvoid*)draw_list->IdxBuffer.Data, GL_STREAM_DRAW));
597 for (
int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
599 const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
600 if (pcmd->UserCallback !=
nullptr)
604 if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
605 ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
607 pcmd->UserCallback(draw_list, pcmd);
612 ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
613 ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
614 if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
618 GL_CALL(glScissor((
int)clip_min.x, (
int)((
float)fb_height - clip_max.y), (
int)(clip_max.x - clip_min.x), (
int)(clip_max.y - clip_min.y)));
621 GL_CALL(glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID()));
622#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
624 GL_CALL(glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount,
sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (
void*)(intptr_t)(pcmd->IdxOffset *
sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset));
627 GL_CALL(glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount,
sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (
void*)(intptr_t)(pcmd->IdxOffset *
sizeof(ImDrawIdx))));
633#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
634 GL_CALL(glDeleteVertexArrays(1, &vertex_array_object));
639 if (last_program == 0 || glIsProgram(last_program)) glUseProgram(last_program);
640 glBindTexture(GL_TEXTURE_2D, last_texture);
641#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
643 glBindSampler(0, last_sampler);
645 glActiveTexture(last_active_texture);
646#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
647 glBindVertexArray(last_vertex_array_object);
649 glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
650#ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
651 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer);
656 glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
657 glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
658 if (last_enable_blend) glEnable(GL_BLEND);
else glDisable(GL_BLEND);
659 if (last_enable_cull_face) glEnable(GL_CULL_FACE);
else glDisable(GL_CULL_FACE);
660 if (last_enable_depth_test) glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
661 if (last_enable_stencil_test) glEnable(GL_STENCIL_TEST);
else glDisable(GL_STENCIL_TEST);
662 if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST);
else glDisable(GL_SCISSOR_TEST);
663#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
664 if (bd->
GlVersion >= 310) {
if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART);
else glDisable(GL_PRIMITIVE_RESTART); }
667#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
669 if (bd->
HasPolygonMode) {
if (bd->
GlVersion <= 310 || bd->
GlProfileIsCompat) { glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]); }
else { glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); } }
672 glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
673 glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
766 GLint last_texture, last_array_buffer;
767 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
768 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
769#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK
770 GLint last_pixel_unpack_buffer = 0;
771 if (bd->
GlVersion >= 210) { glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &last_pixel_unpack_buffer); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); }
773#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
774 GLint last_vertex_array;
775 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
779 int glsl_version = 130;
782 const GLchar* vertex_shader_glsl_120 =
783 "uniform mat4 ProjMtx;\n"
784 "attribute vec2 Position;\n"
785 "attribute vec2 UV;\n"
786 "attribute vec4 Color;\n"
787 "varying vec2 Frag_UV;\n"
788 "varying vec4 Frag_Color;\n"
792 " Frag_Color = Color;\n"
793 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
796 const GLchar* vertex_shader_glsl_130 =
797 "uniform mat4 ProjMtx;\n"
798 "in vec2 Position;\n"
801 "out vec2 Frag_UV;\n"
802 "out vec4 Frag_Color;\n"
806 " Frag_Color = Color;\n"
807 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
810 const GLchar* vertex_shader_glsl_300_es =
811 "precision highp float;\n"
812 "layout (location = 0) in vec2 Position;\n"
813 "layout (location = 1) in vec2 UV;\n"
814 "layout (location = 2) in vec4 Color;\n"
815 "uniform mat4 ProjMtx;\n"
816 "out vec2 Frag_UV;\n"
817 "out vec4 Frag_Color;\n"
821 " Frag_Color = Color;\n"
822 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
825 const GLchar* vertex_shader_glsl_410_core =
826 "layout (location = 0) in vec2 Position;\n"
827 "layout (location = 1) in vec2 UV;\n"
828 "layout (location = 2) in vec4 Color;\n"
829 "uniform mat4 ProjMtx;\n"
830 "out vec2 Frag_UV;\n"
831 "out vec4 Frag_Color;\n"
835 " Frag_Color = Color;\n"
836 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
839 const GLchar* fragment_shader_glsl_120 =
841 " precision mediump float;\n"
843 "uniform sampler2D Texture;\n"
844 "varying vec2 Frag_UV;\n"
845 "varying vec4 Frag_Color;\n"
848 " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n"
851 const GLchar* fragment_shader_glsl_130 =
852 "uniform sampler2D Texture;\n"
854 "in vec4 Frag_Color;\n"
855 "out vec4 Out_Color;\n"
858 " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
861 const GLchar* fragment_shader_glsl_300_es =
862 "precision mediump float;\n"
863 "uniform sampler2D Texture;\n"
865 "in vec4 Frag_Color;\n"
866 "layout (location = 0) out vec4 Out_Color;\n"
869 " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
872 const GLchar* fragment_shader_glsl_410_core =
874 "in vec4 Frag_Color;\n"
875 "uniform sampler2D Texture;\n"
876 "layout (location = 0) out vec4 Out_Color;\n"
879 " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
883 const GLchar* vertex_shader =
nullptr;
884 const GLchar* fragment_shader =
nullptr;
885 if (glsl_version < 130)
887 vertex_shader = vertex_shader_glsl_120;
888 fragment_shader = fragment_shader_glsl_120;
890 else if (glsl_version >= 410)
892 vertex_shader = vertex_shader_glsl_410_core;
893 fragment_shader = fragment_shader_glsl_410_core;
895 else if (glsl_version == 300)
897 vertex_shader = vertex_shader_glsl_300_es;
898 fragment_shader = fragment_shader_glsl_300_es;
902 vertex_shader = vertex_shader_glsl_130;
903 fragment_shader = fragment_shader_glsl_130;
907 const GLchar* vertex_shader_with_version[2] = { bd->
GlslVersionString, vertex_shader };
909 GL_CALL(vert_handle = glCreateShader(GL_VERTEX_SHADER));
910 glShaderSource(vert_handle, 2, vertex_shader_with_version,
nullptr);
911 glCompileShader(vert_handle);
912 CheckShader(vert_handle,
"vertex shader");
914 const GLchar* fragment_shader_with_version[2] = { bd->
GlslVersionString, fragment_shader };
916 GL_CALL(frag_handle = glCreateShader(GL_FRAGMENT_SHADER));
917 glShaderSource(frag_handle, 2, fragment_shader_with_version,
nullptr);
918 glCompileShader(frag_handle);
919 CheckShader(frag_handle,
"fragment shader");
930 glDeleteShader(vert_handle);
931 glDeleteShader(frag_handle);
946 glBindTexture(GL_TEXTURE_2D, last_texture);
947 glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
948#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK
949 if (bd->
GlVersion >= 210) { glBindBuffer(GL_PIXEL_UNPACK_BUFFER, last_pixel_unpack_buffer); }
951#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
952 glBindVertexArray(last_vertex_array);