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

Getting the mouse position in screen coordinates? Why not framebuffer coordinates instead? #2530

Open
cblc opened this issue Apr 8, 2024 · 2 comments
Labels
input Keyboard, joystick or mouse support

Comments

@cblc
Copy link

cblc commented Apr 8, 2024

I don't understand the rationale behind glfwGetCursorPos() returning the mouse position in screen coordinates. Why would you want that, if your OpenGL rendering is done in pixels and you set your glViewport() in pixels? Whatever you do with the mouse in OpenGL, you need to know its position in pixels (picking needs to know the pixel at which you clicked, dragging needs to know how many pixels the mouse travelled so that the displacement in pixels is right, etc). Honestly, I don't find a case in which I would prefer to have screen coordinates for the mouse.

Said that, and assuming there's a good reason for it, is there any convenience function for converting from screen coordinates to framebuffer coordinates?

Currently I'm doing it manually by calling

glfwGetWindowSize(window, &width_sc, &height_sc);
glfwGetFramebufferSize(window, &width_px, &height_px);

then calculating the scale factors between screen and framebuffer, and applying that to the mouse coordinates, but I feel like this is overkill for such a common task, and I feel like I'm probably undoing a scale conversion that was internally done by GLFW for converting from framebuffer coordinates to screen coordinates...

@dougbinks
Copy link
Contributor

All the operating systems / window systems supported by GLFW have cursor positions in screen coordinates, and so this is what GLFW exposes. The coordinate system is discussed in the documentation.

Note that only MacOS and Wayland have potentially different window and framebuffer sizes.

GLFW doesn't have an explicit cursor pixel position function, and I'm not sure this would be wise to implement in GLFW as OpenGL / Vulkan etc. have different pixel coordinate origins. The added computational complexity of doing it in your own code is low, and since there are a number of ways of handling UI / cursor interaction it seems that user code is probably the best place to do this.

@cblc
Copy link
Author

cblc commented Apr 9, 2024

Yes, I was thinking twice about this yesterday, and came to the same conclusion as you. I'm leaving it open just in case anybody has anything to add, but feel free to close it.

@elmindreda elmindreda added the input Keyboard, joystick or mouse label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
input Keyboard, joystick or mouse support
Projects
None yet
Development

No branches or pull requests

3 participants