Skip to content

Commit 1dcb9c8

Browse files
committed
Examples: OpenGL: Fix early return on zero-sized framebuffer breaking GL state (ocornut#486, ocornut#547)
1 parent 6346690 commit 1dcb9c8

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

examples/opengl3_example/imgui_impl_glfw_gl3.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ static unsigned int g_VboHandle = 0, g_VaoHandle = 0, g_ElementsHandle = 0;
3333
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
3434
void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawData* draw_data)
3535
{
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;
42+
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
43+
3644
// Backup GL state
3745
GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
3846
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
@@ -58,14 +66,6 @@ void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawData* draw_data)
5866
glEnable(GL_SCISSOR_TEST);
5967
glActiveTexture(GL_TEXTURE0);
6068

61-
// Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
62-
ImGuiIO& io = ImGui::GetIO();
63-
int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
64-
int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
65-
if (fb_width == 0 || fb_height == 0)
66-
return;
67-
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
68-
6969
// Setup viewport, orthographic projection matrix
7070
glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
7171
const float ortho_projection[4][4] =

examples/opengl_example/imgui_impl_glfw.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ static GLuint g_FontTexture = 0;
2828
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
2929
void ImGui_ImplGlfw_RenderDrawLists(ImDrawData* draw_data)
3030
{
31+
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
32+
ImGuiIO& io = ImGui::GetIO();
33+
int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
34+
int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
35+
if (fb_width == 0 || fb_height == 0)
36+
return;
37+
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
38+
3139
// We are using the OpenGL fixed pipeline to make the example code simpler to read!
3240
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
3341
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
@@ -44,14 +52,6 @@ void ImGui_ImplGlfw_RenderDrawLists(ImDrawData* draw_data)
4452
glEnable(GL_TEXTURE_2D);
4553
//glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context
4654

47-
// Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
48-
ImGuiIO& io = ImGui::GetIO();
49-
int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
50-
int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
51-
if (fb_width == 0 || fb_height == 0)
52-
return;
53-
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
54-
5555
// Setup viewport, orthographic projection matrix
5656
glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
5757
glMatrixMode(GL_PROJECTION);

examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ static unsigned int g_VboHandle = 0, g_VaoHandle = 0, g_ElementsHandle = 0;
2828
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
2929
void ImGui_ImplSdlGL3_RenderDrawLists(ImDrawData* draw_data)
3030
{
31+
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
32+
ImGuiIO& io = ImGui::GetIO();
33+
int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
34+
int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
35+
if (fb_width == 0 || fb_height == 0)
36+
return;
37+
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
38+
3139
// Backup GL state
3240
GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
3341
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
@@ -53,14 +61,6 @@ void ImGui_ImplSdlGL3_RenderDrawLists(ImDrawData* draw_data)
5361
glEnable(GL_SCISSOR_TEST);
5462
glActiveTexture(GL_TEXTURE0);
5563

56-
// Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
57-
ImGuiIO& io = ImGui::GetIO();
58-
int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
59-
int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
60-
if (fb_width == 0 || fb_height == 0)
61-
return;
62-
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
63-
6464
// Setup orthographic projection matrix
6565
glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
6666
const float ortho_projection[4][4] =

examples/sdl_opengl_example/imgui_impl_sdl.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ static GLuint g_FontTexture = 0;
2121
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
2222
void ImGui_ImplSdl_RenderDrawLists(ImDrawData* draw_data)
2323
{
24+
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
25+
ImGuiIO& io = ImGui::GetIO();
26+
int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
27+
int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
28+
if (fb_width == 0 || fb_height == 0)
29+
return;
30+
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
31+
2432
// We are using the OpenGL fixed pipeline to make the example code simpler to read!
2533
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
2634
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
@@ -37,14 +45,6 @@ void ImGui_ImplSdl_RenderDrawLists(ImDrawData* draw_data)
3745
glEnable(GL_TEXTURE_2D);
3846
//glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context
3947

40-
// Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
41-
ImGuiIO& io = ImGui::GetIO();
42-
int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
43-
int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
44-
if (fb_width == 0 || fb_height == 0)
45-
return;
46-
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
47-
4848
// Setup viewport, orthographic projection matrix
4949
glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
5050
glMatrixMode(GL_PROJECTION);

0 commit comments

Comments
 (0)