Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for transparent backbuffers to GLFW contexts (OpenGL3 and Vulkan) (reopens #2766) #7912

Open
wants to merge 1 commit into
base: docking
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backends/imgui_impl_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
#define GLFW_HAS_GAMEPAD_API (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetGamepadState() new api
#define GLFW_HAS_GETKEYNAME (GLFW_VERSION_COMBINED >= 3200) // 3.2+ glfwGetKeyName()
#define GLFW_HAS_GETERROR (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetError()
#define GLFW_HAS_TRANSPARENT_FRAMEBUFFERS (GLFW_VERSION_COMBINED >= 3300)

// GLFW data
enum GlfwClientApi
Expand Down Expand Up @@ -1089,6 +1090,9 @@ static void ImGui_ImplGlfw_CreateWindow(ImGuiViewport* viewport)
glfwWindowHint(GLFW_DECORATED, (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? false : true);
#if GLFW_HAS_WINDOW_TOPMOST
glfwWindowHint(GLFW_FLOATING, (viewport->Flags & ImGuiViewportFlags_TopMost) ? true : false);
#endif
#if GLFW_HAS_TRANSPARENT_FRAMEBUFFERS
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, true);
#endif
GLFWwindow* share_window = (bd->ClientApi == GlfwClientApi_OpenGL) ? bd->Window : nullptr;
vd->Window = glfwCreateWindow((int)viewport->Size.x, (int)viewport->Size.y, "No Title Yet", nullptr, share_window);
Expand Down
3 changes: 3 additions & 0 deletions backends/imgui_impl_opengl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ static void ImGui_ImplOpenGL2_SetupRenderState(ImDrawData* draw_data, int fb_wid
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers, polygon fill.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// technically supported by openGL2, but not exposed via GL.h, may need a loader?
// See https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glBlendFuncSeparate.xml
// https://registry.khronos.org/OpenGL/specs/gl/glspec20.pdf
//glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // In order to composite our output buffer we need to preserve alpha
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
Expand Down
2 changes: 1 addition & 1 deletion backends/imgui_impl_opengl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ static void ImGui_ImplOpenGL3_RenderWindow(ImGuiViewport* viewport, void*)
{
if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear))
{
ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
ImVec4 clear_color = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
}
Expand Down
4 changes: 3 additions & 1 deletion backends/imgui_impl_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,8 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, V
VkSurfaceCapabilitiesKHR cap;
err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, wd->Surface, &cap);
check_vk_result(err);
if (cap.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR)
info.compositeAlpha = VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR;
if (info.minImageCount < cap.minImageCount)
info.minImageCount = cap.minImageCount;
else if (cap.maxImageCount != 0 && info.minImageCount > cap.maxImageCount)
Expand Down Expand Up @@ -1770,7 +1772,7 @@ static void ImGui_ImplVulkan_RenderWindow(ImGuiViewport* viewport, void*)
check_vk_result(err);
}
{
ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
ImVec4 clear_color = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
memcpy(&wd->ClearValue.color.float32[0], &clear_color, 4 * sizeof(float));
}
#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
Expand Down
11 changes: 8 additions & 3 deletions examples/example_glfw_opengl3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ int main(int, char**)
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GL_TRUE); // Enable transparent backbuffers

// Create window with graphics context
GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", nullptr, nullptr);
Expand All @@ -74,11 +75,13 @@ int main(int, char**)
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();

ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
io.ConfigFlags |= ImGuiConfigFlags_TransparentBackbuffers; // Enable transparent backbuffer
//io.ConfigViewportsNoAutoMerge = true;
//io.ConfigViewportsNoTaskBarIcon = true;

Expand Down Expand Up @@ -156,7 +159,7 @@ int main(int, char**)

// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
{
static float f = 0.0f;
static float f = 1.0f;
static int counter = 0;

ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
Expand All @@ -165,8 +168,10 @@ int main(int, char**)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);

ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
ImGui::SliderFloat("Window Opacity", &ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w, 0.0f, 1.0f); // Edit background window opacity (alpha channel)
ImGui::ColorEdit4("clear color", (float*)&clear_color); // Edit 4 floats representing an RGBA color
clear_color.w = ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w;


if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
Expand Down
15 changes: 9 additions & 6 deletions examples/example_glfw_vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ int main(int, char**)

// Create window with Vulkan context
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+Vulkan example", nullptr, nullptr);
if (!glfwVulkanSupported())
{
Expand Down Expand Up @@ -427,10 +428,11 @@ int main(int, char**)
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
io.ConfigFlags |= ImGuiConfigFlags_TransparentBackbuffers; // Enable transparent backbuffer support
//io.ConfigViewportsNoAutoMerge = true;
//io.ConfigViewportsNoTaskBarIcon = true;

Expand Down Expand Up @@ -532,8 +534,9 @@ int main(int, char**)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);

ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
ImGui::SliderFloat("Window Opacity", &ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w, 0.0f, 1.0f); // Edit background window opacity (alpha channel)
ImGui::ColorEdit4("clear color", (float*)&clear_color); // Edit 4 floats representing an RGBA color
clear_color.w = ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w;

if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
Expand Down
Loading