-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Scale to window maintaining aspect ratio, but Viewport won't center #7590
Comments
Okay, I worked it out.... had to very slightly modify the backend example-2024-05-16_16.49.55.mp4BackendModify //GL_CALL(glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height)); // <------ Commented out by me Modify // Apply scissor/clipping rectangle (Y is inverted in OpenGL)
GL_CALL(glScissor((int)clip_min.x+offset.x, (int)((float)fb_height - clip_max.y+offset.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y))); Example
// --- Start new backend frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
// Calculations to fit logical display into the current window
// while maintaining aspect ratio and centering the display
constexpr float logicalDisplayX = 1920.0f;
constexpr float logicalDisplayY = 1080.0f;
int windowWidth, windowHeight;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
float scaleX = windowWidth / logicalDisplayX;
float scaleY = windowHeight / logicalDisplayY;
float scale = scaleX < scaleY ? scaleX : scaleY;
int viewportWidth = (int)(logicalDisplayX * scale);
int viewportHeight = (int)(logicalDisplayY * scale);
int viewportX = (windowWidth - viewportWidth) / 2;
int viewportY = (windowHeight - viewportHeight) / 2;
io.DisplaySize = ImVec2(logicalDisplayX, logicalDisplayY);
io.DisplayFramebufferScale = ImVec2(scale, scale);
// scale the mouse position
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
io.AddMousePosEvent((mouseX - viewportX) / scale, (mouseY - viewportY) / scale);
ImGui::Render();
glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
// modified back end to accept the view port offsets
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData(), ImVec2(viewportX, viewportY));
|
io.AddMousePosEvent((mouseX - viewportX) / scale, (mouseY - viewportY) / scale); Adding a Mouse Pos Event may conflicts with the one added by the backend. Ideally you would parse the event list It has been a recurrent topic that we should eventually allow backend to "transform" inputs and also work within a subsection of the main host window. Some forms of DPI scaling (for Mac) may be facilitated by it. And also see stuff like #6942, #6714, #6064, #3972 (yours!), #3814, this is why those issues are kept open. In addition it is possible that the ideal solution for #2176 will be that we offset all viewports and our coordinate system to ensure everything use positive coordinates. |
Version/Branch of Dear ImGui:
Version 1.90.7
Back-ends:
imgui_impl_sdl2.cpp + imgui_impl_opengl3.cpp
Compiler, OS:
GCC 10.5.0, Linux Ubuntu
Full config/build information:
Details:
My Issue/Question:
I am using Dear ImGui as the primary UI for an application, not drawing or rendering anything with SDL2 or OpenGL other than Dear ImGui. It's a system designed to run at full-screen on a 1920x1080 LCD panel, as a kiosk sort of thing.
So as far as Dear ImGui should be concerned, the UI logical display size should be always 1920x1080.
That is all working fine...
Now, when using on a PC with window manager and task bar etc, we have a window content area which is smaller than 1920x1080.
Rather than lose part of the "logical display area" due to the window title bar or window resizing, I want the display scaled, maintaining aspect, and being centered in the window (even if there is letter boxing).
I have provided an MCVE which almost works... but 2 things:
Screenshots/Video:
The areas with the red squiggly lines should not be there, rather display should be centered and black letter-boxing, like a wide screen video would on a 4:3 display...
Minimal, Complete and Verifiable Example code:
The text was updated successfully, but these errors were encountered: