imgui_impl_glfw.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 _MSC_VER
  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(ImDrawList** const cmd_lists, int cmd_lists_count)
  23. {
  24. if (cmd_lists_count == 0)
  25. return;
  26. // We are using the OpenGL fixed pipeline to make the example code simpler to read!
  27. // A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer.
  28. // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
  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. // Setup orthographic projection matrix
  41. const float width = ImGui::GetIO().DisplaySize.x;
  42. const float height = ImGui::GetIO().DisplaySize.y;
  43. glMatrixMode(GL_PROJECTION);
  44. glPushMatrix();
  45. glLoadIdentity();
  46. glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f);
  47. glMatrixMode(GL_MODELVIEW);
  48. glPushMatrix();
  49. glLoadIdentity();
  50. // Render command lists
  51. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
  52. for (int n = 0; n < cmd_lists_count; n++)
  53. {
  54. const ImDrawList* cmd_list = cmd_lists[n];
  55. const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->vtx_buffer.front();
  56. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
  57. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
  58. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
  59. int vtx_offset = 0;
  60. for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
  61. {
  62. const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
  63. if (pcmd->user_callback)
  64. {
  65. pcmd->user_callback(cmd_list, pcmd);
  66. }
  67. else
  68. {
  69. glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id);
  70. glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
  71. glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
  72. }
  73. vtx_offset += pcmd->vtx_count;
  74. }
  75. }
  76. #undef OFFSETOF
  77. // Restore modified state
  78. glDisableClientState(GL_COLOR_ARRAY);
  79. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  80. glDisableClientState(GL_VERTEX_ARRAY);
  81. glBindTexture(GL_TEXTURE_2D, 0);
  82. glMatrixMode(GL_MODELVIEW);
  83. glPopMatrix();
  84. glMatrixMode(GL_PROJECTION);
  85. glPopMatrix();
  86. glPopAttrib();
  87. }
  88. static const char* ImGui_ImplGlfw_GetClipboardText()
  89. {
  90. return glfwGetClipboardString(g_Window);
  91. }
  92. static void ImGui_ImplGlfw_SetClipboardText(const char* text)
  93. {
  94. glfwSetClipboardString(g_Window, text);
  95. }
  96. void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
  97. {
  98. if (action == GLFW_PRESS && button >= 0 && button < 3)
  99. g_MousePressed[button] = true;
  100. }
  101. void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double /*xoffset*/, double yoffset)
  102. {
  103. g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines.
  104. }
  105. void ImGui_ImplGlFw_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
  106. {
  107. ImGuiIO& io = ImGui::GetIO();
  108. if (action == GLFW_PRESS)
  109. io.KeysDown[key] = true;
  110. if (action == GLFW_RELEASE)
  111. io.KeysDown[key] = false;
  112. (void)mods; // Modifiers are not reliable across systems
  113. io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
  114. io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
  115. io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
  116. }
  117. void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c)
  118. {
  119. ImGuiIO& io = ImGui::GetIO();
  120. if (c > 0 && c < 0x10000)
  121. io.AddInputCharacter((unsigned short)c);
  122. }
  123. bool ImGui_ImplGlfw_CreateDeviceObjects()
  124. {
  125. ImGuiIO& io = ImGui::GetIO();
  126. // Build texture
  127. unsigned char* pixels;
  128. int width, height;
  129. io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
  130. // Create texture
  131. glGenTextures(1, &g_FontTexture);
  132. glBindTexture(GL_TEXTURE_2D, g_FontTexture);
  133. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  134. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  135. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
  136. // Store our identifier
  137. io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
  138. return true;
  139. }
  140. void ImGui_ImplGlfw_InvalidateDeviceObjects()
  141. {
  142. if (g_FontTexture)
  143. {
  144. glDeleteTextures(1, &g_FontTexture);
  145. ImGui::GetIO().Fonts->TexID = 0;
  146. g_FontTexture = 0;
  147. }
  148. }
  149. bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks)
  150. {
  151. g_Window = window;
  152. ImGuiIO& io = ImGui::GetIO();
  153. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  154. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  155. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  156. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  157. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  158. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  159. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  160. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  161. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  162. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  163. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  164. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  165. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  166. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  167. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  168. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  169. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  170. io.RenderDrawListsFn = ImGui_ImplGlfw_RenderDrawLists;
  171. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  172. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  173. #ifdef _MSC_VER
  174. io.ImeWindowHandle = glfwGetWin32Window(g_Window);
  175. #endif
  176. if (install_callbacks)
  177. {
  178. glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  179. glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  180. glfwSetKeyCallback(window, ImGui_ImplGlFw_KeyCallback);
  181. glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  182. }
  183. return true;
  184. }
  185. void ImGui_ImplGlfw_Shutdown()
  186. {
  187. ImGui_ImplGlfw_InvalidateDeviceObjects();
  188. ImGui::Shutdown();
  189. }
  190. void ImGui_ImplGlfw_NewFrame()
  191. {
  192. if (!g_FontTexture)
  193. ImGui_ImplGlfw_CreateDeviceObjects();
  194. ImGuiIO& io = ImGui::GetIO();
  195. // Setup display size (every frame to accommodate for window resizing)
  196. int w, h;
  197. int display_w, display_h;
  198. glfwGetWindowSize(g_Window, &w, &h);
  199. glfwGetFramebufferSize(g_Window, &display_w, &display_h);
  200. io.DisplaySize = ImVec2((float)display_w, (float)display_h);
  201. // Setup time step
  202. double current_time = glfwGetTime();
  203. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  204. g_Time = current_time;
  205. // Setup inputs
  206. // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
  207. if (glfwGetWindowAttrib(g_Window, GLFW_FOCUSED))
  208. {
  209. double mouse_x, mouse_y;
  210. glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
  211. mouse_x *= (float)display_w / w; // Convert mouse coordinates to pixels
  212. mouse_y *= (float)display_h / h;
  213. io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
  214. }
  215. else
  216. {
  217. io.MousePos = ImVec2(-1,-1);
  218. }
  219. for (int i = 0; i < 3; i++)
  220. {
  221. 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.
  222. g_MousePressed[i] = false;
  223. }
  224. io.MouseWheel = g_MouseWheel;
  225. g_MouseWheel = 0.0f;
  226. // Hide OS mouse cursor if ImGui is drawing it
  227. glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
  228. // Start the frame
  229. ImGui::NewFrame();
  230. }