Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
imgui_impl_glfw_gl3.cpp
Go to the documentation of this file.
1 // ImGui GLFW binding with OpenGL3 + shaders
2 // In this binding, ImTextureID is used to store an OpenGL 'GLuint' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.
3 
4 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
5 // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
6 // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
7 // https://github.com/ocornut/imgui
8 
10 #ifdef USING_OPENGL
11 
13 
14 #include "Engine/engine.hpp"
17 
18 // GL3W/GLFW
19 #ifdef _WIN32
20 #undef APIENTRY
21 #endif
22 
23 // Data
24 static GLFWwindow* g_Window = NULL;
25 static GLuint g_FontTexture = 0;
26 static int g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
27 static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0;
28 static int g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0;
29 static unsigned int g_VboHandle = 0, g_VaoHandle = 0, g_ElementsHandle = 0;
30 
31 // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
32 // 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.
33 // If text or lines are blurry when integrating ImGui in your engine: in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
34 void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawData* draw_data)
35 {
36  // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
37  ImGuiIO& io = ImGui::GetIO();
38  int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
39  int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
40  if (fb_width == 0 || fb_height == 0)
41  return;
43 
44  // Backup GL state
45  GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture);
46  glActiveTexture(GL_TEXTURE0);
47  GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
48  GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
49  GLint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, &last_sampler);
50  GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
51  GLint last_element_array_buffer; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
52  GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
53  GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
54  GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
55  GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
56  GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb);
57  GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb);
58  GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha);
59  GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha);
60  GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb);
61  GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha);
62  GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
63  GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
64  GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
65  GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
66 
67  // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
68  glEnable(GL_BLEND);
69  glBlendEquation(GL_FUNC_ADD);
70  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
71  glDisable(GL_CULL_FACE);
72  glDisable(GL_DEPTH_TEST);
73  glEnable(GL_SCISSOR_TEST);
74  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
75 
76  // Setup viewport, orthographic projection matrix
77  glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
78  const float ortho_projection[4][4] =
79  {
80  { 2.0f/io.DisplaySize.x, 0.0f, 0.0f, 0.0f },
81  { 0.0f, 2.0f/-io.DisplaySize.y, 0.0f, 0.0f },
82  { 0.0f, 0.0f, -1.0f, 0.0f },
83  {-1.0f, 1.0f, 0.0f, 1.0f },
84  };
85  glUseProgram(g_ShaderHandle);
86  glUniform1i(g_AttribLocationTex, 0);
87  glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
88  glBindVertexArray(g_VaoHandle);
89  glBindSampler(0, 0); // Rely on combined texture/sampler state.
90 
91  for (int n = 0; n < draw_data->CmdListsCount; n++)
92  {
93  const ImDrawList* cmd_list = draw_data->CmdLists[n];
94  const ImDrawIdx* idx_buffer_offset = 0;
95 
96  glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
97  glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW);
98 
99  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle);
100  glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW);
101 
102  for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
103  {
104  const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
105  if (pcmd->UserCallback)
106  {
107  pcmd->UserCallback(cmd_list, pcmd);
108  }
109  else
110  {
111  glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
112  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));
113  glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset);
114  }
115  idx_buffer_offset += pcmd->ElemCount;
116  }
117  }
118 
119  // Restore modified GL state
120  glUseProgram(last_program);
121  glBindTexture(GL_TEXTURE_2D, last_texture);
122  glBindSampler(0, last_sampler);
123  glActiveTexture(last_active_texture);
124  glBindVertexArray(last_vertex_array);
125  glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
126  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer);
127  glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
128  glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
129  if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
130  if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
131  if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
132  if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
133  glPolygonMode(GL_FRONT_AND_BACK, last_polygon_mode[0]);
134  glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
135  glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
136 }
137 
138 static const char* ImGui_ImplGlfwGL3_GetClipboardText(void* user_data)
139 {
140  return glfwGetClipboardString((GLFWwindow*)user_data);
141 }
142 
143 static void ImGui_ImplGlfwGL3_SetClipboardText(void* user_data, const char* text)
144 {
145  glfwSetClipboardString((GLFWwindow*)user_data, text);
146 }
147 
148 bool ImGui_ImplGlfwGL3_CreateFontsTexture()
149 {
150  // Build texture atlas
151  ImGuiIO& io = ImGui::GetIO();
152  unsigned char* pixels;
153  int width, height;
154  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.
155 
156  // Upload texture to graphics system
157  GLint last_texture;
158  glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
159  glGenTextures(1, &g_FontTexture);
160  glBindTexture(GL_TEXTURE_2D, g_FontTexture);
161  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
162  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
163  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
164 
165  // Store our identifier
166  io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
167 
168  // Restore state
169  glBindTexture(GL_TEXTURE_2D, last_texture);
170 
171  return true;
172 }
173 
174 bool ImGui_ImplGlfwGL3_CreateDeviceObjects()
175 {
176  // Backup GL state
177  GLint last_texture, last_array_buffer, last_vertex_array;
178  glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
179  glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
180  glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
181 
182  const GLchar *vertex_shader =
183  "#version 330\n"
184  "uniform mat4 ProjMtx;\n"
185  "in vec2 Position;\n"
186  "in vec2 UV;\n"
187  "in vec4 Color;\n"
188  "out vec2 Frag_UV;\n"
189  "out vec4 Frag_Color;\n"
190  "void main()\n"
191  "{\n"
192  " Frag_UV = UV;\n"
193  " Frag_Color = Color;\n"
194  " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
195  "}\n";
196 
197  const GLchar* fragment_shader =
198  "#version 330\n"
199  "uniform sampler2D Texture;\n"
200  "in vec2 Frag_UV;\n"
201  "in vec4 Frag_Color;\n"
202  "out vec4 Out_Color;\n"
203  "void main()\n"
204  "{\n"
205  " Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n"
206  "}\n";
207 
208  g_ShaderHandle = glCreateProgram();
209  g_VertHandle = glCreateShader(GL_VERTEX_SHADER);
210  g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER);
211  glShaderSource(g_VertHandle, 1, &vertex_shader, 0);
212  glShaderSource(g_FragHandle, 1, &fragment_shader, 0);
213  glCompileShader(g_VertHandle);
214  glCompileShader(g_FragHandle);
215  glAttachShader(g_ShaderHandle, g_VertHandle);
216  glAttachShader(g_ShaderHandle, g_FragHandle);
217  glLinkProgram(g_ShaderHandle);
218 
219  g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture");
220  g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx");
221  g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position");
222  g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV");
223  g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color");
224 
225  glGenBuffers(1, &g_VboHandle);
226  glGenBuffers(1, &g_ElementsHandle);
227 
228  glGenVertexArrays(1, &g_VaoHandle);
229  glBindVertexArray(g_VaoHandle);
230  glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
231  glEnableVertexAttribArray(g_AttribLocationPosition);
232  glEnableVertexAttribArray(g_AttribLocationUV);
233  glEnableVertexAttribArray(g_AttribLocationColor);
234 
235 #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
236  glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos));
237  glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv));
238  glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col));
239 #undef OFFSETOF
240 
241  ImGui_ImplGlfwGL3_CreateFontsTexture();
242 
243  // Restore modified GL state
244  glBindTexture(GL_TEXTURE_2D, last_texture);
245  glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
246  glBindVertexArray(last_vertex_array);
247 
248  return true;
249 }
250 
251 void ImGui_ImplGlfwGL3_InvalidateDeviceObjects()
252 {
253  if (g_VaoHandle) glDeleteVertexArrays(1, &g_VaoHandle);
254  if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle);
255  if (g_ElementsHandle) glDeleteBuffers(1, &g_ElementsHandle);
256  g_VaoHandle = g_VboHandle = g_ElementsHandle = 0;
257 
258  if (g_ShaderHandle && g_VertHandle) glDetachShader(g_ShaderHandle, g_VertHandle);
259  if (g_VertHandle) glDeleteShader(g_VertHandle);
260  g_VertHandle = 0;
261 
262  if (g_ShaderHandle && g_FragHandle) glDetachShader(g_ShaderHandle, g_FragHandle);
263  if (g_FragHandle) glDeleteShader(g_FragHandle);
264  g_FragHandle = 0;
265 
266  if (g_ShaderHandle) glDeleteProgram(g_ShaderHandle);
267  g_ShaderHandle = 0;
268 
269  if (g_FontTexture)
270  {
271  glDeleteTextures(1, &g_FontTexture);
272  ImGui::GetIO().Fonts->TexID = 0;
273  g_FontTexture = 0;
274  }
275 }
276 
277 bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window)
278 {
279  g_Window = window;
280 
281  ImGuiIO& io = ImGui::GetIO();
282 
283  io.RenderDrawListsFn = ImGui_ImplGlfwGL3_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
284  io.SetClipboardTextFn = ImGui_ImplGlfwGL3_SetClipboardText;
285  io.GetClipboardTextFn = ImGui_ImplGlfwGL3_GetClipboardText;
286  io.ClipboardUserData = g_Window;
287 #ifdef _WIN32
288  io.ImeWindowHandle = glfwGetWin32Window(g_Window);
289 #endif
290 
291  return true;
292 }
293 
294 void ImGui_ImplGlfwGL3_Shutdown()
295 {
296  ImGui_ImplGlfwGL3_InvalidateDeviceObjects();
297  ImGui::Shutdown();
298 }
299 
300 void ImGui_ImplGlfwGL3_NewFrame()
301 {
302  if (!g_FontTexture)
303  ImGui_ImplGlfwGL3_CreateDeviceObjects();
304 
305  ImGuiIO& io = ImGui::GetIO();
306 
307  // TODO pass in delta time in Time class
308  //double current_time = glfwGetTime();
309  //io.DeltaTime = g_Time > 0.0 ? float(current_time - g_Time) : float(1.0f / 60.0f);
310  //g_Time = current_time;
311  io.DeltaTime = Engine::Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime();
312 
313  // Start the frame
314  ImGui::NewFrame();
315 }
316 #endif USING_OPENGL
const char *(* GetClipboardTextFn)(void *user_data)
Definition: imgui.h:818
ImDrawList ** CmdLists
Definition: imgui.h:1316
void(* SetClipboardTextFn)(void *user_data, const char *text)
Definition: imgui.h:819
ImVec2 DisplaySize
Definition: imgui.h:783
IMGUI_API void NewFrame()
Definition: imgui.cpp:2170
ImVec4 ClipRect
Definition: imgui.h:1183
int CmdListsCount
Definition: imgui.h:1317
T * Data
Definition: imgui.h:899
float DeltaTime
Definition: imgui.h:784
unsigned short ImDrawIdx
Definition: imgui.h:1193
IMGUI_API void ScaleClipRects(const ImVec2 &sc)
int Size
Definition: imgui.h:897
float w
Definition: imgui.h:108
ImVector< ImDrawCmd > CmdBuffer
Definition: imgui.h:1230
ImTextureID TextureId
Definition: imgui.h:1184
void * ClipboardUserData
Definition: imgui.h:820
IMGUI_API ImGuiIO & GetIO()
Definition: imgui.cpp:2144
float z
Definition: imgui.h:108
IMGUI_API void GetTexDataAsRGBA32(unsigned char **out_pixels, int *out_width, int *out_height, int *out_bytes_per_pixel=NULL)
ImTextureID TexID
Definition: imgui.h:1443
float x
Definition: imgui.h:108
float y
Definition: imgui.h:98
ImDrawCallback UserCallback
Definition: imgui.h:1185
IMGUI_API void Shutdown()
Definition: imgui.cpp:2414
void(* RenderDrawListsFn)(ImDrawData *data)
Definition: imgui.h:814
void * ImeWindowHandle
Definition: imgui.h:830
Definition: imgui.h:777
ImVector< ImDrawVert > VtxBuffer
Definition: imgui.h:1232
unsigned int ElemCount
Definition: imgui.h:1182
ImVec2 DisplayFramebufferScale
Definition: imgui.h:800
static eastl::weak_ptr< Engine > GetEngine() noexcept
This method allows you to get the instance of the Engine. This method will automatically initialize t...
Definition: engine.cpp:130
ImVector< ImDrawIdx > IdxBuffer
Definition: imgui.h:1231
ImFontAtlas * Fonts
Definition: imgui.h:796
float y
Definition: imgui.h:108
float x
Definition: imgui.h:98