imgui_impl_glfw.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // ImGui GLFW binding with OpenGL
  2. // https://github.com/ocornut/imgui
  3. #include <imgui.h>
  4. #include "imgui_impl_glfw.h"
  5. // GLFW
  6. #include <GLFW/glfw3.h>
  7. #ifdef _WIN32
  8. #undef APIENTRY
  9. #define GLFW_EXPOSE_NATIVE_WIN32
  10. #define GLFW_EXPOSE_NATIVE_WGL
  11. #include <GLFW/glfw3native.h>
  12. #endif
  13. // Data
  14. static GLFWwindow* g_Window = NULL;
  15. static double g_Time = 0.0f;
  16. static bool g_MousePressed[3] = { false, false, false };
  17. static float g_MouseWheel = 0.0f;
  18. static GLuint g_FontTexture = 0;
  19. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  20. // If text or lines are blurry when integrating ImGui in your engine:
  21. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  22. static void ImGui_ImplGlfw_RenderDrawLists(ImDrawData* draw_data)
  23. {
  24. // We are using the OpenGL fixed pipeline to make the example code simpler to read!
  25. // A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer.
  26. // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
  27. GLint last_texture;
  28. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
  29. glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
  30. glEnable(GL_BLEND);
  31. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  32. glDisable(GL_CULL_FACE);
  33. glDisable(GL_DEPTH_TEST);
  34. glEnable(GL_SCISSOR_TEST);
  35. glEnableClientState(GL_VERTEX_ARRAY);
  36. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  37. glEnableClientState(GL_COLOR_ARRAY);
  38. glEnable(GL_TEXTURE_2D);
  39. //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context
  40. // Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
  41. ImGuiIO& io = ImGui::GetIO();
  42. float fb_height = io.DisplaySize.y * io.DisplayFramebufferScale.y;
  43. draw_data->ScaleClipRects(io.DisplayFramebufferScale);
  44. // Setup orthographic projection matrix
  45. glMatrixMode(GL_PROJECTION);
  46. glPushMatrix();
  47. glLoadIdentity();
  48. glOrtho(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f, -1.0f, +1.0f);
  49. glMatrixMode(GL_MODELVIEW);
  50. glPushMatrix();
  51. glLoadIdentity();
  52. // Render command lists
  53. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
  54. for (int n = 0; n < draw_data->CmdListsCount; n++)
  55. {
  56. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  57. const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front();
  58. const ImDrawIdx* idx_buffer = &cmd_list->IdxBuffer.front();
  59. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
  60. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
  61. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
  62. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
  63. {
  64. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  65. if (pcmd->UserCallback)
  66. {
  67. pcmd->UserCallback(cmd_list, pcmd);
  68. }
  69. else
  70. {
  71. glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
  72. glScissor((int)pcmd->ClipRect.x, (int)(fb_height - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
  73. glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, GL_UNSIGNED_SHORT, idx_buffer);
  74. }
  75. idx_buffer += pcmd->ElemCount;
  76. }
  77. }
  78. #undef OFFSETOF
  79. // Restore modified state
  80. glDisableClientState(GL_COLOR_ARRAY);
  81. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  82. glDisableClientState(GL_VERTEX_ARRAY);
  83. glBindTexture(GL_TEXTURE_2D, last_texture);
  84. glMatrixMode(GL_MODELVIEW);
  85. glPopMatrix();
  86. glMatrixMode(GL_PROJECTION);
  87. glPopMatrix();
  88. glPopAttrib();
  89. }
  90. static const char* ImGui_ImplGlfw_GetClipboardText()
  91. {
  92. return glfwGetClipboardString(g_Window);
  93. }
  94. static void ImGui_ImplGlfw_SetClipboardText(const char* text)
  95. {
  96. glfwSetClipboardString(g_Window, text);
  97. }
  98. void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
  99. {
  100. if (action == GLFW_PRESS && button >= 0 && button < 3)
  101. g_MousePressed[button] = true;
  102. }
  103. void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double /*xoffset*/, double yoffset)
  104. {
  105. g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines.
  106. }
  107. void ImGui_ImplGlFw_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
  108. {
  109. ImGuiIO& io = ImGui::GetIO();
  110. if (action == GLFW_PRESS)
  111. io.KeysDown[key] = true;
  112. if (action == GLFW_RELEASE)
  113. io.KeysDown[key] = false;
  114. (void)mods; // Modifiers are not reliable across systems
  115. io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
  116. io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
  117. io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
  118. }
  119. void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c)
  120. {
  121. ImGuiIO& io = ImGui::GetIO();
  122. if (c > 0 && c < 0x10000)
  123. io.AddInputCharacter((unsigned short)c);
  124. }
  125. bool ImGui_ImplGlfw_CreateDeviceObjects()
  126. {
  127. ImGuiIO& io = ImGui::GetIO();
  128. // Build texture
  129. unsigned char* pixels;
  130. int width, height;
  131. io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
  132. // Create texture
  133. GLint last_texture;
  134. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
  135. glGenTextures(1, &g_FontTexture);
  136. glBindTexture(GL_TEXTURE_2D, g_FontTexture);
  137. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  138. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  139. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
  140. // Store our identifier
  141. io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
  142. // Cleanup (don't clear the input data if you want to append new fonts later)
  143. io.Fonts->ClearInputData();
  144. io.Fonts->ClearTexData();
  145. glBindTexture(GL_TEXTURE_2D, last_texture);
  146. return true;
  147. }
  148. void ImGui_ImplGlfw_InvalidateDeviceObjects()
  149. {
  150. if (g_FontTexture)
  151. {
  152. glDeleteTextures(1, &g_FontTexture);
  153. ImGui::GetIO().Fonts->TexID = 0;
  154. g_FontTexture = 0;
  155. }
  156. }
  157. bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks)
  158. {
  159. g_Window = window;
  160. ImGuiIO& io = ImGui::GetIO();
  161. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  162. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  163. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  164. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  165. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  166. io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
  167. io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
  168. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  169. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  170. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  171. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  172. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  173. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  174. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  175. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  176. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  177. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  178. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  179. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  180. io.RenderDrawListsFn = ImGui_ImplGlfw_RenderDrawLists;
  181. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  182. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  183. #ifdef _WIN32
  184. io.ImeWindowHandle = glfwGetWin32Window(g_Window);
  185. #endif
  186. if (install_callbacks)
  187. {
  188. glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  189. glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  190. glfwSetKeyCallback(window, ImGui_ImplGlFw_KeyCallback);
  191. glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  192. }
  193. return true;
  194. }
  195. void ImGui_ImplGlfw_Shutdown()
  196. {
  197. ImGui_ImplGlfw_InvalidateDeviceObjects();
  198. ImGui::Shutdown();
  199. }
  200. void ImGui_ImplGlfw_NewFrame()
  201. {
  202. if (!g_FontTexture)
  203. ImGui_ImplGlfw_CreateDeviceObjects();
  204. ImGuiIO& io = ImGui::GetIO();
  205. // Setup display size (every frame to accommodate for window resizing)
  206. int w, h;
  207. int display_w, display_h;
  208. glfwGetWindowSize(g_Window, &w, &h);
  209. glfwGetFramebufferSize(g_Window, &display_w, &display_h);
  210. io.DisplaySize = ImVec2((float)w, (float)h);
  211. io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
  212. // Setup time step
  213. double current_time = glfwGetTime();
  214. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  215. g_Time = current_time;
  216. // Setup inputs
  217. // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
  218. if (glfwGetWindowAttrib(g_Window, GLFW_FOCUSED))
  219. {
  220. double mouse_x, mouse_y;
  221. glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
  222. io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position in screen coordinates (set to -1,-1 if no mouse / on another screen, etc.)
  223. }
  224. else
  225. {
  226. io.MousePos = ImVec2(-1,-1);
  227. }
  228. for (int i = 0; i < 3; i++)
  229. {
  230. io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
  231. g_MousePressed[i] = false;
  232. }
  233. io.MouseWheel = g_MouseWheel;
  234. g_MouseWheel = 0.0f;
  235. // Hide OS mouse cursor if ImGui is drawing it
  236. glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
  237. // Start the frame
  238. ImGui::NewFrame();
  239. }