@ -12,6 +12,7 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2019-03-29: OpenGL: Not calling glBindBuffer more than necessary in the render loop.
// 2019-03-15: OpenGL: Added a dummy GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early.
// 2019-03-03: OpenGL: Fix support for ES 2.0 (WebGL 1.0).
// 2019-02-20: OpenGL: Fix for OSX not supporting OpenGL 4.5, we don't try to read GL_CLIP_ORIGIN even if defined by the headers/loader.
@ -104,8 +105,8 @@
static char g_GlslVersionString [ 32 ] = " " ;
static GLuint g_FontTexture = 0 ;
static GLuint g_ShaderHandle = 0 , g_VertHandle = 0 , g_FragHandle = 0 ;
static int g_AttribLocationTex = 0 , g_AttribLocationProjMtx = 0 ;
static int g_AttribLocation Position = 0 , g_AttribLocation UV = 0 , g_AttribLocation Color = 0 ;
static int g_AttribLocationTex = 0 , g_AttribLocationProjMtx = 0 ; // Uniforms location
static int g_AttribLocation Vtx Pos = 0 , g_AttribLocation Vtx UV = 0 , g_AttribLocation Vtx Color = 0 ; // Vertex attributes location
static unsigned int g_VboHandle = 0 , g_ElementsHandle = 0 ;
// Functions
@ -170,7 +171,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
# endif
GLint last_array_buffer ; glGetIntegerv ( GL_ARRAY_BUFFER_BINDING , & last_array_buffer ) ;
# ifndef IMGUI_IMPL_OPENGL_ES2
GLint last_vertex_array ; glGetIntegerv ( GL_VERTEX_ARRAY_BINDING , & last_vertex_array ) ;
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 ) ;
@ -226,20 +227,23 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
glBindSampler ( 0 , 0 ) ; // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.
# endif
// 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.
# ifndef IMGUI_IMPL_OPENGL_ES2
// Recreate the VAO every time
// (This is to easily allow multiple GL contexts. VAO are not shared among GL contexts, and we don't track creation/deletion of windows so we don't have an obvious key to use to cache them.)
GLuint vao_handle = 0 ;
glGenVertexArrays ( 1 , & vao_handle ) ;
glBindVertexArray ( vao_handle ) ;
GLuint vertex_array_object = 0 ;
glGenVertexArrays ( 1 , & vertex_array_object ) ;
glBindVertexArray ( vertex_array_object ) ;
# endif
// Bind vertex/index buffers and setup attributes for ImDrawVert
glBindBuffer ( GL_ARRAY_BUFFER , g_VboHandle ) ;
glEnableVertexAttribArray ( g_AttribLocationPosition ) ;
glEnableVertexAttribArray ( g_AttribLocationUV ) ;
glEnableVertexAttribArray ( g_AttribLocationColor ) ;
glVertexAttribPointer ( g_AttribLocationPosition , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) IM_OFFSETOF ( ImDrawVert , pos ) ) ;
glVertexAttribPointer ( g_AttribLocationUV , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) IM_OFFSETOF ( ImDrawVert , uv ) ) ;
glVertexAttribPointer ( g_AttribLocationColor , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof ( ImDrawVert ) , ( GLvoid * ) IM_OFFSETOF ( ImDrawVert , col ) ) ;
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 ) ) ;
// Will project scissor/clipping rectangles into framebuffer space
ImVec2 clip_off = draw_data - > DisplayPos ; // (0,0) unless using multi-viewports
@ -251,10 +255,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
const ImDrawList * cmd_list = draw_data - > CmdLists [ n ] ;
size_t idx_buffer_offset = 0 ;
glBindBuffer ( GL_ARRAY_BUFFER , g_VboHandle ) ;
// Upload vertex/index buffers
glBufferData ( GL_ARRAY_BUFFER , ( GLsizeiptr ) cmd_list - > VtxBuffer . Size * sizeof ( ImDrawVert ) , ( const GLvoid * ) cmd_list - > VtxBuffer . Data , GL_STREAM_DRAW ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , g_ElementsHandle ) ;
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 + + )
@ -280,7 +282,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
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 's glClipControl(GL_UPPER_LEFT)
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 ) ;
@ -290,8 +292,10 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
idx_buffer_offset + = pcmd - > ElemCount * sizeof ( ImDrawIdx ) ;
}
}
// Destroy the temporary VAO
# ifndef IMGUI_IMPL_OPENGL_ES2
glDeleteVertexArrays ( 1 , & vao_handle ) ;
glDeleteVertexArrays ( 1 , & v ertex_array_object ) ;
# endif
// Restore modified GL state
@ -302,7 +306,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
# endif
glActiveTexture ( last_active_texture ) ;
# ifndef IMGUI_IMPL_OPENGL_ES2
glBindVertexArray ( last_vertex_array ) ;
glBindVertexArray ( last_vertex_array _object ) ;
# endif
glBindBuffer ( GL_ARRAY_BUFFER , last_array_buffer ) ;
glBlendEquationSeparate ( last_blend_equation_rgb , last_blend_equation_alpha ) ;
@ -554,9 +558,9 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects()
g_AttribLocationTex = glGetUniformLocation ( g_ShaderHandle , " Texture " ) ;
g_AttribLocationProjMtx = glGetUniformLocation ( g_ShaderHandle , " ProjMtx " ) ;
g_AttribLocation Position = glGetAttribLocation ( g_ShaderHandle , " Position " ) ;
g_AttribLocation UV = glGetAttribLocation ( g_ShaderHandle , " UV " ) ;
g_AttribLocation Color = glGetAttribLocation ( g_ShaderHandle , " Color " ) ;
g_AttribLocation Vtx Pos = glGetAttribLocation ( g_ShaderHandle , " Position " ) ;
g_AttribLocation Vtx UV = glGetAttribLocation ( g_ShaderHandle , " UV " ) ;
g_AttribLocation Vtx Color = glGetAttribLocation ( g_ShaderHandle , " Color " ) ;
// Create buffers
glGenBuffers ( 1 , & g_VboHandle ) ;