imgui_impl_opengl3.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // ImGui Renderer for: OpenGL3 (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. // Implemented features:
  5. // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
  6. // [X] Renderer: Multi-viewport support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
  7. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  8. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
  9. // https://github.com/ocornut/imgui
  10. // CHANGELOG
  11. // (minor and older changes stripped away, please see git history for details)
  12. // 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  13. // 2018-07-10: OpenGL: Support for more GLSL versions (based on the GLSL version string). Added error output when shaders fail to compile/link.
  14. // 2018-06-08: Misc: Extracted imgui_impl_opengl3.cpp/.h away from the old combined GLFW/SDL+OpenGL3 examples.
  15. // 2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
  16. // 2018-05-25: OpenGL: Removed unnecessary backup/restore of GL_ELEMENT_ARRAY_BUFFER_BINDING since this is part of the VAO state.
  17. // 2018-05-14: OpenGL: Making the call to glBindSampler() optional so 3.2 context won't fail if the function is a NULL pointer.
  18. // 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".
  19. // 2018-02-23: OpenGL: Create the VAO in the render function so the setup can more easily be used with multiple shared GL context.
  20. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplSdlGL3_RenderDrawData() in the .h file so you can call it yourself.
  21. // 2018-01-07: OpenGL: Changed GLSL shader version from 330 to 150.
  22. // 2017-09-01: OpenGL: Save and restore current bound sampler. Save and restore current polygon mode.
  23. // 2017-05-01: OpenGL: Fixed save and restore of current blend func state.
  24. // 2017-05-01: OpenGL: Fixed save and restore of current GL_ACTIVE_TEXTURE.
  25. // 2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle.
  26. // 2016-07-29: OpenGL: Explicitly setting GL_UNPACK_ROW_LENGTH to reduce issues because SDL changes it. (#752)
  27. //----------------------------------------
  28. // OpenGL GLSL GLSL
  29. // version version string
  30. //----------------------------------------
  31. // 2.0 110 "#version 110"
  32. // 2.1 120
  33. // 3.0 130
  34. // 3.1 140
  35. // 3.2 150 "#version 150"
  36. // 3.3 330
  37. // 4.0 400
  38. // 4.1 410
  39. // 4.2 420
  40. // 4.3 430
  41. // ES 2.0 100 "#version 100"
  42. // ES 3.0 300 "#version 300 es"
  43. //----------------------------------------
  44. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  45. #define _CRT_SECURE_NO_WARNINGS
  46. #endif
  47. #include "imgui.h"
  48. #include "imgui_impl_opengl3.h"
  49. #include <stdio.h>
  50. #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier
  51. #include <stddef.h> // intptr_t
  52. #else
  53. #include <stdint.h> // intptr_t
  54. #endif
  55. #include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions. You may use another OpenGL loader/header such as: glew, glext, glad, glLoadGen, etc.
  56. //#include <glew.h>
  57. //#include <glext.h>
  58. //#include <glad/glad.h>
  59. // OpenGL Data
  60. static char g_GlslVersionString[32] = "";
  61. static GLuint g_FontTexture = 0;
  62. static GLuint g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
  63. static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0;
  64. static int g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0;
  65. static unsigned int g_VboHandle = 0, g_ElementsHandle = 0;
  66. // Forward Declarations
  67. static void ImGui_ImplOpenGL3_InitPlatformInterface();
  68. static void ImGui_ImplOpenGL3_ShutdownPlatformInterface();
  69. // Functions
  70. bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
  71. {
  72. // 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.
  73. if (glsl_version == NULL)
  74. glsl_version = "#version 130";
  75. IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString));
  76. strcpy(g_GlslVersionString, glsl_version);
  77. strcat(g_GlslVersionString, "\n");
  78. // Setup back-end capabilities flags
  79. ImGuiIO& io = ImGui::GetIO();
  80. io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
  81. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  82. ImGui_ImplOpenGL3_InitPlatformInterface();
  83. return true;
  84. }
  85. void ImGui_ImplOpenGL3_Shutdown()
  86. {
  87. ImGui_ImplOpenGL3_ShutdownPlatformInterface();
  88. ImGui_ImplOpenGL3_DestroyDeviceObjects();
  89. }
  90. void ImGui_ImplOpenGL3_NewFrame()
  91. {
  92. if (!g_FontTexture)
  93. ImGui_ImplOpenGL3_CreateDeviceObjects();
  94. }
  95. // OpenGL3 Render function.
  96. // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
  97. // 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.
  98. void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
  99. {
  100. // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
  101. ImGuiIO& io = ImGui::GetIO();
  102. int fb_width = (int)(draw_data->DisplaySize.x * io.DisplayFramebufferScale.x);
  103. int fb_height = (int)(draw_data->DisplaySize.y * io.DisplayFramebufferScale.y);
  104. if (fb_width <= 0 || fb_height <= 0)
  105. return;
  106. draw_data->ScaleClipRects(io.DisplayFramebufferScale);
  107. // Backup GL state
  108. GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture);
  109. glActiveTexture(GL_TEXTURE0);
  110. GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
  111. GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
  112. GLint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, &last_sampler);
  113. GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
  114. GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
  115. GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
  116. GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
  117. GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
  118. GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb);
  119. GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb);
  120. GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha);
  121. GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha);
  122. GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb);
  123. GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha);
  124. GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
  125. GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
  126. GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
  127. GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
  128. // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
  129. glEnable(GL_BLEND);
  130. glBlendEquation(GL_FUNC_ADD);
  131. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  132. glDisable(GL_CULL_FACE);
  133. glDisable(GL_DEPTH_TEST);
  134. glEnable(GL_SCISSOR_TEST);
  135. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  136. // Setup viewport, orthographic projection matrix
  137. // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is (0,0) for single viewport apps.
  138. glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
  139. float L = draw_data->DisplayPos.x;
  140. float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
  141. float T = draw_data->DisplayPos.y;
  142. float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
  143. const float ortho_projection[4][4] =
  144. {
  145. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
  146. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
  147. { 0.0f, 0.0f, -1.0f, 0.0f },
  148. { (R+L)/(L-R), (T+B)/(B-T), 0.0f, 1.0f },
  149. };
  150. glUseProgram(g_ShaderHandle);
  151. glUniform1i(g_AttribLocationTex, 0);
  152. glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
  153. if (glBindSampler) glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.
  154. // Recreate the VAO every time
  155. // (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.)
  156. GLuint vao_handle = 0;
  157. glGenVertexArrays(1, &vao_handle);
  158. glBindVertexArray(vao_handle);
  159. glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
  160. glEnableVertexAttribArray(g_AttribLocationPosition);
  161. glEnableVertexAttribArray(g_AttribLocationUV);
  162. glEnableVertexAttribArray(g_AttribLocationColor);
  163. glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos));
  164. glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv));
  165. glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col));
  166. // Draw
  167. ImVec2 pos = draw_data->DisplayPos;
  168. for (int n = 0; n < draw_data->CmdListsCount; n++)
  169. {
  170. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  171. const ImDrawIdx* idx_buffer_offset = 0;
  172. glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
  173. glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW);
  174. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle);
  175. glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW);
  176. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  177. {
  178. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  179. if (pcmd->UserCallback)
  180. {
  181. // User callback (registered via ImDrawList::AddCallback)
  182. pcmd->UserCallback(cmd_list, pcmd);
  183. }
  184. else
  185. {
  186. ImVec4 clip_rect = ImVec4(pcmd->ClipRect.x - pos.x, pcmd->ClipRect.y - pos.y, pcmd->ClipRect.z - pos.x, pcmd->ClipRect.w - pos.y);
  187. if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f)
  188. {
  189. // Apply scissor/clipping rectangle
  190. 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));
  191. // Bind texture, Draw
  192. glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
  193. glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset);
  194. }
  195. }
  196. idx_buffer_offset += pcmd->ElemCount;
  197. }
  198. }
  199. glDeleteVertexArrays(1, &vao_handle);
  200. // Restore modified GL state
  201. glUseProgram(last_program);
  202. glBindTexture(GL_TEXTURE_2D, last_texture);
  203. if (glBindSampler) glBindSampler(0, last_sampler);
  204. glActiveTexture(last_active_texture);
  205. glBindVertexArray(last_vertex_array);
  206. glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
  207. glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
  208. glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
  209. if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
  210. if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
  211. if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
  212. if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
  213. glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]);
  214. glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
  215. glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
  216. }
  217. bool ImGui_ImplOpenGL3_CreateFontsTexture()
  218. {
  219. // Build texture atlas
  220. ImGuiIO& io = ImGui::GetIO();
  221. unsigned char* pixels;
  222. int width, height;
  223. 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.
  224. // Upload texture to graphics system
  225. GLint last_texture;
  226. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
  227. glGenTextures(1, &g_FontTexture);
  228. glBindTexture(GL_TEXTURE_2D, g_FontTexture);
  229. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  230. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  231. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  232. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  233. // Store our identifier
  234. io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
  235. // Restore state
  236. glBindTexture(GL_TEXTURE_2D, last_texture);
  237. return true;
  238. }
  239. void ImGui_ImplOpenGL3_DestroyFontsTexture()
  240. {
  241. if (g_FontTexture)
  242. {
  243. ImGuiIO& io = ImGui::GetIO();
  244. glDeleteTextures(1, &g_FontTexture);
  245. io.Fonts->TexID = 0;
  246. g_FontTexture = 0;
  247. }
  248. }
  249. // If you get an error please report on github. You may try different GL context version or GLSL version.
  250. static bool CheckShader(GLuint handle, const char* desc)
  251. {
  252. GLint status = 0, log_length = 0;
  253. glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
  254. glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length);
  255. if (status == GL_FALSE)
  256. fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s!\n", desc);
  257. if (log_length > 0)
  258. {
  259. ImVector<char> buf;
  260. buf.resize((int)(log_length + 1));
  261. glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
  262. fprintf(stderr, "%s\n", buf.begin());
  263. }
  264. return status == GL_TRUE;
  265. }
  266. // If you get an error please report on github. You may try different GL context version or GLSL version.
  267. static bool CheckProgram(GLuint handle, const char* desc)
  268. {
  269. GLint status = 0, log_length = 0;
  270. glGetProgramiv(handle, GL_LINK_STATUS, &status);
  271. glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length);
  272. if (status == GL_FALSE)
  273. fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s!\n", desc);
  274. if (log_length > 0)
  275. {
  276. ImVector<char> buf;
  277. buf.resize((int)(log_length + 1));
  278. glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
  279. fprintf(stderr, "%s\n", buf.begin());
  280. }
  281. return status == GL_TRUE;
  282. }
  283. bool ImGui_ImplOpenGL3_CreateDeviceObjects()
  284. {
  285. // Backup GL state
  286. GLint last_texture, last_array_buffer, last_vertex_array;
  287. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
  288. glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
  289. glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
  290. // Parse GLSL version string
  291. int glsl_version = 130;
  292. sscanf(g_GlslVersionString, "#version %d", &glsl_version);
  293. const GLchar* vertex_shader_glsl_120 =
  294. "uniform mat4 ProjMtx;\n"
  295. "attribute vec2 Position;\n"
  296. "attribute vec2 UV;\n"
  297. "attribute vec4 Color;\n"
  298. "varying vec2 Frag_UV;\n"
  299. "varying vec4 Frag_Color;\n"
  300. "void main()\n"
  301. "{\n"
  302. " Frag_UV = UV;\n"
  303. " Frag_Color = Color;\n"
  304. " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
  305. "}\n";
  306. const GLchar* vertex_shader_glsl_130 =
  307. "uniform mat4 ProjMtx;\n"
  308. "in vec2 Position;\n"
  309. "in vec2 UV;\n"
  310. "in vec4 Color;\n"
  311. "out vec2 Frag_UV;\n"
  312. "out vec4 Frag_Color;\n"
  313. "void main()\n"
  314. "{\n"
  315. " Frag_UV = UV;\n"
  316. " Frag_Color = Color;\n"
  317. " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
  318. "}\n";
  319. const GLchar* fragment_shader_glsl_120 =
  320. "#ifdef GL_ES\n"
  321. " precision mediump float;\n"
  322. "#endif\n"
  323. "uniform sampler2D Texture;\n"
  324. "varying vec2 Frag_UV;\n"
  325. "varying vec4 Frag_Color;\n"
  326. "void main()\n"
  327. "{\n"
  328. " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n"
  329. "}\n";
  330. const GLchar* fragment_shader_glsl_130 =
  331. "uniform sampler2D Texture;\n"
  332. "in vec2 Frag_UV;\n"
  333. "in vec4 Frag_Color;\n"
  334. "out vec4 Out_Color;\n"
  335. "void main()\n"
  336. "{\n"
  337. " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
  338. "}\n";
  339. // Select shaders matching our GLSL versions
  340. const GLchar* vertex_shader = NULL;
  341. const GLchar* fragment_shader = NULL;
  342. if (glsl_version < 130)
  343. {
  344. vertex_shader = vertex_shader_glsl_120;
  345. fragment_shader = fragment_shader_glsl_120;
  346. }
  347. else
  348. {
  349. vertex_shader = vertex_shader_glsl_130;
  350. fragment_shader = fragment_shader_glsl_130;
  351. }
  352. // Create shaders
  353. const GLchar* vertex_shader_with_version[2] = { g_GlslVersionString, vertex_shader };
  354. g_VertHandle = glCreateShader(GL_VERTEX_SHADER);
  355. glShaderSource(g_VertHandle, 2, vertex_shader_with_version, NULL);
  356. glCompileShader(g_VertHandle);
  357. CheckShader(g_VertHandle, "vertex shader");
  358. const GLchar* fragment_shader_with_version[2] = { g_GlslVersionString, fragment_shader };
  359. g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER);
  360. glShaderSource(g_FragHandle, 2, fragment_shader_with_version, NULL);
  361. glCompileShader(g_FragHandle);
  362. CheckShader(g_FragHandle, "fragment shader");
  363. g_ShaderHandle = glCreateProgram();
  364. glAttachShader(g_ShaderHandle, g_VertHandle);
  365. glAttachShader(g_ShaderHandle, g_FragHandle);
  366. glLinkProgram(g_ShaderHandle);
  367. CheckProgram(g_ShaderHandle, "shader program");
  368. g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture");
  369. g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx");
  370. g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position");
  371. g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV");
  372. g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color");
  373. // Create buffers
  374. glGenBuffers(1, &g_VboHandle);
  375. glGenBuffers(1, &g_ElementsHandle);
  376. ImGui_ImplOpenGL3_CreateFontsTexture();
  377. // Restore modified GL state
  378. glBindTexture(GL_TEXTURE_2D, last_texture);
  379. glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
  380. glBindVertexArray(last_vertex_array);
  381. return true;
  382. }
  383. void ImGui_ImplOpenGL3_DestroyDeviceObjects()
  384. {
  385. if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle);
  386. if (g_ElementsHandle) glDeleteBuffers(1, &g_ElementsHandle);
  387. g_VboHandle = g_ElementsHandle = 0;
  388. if (g_ShaderHandle && g_VertHandle) glDetachShader(g_ShaderHandle, g_VertHandle);
  389. if (g_VertHandle) glDeleteShader(g_VertHandle);
  390. g_VertHandle = 0;
  391. if (g_ShaderHandle && g_FragHandle) glDetachShader(g_ShaderHandle, g_FragHandle);
  392. if (g_FragHandle) glDeleteShader(g_FragHandle);
  393. g_FragHandle = 0;
  394. if (g_ShaderHandle) glDeleteProgram(g_ShaderHandle);
  395. g_ShaderHandle = 0;
  396. ImGui_ImplOpenGL3_DestroyFontsTexture();
  397. }
  398. //--------------------------------------------------------------------------------------------------------
  399. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  400. // This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously.
  401. // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
  402. //--------------------------------------------------------------------------------------------------------
  403. static void ImGui_ImplOpenGL3_RenderWindow(ImGuiViewport* viewport, void*)
  404. {
  405. if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear))
  406. {
  407. ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
  408. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  409. glClear(GL_COLOR_BUFFER_BIT);
  410. }
  411. ImGui_ImplOpenGL3_RenderDrawData(viewport->DrawData);
  412. }
  413. static void ImGui_ImplOpenGL3_InitPlatformInterface()
  414. {
  415. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  416. platform_io.Renderer_RenderWindow = ImGui_ImplOpenGL3_RenderWindow;
  417. }
  418. static void ImGui_ImplOpenGL3_ShutdownPlatformInterface()
  419. {
  420. ImGui::DestroyPlatformWindows();
  421. }