1 // dear imgui: Renderer for OpenGL3 / OpenGL ES2 / OpenGL ES3 (modern OpenGL with shaders / programmatic pipeline)
2 // This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..)
3 // (Note: We are using GL3W as a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc..)
4 
5 // Implemented features:
6 //  [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
7 
8 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
9 // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
10 // https://github.com/ocornut/imgui
11 
12 // CHANGELOG
13 // (minor and older changes stripped away, please see git history for details)
14 //  2019-02-01: OpenGL: Using GLSL 410 shaders for any version over 410 (e.g. 430, 450).
15 //  2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
16 //  2018-11-13: OpenGL: Support for GL 4.5's glClipControl(GL_UPPER_LEFT).
17 //  2018-08-29: OpenGL: Added support for more OpenGL loaders: glew and glad, with comments indicative that any loader can be used.
18 //  2018-08-09: OpenGL: Default to OpenGL ES 3 on iOS and Android. GLSL version default to "#version 300 ES".
19 //  2018-07-30: OpenGL: Support for GLSL 300 ES and 410 core. Fixes for Emscripten compilation.
20 //  2018-07-10: OpenGL: Support for more GLSL versions (based on the GLSL version string). Added error output when shaders fail to compile/link.
21 //  2018-06-08: Misc: Extracted imgui_impl_opengl3.cpp/.h away from the old combined GLFW/SDL+OpenGL3 examples.
22 //  2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
23 //  2018-05-25: OpenGL: Removed unnecessary backup/restore of GL_ELEMENT_ARRAY_BUFFER_BINDING since this is part of the VAO state.
24 //  2018-05-14: OpenGL: Making the call to glBindSampler() optional so 3.2 context won't fail if the function is a NULL pointer.
25 //  2018-03-06: OpenGL: Added const char* glsl_version parameter to ImGui_ImplOpenGL3_Init() so user can override the GLSL version e.g. "#version 150".
26 //  2018-02-23: OpenGL: Create the VAO in the render function so the setup can more easily be used with multiple shared GL context.
27 //  2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplSdlGL3_RenderDrawData() in the .h file so you can call it yourself.
28 //  2018-01-07: OpenGL: Changed GLSL shader version from 330 to 150.
29 //  2017-09-01: OpenGL: Save and restore current bound sampler. Save and restore current polygon mode.
30 //  2017-05-01: OpenGL: Fixed save and restore of current blend func state.
31 //  2017-05-01: OpenGL: Fixed save and restore of current GL_ACTIVE_TEXTURE.
32 //  2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle.
33 //  2016-07-29: OpenGL: Explicitly setting GL_UNPACK_ROW_LENGTH to reduce issues because SDL changes it. (#752)
34 
35 //----------------------------------------
36 // OpenGL    GLSL      GLSL
37 // version   version   string
38 //----------------------------------------
39 //  2.0       110       "#version 110"
40 //  2.1       120
41 //  3.0       130
42 //  3.1       140
43 //  3.2       150       "#version 150"
44 //  3.3       330
45 //  4.0       400
46 //  4.1       410       "#version 410 core"
47 //  4.2       420
48 //  4.3       430
49 //  ES 2.0    100       "#version 100"
50 //  ES 3.0    300       "#version 300 es"
51 //----------------------------------------
52 
53 #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
54 #define _CRT_SECURE_NO_WARNINGS
55 #endif
56 
57 // MOD_ERIN
58 #include "imgui/imgui.h"
59 #include "imgui_impl_opengl3.h"
60 #include <stdio.h>
61 #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier
62 #include <stddef.h>     // intptr_t
63 #else
64 #include <stdint.h>     // intptr_t
65 #endif
66 #if defined(__APPLE__)
67 #include "TargetConditionals.h"
68 #endif
69 
70 // iOS, Android and Emscripten can use GL ES 3
71 // Call ImGui_ImplOpenGL3_Init() with "#version 300 es"
72 #if (defined(__APPLE__) && TARGET_OS_IOS) || (defined(__ANDROID__)) || (defined(__EMSCRIPTEN__))
73 #define USE_GL_ES3
74 #endif
75 
76 #ifdef USE_GL_ES3
77 // OpenGL ES 3
78 #include <GLES3/gl3.h>  // Use GL ES 3
79 #else
80 // Regular OpenGL
81 // About OpenGL function loaders: modern OpenGL doesn't have a standard header file and requires individual function pointers to be loaded manually.
82 // Helper libraries are often used for this purpose! Here we are supporting a few common ones: gl3w, glew, glad.
83 // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
84 #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
85 #include <GL/gl3w.h>
86 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
87 #include <GL/glew.h>
88 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
89 // MOD_ERIN
90 #include "glad/gl.h"
91 #else
92 #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM
93 #endif
94 #endif
95 
96 // OpenGL Data
97 static char         g_GlslVersionString[32] = "";
98 static GLuint       g_FontTexture = 0;
99 static GLuint       g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
100 static int          g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0;
101 static int          g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0;
102 static unsigned int g_VboHandle = 0, g_ElementsHandle = 0;
103 
104 // Functions
ImGui_ImplOpenGL3_Init(const char * glsl_version)105 bool    ImGui_ImplOpenGL3_Init(const char* glsl_version)
106 {
107     ImGuiIO& io = ImGui::GetIO();
108     io.BackendRendererName = "imgui_impl_opengl3";
109 
110     // 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.
111 #ifdef USE_GL_ES3
112     if (glsl_version == NULL)
113         glsl_version = "#version 300 es";
114 #else
115     if (glsl_version == NULL)
116         glsl_version = "#version 130";
117 #endif
118     IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString));
119     strcpy(g_GlslVersionString, glsl_version);
120     strcat(g_GlslVersionString, "\n");
121 
122     return true;
123 }
124 
ImGui_ImplOpenGL3_Shutdown()125 void    ImGui_ImplOpenGL3_Shutdown()
126 {
127     ImGui_ImplOpenGL3_DestroyDeviceObjects();
128 }
129 
ImGui_ImplOpenGL3_NewFrame()130 void    ImGui_ImplOpenGL3_NewFrame()
131 {
132     if (!g_FontTexture)
133         ImGui_ImplOpenGL3_CreateDeviceObjects();
134 }
135 
136 // OpenGL3 Render function.
137 // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
138 // 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.
ImGui_ImplOpenGL3_RenderDrawData(ImDrawData * draw_data)139 void    ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
140 {
141     // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
142     ImGuiIO& io = ImGui::GetIO();
143     int fb_width = (int)(draw_data->DisplaySize.x * io.DisplayFramebufferScale.x);
144     int fb_height = (int)(draw_data->DisplaySize.y * io.DisplayFramebufferScale.y);
145     if (fb_width <= 0 || fb_height <= 0)
146         return;
147     draw_data->ScaleClipRects(io.DisplayFramebufferScale);
148 
149     // Backup GL state
150     GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture);
151     glActiveTexture(GL_TEXTURE0);
152     GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
153     GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
154 #ifdef GL_SAMPLER_BINDING
155     GLint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, &last_sampler);
156 #endif
157     GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
158     GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
159 #ifdef GL_POLYGON_MODE
160     GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
161 #endif
162     GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
163     GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
164     GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb);
165     GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb);
166     GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha);
167     GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha);
168     GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb);
169     GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha);
170     GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
171     GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
172     GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
173     GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
174     bool clip_origin_lower_left = true;
175 #ifdef GL_CLIP_ORIGIN
176     GLenum last_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)&last_clip_origin); // Support for GL 4.5's glClipControl(GL_UPPER_LEFT)
177     if (last_clip_origin == GL_UPPER_LEFT)
178         clip_origin_lower_left = false;
179 #endif
180 
181     // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
182     glEnable(GL_BLEND);
183     glBlendEquation(GL_FUNC_ADD);
184     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
185     glDisable(GL_CULL_FACE);
186     glDisable(GL_DEPTH_TEST);
187     glEnable(GL_SCISSOR_TEST);
188 #ifdef GL_POLYGON_MODE
189     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
190 #endif
191 
192     // Setup viewport, orthographic projection matrix
193     // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is typically (0,0) for single viewport apps.
194     glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
195     float L = draw_data->DisplayPos.x;
196     float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
197     float T = draw_data->DisplayPos.y;
198     float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
199     const float ortho_projection[4][4] =
200     {
201         { 2.0f/(R-L),   0.0f,         0.0f,   0.0f },
202         { 0.0f,         2.0f/(T-B),   0.0f,   0.0f },
203         { 0.0f,         0.0f,        -1.0f,   0.0f },
204         { (R+L)/(L-R),  (T+B)/(B-T),  0.0f,   1.0f },
205     };
206     glUseProgram(g_ShaderHandle);
207     glUniform1i(g_AttribLocationTex, 0);
208     glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
209 #ifdef GL_SAMPLER_BINDING
210     glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.
211 #endif
212     // Recreate the VAO every time
213     // (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.)
214     GLuint vao_handle = 0;
215     glGenVertexArrays(1, &vao_handle);
216     glBindVertexArray(vao_handle);
217     glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
218     glEnableVertexAttribArray(g_AttribLocationPosition);
219     glEnableVertexAttribArray(g_AttribLocationUV);
220     glEnableVertexAttribArray(g_AttribLocationColor);
221     glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos));
222     glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv));
223     glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col));
224 
225     // Draw
226     ImVec2 pos = draw_data->DisplayPos;
227     for (int n = 0; n < draw_data->CmdListsCount; n++)
228     {
229         const ImDrawList* cmd_list = draw_data->CmdLists[n];
230         const ImDrawIdx* idx_buffer_offset = 0;
231 
232         glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
233         glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW);
234 
235         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle);
236         glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW);
237 
238         for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
239         {
240             const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
241             if (pcmd->UserCallback)
242             {
243                 // User callback (registered via ImDrawList::AddCallback)
244                 pcmd->UserCallback(cmd_list, pcmd);
245             }
246             else
247             {
248                 ImVec4 clip_rect = ImVec4(pcmd->ClipRect.x - pos.x, pcmd->ClipRect.y - pos.y, pcmd->ClipRect.z - pos.x, pcmd->ClipRect.w - pos.y);
249                 if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f)
250                 {
251                     // Apply scissor/clipping rectangle
252                     if (clip_origin_lower_left)
253                         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));
254                     else
255                         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)
256 
257                     // Bind texture, Draw
258                     glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
259                     glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset);
260                 }
261             }
262             idx_buffer_offset += pcmd->ElemCount;
263         }
264     }
265     glDeleteVertexArrays(1, &vao_handle);
266 
267     // Restore modified GL state
268     glUseProgram(last_program);
269     glBindTexture(GL_TEXTURE_2D, last_texture);
270 #ifdef GL_SAMPLER_BINDING
271     glBindSampler(0, last_sampler);
272 #endif
273     glActiveTexture(last_active_texture);
274     glBindVertexArray(last_vertex_array);
275     glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
276     glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
277     glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
278     if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
279     if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
280     if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
281     if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
282 #ifdef GL_POLYGON_MODE
283     glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]);
284 #endif
285     glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
286     glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
287 }
288 
ImGui_ImplOpenGL3_CreateFontsTexture()289 bool ImGui_ImplOpenGL3_CreateFontsTexture()
290 {
291     // Build texture atlas
292     ImGuiIO& io = ImGui::GetIO();
293     unsigned char* pixels;
294     int width, height;
295     io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);   // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
296 
297     // Upload texture to graphics system
298     GLint last_texture;
299     glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
300     glGenTextures(1, &g_FontTexture);
301     glBindTexture(GL_TEXTURE_2D, g_FontTexture);
302     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
303     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
304     glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
305     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
306 
307     // Store our identifier
308     io.Fonts->TexID = (ImTextureID)(intptr_t)g_FontTexture;
309 
310     // Restore state
311     glBindTexture(GL_TEXTURE_2D, last_texture);
312 
313     return true;
314 }
315 
ImGui_ImplOpenGL3_DestroyFontsTexture()316 void ImGui_ImplOpenGL3_DestroyFontsTexture()
317 {
318     if (g_FontTexture)
319     {
320         ImGuiIO& io = ImGui::GetIO();
321         glDeleteTextures(1, &g_FontTexture);
322         io.Fonts->TexID = 0;
323         g_FontTexture = 0;
324     }
325 }
326 
327 // If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file.
CheckShader(GLuint handle,const char * desc)328 static bool CheckShader(GLuint handle, const char* desc)
329 {
330     GLint status = 0, log_length = 0;
331     glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
332     glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length);
333     if ((GLboolean)status == GL_FALSE)
334         fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s!\n", desc);
335     if (log_length > 0)
336     {
337         ImVector<char> buf;
338         buf.resize((int)(log_length + 1));
339         glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
340         fprintf(stderr, "%s\n", buf.begin());
341     }
342     return (GLboolean)status == GL_TRUE;
343 }
344 
345 // If you get an error please report on GitHub. You may try different GL context version or GLSL version.
CheckProgram(GLuint handle,const char * desc)346 static bool CheckProgram(GLuint handle, const char* desc)
347 {
348     GLint status = 0, log_length = 0;
349     glGetProgramiv(handle, GL_LINK_STATUS, &status);
350     glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length);
351     if ((GLboolean)status == GL_FALSE)
352         fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! (with GLSL '%s')\n", desc, g_GlslVersionString);
353     if (log_length > 0)
354     {
355         ImVector<char> buf;
356         buf.resize((int)(log_length + 1));
357         glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
358         fprintf(stderr, "%s\n", buf.begin());
359     }
360     return (GLboolean)status == GL_TRUE;
361 }
362 
ImGui_ImplOpenGL3_CreateDeviceObjects()363 bool    ImGui_ImplOpenGL3_CreateDeviceObjects()
364 {
365     // Backup GL state
366     GLint last_texture, last_array_buffer, last_vertex_array;
367     glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
368     glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
369     glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
370 
371     // Parse GLSL version string
372     int glsl_version = 130;
373     sscanf(g_GlslVersionString, "#version %d", &glsl_version);
374 
375     const GLchar* vertex_shader_glsl_120 =
376         "uniform mat4 ProjMtx;\n"
377         "attribute vec2 Position;\n"
378         "attribute vec2 UV;\n"
379         "attribute vec4 Color;\n"
380         "varying vec2 Frag_UV;\n"
381         "varying vec4 Frag_Color;\n"
382         "void main()\n"
383         "{\n"
384         "    Frag_UV = UV;\n"
385         "    Frag_Color = Color;\n"
386         "    gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
387         "}\n";
388 
389     const GLchar* vertex_shader_glsl_130 =
390         "uniform mat4 ProjMtx;\n"
391         "in vec2 Position;\n"
392         "in vec2 UV;\n"
393         "in vec4 Color;\n"
394         "out vec2 Frag_UV;\n"
395         "out vec4 Frag_Color;\n"
396         "void main()\n"
397         "{\n"
398         "    Frag_UV = UV;\n"
399         "    Frag_Color = Color;\n"
400         "    gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
401         "}\n";
402 
403     const GLchar* vertex_shader_glsl_300_es =
404         "precision mediump float;\n"
405         "layout (location = 0) in vec2 Position;\n"
406         "layout (location = 1) in vec2 UV;\n"
407         "layout (location = 2) in vec4 Color;\n"
408         "uniform mat4 ProjMtx;\n"
409         "out vec2 Frag_UV;\n"
410         "out vec4 Frag_Color;\n"
411         "void main()\n"
412         "{\n"
413         "    Frag_UV = UV;\n"
414         "    Frag_Color = Color;\n"
415         "    gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
416         "}\n";
417 
418     const GLchar* vertex_shader_glsl_410_core =
419         "layout (location = 0) in vec2 Position;\n"
420         "layout (location = 1) in vec2 UV;\n"
421         "layout (location = 2) in vec4 Color;\n"
422         "uniform mat4 ProjMtx;\n"
423         "out vec2 Frag_UV;\n"
424         "out vec4 Frag_Color;\n"
425         "void main()\n"
426         "{\n"
427         "    Frag_UV = UV;\n"
428         "    Frag_Color = Color;\n"
429         "    gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
430         "}\n";
431 
432     const GLchar* fragment_shader_glsl_120 =
433         "#ifdef GL_ES\n"
434         "    precision mediump float;\n"
435         "#endif\n"
436         "uniform sampler2D Texture;\n"
437         "varying vec2 Frag_UV;\n"
438         "varying vec4 Frag_Color;\n"
439         "void main()\n"
440         "{\n"
441         "    gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n"
442         "}\n";
443 
444     const GLchar* fragment_shader_glsl_130 =
445         "uniform sampler2D Texture;\n"
446         "in vec2 Frag_UV;\n"
447         "in vec4 Frag_Color;\n"
448         "out vec4 Out_Color;\n"
449         "void main()\n"
450         "{\n"
451         "    Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
452         "}\n";
453 
454     const GLchar* fragment_shader_glsl_300_es =
455         "precision mediump float;\n"
456         "uniform sampler2D Texture;\n"
457         "in vec2 Frag_UV;\n"
458         "in vec4 Frag_Color;\n"
459         "layout (location = 0) out vec4 Out_Color;\n"
460         "void main()\n"
461         "{\n"
462         "    Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
463         "}\n";
464 
465     const GLchar* fragment_shader_glsl_410_core =
466         "in vec2 Frag_UV;\n"
467         "in vec4 Frag_Color;\n"
468         "uniform sampler2D Texture;\n"
469         "layout (location = 0) out vec4 Out_Color;\n"
470         "void main()\n"
471         "{\n"
472         "    Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
473         "}\n";
474 
475     // Select shaders matching our GLSL versions
476     const GLchar* vertex_shader = NULL;
477     const GLchar* fragment_shader = NULL;
478     if (glsl_version < 130)
479     {
480         vertex_shader = vertex_shader_glsl_120;
481         fragment_shader = fragment_shader_glsl_120;
482     }
483     else if (glsl_version >= 410)
484     {
485         vertex_shader = vertex_shader_glsl_410_core;
486         fragment_shader = fragment_shader_glsl_410_core;
487     }
488     else if (glsl_version == 300)
489     {
490         vertex_shader = vertex_shader_glsl_300_es;
491         fragment_shader = fragment_shader_glsl_300_es;
492     }
493     else
494     {
495         vertex_shader = vertex_shader_glsl_130;
496         fragment_shader = fragment_shader_glsl_130;
497     }
498 
499     // Create shaders
500     const GLchar* vertex_shader_with_version[2] = { g_GlslVersionString, vertex_shader };
501     g_VertHandle = glCreateShader(GL_VERTEX_SHADER);
502     glShaderSource(g_VertHandle, 2, vertex_shader_with_version, NULL);
503     glCompileShader(g_VertHandle);
504     CheckShader(g_VertHandle, "vertex shader");
505 
506     const GLchar* fragment_shader_with_version[2] = { g_GlslVersionString, fragment_shader };
507     g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER);
508     glShaderSource(g_FragHandle, 2, fragment_shader_with_version, NULL);
509     glCompileShader(g_FragHandle);
510     CheckShader(g_FragHandle, "fragment shader");
511 
512     g_ShaderHandle = glCreateProgram();
513     glAttachShader(g_ShaderHandle, g_VertHandle);
514     glAttachShader(g_ShaderHandle, g_FragHandle);
515     glLinkProgram(g_ShaderHandle);
516     CheckProgram(g_ShaderHandle, "shader program");
517 
518     g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture");
519     g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx");
520     g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position");
521     g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV");
522     g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color");
523 
524     // Create buffers
525     glGenBuffers(1, &g_VboHandle);
526     glGenBuffers(1, &g_ElementsHandle);
527 
528     ImGui_ImplOpenGL3_CreateFontsTexture();
529 
530     // Restore modified GL state
531     glBindTexture(GL_TEXTURE_2D, last_texture);
532     glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
533     glBindVertexArray(last_vertex_array);
534 
535     return true;
536 }
537 
ImGui_ImplOpenGL3_DestroyDeviceObjects()538 void    ImGui_ImplOpenGL3_DestroyDeviceObjects()
539 {
540     if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle);
541     if (g_ElementsHandle) glDeleteBuffers(1, &g_ElementsHandle);
542     g_VboHandle = g_ElementsHandle = 0;
543 
544     if (g_ShaderHandle && g_VertHandle) glDetachShader(g_ShaderHandle, g_VertHandle);
545     if (g_VertHandle) glDeleteShader(g_VertHandle);
546     g_VertHandle = 0;
547 
548     if (g_ShaderHandle && g_FragHandle) glDetachShader(g_ShaderHandle, g_FragHandle);
549     if (g_FragHandle) glDeleteShader(g_FragHandle);
550     g_FragHandle = 0;
551 
552     if (g_ShaderHandle) glDeleteProgram(g_ShaderHandle);
553     g_ShaderHandle = 0;
554 
555     ImGui_ImplOpenGL3_DestroyFontsTexture();
556 }
557