@ -14,18 +14,18 @@
# include <GLFW/glfw3native.h>
# include <GLFW/glfw3native.h>
# endif
# endif
static GLFWwindow * g_ w indow = NULL ;
static GLFWwindow * g_ W indow = NULL ;
static bool g_ mouse_p ressed[ 3 ] = { false , false , false } ;
static bool g_ MouseP ressed[ 3 ] = { false , false , false } ;
static float g_ mouse_w heel = 0.0f ;
static float g_ MouseW heel = 0.0f ;
static double g_ t ime = 0.0f ;
static double g_ T ime = 0.0f ;
static bool g_ font_texture_l oaded = false ;
static bool g_ FontTextureL oaded = false ;
// Handles for OpenGL3 rendering
// Handles for OpenGL3 rendering
static int g_ shader_handle = 0 , g_vert_handle = 0 , g_frag_h andle = 0 ;
static int g_ ShaderHandle = 0 , g_VertHandle = 0 , g_FragH andle = 0 ;
static int g_ texture_location = 0 , g_proj_mtx_location = 0 ;
static int g_ AttribLocationTex = 0 , g_AttribLocationProjMtx = 0 ;
static int g_ position_location = 0 , g_uv_location = 0 , g_colour_location = 0 ;
static int g_ AttribLocationPosition = 0 , g_AttribLocationUV = 0 , g_AttribLocationColor = 0 ;
static size_t g_ vbo_max_s ize = 20000 ;
static size_t g_ VboMaxS ize = 20000 ;
static unsigned int g_ vbo_handle = 0 , g_vao_h andle = 0 ;
static unsigned int g_ VboHandle = 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)
// 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:
// 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 } ,
{ 0.0f , 0.0f , - 1.0f , 0.0f } ,
{ - 1.0f , 1.0f , 0.0f , 1.0f } ,
{ - 1.0f , 1.0f , 0.0f , 1.0f } ,
} ;
} ;
glUseProgram ( g_ shader_h andle) ;
glUseProgram ( g_ ShaderH andle) ;
glUniform1i ( g_ texture_location , 0 ) ;
glUniform1i ( g_ AttribLocationTex , 0 ) ;
glUniformMatrix4fv ( g_ proj_mtx_location , 1 , GL_FALSE , & ortho_projection [ 0 ] [ 0 ] ) ;
glUniformMatrix4fv ( g_ AttribLocationProjMtx , 1 , GL_FALSE , & ortho_projection [ 0 ] [ 0 ] ) ;
// Grow our buffer according to what we need
// Grow our buffer according to what we need
size_t total_vtx_count = 0 ;
size_t total_vtx_count = 0 ;
for ( int n = 0 ; n < cmd_lists_count ; n + + )
for ( int n = 0 ; n < cmd_lists_count ; n + + )
total_vtx_count + = cmd_lists [ n ] - > vtx_buffer . size ( ) ;
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 ) ;
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
g_ VboMaxS ize = neededBufferSize + 5000 ; // Grow buffer
glBufferData ( GL_ARRAY_BUFFER , g_ vbo_max_s ize, NULL , GL_STREAM_DRAW ) ;
glBufferData ( GL_ARRAY_BUFFER , g_ VboMaxS ize, NULL , GL_STREAM_DRAW ) ;
}
}
// Copy and convert all vertices into a single contiguous buffer
// 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 ) ;
glUnmapBuffer ( GL_ARRAY_BUFFER ) ;
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
glBindVertexArray ( g_ vao_h andle) ;
glBindVertexArray ( g_ VaoH andle) ;
int cmd_offset = 0 ;
int cmd_offset = 0 ;
for ( int n = 0 ; n < cmd_lists_count ; n + + )
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 ( )
static const char * ImGui_ImplGlfwGL3_GetClipboardText ( )
{
{
return glfwGetClipboardString ( g_ w indow) ;
return glfwGetClipboardString ( g_ W indow) ;
}
}
static void ImGui_ImplGlfwGL3_SetClipboardText ( const char * text )
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 )
void ImGui_ImplGlfwGL3_MouseButtonCallback ( GLFWwindow * window , int button , int action , int mods )
{
{
if ( action = = GLFW_PRESS & & button > = 0 & & button < 3 )
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 )
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 )
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
// Store our identifier
io . Fonts - > TexID = ( void * ) ( intptr_t ) tex_id ;
io . Fonts - > TexID = ( void * ) ( intptr_t ) tex_id ;
g_ font_texture_l oaded = true ;
g_ FontTextureL oaded = true ;
}
}
static void InitGL ( )
static void InitGL ( )
@ -195,38 +195,38 @@ static void InitGL()
" Out_Color = Frag_Color * texture( Texture, Frag_UV.st); \n "
" Out_Color = Frag_Color * texture( Texture, Frag_UV.st); \n "
" } \n " ;
" } \n " ;
g_ shader_h andle = glCreateProgram ( ) ;
g_ ShaderH andle = glCreateProgram ( ) ;
g_ vert_h andle = glCreateShader ( GL_VERTEX_SHADER ) ;
g_ VertH andle = glCreateShader ( GL_VERTEX_SHADER ) ;
g_ frag_h andle = glCreateShader ( GL_FRAGMENT_SHADER ) ;
g_ FragH andle = glCreateShader ( GL_FRAGMENT_SHADER ) ;
glShaderSource ( g_ vert_h andle, 1 , & vertex_shader , 0 ) ;
glShaderSource ( g_ VertH andle, 1 , & vertex_shader , 0 ) ;
glShaderSource ( g_ frag_h andle, 1 , & fragment_shader , 0 ) ;
glShaderSource ( g_ FragH andle, 1 , & fragment_shader , 0 ) ;
glCompileShader ( g_ vert_h andle) ;
glCompileShader ( g_ VertH andle) ;
glCompileShader ( g_ frag_h andle) ;
glCompileShader ( g_ FragH andle) ;
glAttachShader ( g_ shader_handle, g_vert_h andle) ;
glAttachShader ( g_ ShaderHandle, g_VertH andle) ;
glAttachShader ( g_ shader_handle, g_frag_h andle) ;
glAttachShader ( g_ ShaderHandle, g_FragH andle) ;
glLinkProgram ( g_ shader_h andle) ;
glLinkProgram ( g_ ShaderH andle) ;
g_ texture_location = glGetUniformLocation ( g_shader_h andle, " Texture " ) ;
g_ AttribLocationTex = glGetUniformLocation ( g_ShaderH andle, " Texture " ) ;
g_ proj_mtx_location = glGetUniformLocation ( g_shader_h andle, " ProjMtx " ) ;
g_ AttribLocationProjMtx = glGetUniformLocation ( g_ShaderH andle, " ProjMtx " ) ;
g_ position_location = glGetAttribLocation ( g_shader_h andle, " Position " ) ;
g_ AttribLocationPosition = glGetAttribLocation ( g_ShaderH andle, " Position " ) ;
g_ uv_location = glGetAttribLocation ( g_shader_h andle, " UV " ) ;
g_ AttribLocationUV = glGetAttribLocation ( g_ShaderH andle, " UV " ) ;
g_ colour_location = glGetAttribLocation ( g_shader_h andle, " Color " ) ;
g_ AttribLocationColor = glGetAttribLocation ( g_ShaderH andle, " Color " ) ;
glGenBuffers ( 1 , & g_ vbo_h andle) ;
glGenBuffers ( 1 , & g_ VboH andle) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_ vbo_h andle) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_ VboH andle) ;
glBufferData ( GL_ARRAY_BUFFER , g_ vbo_max_s ize, NULL , GL_DYNAMIC_DRAW ) ;
glBufferData ( GL_ARRAY_BUFFER , g_ VboMaxS ize, NULL , GL_DYNAMIC_DRAW ) ;
glGenVertexArrays ( 1 , & g_ vao_h andle) ;
glGenVertexArrays ( 1 , & g_ VaoH andle) ;
glBindVertexArray ( g_ vao_h andle) ;
glBindVertexArray ( g_ VaoH andle) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_ vbo_h andle) ;
glBindBuffer ( GL_ARRAY_BUFFER , g_ VboH andle) ;
glEnableVertexAttribArray ( g_ position_loca tion) ;
glEnableVertexAttribArray ( g_ AttribLocationPosi tion) ;
glEnableVertexAttribArray ( g_ uv_location ) ;
glEnableVertexAttribArray ( g_ AttribLocationUV ) ;
glEnableVertexAttribArray ( g_ colour_location ) ;
glEnableVertexAttribArray ( g_ AttribLocationColor ) ;
# define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
# 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_ AttribLocationPosi 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_ AttribLocationUV , 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_ AttribLocationColor , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof ( ImDrawVert ) , ( GLvoid * ) OFFSETOF ( ImDrawVert , col ) ) ;
# undef OFFSETOF
# undef OFFSETOF
glBindVertexArray ( 0 ) ;
glBindVertexArray ( 0 ) ;
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
@ -236,7 +236,7 @@ bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks)
{
{
InitGL ( ) ;
InitGL ( ) ;
g_ w indow = window ;
g_ W indow = window ;
ImGuiIO & io = ImGui : : GetIO ( ) ;
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.
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 . SetClipboardTextFn = ImGui_ImplGlfwGL3_SetClipboardText ;
io . GetClipboardTextFn = ImGui_ImplGlfwGL3_GetClipboardText ;
io . GetClipboardTextFn = ImGui_ImplGlfwGL3_GetClipboardText ;
# ifdef _MSC_VER
# ifdef _MSC_VER
io . ImeWindowHandle = glfwGetWin32Window ( g_ w indow) ;
io . ImeWindowHandle = glfwGetWin32Window ( g_ W indow) ;
# endif
# endif
if ( install_callbacks )
if ( install_callbacks )
@ -277,21 +277,21 @@ bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks)
void ImGui_ImplGlfwGL3_Shutdown ( )
void ImGui_ImplGlfwGL3_Shutdown ( )
{
{
if ( g_ vao_handle) glDeleteVertexArrays ( 1 , & g_vao_h andle) ;
if ( g_ VaoHandle) glDeleteVertexArrays ( 1 , & g_VaoH andle) ;
if ( g_ vbo_handle) glDeleteBuffers ( 1 , & g_vbo_h andle) ;
if ( g_ VboHandle) glDeleteBuffers ( 1 , & g_VboH andle) ;
g_ vao_h andle = 0 ;
g_ VaoH andle = 0 ;
g_ vbo_h andle = 0 ;
g_ VboH andle = 0 ;
glDetachShader ( g_ shader_handle, g_vert_h andle) ;
glDetachShader ( g_ ShaderHandle, g_VertH andle) ;
glDeleteShader ( g_ vert_h andle) ;
glDeleteShader ( g_ VertH andle) ;
g_ vert_h andle = 0 ;
g_ VertH andle = 0 ;
glDetachShader ( g_ shader_handle, g_frag_h andle) ;
glDetachShader ( g_ ShaderHandle, g_FragH andle) ;
glDeleteShader ( g_ frag_h andle) ;
glDeleteShader ( g_ FragH andle) ;
g_ frag_h andle = 0 ;
g_ FragH andle = 0 ;
glDeleteProgram ( g_ shader_h andle) ;
glDeleteProgram ( g_ ShaderH andle) ;
g_ shader_h andle = 0 ;
g_ ShaderH andle = 0 ;
GLuint tex_id = ( GLuint ) ImGui : : GetIO ( ) . Fonts - > TexID ;
GLuint tex_id = ( GLuint ) ImGui : : GetIO ( ) . Fonts - > TexID ;
if ( tex_id )
if ( tex_id )
@ -304,7 +304,7 @@ void ImGui_ImplGlfwGL3_Shutdown()
void ImGui_ImplGlfwGL3_NewFrame ( )
void ImGui_ImplGlfwGL3_NewFrame ( )
{
{
if ( ! g_ font_texture_l oaded)
if ( ! g_ FontTextureL oaded)
ImGui_ImplGlfwGL3_LoadFontsTexture ( ) ;
ImGui_ImplGlfwGL3_LoadFontsTexture ( ) ;
ImGuiIO & io = ImGui : : GetIO ( ) ;
ImGuiIO & io = ImGui : : GetIO ( ) ;
@ -312,32 +312,32 @@ void ImGui_ImplGlfwGL3_NewFrame()
// Setup display size (every frame to accommodate for window resizing)
// Setup display size (every frame to accommodate for window resizing)
int w , h ;
int w , h ;
int display_w , display_h ;
int display_w , display_h ;
glfwGetWindowSize ( g_ w indow, & w , & h ) ;
glfwGetWindowSize ( g_ W indow, & w , & h ) ;
glfwGetFramebufferSize ( g_ w indow, & display_w , & display_h ) ;
glfwGetFramebufferSize ( g_ W indow, & display_w , & display_h ) ;
io . DisplaySize = ImVec2 ( ( float ) display_w , ( float ) display_h ) ;
io . DisplaySize = ImVec2 ( ( float ) display_w , ( float ) display_h ) ;
// Setup time step
// Setup time step
double current_time = glfwGetTime ( ) ;
double current_time = glfwGetTime ( ) ;
io . DeltaTime = g_ t ime > 0.0 ? ( float ) ( current_time - g_ t ime) : ( float ) ( 1.0f / 60.0f ) ;
io . DeltaTime = g_ T ime > 0.0 ? ( float ) ( current_time - g_ T ime) : ( float ) ( 1.0f / 60.0f ) ;
g_ t ime = current_time ;
g_ T ime = current_time ;
// Setup inputs
// Setup inputs
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
double mouse_x , mouse_y ;
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_x * = ( float ) display_w / w ; // Convert mouse coordinates to pixels
mouse_y * = ( float ) display_h / h ;
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 . 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_pressed[ 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 [ 0 ] = g_ MousePressed[ 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_pressed[ 1 ] | | glfwGetMouseButton ( g_w indow, GLFW_MOUSE_BUTTON_RIGHT ) ! = 0 ;
io . MouseDown [ 1 ] = g_ MousePressed[ 1 ] | | glfwGetMouseButton ( g_W indow, GLFW_MOUSE_BUTTON_RIGHT ) ! = 0 ;
io . MouseDown [ 2 ] = g_ mouse_pressed[ 2 ] | | glfwGetMouseButton ( g_w indow, GLFW_MOUSE_BUTTON_MIDDLE ) ! = 0 ;
io . MouseDown [ 2 ] = g_ MousePressed[ 2 ] | | glfwGetMouseButton ( g_W indow, GLFW_MOUSE_BUTTON_MIDDLE ) ! = 0 ;
g_ mouse_p ressed[ 0 ] = false ;
g_ MouseP ressed[ 0 ] = false ;
g_ mouse_p ressed[ 1 ] = false ;
g_ MouseP ressed[ 1 ] = false ;
g_ mouse_p ressed[ 2 ] = false ;
g_ MouseP ressed[ 2 ] = false ;
io . MouseWheel = g_ mouse_w heel;
io . MouseWheel = g_ MouseW heel;
g_ mouse_w heel = 0.0f ;
g_ MouseW heel = 0.0f ;
// Start the frame
// Start the frame
ImGui : : NewFrame ( ) ;
ImGui : : NewFrame ( ) ;