@ -63,19 +63,25 @@
// ES 3.0 300 "#version 300 es" = WebGL 2.0
//----------------------------------------
# include "imgui.h"
# include <imgui.h>
# include "imgui_impl_opengl3.h"
# include <stdio.h>
# include <stdint.h> // intptr_t
# ifdef IMGUI_GLX
# include <GL/gl3w.h>
# endif
# ifdef IMGUI_GLX
# define GL_CLIP_ORIGIN 0x935C
# define GL_NEGATIVE_ONE_TO_ONE 0x935E
# define GL_ZERO_TO_ONE 0x935F
# define GL_CLIP_DEPTH_MODE 0x935D
void * get_glx_proc_address ( const char * name ) ;
void ( * glClipControl ) ( int origin , int depth ) ;
void ( * glClipControl ) ( int origin , int depth ) = nullptr ;
# endif
namespace MangoHud {
@ -96,263 +102,7 @@ static int g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0, g_Att
static unsigned int g_VboHandle = 0 , g_ElementsHandle = 0 ;
// Functions
bool ImGui_ImplOpenGL3_Init ( const char * glsl_version )
{
glClipControl = reinterpret_cast < decltype ( glClipControl ) > ( get_glx_proc_address ( " glClipControl " ) ) ;
// Query for GL version
# if !defined(IMGUI_IMPL_OPENGL_ES2)
glsl_version = " #version 130 " ;
GLint major = - 1 , minor = - 1 ;
glGetIntegerv ( GL_MAJOR_VERSION , & major ) ;
glGetIntegerv ( GL_MINOR_VERSION , & minor ) ;
g_GlVersion = major * 1000 + minor ;
printf ( " Version: %d.%d \n " , major , minor ) ;
if ( major > = 4 & & minor > = 1 )
glsl_version = " #version 410 " ;
else if ( major > 3 | | ( major = = 3 & & minor > = 2 ) )
glsl_version = " #version 150 " ;
# else
g_GlVersion = 2000 ; // GLES 2
# endif
// Setup back-end capabilities flags
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . BackendRendererName = " imgui_impl_opengl3 " ;
# if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
if ( g_GlVersion > = 3200 )
io . BackendFlags | = ImGuiBackendFlags_RendererHasVtxOffset ; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
# endif
// Store GLSL version string so we can refer to it later in case we recreate shaders.
// Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure.
if ( glsl_version = = NULL )
glsl_version = " #version 130 " ;
IM_ASSERT ( ( int ) strlen ( glsl_version ) + 2 < IM_ARRAYSIZE ( g_GlslVersionString ) ) ;
strcpy ( g_GlslVersionString , glsl_version ) ;
strcat ( g_GlslVersionString , " \n " ) ;
// Make a dummy GL call (we don't actually need the result)
// IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code.
// Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above.
GLint current_texture ;
glGetIntegerv ( GL_TEXTURE_BINDING_2D , & current_texture ) ;
return true ;
}
void ImGui_ImplOpenGL3_Shutdown ( )
{
ImGui_ImplOpenGL3_DestroyDeviceObjects ( ) ;
}
void ImGui_ImplOpenGL3_NewFrame ( )
{
if ( ! g_ShaderHandle )
ImGui_ImplOpenGL3_CreateDeviceObjects ( ) ;
}
static void ImGui_ImplOpenGL3_SetupRenderState ( ImDrawData * draw_data , int fb_width , int fb_height , GLuint vertex_array_object )
{
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
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 ) ;
# ifdef GL_POLYGON_MODE
glPolygonMode ( GL_FRONT_AND_BACK , GL_FILL ) ;
# endif
// Setup viewport, orthographic projection matrix
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
glViewport ( 0 , 0 , ( GLsizei ) fb_width , ( GLsizei ) fb_height ) ;
float L = draw_data - > DisplayPos . x ;
float R = draw_data - > DisplayPos . x + draw_data - > DisplaySize . x ;
float T = draw_data - > DisplayPos . y ;
float B = draw_data - > DisplayPos . y + draw_data - > DisplaySize . y ;
const float ortho_projection [ 4 ] [ 4 ] =
{
{ 2.0f / ( R - L ) , 0.0f , 0.0f , 0.0f } ,
{ 0.0f , 2.0f / ( T - B ) , 0.0f , 0.0f } ,
{ 0.0f , 0.0f , - 1.0f , 0.0f } ,
{ ( R + L ) / ( L - R ) , ( T + B ) / ( B - T ) , 0.0f , 1.0f } ,
} ;
glUseProgram ( g_ShaderHandle ) ;
glUniform1i ( g_AttribLocationTex , 0 ) ;
glUniformMatrix4fv ( g_AttribLocationProjMtx , 1 , GL_FALSE , & ortho_projection [ 0 ] [ 0 ] ) ;
# ifdef GL_SAMPLER_BINDING
glBindSampler ( 0 , 0 ) ; // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.
# endif
( void ) vertex_array_object ;
# ifndef IMGUI_IMPL_OPENGL_ES2
glBindVertexArray ( vertex_array_object ) ;
# endif
// Bind vertex/index buffers and setup attributes for ImDrawVert
glBindBuffer ( GL_ARRAY_BUFFER , g_VboHandle ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , g_ElementsHandle ) ;
glEnableVertexAttribArray ( g_AttribLocationVtxPos ) ;
glEnableVertexAttribArray ( g_AttribLocationVtxUV ) ;
glEnableVertexAttribArray ( g_AttribLocationVtxColor ) ;
glVertexAttribPointer ( g_AttribLocationVtxPos , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) IM_OFFSETOF ( ImDrawVert , pos ) ) ;
glVertexAttribPointer ( g_AttribLocationVtxUV , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) IM_OFFSETOF ( ImDrawVert , uv ) ) ;
glVertexAttribPointer ( g_AttribLocationVtxColor , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof ( ImDrawVert ) , ( GLvoid * ) IM_OFFSETOF ( ImDrawVert , col ) ) ;
}
// OpenGL3 Render function.
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
void ImGui_ImplOpenGL3_RenderDrawData ( ImDrawData * draw_data )
{
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
int fb_width = ( int ) ( draw_data - > DisplaySize . x * draw_data - > FramebufferScale . x ) ;
int fb_height = ( int ) ( draw_data - > DisplaySize . y * draw_data - > FramebufferScale . y ) ;
if ( fb_width < = 0 | | fb_height < = 0 )
return ;
// Backup GL state
GLenum last_active_texture ; glGetIntegerv ( GL_ACTIVE_TEXTURE , ( GLint * ) & last_active_texture ) ;
glActiveTexture ( GL_TEXTURE0 ) ;
GLint last_program ; glGetIntegerv ( GL_CURRENT_PROGRAM , & last_program ) ;
GLint last_texture ; glGetIntegerv ( GL_TEXTURE_BINDING_2D , & last_texture ) ;
# ifdef GL_SAMPLER_BINDING
GLint last_sampler ; glGetIntegerv ( GL_SAMPLER_BINDING , & last_sampler ) ;
# endif
GLint last_array_buffer ; glGetIntegerv ( GL_ARRAY_BUFFER_BINDING , & last_array_buffer ) ;
# ifndef IMGUI_IMPL_OPENGL_ES2
GLint last_vertex_array_object ; glGetIntegerv ( GL_VERTEX_ARRAY_BINDING , & last_vertex_array_object ) ;
# endif
# ifdef GL_POLYGON_MODE
GLint last_polygon_mode [ 2 ] ; glGetIntegerv ( GL_POLYGON_MODE , last_polygon_mode ) ;
# endif
GLint last_viewport [ 4 ] ; glGetIntegerv ( GL_VIEWPORT , last_viewport ) ;
GLint last_scissor_box [ 4 ] ; glGetIntegerv ( GL_SCISSOR_BOX , last_scissor_box ) ;
GLenum last_blend_src_rgb ; glGetIntegerv ( GL_BLEND_SRC_RGB , ( GLint * ) & last_blend_src_rgb ) ;
GLenum last_blend_dst_rgb ; glGetIntegerv ( GL_BLEND_DST_RGB , ( GLint * ) & last_blend_dst_rgb ) ;
GLenum last_blend_src_alpha ; glGetIntegerv ( GL_BLEND_SRC_ALPHA , ( GLint * ) & last_blend_src_alpha ) ;
GLenum last_blend_dst_alpha ; glGetIntegerv ( GL_BLEND_DST_ALPHA , ( GLint * ) & last_blend_dst_alpha ) ;
GLenum last_blend_equation_rgb ; glGetIntegerv ( GL_BLEND_EQUATION_RGB , ( GLint * ) & last_blend_equation_rgb ) ;
GLenum last_blend_equation_alpha ; glGetIntegerv ( GL_BLEND_EQUATION_ALPHA , ( GLint * ) & last_blend_equation_alpha ) ;
GLboolean last_enable_blend = glIsEnabled ( GL_BLEND ) ;
GLboolean last_enable_cull_face = glIsEnabled ( GL_CULL_FACE ) ;
GLboolean last_enable_depth_test = glIsEnabled ( GL_DEPTH_TEST ) ;
GLboolean last_enable_scissor_test = glIsEnabled ( GL_SCISSOR_TEST ) ;
bool clip_origin_lower_left = true ;
# if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__)
GLenum last_clip_origin = 0 ; glGetIntegerv ( GL_CLIP_ORIGIN , ( GLint * ) & last_clip_origin ) ; // Support for GL 4.5's glClipControl(GL_UPPER_LEFT)
GLenum last_clip_depth_mode = 0 ; glGetIntegerv ( GL_CLIP_DEPTH_MODE , ( GLint * ) & last_clip_depth_mode ) ;
if ( last_clip_origin = = GL_UPPER_LEFT ) {
clip_origin_lower_left = false ;
if ( glClipControl )
glClipControl ( GL_LOWER_LEFT , GL_ZERO_TO_ONE ) ;
}
# endif
// Setup desired GL state
// Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts)
// The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound.
GLuint vertex_array_object = 0 ;
# ifndef IMGUI_IMPL_OPENGL_ES2
glGenVertexArrays ( 1 , & vertex_array_object ) ;
# endif
ImGui_ImplOpenGL3_SetupRenderState ( draw_data , fb_width , fb_height , vertex_array_object ) ;
// Will project scissor/clipping rectangles into framebuffer space
ImVec2 clip_off = draw_data - > DisplayPos ; // (0,0) unless using multi-viewports
ImVec2 clip_scale = draw_data - > FramebufferScale ; // (1,1) unless using retina display which are often (2,2)
// Render command lists
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
{
const ImDrawList * cmd_list = draw_data - > CmdLists [ n ] ;
// Upload vertex/index buffers
glBufferData ( GL_ARRAY_BUFFER , ( GLsizeiptr ) cmd_list - > VtxBuffer . Size * sizeof ( ImDrawVert ) , ( const GLvoid * ) cmd_list - > VtxBuffer . Data , GL_STREAM_DRAW ) ;
glBufferData ( GL_ELEMENT_ARRAY_BUFFER , ( GLsizeiptr ) cmd_list - > IdxBuffer . Size * sizeof ( ImDrawIdx ) , ( const GLvoid * ) cmd_list - > IdxBuffer . Data , GL_STREAM_DRAW ) ;
for ( int cmd_i = 0 ; cmd_i < cmd_list - > CmdBuffer . Size ; cmd_i + + )
{
const ImDrawCmd * pcmd = & cmd_list - > CmdBuffer [ cmd_i ] ;
if ( pcmd - > UserCallback ! = NULL )
{
// User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
if ( pcmd - > UserCallback = = ImDrawCallback_ResetRenderState )
ImGui_ImplOpenGL3_SetupRenderState ( draw_data , fb_width , fb_height , vertex_array_object ) ;
else
pcmd - > UserCallback ( cmd_list , pcmd ) ;
}
else
{
// Project scissor/clipping rectangles into framebuffer space
ImVec4 clip_rect ;
clip_rect . x = ( pcmd - > ClipRect . x - clip_off . x ) * clip_scale . x ;
clip_rect . y = ( pcmd - > ClipRect . y - clip_off . y ) * clip_scale . y ;
clip_rect . z = ( pcmd - > ClipRect . z - clip_off . x ) * clip_scale . x ;
clip_rect . w = ( pcmd - > ClipRect . w - clip_off . y ) * clip_scale . y ;
if ( clip_rect . x < fb_width & & clip_rect . y < fb_height & & clip_rect . z > = 0.0f & & clip_rect . w > = 0.0f )
{
// Apply scissor/clipping rectangle
//if (clip_origin_lower_left)
glScissor ( ( int ) clip_rect . x , ( int ) ( fb_height - clip_rect . w ) , ( int ) ( clip_rect . z - clip_rect . x ) , ( int ) ( clip_rect . w - clip_rect . y ) ) ;
//else
// glScissor((int)clip_rect.x, (int)clip_rect.y, (int)clip_rect.z, (int)clip_rect.w); // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT)
// Bind texture, Draw
glBindTexture ( GL_TEXTURE_2D , ( GLuint ) ( intptr_t ) pcmd - > TextureId ) ;
# if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
if ( g_GlVersion > = 3200 )
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 ) ;
else
# endif
glDrawElements ( GL_TRIANGLES , ( GLsizei ) pcmd - > ElemCount , sizeof ( ImDrawIdx ) = = 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT , ( void * ) ( intptr_t ) ( pcmd - > IdxOffset * sizeof ( ImDrawIdx ) ) ) ;
}
}
}
}
// Destroy the temporary VAO
# ifndef IMGUI_IMPL_OPENGL_ES2
glDeleteVertexArrays ( 1 , & vertex_array_object ) ;
# endif
// Restore modified GL state
glUseProgram ( last_program ) ;
glBindTexture ( GL_TEXTURE_2D , last_texture ) ;
# ifdef GL_SAMPLER_BINDING
glBindSampler ( 0 , last_sampler ) ;
# endif
glActiveTexture ( last_active_texture ) ;
# ifndef IMGUI_IMPL_OPENGL_ES2
glBindVertexArray ( last_vertex_array_object ) ;
# endif
glBindBuffer ( GL_ARRAY_BUFFER , last_array_buffer ) ;
glBlendEquationSeparate ( last_blend_equation_rgb , last_blend_equation_alpha ) ;
glBlendFuncSeparate ( last_blend_src_rgb , last_blend_dst_rgb , last_blend_src_alpha , last_blend_dst_alpha ) ;
if ( last_enable_blend ) glEnable ( GL_BLEND ) ; else glDisable ( GL_BLEND ) ;
if ( last_enable_cull_face ) glEnable ( GL_CULL_FACE ) ; else glDisable ( GL_CULL_FACE ) ;
if ( last_enable_depth_test ) glEnable ( GL_DEPTH_TEST ) ; else glDisable ( GL_DEPTH_TEST ) ;
if ( last_enable_scissor_test ) glEnable ( GL_SCISSOR_TEST ) ; else glDisable ( GL_SCISSOR_TEST ) ;
# ifdef GL_POLYGON_MODE
glPolygonMode ( GL_FRONT_AND_BACK , ( GLenum ) last_polygon_mode [ 0 ] ) ;
# endif
glViewport ( last_viewport [ 0 ] , last_viewport [ 1 ] , ( GLsizei ) last_viewport [ 2 ] , ( GLsizei ) last_viewport [ 3 ] ) ;
glScissor ( last_scissor_box [ 0 ] , last_scissor_box [ 1 ] , ( GLsizei ) last_scissor_box [ 2 ] , ( GLsizei ) last_scissor_box [ 3 ] ) ;
# if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__)
if ( ! clip_origin_lower_left & & glClipControl )
glClipControl ( last_clip_origin , last_clip_depth_mode ) ;
# endif
}
bool ImGui_ImplOpenGL3_CreateFontsTexture ( )
static bool ImGui_ImplOpenGL3_CreateFontsTexture ( )
{
// Build texture atlas
ImGuiIO & io = ImGui : : GetIO ( ) ;
@ -381,7 +131,7 @@ bool ImGui_ImplOpenGL3_CreateFontsTexture()
return true ;
}
void ImGui_ImplOpenGL3_DestroyFontsTexture ( )
static void ImGui_ImplOpenGL3_DestroyFontsTexture ( )
{
if ( g_FontTexture )
{
@ -428,7 +178,7 @@ static bool CheckProgram(GLuint handle, const char* desc)
return ( GLboolean ) status = = GL_TRUE ;
}
bool ImGui_ImplOpenGL3_CreateDeviceObjects ( )
static bool ImGui_ImplOpenGL3_CreateDeviceObjects ( )
{
// Backup GL state
GLint last_texture , last_array_buffer ;
@ -612,7 +362,7 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects()
return true ;
}
void ImGui_ImplOpenGL3_DestroyDeviceObjects ( )
static void ImGui_ImplOpenGL3_DestroyDeviceObjects ( )
{
# ifndef NDEBUG
printf ( " %s \n " , __func__ ) ;
@ -627,4 +377,263 @@ void ImGui_ImplOpenGL3_DestroyDeviceObjects()
ImGui_ImplOpenGL3_DestroyFontsTexture ( ) ;
}
bool VARIANT ( ImGui_ImplOpenGL3_Init ) ( const char * glsl_version )
{
# ifdef IMGUI_GLX
glClipControl = reinterpret_cast < decltype ( glClipControl ) > ( get_glx_proc_address ( " glClipControl " ) ) ;
# endif
// Query for GL version
# if !defined(IMGUI_IMPL_OPENGL_ES2)
glsl_version = " #version 130 " ;
GLint major = - 1 , minor = - 1 ;
glGetIntegerv ( GL_MAJOR_VERSION , & major ) ;
glGetIntegerv ( GL_MINOR_VERSION , & minor ) ;
g_GlVersion = major * 1000 + minor ;
printf ( " Version: %d.%d \n " , major , minor ) ;
if ( major > = 4 & & minor > = 1 )
glsl_version = " #version 410 " ;
else if ( major > 3 | | ( major = = 3 & & minor > = 2 ) )
glsl_version = " #version 150 " ;
# else
g_GlVersion = 2000 ; // GLES 2
# endif
// Setup back-end capabilities flags
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . BackendRendererName = " imgui_impl_opengl3 " ;
# if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
if ( g_GlVersion > = 3200 )
io . BackendFlags | = ImGuiBackendFlags_RendererHasVtxOffset ; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
# endif
// Store GLSL version string so we can refer to it later in case we recreate shaders.
// Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure.
if ( glsl_version = = NULL )
glsl_version = " #version 130 " ;
IM_ASSERT ( ( int ) strlen ( glsl_version ) + 2 < IM_ARRAYSIZE ( g_GlslVersionString ) ) ;
strcpy ( g_GlslVersionString , glsl_version ) ;
strcat ( g_GlslVersionString , " \n " ) ;
// Make a dummy GL call (we don't actually need the result)
// IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code.
// Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above.
GLint current_texture ;
glGetIntegerv ( GL_TEXTURE_BINDING_2D , & current_texture ) ;
return true ;
}
void VARIANT ( ImGui_ImplOpenGL3_Shutdown ) ( )
{
ImGui_ImplOpenGL3_DestroyDeviceObjects ( ) ;
}
void VARIANT ( ImGui_ImplOpenGL3_NewFrame ) ( )
{
if ( ! g_ShaderHandle )
ImGui_ImplOpenGL3_CreateDeviceObjects ( ) ;
}
static void VARIANT ( ImGui_ImplOpenGL3_SetupRenderState ) ( ImDrawData * draw_data , int fb_width , int fb_height , GLuint vertex_array_object )
{
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
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 ) ;
# ifdef GL_POLYGON_MODE
glPolygonMode ( GL_FRONT_AND_BACK , GL_FILL ) ;
# endif
// Setup viewport, orthographic projection matrix
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
glViewport ( 0 , 0 , ( GLsizei ) fb_width , ( GLsizei ) fb_height ) ;
float L = draw_data - > DisplayPos . x ;
float R = draw_data - > DisplayPos . x + draw_data - > DisplaySize . x ;
float T = draw_data - > DisplayPos . y ;
float B = draw_data - > DisplayPos . y + draw_data - > DisplaySize . y ;
const float ortho_projection [ 4 ] [ 4 ] =
{
{ 2.0f / ( R - L ) , 0.0f , 0.0f , 0.0f } ,
{ 0.0f , 2.0f / ( T - B ) , 0.0f , 0.0f } ,
{ 0.0f , 0.0f , - 1.0f , 0.0f } ,
{ ( R + L ) / ( L - R ) , ( T + B ) / ( B - T ) , 0.0f , 1.0f } ,
} ;
glUseProgram ( g_ShaderHandle ) ;
glUniform1i ( g_AttribLocationTex , 0 ) ;
glUniformMatrix4fv ( g_AttribLocationProjMtx , 1 , GL_FALSE , & ortho_projection [ 0 ] [ 0 ] ) ;
# ifdef GL_SAMPLER_BINDING
glBindSampler ( 0 , 0 ) ; // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.
# endif
( void ) vertex_array_object ;
# ifndef IMGUI_IMPL_OPENGL_ES2
glBindVertexArray ( vertex_array_object ) ;
# endif
// Bind vertex/index buffers and setup attributes for ImDrawVert
glBindBuffer ( GL_ARRAY_BUFFER , g_VboHandle ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , g_ElementsHandle ) ;
glEnableVertexAttribArray ( g_AttribLocationVtxPos ) ;
glEnableVertexAttribArray ( g_AttribLocationVtxUV ) ;
glEnableVertexAttribArray ( g_AttribLocationVtxColor ) ;
glVertexAttribPointer ( g_AttribLocationVtxPos , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) IM_OFFSETOF ( ImDrawVert , pos ) ) ;
glVertexAttribPointer ( g_AttribLocationVtxUV , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) IM_OFFSETOF ( ImDrawVert , uv ) ) ;
glVertexAttribPointer ( g_AttribLocationVtxColor , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof ( ImDrawVert ) , ( GLvoid * ) IM_OFFSETOF ( ImDrawVert , col ) ) ;
}
// OpenGL3 Render function.
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
void VARIANT ( ImGui_ImplOpenGL3_RenderDrawData ) ( ImDrawData * draw_data )
{
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
int fb_width = ( int ) ( draw_data - > DisplaySize . x * draw_data - > FramebufferScale . x ) ;
int fb_height = ( int ) ( draw_data - > DisplaySize . y * draw_data - > FramebufferScale . y ) ;
if ( fb_width < = 0 | | fb_height < = 0 )
return ;
// Backup GL state
GLenum last_active_texture ; glGetIntegerv ( GL_ACTIVE_TEXTURE , ( GLint * ) & last_active_texture ) ;
glActiveTexture ( GL_TEXTURE0 ) ;
GLint last_program ; glGetIntegerv ( GL_CURRENT_PROGRAM , & last_program ) ;
GLint last_texture ; glGetIntegerv ( GL_TEXTURE_BINDING_2D , & last_texture ) ;
# ifdef GL_SAMPLER_BINDING
GLint last_sampler ; glGetIntegerv ( GL_SAMPLER_BINDING , & last_sampler ) ;
# endif
GLint last_array_buffer ; glGetIntegerv ( GL_ARRAY_BUFFER_BINDING , & last_array_buffer ) ;
# ifndef IMGUI_IMPL_OPENGL_ES2
GLint last_vertex_array_object ; glGetIntegerv ( GL_VERTEX_ARRAY_BINDING , & last_vertex_array_object ) ;
# endif
# ifdef GL_POLYGON_MODE
GLint last_polygon_mode [ 2 ] ; glGetIntegerv ( GL_POLYGON_MODE , last_polygon_mode ) ;
# endif
GLint last_viewport [ 4 ] ; glGetIntegerv ( GL_VIEWPORT , last_viewport ) ;
GLint last_scissor_box [ 4 ] ; glGetIntegerv ( GL_SCISSOR_BOX , last_scissor_box ) ;
GLenum last_blend_src_rgb ; glGetIntegerv ( GL_BLEND_SRC_RGB , ( GLint * ) & last_blend_src_rgb ) ;
GLenum last_blend_dst_rgb ; glGetIntegerv ( GL_BLEND_DST_RGB , ( GLint * ) & last_blend_dst_rgb ) ;
GLenum last_blend_src_alpha ; glGetIntegerv ( GL_BLEND_SRC_ALPHA , ( GLint * ) & last_blend_src_alpha ) ;
GLenum last_blend_dst_alpha ; glGetIntegerv ( GL_BLEND_DST_ALPHA , ( GLint * ) & last_blend_dst_alpha ) ;
GLenum last_blend_equation_rgb ; glGetIntegerv ( GL_BLEND_EQUATION_RGB , ( GLint * ) & last_blend_equation_rgb ) ;
GLenum last_blend_equation_alpha ; glGetIntegerv ( GL_BLEND_EQUATION_ALPHA , ( GLint * ) & last_blend_equation_alpha ) ;
GLboolean last_enable_blend = glIsEnabled ( GL_BLEND ) ;
GLboolean last_enable_cull_face = glIsEnabled ( GL_CULL_FACE ) ;
GLboolean last_enable_depth_test = glIsEnabled ( GL_DEPTH_TEST ) ;
GLboolean last_enable_scissor_test = glIsEnabled ( GL_SCISSOR_TEST ) ;
bool clip_origin_lower_left = true ;
# if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__)
GLenum last_clip_origin = 0 ; glGetIntegerv ( GL_CLIP_ORIGIN , ( GLint * ) & last_clip_origin ) ; // Support for GL 4.5's glClipControl(GL_UPPER_LEFT)
GLenum last_clip_depth_mode = 0 ; glGetIntegerv ( GL_CLIP_DEPTH_MODE , ( GLint * ) & last_clip_depth_mode ) ;
if ( last_clip_origin = = GL_UPPER_LEFT ) {
clip_origin_lower_left = false ;
if ( glClipControl )
glClipControl ( GL_LOWER_LEFT , GL_ZERO_TO_ONE ) ;
}
# endif
// Setup desired GL state
// Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts)
// The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound.
GLuint vertex_array_object = 0 ;
# ifndef IMGUI_IMPL_OPENGL_ES2
glGenVertexArrays ( 1 , & vertex_array_object ) ;
# endif
VARIANT ( ImGui_ImplOpenGL3_SetupRenderState ) ( draw_data , fb_width , fb_height , vertex_array_object ) ;
// Will project scissor/clipping rectangles into framebuffer space
ImVec2 clip_off = draw_data - > DisplayPos ; // (0,0) unless using multi-viewports
ImVec2 clip_scale = draw_data - > FramebufferScale ; // (1,1) unless using retina display which are often (2,2)
// Render command lists
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
{
const ImDrawList * cmd_list = draw_data - > CmdLists [ n ] ;
// Upload vertex/index buffers
glBufferData ( GL_ARRAY_BUFFER , ( GLsizeiptr ) cmd_list - > VtxBuffer . Size * sizeof ( ImDrawVert ) , ( const GLvoid * ) cmd_list - > VtxBuffer . Data , GL_STREAM_DRAW ) ;
glBufferData ( GL_ELEMENT_ARRAY_BUFFER , ( GLsizeiptr ) cmd_list - > IdxBuffer . Size * sizeof ( ImDrawIdx ) , ( const GLvoid * ) cmd_list - > IdxBuffer . Data , GL_STREAM_DRAW ) ;
for ( int cmd_i = 0 ; cmd_i < cmd_list - > CmdBuffer . Size ; cmd_i + + )
{
const ImDrawCmd * pcmd = & cmd_list - > CmdBuffer [ cmd_i ] ;
if ( pcmd - > UserCallback ! = NULL )
{
// User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
if ( pcmd - > UserCallback = = ImDrawCallback_ResetRenderState )
VARIANT ( ImGui_ImplOpenGL3_SetupRenderState ) ( draw_data , fb_width , fb_height , vertex_array_object ) ;
else
pcmd - > UserCallback ( cmd_list , pcmd ) ;
}
else
{
// Project scissor/clipping rectangles into framebuffer space
ImVec4 clip_rect ;
clip_rect . x = ( pcmd - > ClipRect . x - clip_off . x ) * clip_scale . x ;
clip_rect . y = ( pcmd - > ClipRect . y - clip_off . y ) * clip_scale . y ;
clip_rect . z = ( pcmd - > ClipRect . z - clip_off . x ) * clip_scale . x ;
clip_rect . w = ( pcmd - > ClipRect . w - clip_off . y ) * clip_scale . y ;
if ( clip_rect . x < fb_width & & clip_rect . y < fb_height & & clip_rect . z > = 0.0f & & clip_rect . w > = 0.0f )
{
// Apply scissor/clipping rectangle
//if (clip_origin_lower_left)
glScissor ( ( int ) clip_rect . x , ( int ) ( fb_height - clip_rect . w ) , ( int ) ( clip_rect . z - clip_rect . x ) , ( int ) ( clip_rect . w - clip_rect . y ) ) ;
//else
// glScissor((int)clip_rect.x, (int)clip_rect.y, (int)clip_rect.z, (int)clip_rect.w); // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT)
// Bind texture, Draw
glBindTexture ( GL_TEXTURE_2D , ( GLuint ) ( intptr_t ) pcmd - > TextureId ) ;
# if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
if ( g_GlVersion > = 3200 )
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 ) ;
else
# endif
glDrawElements ( GL_TRIANGLES , ( GLsizei ) pcmd - > ElemCount , sizeof ( ImDrawIdx ) = = 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT , ( void * ) ( intptr_t ) ( pcmd - > IdxOffset * sizeof ( ImDrawIdx ) ) ) ;
}
}
}
}
// Destroy the temporary VAO
# ifndef IMGUI_IMPL_OPENGL_ES2
glDeleteVertexArrays ( 1 , & vertex_array_object ) ;
# endif
// Restore modified GL state
glUseProgram ( last_program ) ;
glBindTexture ( GL_TEXTURE_2D , last_texture ) ;
# ifdef GL_SAMPLER_BINDING
glBindSampler ( 0 , last_sampler ) ;
# endif
glActiveTexture ( last_active_texture ) ;
# ifndef IMGUI_IMPL_OPENGL_ES2
glBindVertexArray ( last_vertex_array_object ) ;
# endif
glBindBuffer ( GL_ARRAY_BUFFER , last_array_buffer ) ;
glBlendEquationSeparate ( last_blend_equation_rgb , last_blend_equation_alpha ) ;
glBlendFuncSeparate ( last_blend_src_rgb , last_blend_dst_rgb , last_blend_src_alpha , last_blend_dst_alpha ) ;
if ( last_enable_blend ) glEnable ( GL_BLEND ) ; else glDisable ( GL_BLEND ) ;
if ( last_enable_cull_face ) glEnable ( GL_CULL_FACE ) ; else glDisable ( GL_CULL_FACE ) ;
if ( last_enable_depth_test ) glEnable ( GL_DEPTH_TEST ) ; else glDisable ( GL_DEPTH_TEST ) ;
if ( last_enable_scissor_test ) glEnable ( GL_SCISSOR_TEST ) ; else glDisable ( GL_SCISSOR_TEST ) ;
# ifdef GL_POLYGON_MODE
glPolygonMode ( GL_FRONT_AND_BACK , ( GLenum ) last_polygon_mode [ 0 ] ) ;
# endif
glViewport ( last_viewport [ 0 ] , last_viewport [ 1 ] , ( GLsizei ) last_viewport [ 2 ] , ( GLsizei ) last_viewport [ 3 ] ) ;
glScissor ( last_scissor_box [ 0 ] , last_scissor_box [ 1 ] , ( GLsizei ) last_scissor_box [ 2 ] , ( GLsizei ) last_scissor_box [ 3 ] ) ;
# if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__)
if ( ! clip_origin_lower_left & & glClipControl )
glClipControl ( last_clip_origin , last_clip_depth_mode ) ;
# endif
}
}