summaryrefslogtreecommitdiff
path: root/3rdparty/imgui/backend/imgui_impl_opengl2.cpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-02-19 13:30:03 -0800
committerrtk0c <[email protected]>2022-02-19 13:30:03 -0800
commit9dcdcf68f6a60741cbdd287e7eda23b4a21a080e (patch)
treef5ff4c569607e1f63de9dd87bb7442e021b1f620 /3rdparty/imgui/backend/imgui_impl_opengl2.cpp
parent2599909052ef552efd5622bf73b60913e815b498 (diff)
Fix build errors and update all dependencies to latest version
- Fixed doctest executable not compiling - Fixed class accessibility error in Workflow_Main.cpp - Fixed selection of OpenGL loader in imgui OpenGL 2 & 3 backends - All loading is handled in our code now, the imgui_*_backend.cpp files are instructed to do nothing
Diffstat (limited to '3rdparty/imgui/backend/imgui_impl_opengl2.cpp')
-rw-r--r--3rdparty/imgui/backend/imgui_impl_opengl2.cpp79
1 files changed, 53 insertions, 26 deletions
diff --git a/3rdparty/imgui/backend/imgui_impl_opengl2.cpp b/3rdparty/imgui/backend/imgui_impl_opengl2.cpp
index 5771049..17a6fae 100644
--- a/3rdparty/imgui/backend/imgui_impl_opengl2.cpp
+++ b/3rdparty/imgui/backend/imgui_impl_opengl2.cpp
@@ -4,7 +4,8 @@
// Implemented features:
// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID!
-// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
+// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
+// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
// Read online: https://github.com/ocornut/imgui/tree/master/docs
@@ -18,6 +19,9 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
+// 2021-12-08: OpenGL: Fixed mishandling of the the ImDrawCmd::IdxOffset field! This is an old bug but it never had an effect until some internal rendering changes in 1.86.
+// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
+// 2021-05-19: OpenGL: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
// 2021-01-03: OpenGL: Backup, setup and restore GL_SHADE_MODEL state, disable GL_STENCIL_TEST and disable GL_NORMAL_ARRAY client state to increase compatibility with legacy OpenGL applications.
// 2020-01-23: OpenGL: Backup, setup and restore GL_TEXTURE_ENV to increase compatibility with legacy OpenGL applications.
// 2019-04-30: OpenGL: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
@@ -53,26 +57,52 @@
#include <GL/gl.h>
#endif
-// OpenGL Data
-static GLuint g_FontTexture = 0;
+struct ImGui_ImplOpenGL2_Data
+{
+ GLuint FontTexture;
+
+ ImGui_ImplOpenGL2_Data() { memset(this, 0, sizeof(*this)); }
+};
+
+// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
+// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
+static ImGui_ImplOpenGL2_Data* ImGui_ImplOpenGL2_GetBackendData()
+{
+ return ImGui::GetCurrentContext() ? (ImGui_ImplOpenGL2_Data*)ImGui::GetIO().BackendRendererUserData : NULL;
+}
// Functions
bool ImGui_ImplOpenGL2_Init()
{
- // Setup backend capabilities flags
ImGuiIO& io = ImGui::GetIO();
+ IM_ASSERT(io.BackendRendererUserData == NULL && "Already initialized a renderer backend!");
+
+ // Setup backend capabilities flags
+ ImGui_ImplOpenGL2_Data* bd = IM_NEW(ImGui_ImplOpenGL2_Data)();
+ io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_opengl2";
+
return true;
}
void ImGui_ImplOpenGL2_Shutdown()
{
+ ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
+ IM_ASSERT(bd != NULL && "No renderer backend to shutdown, or already shutdown?");
+ ImGuiIO& io = ImGui::GetIO();
+
ImGui_ImplOpenGL2_DestroyDeviceObjects();
+ io.BackendRendererName = NULL;
+ io.BackendRendererUserData = NULL;
+ IM_DELETE(bd);
}
void ImGui_ImplOpenGL2_NewFrame()
{
- if (!g_FontTexture)
+ ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
+ IM_ASSERT(bd != NULL && "Did you call ImGui_ImplOpenGL2_Init()?");
+
+ if (!bd->FontTexture)
ImGui_ImplOpenGL2_CreateDeviceObjects();
}
@@ -172,23 +202,18 @@ void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data)
else
{
// Project scissor/clipping rectangles into framebuffer space
- ImVec4 clip_rect;
- clip_rect.x = (pcmd->ClipRect.x - clip_off.x) * clip_scale.x;
- clip_rect.y = (pcmd->ClipRect.y - clip_off.y) * clip_scale.y;
- clip_rect.z = (pcmd->ClipRect.z - clip_off.x) * clip_scale.x;
- clip_rect.w = (pcmd->ClipRect.w - clip_off.y) * clip_scale.y;
+ ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
+ ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
+ if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
+ continue;
- if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f)
- {
- // Apply scissor/clipping rectangle
- 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));
+ // Apply scissor/clipping rectangle (Y is inverted in OpenGL)
+ glScissor((int)clip_min.x, (int)(fb_height - clip_max.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y));
- // Bind texture, Draw
- glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
- glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer);
- }
+ // Bind texture, Draw
+ glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID());
+ glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer + pcmd->IdxOffset);
}
- idx_buffer += pcmd->ElemCount;
}
}
@@ -213,6 +238,7 @@ bool ImGui_ImplOpenGL2_CreateFontsTexture()
{
// Build texture atlas
ImGuiIO& io = ImGui::GetIO();
+ ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
unsigned char* pixels;
int width, height;
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (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.
@@ -220,15 +246,15 @@ bool ImGui_ImplOpenGL2_CreateFontsTexture()
// Upload texture to graphics system
GLint last_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
- glGenTextures(1, &g_FontTexture);
- glBindTexture(GL_TEXTURE_2D, g_FontTexture);
+ glGenTextures(1, &bd->FontTexture);
+ glBindTexture(GL_TEXTURE_2D, bd->FontTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// Store our identifier
- io.Fonts->SetTexID((ImTextureID)(intptr_t)g_FontTexture);
+ io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture);
// Restore state
glBindTexture(GL_TEXTURE_2D, last_texture);
@@ -238,12 +264,13 @@ bool ImGui_ImplOpenGL2_CreateFontsTexture()
void ImGui_ImplOpenGL2_DestroyFontsTexture()
{
- if (g_FontTexture)
+ ImGuiIO& io = ImGui::GetIO();
+ ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
+ if (bd->FontTexture)
{
- ImGuiIO& io = ImGui::GetIO();
- glDeleteTextures(1, &g_FontTexture);
+ glDeleteTextures(1, &bd->FontTexture);
io.Fonts->SetTexID(0);
- g_FontTexture = 0;
+ bd->FontTexture = 0;
}
}