Skip to content

Commit de7cb42

Browse files
committed
Fix ImGui window framebuffer clearing
1 parent 26611c7 commit de7cb42

6 files changed

Lines changed: 61 additions & 0 deletions

File tree

examples/glfw_opengl3/src/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ int main() {
5050
ImGui::End();
5151
ImGui::Render();
5252

53+
int framebufferWidth = 0;
54+
int framebufferHeight = 0;
55+
ImGui::Backend::GlfwOpenGL3::GetFramebufferSize(window, &framebufferWidth, &framebufferHeight);
56+
ImGui::Backend::GlfwOpenGL3::Viewport(0, 0, framebufferWidth, framebufferHeight);
57+
ImGui::Backend::GlfwOpenGL3::ClearColor(0.08f, 0.09f, 0.10f, 1.0f);
58+
ImGui::Backend::GlfwOpenGL3::ClearColorBuffer();
5359
ImGui::Backend::GlfwOpenGL3::RenderDrawData(ImGui::GetDrawData());
5460
ImGui::Backend::GlfwOpenGL3::SwapBuffers(window);
5561

examples/minimal_window/src/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ int main() {
6565
ImGui::End();
6666
ImGui::Render();
6767

68+
int framebufferWidth = 0;
69+
int framebufferHeight = 0;
70+
Backend::GetFramebufferSize(window, &framebufferWidth, &framebufferHeight);
71+
Backend::Viewport(0, 0, framebufferWidth, framebufferHeight);
72+
Backend::ClearColor(0.08f, 0.09f, 0.10f, 1.0f);
73+
Backend::ClearColorBuffer();
6874
Backend::RenderDrawData(ImGui::GetDrawData());
6975
Backend::SwapBuffers(window);
7076
}

src/backends/glfw.cppm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export namespace ImGui::Backend::Glfw {
6363
glfwMakeContextCurrent(window);
6464
}
6565

66+
void GetFramebufferSize(Window* window, int* width, int* height) {
67+
glfwGetFramebufferSize(window, width, height);
68+
}
69+
6670
void SwapInterval(int interval) {
6771
glfwSwapInterval(interval);
6872
}

src/backends/glfw_opengl3.cppm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export namespace ImGui::Backend::GlfwOpenGL3 {
5454
Glfw::MakeContextCurrent(window);
5555
}
5656

57+
void GetFramebufferSize(Window* window, int* width, int* height) {
58+
Glfw::GetFramebufferSize(window, width, height);
59+
}
60+
5761
void SwapInterval(int interval) {
5862
Glfw::SwapInterval(interval);
5963
}
@@ -111,6 +115,18 @@ export namespace ImGui::Backend::GlfwOpenGL3 {
111115
Glfw::NewFrame();
112116
}
113117

118+
void Viewport(int x, int y, int width, int height) {
119+
OpenGL3::Viewport(x, y, width, height);
120+
}
121+
122+
void ClearColor(float red, float green, float blue, float alpha) {
123+
OpenGL3::ClearColor(red, green, blue, alpha);
124+
}
125+
126+
void ClearColorBuffer() {
127+
OpenGL3::ClearColorBuffer();
128+
}
129+
114130
void RenderDrawData(ImDrawData* drawData) {
115131
OpenGL3::RenderDrawData(drawData);
116132
}

src/backends/opengl3.cppm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module;
22

33
#include <imgui_impl_opengl3.h>
4+
#include <imgui_impl_opengl3_loader.h>
45

56
export module imgui.backend.opengl3;
67

@@ -19,6 +20,18 @@ export namespace ImGui::Backend::OpenGL3 {
1920
ImGui_ImplOpenGL3_NewFrame();
2021
}
2122

23+
void Viewport(int x, int y, int width, int height) {
24+
glViewport(x, y, width, height);
25+
}
26+
27+
void ClearColor(float red, float green, float blue, float alpha) {
28+
glClearColor(red, green, blue, alpha);
29+
}
30+
31+
void ClearColorBuffer() {
32+
glClear(GL_COLOR_BUFFER_BIT);
33+
}
34+
2235
void RenderDrawData(ImDrawData* drawData) {
2336
ImGui_ImplOpenGL3_RenderDrawData(drawData);
2437
}

tests/backend_test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ TEST(ImGuiGlfwBackendModuleTest, ExposesPlatformApi) {
1616
auto restoreCallbacks = &ImGui::Backend::Glfw::RestoreCallbacks;
1717
auto getContentScaleForWindow = &ImGui::Backend::Glfw::GetContentScaleForWindow;
1818
auto getContentScaleForMonitor = &ImGui::Backend::Glfw::GetContentScaleForMonitor;
19+
auto getFramebufferSize = &ImGui::Backend::Glfw::GetFramebufferSize;
1920
auto getError = &ImGui::Backend::Glfw::GetError;
2021

2122
EXPECT_EQ(window, nullptr);
@@ -27,18 +28,25 @@ TEST(ImGuiGlfwBackendModuleTest, ExposesPlatformApi) {
2728
EXPECT_NE(restoreCallbacks, nullptr);
2829
EXPECT_NE(getContentScaleForWindow, nullptr);
2930
EXPECT_NE(getContentScaleForMonitor, nullptr);
31+
EXPECT_NE(getFramebufferSize, nullptr);
3032
EXPECT_NE(getError, nullptr);
3133
}
3234

3335
TEST(ImGuiOpenGL3BackendModuleTest, ExposesRendererApi) {
3436
auto init = &ImGui::Backend::OpenGL3::Init;
3537
auto shutdown = &ImGui::Backend::OpenGL3::Shutdown;
3638
auto newFrame = &ImGui::Backend::OpenGL3::NewFrame;
39+
auto viewport = &ImGui::Backend::OpenGL3::Viewport;
40+
auto clearColor = &ImGui::Backend::OpenGL3::ClearColor;
41+
auto clearColorBuffer = &ImGui::Backend::OpenGL3::ClearColorBuffer;
3742
auto renderDrawData = &ImGui::Backend::OpenGL3::RenderDrawData;
3843

3944
EXPECT_NE(init, nullptr);
4045
EXPECT_NE(shutdown, nullptr);
4146
EXPECT_NE(newFrame, nullptr);
47+
EXPECT_NE(viewport, nullptr);
48+
EXPECT_NE(clearColor, nullptr);
49+
EXPECT_NE(clearColorBuffer, nullptr);
4250
EXPECT_NE(renderDrawData, nullptr);
4351
}
4452

@@ -50,9 +58,13 @@ TEST(ImGuiGlfwOpenGL3BackendModuleTest, ExposesCombinedApi) {
5058
auto createWindow = &ImGui::Backend::GlfwOpenGL3::CreateWindow;
5159
auto destroyWindow = &ImGui::Backend::GlfwOpenGL3::DestroyWindow;
5260
auto makeContextCurrent = &ImGui::Backend::GlfwOpenGL3::MakeContextCurrent;
61+
auto getFramebufferSize = &ImGui::Backend::GlfwOpenGL3::GetFramebufferSize;
5362
auto init = &ImGui::Backend::GlfwOpenGL3::Init;
5463
auto shutdown = &ImGui::Backend::GlfwOpenGL3::Shutdown;
5564
auto newFrame = &ImGui::Backend::GlfwOpenGL3::NewFrame;
65+
auto viewport = &ImGui::Backend::GlfwOpenGL3::Viewport;
66+
auto clearColor = &ImGui::Backend::GlfwOpenGL3::ClearColor;
67+
auto clearColorBuffer = &ImGui::Backend::GlfwOpenGL3::ClearColorBuffer;
5668
auto renderDrawData = &ImGui::Backend::GlfwOpenGL3::RenderDrawData;
5769
auto getError = &ImGui::Backend::GlfwOpenGL3::GetError;
5870

@@ -63,9 +75,13 @@ TEST(ImGuiGlfwOpenGL3BackendModuleTest, ExposesCombinedApi) {
6375
EXPECT_NE(createWindow, nullptr);
6476
EXPECT_NE(destroyWindow, nullptr);
6577
EXPECT_NE(makeContextCurrent, nullptr);
78+
EXPECT_NE(getFramebufferSize, nullptr);
6679
EXPECT_NE(init, nullptr);
6780
EXPECT_NE(shutdown, nullptr);
6881
EXPECT_NE(newFrame, nullptr);
82+
EXPECT_NE(viewport, nullptr);
83+
EXPECT_NE(clearColor, nullptr);
84+
EXPECT_NE(clearColorBuffer, nullptr);
6985
EXPECT_NE(renderDrawData, nullptr);
7086
EXPECT_NE(getError, nullptr);
7187
}

0 commit comments

Comments
 (0)