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