如何在 OpenGL 窗口模式下扩展鼠标空间
如何延长鼠标在 OpenGL 窗口中的移动距离?
我希望实现的是类似 fps 的界面,其中光标被隐藏,并且相机旋转不受鼠标必须保留在窗口边界内的限制。
How do I extend the distance the mouse can move in an OpenGL window?
What I wish to achieve is an fps like interface where the cursor is hidden and camera rotations are not limited by the mouse having to remain inside the window boundaries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每次鼠标移动后,使用 glutWarpPointer 将鼠标移回到屏幕中间(这将在 glutMotionFunc、glutPassiveMotionFunc 中)。
使用 glutsetcursor 更改或隐藏光标的外观。
Use glutWarpPointer to move the mouse back to the middle of the screen after every mouse move(that would be in glutMotionFunc, glutPassiveMotionFunc).
Use glutsetcursor to change or hide what the cursor looks like.
在 Linux 中,这通常是通过将鼠标“变形”回屏幕中心来实现的。 这里是一个关于此问题的论坛主题,使用流行的 SDL库进行实际的鼠标读取。
在 Windows 中,考虑使用较低级别的输入 API,例如 XInput。
This is often implemented by "warping" the mouse back to the center of the screen, in Linux. Here is a forum thread on this, using the popular SDL library to do the actual mouse reading.
In Windows, look into using lower-level input API:s, such as XInput.
执行此操作的某些代码可能需要特定于平台。例如,在 Windows 上,您通常会在用户拖动鼠标时旋转相机。您可以通过捕获鼠标并在捕获鼠标时响应
WM_MOUSEMOVE
消息来处理它。捕获鼠标时,即使光标位置超出窗口边界,您也将继续收到鼠标移动消息。看起来unwind已经很好地涵盖了X,所以这里不再重复。
Some of the code to do this will probably need to be platform specific. On Windows, for example, you'd typically do your camera rotations when the user drags the mouse. You'd handle it by capturing the mouse and responding to
WM_MOUSEMOVE
messages while it's captured. While the mouse is captured, you'll continue to receive the mouse move messages even if the cursor position goes outside the boundaries of your window.It looks like unwind has already covered X pretty well, so I won't repeat it here.
根据您使用的平台和工具,您可以让鼠标移动,然后计算距离,然后将其移回到屏幕(或 OpenGl 窗口)的中心。
这样您就可以轻松地移动鼠标,同时将鼠标保持在同一位置,从而避免出现问题。请注意,
GetMousePosition
和MoveMouse
是通用函数名称,因为这取决于您使用的操作系统和/或库。Depending on what platform and tools you use you can just let the mouse move, then calculate the distance and then move it back in the center of the screen(or OpenGl window).
This way you easily get the mouse movement while keeping the mouse in the same place and thus avoiding the problem. Note that the
GetMousePosition
andMoveMouse
are generic function names as this depends on the OS and/or libraries you use.