imgui_impl_opengl3.cpp 22 KB

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