imgui_impl_glfw.cpp
Go to the documentation of this file.
1 // ImGui GLFW binding with OpenGL
2 // In this binding, ImTextureID is used to store an OpenGL 'GLuint' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.
3 
4 // If your context is GL3/GL3 then prefer using the code in opengl3_example.
5 // You *might* use this code with a GL3/GL4 context but make sure you disable the programmable pipeline by calling "glUseProgram(0)" before ImGui::Render().
6 // We cannot do that from GL2 code because the function doesn't exist.
7 
8 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
9 // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
10 // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
11 // https://github.com/ocornut/imgui
12 
13 #include "imgui.h"
14 #include "imgui_impl_glfw.h"
15 
16 // GLFW
17 #include <GLFW/glfw3.h>
18 #ifdef _WIN32
19 #undef APIENTRY
20 #define GLFW_EXPOSE_NATIVE_WIN32
21 #define GLFW_EXPOSE_NATIVE_WGL
22 #include <GLFW/glfw3native.h>
23 #endif
24 
25 // Data
27 static double g_Time = 0.0f;
28 static bool g_MousePressed[3] = { false, false, false };
29 static float g_MouseWheel = 0.0f;
30 static GLuint g_FontTexture = 0;
31 
32 // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
33 // If text or lines are blurry when integrating ImGui in your engine:
34 // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
36 {
37  // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
38  ImGuiIO& io = ImGui::GetIO();
39  int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
40  int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
41  if (fb_width == 0 || fb_height == 0)
42  return;
44 
45  // We are using the OpenGL fixed pipeline to make the example code simpler to read!
46  // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
47  GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
48  GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
59  //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context
60 
61  // Setup viewport, orthographic projection matrix
62  glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
64  glPushMatrix();
66  glOrtho(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f, -1.0f, +1.0f);
68  glPushMatrix();
70 
71  // Render command lists
72  #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
73  for (int n = 0; n < draw_data->CmdListsCount; n++)
74  {
75  const ImDrawList* cmd_list = draw_data->CmdLists[n];
76  const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front();
77  const ImDrawIdx* idx_buffer = &cmd_list->IdxBuffer.front();
78  glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
79  glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
80  glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
81 
82  for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
83  {
84  const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
85  if (pcmd->UserCallback)
86  {
87  pcmd->UserCallback(cmd_list, pcmd);
88  }
89  else
90  {
92  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));
94  }
95  idx_buffer += pcmd->ElemCount;
96  }
97  }
98  #undef OFFSETOF
99 
100  // Restore modified state
104  glBindTexture(GL_TEXTURE_2D, (GLuint)last_texture);
106  glPopMatrix();
108  glPopMatrix();
109  glPopAttrib();
110  glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
111 }
112 
114 {
116 }
117 
118 static void ImGui_ImplGlfw_SetClipboardText(const char* text)
119 {
121 }
122 
123 void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
124 {
125  if (action == GLFW_PRESS && button >= 0 && button < 3)
126  g_MousePressed[button] = true;
127 }
128 
129 void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double /*xoffset*/, double yoffset)
130 {
131  g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines.
132 }
133 
134 void ImGui_ImplGlFw_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
135 {
136  ImGuiIO& io = ImGui::GetIO();
137  if (action == GLFW_PRESS)
138  io.KeysDown[key] = true;
139  if (action == GLFW_RELEASE)
140  io.KeysDown[key] = false;
141 
142  (void)mods; // Modifiers are not reliable across systems
147 }
148 
150 {
151  ImGuiIO& io = ImGui::GetIO();
152  if (c > 0 && c < 0x10000)
153  io.AddInputCharacter((unsigned short)c);
154 }
155 
157 {
158  // Build texture atlas
159  ImGuiIO& io = ImGui::GetIO();
160  unsigned char* pixels;
161  int width, height;
162  io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
163 
164  // Upload texture to graphics system
165  GLint last_texture;
166  glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
171  glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
172 
173  // Store our identifier
174  io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
175 
176  // Restore state
177  glBindTexture(GL_TEXTURE_2D, last_texture);
178 
179  return true;
180 }
181 
183 {
184  if (g_FontTexture)
185  {
187  ImGui::GetIO().Fonts->TexID = 0;
188  g_FontTexture = 0;
189  }
190 }
191 
192 bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks)
193 {
194  g_Window = window;
195 
196  ImGuiIO& io = ImGui::GetIO();
197  io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
216 
217  io.RenderDrawListsFn = ImGui_ImplGlfw_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
220 #ifdef _WIN32
222 #endif
223 
224  if (install_callbacks)
225  {
230  }
231 
232  return true;
233 }
234 
236 {
238  ImGui::Shutdown();
239 }
240 
241 void ImGui_ImplGlfw_NewFrame(float scale_factor)
242 {
243  if (!g_FontTexture)
245 
246  ImGuiIO& io = ImGui::GetIO();
247 
248  // Setup display size (every frame to accommodate for window resizing)
249  int w, h;
250  int display_w, display_h;
251  glfwGetWindowSize(g_Window, &w, &h);
252  if (scale_factor > 0.f)
253  {
254  w = static_cast<int>(w / scale_factor);
255  h = static_cast<int>(h / scale_factor);
256  }
257  glfwGetFramebufferSize(g_Window, &display_w, &display_h);
258  io.DisplaySize = ImVec2((float)w, (float)h);
259  io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
260 
261  // Setup time step
262  double current_time = glfwGetTime();
263  io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
265 
266  // Setup inputs
267  // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
269  {
270  double mouse_x, mouse_y;
271  glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
272  io.MousePos = ImVec2((float)mouse_x / scale_factor, (float)mouse_y / scale_factor); // Mouse position in screen coordinates (set to -1,-1 if no mouse / on another screen, etc.)
273  }
274  else
275  {
276  io.MousePos = ImVec2(-1,-1);
277  }
278 
279  for (int i = 0; i < 3; i++)
280  {
281  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.
282  g_MousePressed[i] = false;
283  }
284 
285  //io.MouseWheel = g_MouseWheel;
286  g_MouseWheel = 0.0f;
287 
288  // Hide OS mouse cursor if ImGui is drawing it
290 
291  // Start the frame
292  ImGui::NewFrame();
293 }
value_type & front()
Definition: imgui.h:923
void ImGui_ImplGlfw_ScrollCallback(GLFWwindow *, double, double yoffset)
#define glDisableClientState
GLFWAPI void glfwSetInputMode(GLFWwindow *window, int mode, int value)
Sets an input option for the specified window.
Definition: input.c:484
#define GL_TEXTURE_MAG_FILTER
#define glEnableClientState
void * TexID
Definition: imgui.h:1375
#define GLFW_KEY_RIGHT_CONTROL
Definition: glfw3.h:478
bool MouseDrawCursor
Definition: imgui.h:843
ImDrawList ** CmdLists
Definition: imgui.h:1300
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:544
#define GLFW_KEY_LEFT
Definition: glfw3.h:419
static const char * ImGui_ImplGlfw_GetClipboardText()
#define OFFSETOF(TYPE, ELEMENT)
typedef void(APIENTRY *GLDEBUGPROC)(GLenum source
GLFWAPI int glfwGetWindowAttrib(GLFWwindow *window, int attrib)
Returns an attribute of the specified window.
Definition: window.c:804
#define GL_VERTEX_ARRAY
The header of the GLFW 3 API.
ImVec2 MousePos
Definition: imgui.h:840
IMGUI_API void AddInputCharacter(ImWchar c)
Definition: imgui.cpp:843
#define GL_TEXTURE_2D
long long current_time()
#define glVertexPointer
#define GL_SCISSOR_TEST
#define glOrtho
#define GL_ALPHA
static GLFWwindow * window
Definition: joysticks.c:55
Definition: imgui.h:88
#define GL_UNSIGNED_BYTE
#define glGetIntegerv
#define GLFW_KEY_UP
Definition: glfw3.h:421
#define GL_TEXTURE_BINDING_2D
ImVec2 DisplaySize
Definition: imgui.h:786
#define glPopMatrix
#define GL_PROJECTION
#define GLFW_KEY_X
Definition: glfw3.h:401
#define glPushAttrib
#define GLFW_CURSOR
Definition: glfw3.h:1001
IMGUI_API void NewFrame()
Definition: imgui.cpp:2040
void ImGui_ImplGlfw_CharCallback(GLFWwindow *, unsigned int c)
void ImGui_ImplGlfw_Shutdown()
GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow *window, GLFWmousebuttonfun cbfun)
Sets the mouse button callback.
Definition: input.c:821
ImVec4 ClipRect
Definition: imgui.h:1172
int GLsizei
#define GLFW_KEY_LEFT_SHIFT
Definition: glfw3.h:473
int CmdListsCount
Definition: imgui.h:1301
#define GLFW_KEY_PAGE_DOWN
Definition: glfw3.h:423
GLdouble GLdouble GLdouble w
#define GL_BLEND
#define GLFW_KEY_HOME
Definition: glfw3.h:424
#define GL_LINEAR
static GLuint g_FontTexture
#define glLoadIdentity
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
#define GLFW_KEY_PAGE_UP
Definition: glfw3.h:422
GLdouble n
Definition: glext.h:1966
#define glTexImage2D
GLFWAPI int glfwGetMouseButton(GLFWwindow *window, int button)
Returns the last reported state of a mouse button for the specified window.
Definition: input.c:614
#define GL_SRC_ALPHA
#define GLFW_KEY_Z
Definition: glfw3.h:403
#define glEnable
#define GLFW_KEY_BACKSPACE
Definition: glfw3.h:415
int KeyMap[ImGuiKey_COUNT]
Definition: imgui.h:794
#define GLFW_KEY_A
Definition: glfw3.h:378
float DeltaTime
Definition: imgui.h:787
#define GLFW_KEY_V
Definition: glfw3.h:399
unsigned short ImDrawIdx
Definition: imgui.h:1182
#define GLFW_RELEASE
The key or mouse button was released.
Definition: glfw3.h:297
GLuint64 key
Definition: glext.h:8966
IMGUI_API void ScaleClipRects(const ImVec2 &sc)
Definition: imgui_draw.cpp:997
GLFWAPI const char * glfwGetClipboardString(GLFWwindow *window)
Returns the contents of the clipboard as a string.
Definition: input.c:1269
GLdouble f
void ImGui_ImplGlFw_KeyCallback(GLFWwindow *, int key, int, int action, int mods)
#define glTexCoordPointer
#define glGenTextures
#define GLFW_KEY_RIGHT_SUPER
Definition: glfw3.h:480
#define GL_FLOAT
#define GL_COLOR_BUFFER_BIT
#define GLFW_CURSOR_NORMAL
Definition: glfw3.h:1006
void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow *, int button, int action, int)
#define GL_TRANSFORM_BIT
#define GL_VIEWPORT
#define GLFW_KEY_END
Definition: glfw3.h:425
#define glTexParameteri
float w
Definition: imgui.h:100
const GLubyte * c
Definition: glext.h:12690
int GLint
#define glScissor
#define glPushMatrix
bool ImGui_ImplGlfw_Init(GLFWwindow *window, bool install_callbacks)
ImVector< ImDrawCmd > CmdBuffer
Definition: imgui.h:1217
void(* SetClipboardTextFn)(const char *text)
Definition: imgui.h:824
ImTextureID TextureId
Definition: imgui.h:1173
#define GLFW_KEY_DELETE
Definition: glfw3.h:417
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
IMGUI_API ImGuiIO & GetIO()
Definition: imgui.cpp:2014
GLint GLsizei GLsizei height
#define GL_UNSIGNED_SHORT
#define GLFW_KEY_TAB
Definition: glfw3.h:414
float z
Definition: imgui.h:100
static GLFWwindow * g_Window
bool KeysDown[512]
Definition: imgui.h:848
#define GL_TEXTURE_MIN_FILTER
static float g_MouseWheel
GLFWAPI GLFWcharfun glfwSetCharCallback(GLFWwindow *window, GLFWcharfun cbfun)
Sets the Unicode character callback.
Definition: input.c:801
bool KeyShift
Definition: imgui.h:845
#define GLFW_KEY_ENTER
Definition: glfw3.h:413
#define GL_ENABLE_BIT
bool MouseDown[5]
Definition: imgui.h:841
GLint GLint GLsizei GLint GLenum GLenum const void * pixels
action
Definition: enums.py:62
#define glViewport
bool KeyCtrl
Definition: imgui.h:844
float x
Definition: imgui.h:100
#define GL_MODELVIEW
#define GL_ONE_MINUS_SRC_ALPHA
#define GLFW_FOCUSED
Input focus window hint and attribute.
Definition: glfw3.h:763
const char *(* GetClipboardTextFn)()
Definition: imgui.h:823
#define glDeleteTextures
GLFWAPI void glfwSetClipboardString(GLFWwindow *window, const char *string)
Sets the clipboard to the specified string.
Definition: input.c:1261
static bool g_MousePressed[3]
float y
Definition: imgui.h:90
The header of the native access functions.
ImDrawCallback UserCallback
Definition: imgui.h:1174
void ImGui_ImplGlfw_NewFrame(float scale_factor)
GLFWAPI void glfwGetCursorPos(GLFWwindow *window, double *xpos, double *ypos)
Retrieves the position of the cursor relative to the client area of the window.
Definition: input.c:637
IMGUI_API void GetTexDataAsAlpha8(unsigned char **out_pixels, int *out_width, int *out_height, int *out_bytes_per_pixel=NULL)
#define GL_CULL_FACE
#define GLFW_KEY_DOWN
Definition: glfw3.h:420
IMGUI_API void Shutdown()
Definition: imgui.cpp:2277
bool KeySuper
Definition: imgui.h:847
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
void(* RenderDrawListsFn)(ImDrawData *data)
Definition: imgui.h:819
void ImGui_ImplGlfw_InvalidateDeviceObjects()
#define GLFW_CURSOR_HIDDEN
Definition: glfw3.h:1007
unsigned int GLuint
GLint GLint GLint yoffset
#define GLFW_KEY_RIGHT_ALT
Definition: glfw3.h:479
GLFWAPI GLFWscrollfun glfwSetScrollCallback(GLFWwindow *window, GLFWscrollfun cbfun)
Sets the scroll callback.
Definition: input.c:854
#define glPopAttrib
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
#define glDrawElements
#define glBindTexture
#define GLFW_KEY_LEFT_CONTROL
Definition: glfw3.h:474
void * ImeWindowHandle
Definition: imgui.h:834
bool ImGui_ImplGlfw_CreateDeviceObjects()
Definition: imgui.h:780
ImVector< ImDrawVert > VtxBuffer
Definition: imgui.h:1219
#define GL_COLOR_ARRAY
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
float rs2_vector::* pos
unsigned int ElemCount
Definition: imgui.h:1171
ImVec2 DisplayFramebufferScale
Definition: imgui.h:802
#define NULL
Definition: tinycthread.c:47
bool KeyAlt
Definition: imgui.h:846
int i
#define GLFW_KEY_LEFT_ALT
Definition: glfw3.h:475
static double g_Time
#define glBlendFunc
#define GLFW_KEY_RIGHT_SHIFT
Definition: glfw3.h:477
#define glColorPointer
#define GL_TEXTURE_COORD_ARRAY
ImVector< ImDrawIdx > IdxBuffer
Definition: imgui.h:1218
#define GLFW_KEY_RIGHT
Definition: glfw3.h:418
#define GLFW_KEY_Y
Definition: glfw3.h:402
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
_W64 signed int intptr_t
Definition: stdint.h:118
void ImGui_ImplGlfw_RenderDrawLists(ImDrawData *draw_data)
ImFontAtlas * Fonts
Definition: imgui.h:799
static void ImGui_ImplGlfw_SetClipboardText(const char *text)
#define glMatrixMode
#define GL_UNSIGNED_INT
struct GLFWwindow GLFWwindow
#define GLFW_KEY_LEFT_SUPER
Definition: glfw3.h:476
GLint GLsizei width
#define GL_TRIANGLES
#define GL_DEPTH_TEST
float y
Definition: imgui.h:100
float x
Definition: imgui.h:90
#define GLFW_KEY_C
Definition: glfw3.h:380
#define glDisable
GLFWAPI HWND glfwGetWin32Window(GLFWwindow *handle)
int size() const
Definition: imgui.h:912


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:17