@ -14,18 +14,18 @@
# include <GLFW/glfw3native.h>
# endif
static GLFWwindow * g_w indow = NULL ;
static bool g_mouse_p ressed [ 3 ] = { false , false , false } ;
static float g_mouse_w heel = 0.0f ;
static double g_t ime = 0.0f ;
static bool g_font_texture_l oaded = false ;
static GLFWwindow * g_W indow = NULL ;
static bool g_MouseP ressed [ 3 ] = { false , false , false } ;
static float g_MouseW heel = 0.0f ;
static double g_T ime = 0.0f ;
static bool g_FontTextureL oaded = false ;
// Handles for OpenGL3 rendering
static int g_shader_h andle = 0 , g_vert_h andle = 0 , g_frag_h andle = 0 ;
static int g_texture_location = 0 , g_proj_mtx_location = 0 ;
static int g_position_loca tion = 0 , g_uv_location = 0 , g_colour_location = 0 ;
static size_t g_vbo_max_s ize = 20000 ;
static unsigned int g_vbo_h andle = 0 , g_vao_h andle = 0 ;
static int g_ShaderH andle = 0 , g_VertH andle = 0 , g_FragH andle = 0 ;
static int g_AttribLocationTex = 0 , g_AttribLocationProjMtx = 0 ;
static int g_AttribLocationPosi tion = 0 , g_AttribLocationUV = 0 , g_AttribLocationColor = 0 ;
static size_t g_VboMaxS ize = 20000 ;
static unsigned int g_VboH andle = 0 , g_VaoH andle = 0 ;
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
// If text or lines are blurry when integrating ImGui in your engine:
@ -54,20 +54,20 @@ static void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawList** const cmd_lists, int
{ 0.0f , 0.0f , - 1.0f , 0.0f } ,
{ - 1.0f , 1.0f , 0.0f , 1.0f } ,
} ;
glUseProgram ( g_shader_h andle ) ;
glUniform1i ( g_texture_location , 0 ) ;
glUniformMatrix4fv ( g_proj_mtx_location , 1 , GL_FALSE , & ortho_projection [ 0 ] [ 0 ] ) ;
glUseProgram ( g_ShaderH andle ) ;
glUniform1i ( g_AttribLocationTex , 0 ) ;
glUniformMatrix4fv ( g_AttribLocationProjMtx , 1 , GL_FALSE , & ortho_projection [ 0 ] [ 0 ] ) ;
// Grow our buffer according to what we need
size_t total_vtx_count = 0 ;
for ( int n = 0 ; n < cmd_lists_count ; n + + )
total_vtx_count + = cmd_lists [ n ] - > vtx_buffer . size ( ) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_vbo_h andle ) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_VboH andle ) ;
size_t neededBufferSize = total_vtx_count * sizeof ( ImDrawVert ) ;
if ( neededBufferSize > g_vbo_max_s ize )
if ( neededBufferSize > g_VboMaxS ize )
{
g_vbo_max_s ize = neededBufferSize + 5000 ; // Grow buffer
glBufferData ( GL_ARRAY_BUFFER , g_vbo_max_s ize , NULL , GL_STREAM_DRAW ) ;
g_VboMaxS ize = neededBufferSize + 5000 ; // Grow buffer
glBufferData ( GL_ARRAY_BUFFER , g_VboMaxS ize , NULL , GL_STREAM_DRAW ) ;
}
// Copy and convert all vertices into a single contiguous buffer
@ -82,7 +82,7 @@ static void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawList** const cmd_lists, int
}
glUnmapBuffer ( GL_ARRAY_BUFFER ) ;
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
glBindVertexArray ( g_vao_h andle ) ;
glBindVertexArray ( g_VaoH andle ) ;
int cmd_offset = 0 ;
for ( int n = 0 ; n < cmd_lists_count ; n + + )
@ -109,23 +109,23 @@ static void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawList** const cmd_lists, int
static const char * ImGui_ImplGlfwGL3_GetClipboardText ( )
{
return glfwGetClipboardString ( g_w indow ) ;
return glfwGetClipboardString ( g_W indow ) ;
}
static void ImGui_ImplGlfwGL3_SetClipboardText ( const char * text )
{
glfwSetClipboardString ( g_w indow , text ) ;
glfwSetClipboardString ( g_W indow , text ) ;
}
void ImGui_ImplGlfwGL3_MouseButtonCallback ( GLFWwindow * window , int button , int action , int mods )
{
if ( action = = GLFW_PRESS & & button > = 0 & & button < 3 )
g_mouse_p ressed [ button ] = true ;
g_MouseP ressed [ button ] = true ;
}
void ImGui_ImplGlfwGL3_ScrollCallback ( GLFWwindow * window , double xoffset , double yoffset )
{
g_mouse_w heel + = ( float ) yoffset ; // Use fractional mouse wheel, 1.0 unit 5 lines.
g_MouseW heel + = ( float ) yoffset ; // Use fractional mouse wheel, 1.0 unit 5 lines.
}
void ImGui_ImplGlfwGL3_KeyCallback ( GLFWwindow * window , int key , int scancode , int action , int mods )
@ -164,7 +164,7 @@ void ImGui_ImplGlfwGL3_LoadFontsTexture()
// Store our identifier
io . Fonts - > TexID = ( void * ) ( intptr_t ) tex_id ;
g_font_texture_l oaded = true ;
g_FontTextureL oaded = true ;
}
static void InitGL ( )
@ -195,38 +195,38 @@ static void InitGL()
" Out_Color = Frag_Color * texture( Texture, Frag_UV.st); \n "
" } \n " ;
g_shader_h andle = glCreateProgram ( ) ;
g_vert_h andle = glCreateShader ( GL_VERTEX_SHADER ) ;
g_frag_h andle = glCreateShader ( GL_FRAGMENT_SHADER ) ;
glShaderSource ( g_vert_h andle , 1 , & vertex_shader , 0 ) ;
glShaderSource ( g_frag_h andle , 1 , & fragment_shader , 0 ) ;
glCompileShader ( g_vert_h andle ) ;
glCompileShader ( g_frag_h andle ) ;
glAttachShader ( g_shader_handle , g_vert_h andle ) ;
glAttachShader ( g_shader_handle , g_frag_h andle ) ;
glLinkProgram ( g_shader_h andle ) ;
g_texture_location = glGetUniformLocation ( g_shader_h andle , " Texture " ) ;
g_proj_mtx_location = glGetUniformLocation ( g_shader_h andle , " ProjMtx " ) ;
g_position_loca tion = glGetAttribLocation ( g_shader_h andle , " Position " ) ;
g_uv_location = glGetAttribLocation ( g_shader_h andle , " UV " ) ;
g_colour_location = glGetAttribLocation ( g_shader_h andle , " Color " ) ;
glGenBuffers ( 1 , & g_vbo_h andle ) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_vbo_h andle ) ;
glBufferData ( GL_ARRAY_BUFFER , g_vbo_max_s ize , NULL , GL_DYNAMIC_DRAW ) ;
glGenVertexArrays ( 1 , & g_vao_h andle ) ;
glBindVertexArray ( g_vao_h andle ) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_vbo_h andle ) ;
glEnableVertexAttribArray ( g_position_loca tion ) ;
glEnableVertexAttribArray ( g_uv_location ) ;
glEnableVertexAttribArray ( g_colour_location ) ;
g_ShaderH andle = glCreateProgram ( ) ;
g_VertH andle = glCreateShader ( GL_VERTEX_SHADER ) ;
g_FragH andle = glCreateShader ( GL_FRAGMENT_SHADER ) ;
glShaderSource ( g_VertH andle , 1 , & vertex_shader , 0 ) ;
glShaderSource ( g_FragH andle , 1 , & fragment_shader , 0 ) ;
glCompileShader ( g_VertH andle ) ;
glCompileShader ( g_FragH andle ) ;
glAttachShader ( g_ShaderHandle , g_VertH andle ) ;
glAttachShader ( g_ShaderHandle , g_FragH andle ) ;
glLinkProgram ( g_ShaderH andle ) ;
g_AttribLocationTex = glGetUniformLocation ( g_ShaderH andle , " Texture " ) ;
g_AttribLocationProjMtx = glGetUniformLocation ( g_ShaderH andle , " ProjMtx " ) ;
g_AttribLocationPosi tion = glGetAttribLocation ( g_ShaderH andle , " Position " ) ;
g_AttribLocationUV = glGetAttribLocation ( g_ShaderH andle , " UV " ) ;
g_AttribLocationColor = glGetAttribLocation ( g_ShaderH andle , " Color " ) ;
glGenBuffers ( 1 , & g_VboH andle ) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_VboH andle ) ;
glBufferData ( GL_ARRAY_BUFFER , g_VboMaxS ize , NULL , GL_DYNAMIC_DRAW ) ;
glGenVertexArrays ( 1 , & g_VaoH andle ) ;
glBindVertexArray ( g_VaoH andle ) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_VboH andle ) ;
glEnableVertexAttribArray ( g_AttribLocationPosi tion ) ;
glEnableVertexAttribArray ( g_AttribLocationUV ) ;
glEnableVertexAttribArray ( g_AttribLocationColor ) ;
# define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
glVertexAttribPointer ( g_position_loca tion , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) OFFSETOF ( ImDrawVert , pos ) ) ;
glVertexAttribPointer ( g_uv_location , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) OFFSETOF ( ImDrawVert , uv ) ) ;
glVertexAttribPointer ( g_colour_location , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof ( ImDrawVert ) , ( GLvoid * ) OFFSETOF ( ImDrawVert , col ) ) ;
glVertexAttribPointer ( g_AttribLocationPosi tion , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) OFFSETOF ( ImDrawVert , pos ) ) ;
glVertexAttribPointer ( g_AttribLocationUV , 2 , GL_FLOAT , GL_FALSE , sizeof ( ImDrawVert ) , ( GLvoid * ) OFFSETOF ( ImDrawVert , uv ) ) ;
glVertexAttribPointer ( g_AttribLocationColor , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof ( ImDrawVert ) , ( GLvoid * ) OFFSETOF ( ImDrawVert , col ) ) ;
# undef OFFSETOF
glBindVertexArray ( 0 ) ;
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
@ -236,7 +236,7 @@ bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks)
{
InitGL ( ) ;
g_w indow = window ;
g_W indow = window ;
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . KeyMap [ ImGuiKey_Tab ] = GLFW_KEY_TAB ; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
@ -261,7 +261,7 @@ bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks)
io . SetClipboardTextFn = ImGui_ImplGlfwGL3_SetClipboardText ;
io . GetClipboardTextFn = ImGui_ImplGlfwGL3_GetClipboardText ;
# ifdef _MSC_VER
io . ImeWindowHandle = glfwGetWin32Window ( g_w indow ) ;
io . ImeWindowHandle = glfwGetWin32Window ( g_W indow ) ;
# endif
if ( install_callbacks )
@ -277,21 +277,21 @@ bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks)
void ImGui_ImplGlfwGL3_Shutdown ( )
{
if ( g_vao_h andle ) glDeleteVertexArrays ( 1 , & g_vao_h andle ) ;
if ( g_vbo_h andle ) glDeleteBuffers ( 1 , & g_vbo_h andle ) ;
g_vao_h andle = 0 ;
g_vbo_h andle = 0 ;
if ( g_VaoH andle ) glDeleteVertexArrays ( 1 , & g_VaoH andle ) ;
if ( g_VboH andle ) glDeleteBuffers ( 1 , & g_VboH andle ) ;
g_VaoH andle = 0 ;
g_VboH andle = 0 ;
glDetachShader ( g_shader_handle , g_vert_h andle ) ;
glDeleteShader ( g_vert_h andle ) ;
g_vert_h andle = 0 ;
glDetachShader ( g_ShaderHandle , g_VertH andle ) ;
glDeleteShader ( g_VertH andle ) ;
g_VertH andle = 0 ;
glDetachShader ( g_shader_handle , g_frag_h andle ) ;
glDeleteShader ( g_frag_h andle ) ;
g_frag_h andle = 0 ;
glDetachShader ( g_ShaderHandle , g_FragH andle ) ;
glDeleteShader ( g_FragH andle ) ;
g_FragH andle = 0 ;
glDeleteProgram ( g_shader_h andle ) ;
g_shader_h andle = 0 ;
glDeleteProgram ( g_ShaderH andle ) ;
g_ShaderH andle = 0 ;
GLuint tex_id = ( GLuint ) ImGui : : GetIO ( ) . Fonts - > TexID ;
if ( tex_id )
@ -304,7 +304,7 @@ void ImGui_ImplGlfwGL3_Shutdown()
void ImGui_ImplGlfwGL3_NewFrame ( )
{
if ( ! g_font_texture_l oaded )
if ( ! g_FontTextureL oaded )
ImGui_ImplGlfwGL3_LoadFontsTexture ( ) ;
ImGuiIO & io = ImGui : : GetIO ( ) ;
@ -312,32 +312,32 @@ void ImGui_ImplGlfwGL3_NewFrame()
// Setup display size (every frame to accommodate for window resizing)
int w , h ;
int display_w , display_h ;
glfwGetWindowSize ( g_w indow , & w , & h ) ;
glfwGetFramebufferSize ( g_w indow , & display_w , & display_h ) ;
glfwGetWindowSize ( g_W indow , & w , & h ) ;
glfwGetFramebufferSize ( g_W indow , & display_w , & display_h ) ;
io . DisplaySize = ImVec2 ( ( float ) display_w , ( float ) display_h ) ;
// Setup time step
double current_time = glfwGetTime ( ) ;
io . DeltaTime = g_t ime > 0.0 ? ( float ) ( current_time - g_t ime ) : ( float ) ( 1.0f / 60.0f ) ;
g_t ime = current_time ;
io . DeltaTime = g_T ime > 0.0 ? ( float ) ( current_time - g_T ime ) : ( float ) ( 1.0f / 60.0f ) ;
g_T ime = current_time ;
// Setup inputs
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
double mouse_x , mouse_y ;
glfwGetCursorPos ( g_w indow , & mouse_x , & mouse_y ) ;
glfwGetCursorPos ( g_W indow , & mouse_x , & mouse_y ) ;
mouse_x * = ( float ) display_w / w ; // Convert mouse coordinates to pixels
mouse_y * = ( float ) display_h / h ;
io . MousePos = ImVec2 ( ( float ) mouse_x , ( float ) mouse_y ) ; // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
io . MouseDown [ 0 ] = g_mouse_p ressed [ 0 ] | | glfwGetMouseButton ( g_w indow , GLFW_MOUSE_BUTTON_LEFT ) ! = 0 ; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
io . MouseDown [ 1 ] = g_mouse_p ressed [ 1 ] | | glfwGetMouseButton ( g_w indow , GLFW_MOUSE_BUTTON_RIGHT ) ! = 0 ;
io . MouseDown [ 2 ] = g_mouse_p ressed [ 2 ] | | glfwGetMouseButton ( g_w indow , GLFW_MOUSE_BUTTON_MIDDLE ) ! = 0 ;
g_mouse_p ressed [ 0 ] = false ;
g_mouse_p ressed [ 1 ] = false ;
g_mouse_p ressed [ 2 ] = false ;
io . MouseDown [ 0 ] = g_MouseP ressed [ 0 ] | | glfwGetMouseButton ( g_W indow , GLFW_MOUSE_BUTTON_LEFT ) ! = 0 ; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
io . MouseDown [ 1 ] = g_MouseP ressed [ 1 ] | | glfwGetMouseButton ( g_W indow , GLFW_MOUSE_BUTTON_RIGHT ) ! = 0 ;
io . MouseDown [ 2 ] = g_MouseP ressed [ 2 ] | | glfwGetMouseButton ( g_W indow , GLFW_MOUSE_BUTTON_MIDDLE ) ! = 0 ;
g_MouseP ressed [ 0 ] = false ;
g_MouseP ressed [ 1 ] = false ;
g_MouseP ressed [ 2 ] = false ;
io . MouseWheel = g_mouse_w heel ;
g_mouse_w heel = 0.0f ;
io . MouseWheel = g_MouseW heel ;
g_MouseW heel = 0.0f ;
// Start the frame
ImGui : : NewFrame ( ) ;